Skip to content

Commit

Permalink
Merge pull request #22 from upsuper-forks/fn-count
Browse files Browse the repository at this point in the history
Rename len() to count() to reflect its performance implication
  • Loading branch information
pczarn authored Dec 1, 2024
2 parents abd75bb + 434d049 commit 4c5677a
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,23 @@ impl<B: BitBlock> BitSet<B> {
}
*/

/// Returns the number of set bits in this set.
/// Counts the number of set bits in this set.
///
/// Note that this function scans the set to calculate the number.
#[inline]
pub fn len(&self) -> usize {
pub fn count(&self) -> usize {
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones())
}

/// Counts the number of set bits in this set.
///
/// Note that this function scans the set to calculate the number.
#[inline]
#[deprecated = "use BitVec::count() instead"]
pub fn len(&self) -> usize {
self.count()
}

/// Returns whether there are no bits set in this set
#[inline]
pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -1190,7 +1201,7 @@ mod tests {
assert!(b.insert(400));
assert!(!b.insert(400));
assert!(b.contains(400));
assert_eq!(b.len(), 3);
assert_eq!(b.count(), 3);
}

#[test]
Expand Down Expand Up @@ -1346,8 +1357,8 @@ mod tests {
let c = a.clone();
a.union_with(&b);
b.union_with(&c);
assert_eq!(a.len(), 4);
assert_eq!(b.len(), 4);
assert_eq!(a.count(), 4);
assert_eq!(b.count(), 4);
}

#[test]
Expand Down Expand Up @@ -1376,8 +1387,8 @@ mod tests {
let c = a.clone();
a.intersect_with(&b);
b.intersect_with(&c);
assert_eq!(a.len(), 2);
assert_eq!(b.len(), 2);
assert_eq!(a.count(), 2);
assert_eq!(b.count(), 2);
}

#[test]
Expand All @@ -1400,8 +1411,8 @@ mod tests {
let c = a.clone();
a.difference_with(&b);
b.difference_with(&c);
assert_eq!(a.len(), 1);
assert_eq!(b.len(), 1);
assert_eq!(a.count(), 1);
assert_eq!(b.count(), 1);
}

#[test]
Expand Down Expand Up @@ -1429,8 +1440,8 @@ mod tests {
let c = a.clone();
a.symmetric_difference_with(&b);
b.symmetric_difference_with(&c);
assert_eq!(a.len(), 2);
assert_eq!(b.len(), 2);
assert_eq!(a.count(), 2);
assert_eq!(b.count(), 2);
}

#[test]
Expand Down Expand Up @@ -1467,31 +1478,31 @@ mod tests {
// and this would end up actually growing the array in a way
// that (safely corrupted the state).
let mut a = BitSet::new();
assert_eq!(a.len(), 0);
assert_eq!(a.count(), 0);
assert_eq!(a.capacity(), 0);
a.shrink_to_fit();
assert_eq!(a.len(), 0);
assert_eq!(a.count(), 0);
assert_eq!(a.capacity(), 0);
assert!(!a.contains(1));
a.insert(3);
assert!(a.contains(3));
assert_eq!(a.len(), 1);
assert_eq!(a.count(), 1);
assert!(a.capacity() > 0);
a.shrink_to_fit();
assert!(a.contains(3));
assert_eq!(a.len(), 1);
assert_eq!(a.count(), 1);
assert!(a.capacity() > 0);
}

#[test]
fn test_bit_set_shrink_to_fit() {
let mut a = BitSet::new();
assert_eq!(a.len(), 0);
assert_eq!(a.count(), 0);
assert_eq!(a.capacity(), 0);
a.insert(259);
a.insert(98);
a.insert(3);
assert_eq!(a.len(), 3);
assert_eq!(a.count(), 3);
assert!(a.capacity() > 0);
assert!(!a.contains(1));
assert!(a.contains(259));
Expand All @@ -1503,7 +1514,7 @@ mod tests {
assert!(a.contains(259));
assert!(a.contains(98));
assert!(a.contains(3));
assert_eq!(a.len(), 3);
assert_eq!(a.count(), 3);
assert!(a.capacity() > 0);

let old_cap = a.capacity();
Expand All @@ -1514,20 +1525,20 @@ mod tests {
assert!(!a.contains(259));
assert!(a.contains(98));
assert!(a.contains(3));
assert_eq!(a.len(), 2);
assert_eq!(a.count(), 2);

let old_cap2 = a.capacity();
a.clear();
assert_eq!(a.capacity(), old_cap2);
assert_eq!(a.len(), 0);
assert_eq!(a.count(), 0);
assert!(!a.contains(1));
assert!(!a.contains(259));
assert!(!a.contains(98));
assert!(!a.contains(3));

a.insert(512);
assert!(a.capacity() > 0);
assert_eq!(a.len(), 1);
assert_eq!(a.count(), 1);
assert!(a.contains(512));
assert!(!a.contains(1));
assert!(!a.contains(259));
Expand All @@ -1537,7 +1548,7 @@ mod tests {
a.remove(512);
a.shrink_to_fit();
assert_eq!(a.capacity(), 0);
assert_eq!(a.len(), 0);
assert_eq!(a.count(), 0);
assert!(!a.contains(512));
assert!(!a.contains(1));
assert!(!a.contains(259));
Expand Down

0 comments on commit 4c5677a

Please sign in to comment.