Skip to content

Commit

Permalink
Add is_clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopap authored and jrraymond committed Jul 3, 2022
1 parent 72bd94d commit f9c9c09
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn div_rem(x: usize, d: usize) -> (usize, usize) {
///
/// The bit set has a fixed capacity in terms of enabling bits (and the
/// capacity can grow using the `grow` method).
///
///
/// Derived traits depend on both the zeros and ones, so [0,1] is not equal to
/// [0,1,0].
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
Expand Down Expand Up @@ -122,18 +122,61 @@ impl FixedBitSet {
}
}

/// Return the length of the [`FixedBitSet`] in bits.
/// The length of the [`FixedBitSet`] in bits.
///
/// Note: `len` includes both set and unset bits.
/// ```
/// # use fixedbitset::FixedBitSet;
/// let bitset = FixedBitSet::with_capacity(10);
/// // there are 0 set bits, but 10 unset bits
/// assert_eq!(bitset.len(), 10);
/// ```
/// `len` does not return the count of set bits. For that, use
/// [`bitset.count_ones(..)`](FixedBitSet::count_ones) instead.
#[inline]
pub fn len(&self) -> usize {
self.length
}

/// Return if the [`FixedBitSet`] is empty.
/// `true` if the [`FixedBitSet`] is empty.
///
/// Note that an "empty" `FixedBitSet` is a `FixedBitSet` with
/// no bits (meaning: it's length is zero). If you want to check
/// if all bits are unset, use [`FixedBitSet::is_clear`].
///
/// ```
/// # use fixedbitset::FixedBitSet;
/// let bitset = FixedBitSet::with_capacity(10);
/// assert!(!bitset.is_empty());
///
/// let bitset = FixedBitSet::with_capacity(0);
/// assert!(bitset.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// `true` if all bits in the [`FixedBitSet`] are unset.
///
/// As opposed to [`FixedBitSet::is_empty`], which is `true` only for
/// sets without any bits, set or unset.
///
/// ```
/// # use fixedbitset::FixedBitSet;
/// let mut bitset = FixedBitSet::with_capacity(10);
/// assert!(bitset.is_clear());
///
/// bitset.insert(2);
/// assert!(!bitset.is_clear());
/// ```
///
/// This is equivalent to [`bitset.count_ones(..) == 0`](FixedBitSet::count_ones).
#[inline]
pub fn is_clear(&self) -> bool {
self.data.iter().all(|block| *block == 0)
}

/// Return **true** if the bit is enabled in the **FixedBitSet**,
/// **false** otherwise.
///
Expand Down

0 comments on commit f9c9c09

Please sign in to comment.