Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_clear method #79

Merged
merged 1 commit into from
Jul 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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