Skip to content

Add count_ones, count_zeros methods #22

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -551,6 +551,58 @@ impl SmallBitVec {
}
}

/// Counts the number of bits in the vector that are set to one/true
pub fn count_ones(&self) -> usize {
let mut len = self.len();
if len == 0 {
return 0;
}

if self.is_inline() {
let mask = inline_ones(len);
(self.data & mask).count_ones() as usize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to return here and deindent the rest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need the & mask for this case? The other bits should always be zero.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I lie, I recall that we use the last non-zero bit as a sentinel, so you do need this.

} else {
let mut count = 0;
for &storage in self.buffer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, can't this loop just do count += storage.count_ones()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do this though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you cannot, because we don't clear storage when we pop(), so never mind :)

if len >= bits_per_storage() {
count += storage.count_ones() as usize;
len -= bits_per_storage();
} else {
let mask = (1 << len) - 1;
count += (storage & mask).count_ones() as usize;
break;
}
}
count
}
}

/// Counts the number of bits in the vector that are set to zero/false
pub fn count_zeros(&self) -> usize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't this be self.len() - self.count_ones()?

let mut len = self.len();
if len == 0 {
return 0;
}

if self.is_inline() {
let mask = inline_ones(len);
(!self.data & mask).count_ones() as usize
} else {
let mut count = 0;
for &storage in self.buffer() {
if len >= bits_per_storage() {
count += storage.count_zeros() as usize;
len -= bits_per_storage();
} else {
let mask = (1 << len) - 1;
count += (!storage & mask).count_ones() as usize;
break;
}
}
count
}
}

/// Shorten the vector, keeping the first `len` elements and dropping the rest.
///
/// If `len` is greater than or equal to the vector's current length, this has no
27 changes: 27 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -365,3 +365,30 @@ fn resize() {
assert_eq!(v.get(98).unwrap(), false);
assert_eq!(v.get(99).unwrap(), false);
}

#[test]
fn count_ones_zeros() {
let random_bits = &[
false, true, true, true, true, false, true, false,
true, true, false, false, false, false, true, false,
false, true, true, false, false, false, false, false,
false, true, false, false, false, false, false, true,
false, true, true, true, false, true, false, true,
true, true, false, false, true, false, false, false,
true, false, true, false, true, false, false, false,
false, true, false, true, false, false, true, true,
true, true, false, true, true, true, false, false,
false, false, true, false, true, true, false, false,
];
let mut v = SmallBitVec::new();
for start in 0..random_bits.len() {
for i in 0..random_bits.len() {
let index = (start + i) % random_bits.len();
let bit = random_bits[index];
v.push(bit);
assert_eq!(v.iter().filter(|&b| b).count(), v.count_ones());
assert_eq!(v.iter().filter(|&b| !b).count(), v.count_zeros());
}
v.clear();
}
}