Skip to content

Commit f9c9c09

Browse files
nicopapjrraymond
authored andcommitted
Add is_clear method
1 parent 72bd94d commit f9c9c09

1 file changed

Lines changed: 46 additions & 3 deletions

File tree

src/lib.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn div_rem(x: usize, d: usize) -> (usize, usize) {
5050
///
5151
/// The bit set has a fixed capacity in terms of enabling bits (and the
5252
/// capacity can grow using the `grow` method).
53-
///
53+
///
5454
/// Derived traits depend on both the zeros and ones, so [0,1] is not equal to
5555
/// [0,1,0].
5656
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
@@ -122,18 +122,61 @@ impl FixedBitSet {
122122
}
123123
}
124124

125-
/// Return the length of the [`FixedBitSet`] in bits.
125+
/// The length of the [`FixedBitSet`] in bits.
126+
///
127+
/// Note: `len` includes both set and unset bits.
128+
/// ```
129+
/// # use fixedbitset::FixedBitSet;
130+
/// let bitset = FixedBitSet::with_capacity(10);
131+
/// // there are 0 set bits, but 10 unset bits
132+
/// assert_eq!(bitset.len(), 10);
133+
/// ```
134+
/// `len` does not return the count of set bits. For that, use
135+
/// [`bitset.count_ones(..)`](FixedBitSet::count_ones) instead.
126136
#[inline]
127137
pub fn len(&self) -> usize {
128138
self.length
129139
}
130140

131-
/// Return if the [`FixedBitSet`] is empty.
141+
/// `true` if the [`FixedBitSet`] is empty.
142+
///
143+
/// Note that an "empty" `FixedBitSet` is a `FixedBitSet` with
144+
/// no bits (meaning: it's length is zero). If you want to check
145+
/// if all bits are unset, use [`FixedBitSet::is_clear`].
146+
///
147+
/// ```
148+
/// # use fixedbitset::FixedBitSet;
149+
/// let bitset = FixedBitSet::with_capacity(10);
150+
/// assert!(!bitset.is_empty());
151+
///
152+
/// let bitset = FixedBitSet::with_capacity(0);
153+
/// assert!(bitset.is_empty());
154+
/// ```
132155
#[inline]
133156
pub fn is_empty(&self) -> bool {
134157
self.len() == 0
135158
}
136159

160+
/// `true` if all bits in the [`FixedBitSet`] are unset.
161+
///
162+
/// As opposed to [`FixedBitSet::is_empty`], which is `true` only for
163+
/// sets without any bits, set or unset.
164+
///
165+
/// ```
166+
/// # use fixedbitset::FixedBitSet;
167+
/// let mut bitset = FixedBitSet::with_capacity(10);
168+
/// assert!(bitset.is_clear());
169+
///
170+
/// bitset.insert(2);
171+
/// assert!(!bitset.is_clear());
172+
/// ```
173+
///
174+
/// This is equivalent to [`bitset.count_ones(..) == 0`](FixedBitSet::count_ones).
175+
#[inline]
176+
pub fn is_clear(&self) -> bool {
177+
self.data.iter().all(|block| *block == 0)
178+
}
179+
137180
/// Return **true** if the bit is enabled in the **FixedBitSet**,
138181
/// **false** otherwise.
139182
///

0 commit comments

Comments
 (0)