-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} else { | ||
let mut count = 0; | ||
for &storage in self.buffer() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, can't this loop just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can do this though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And you cannot, because we don't clear storage when we |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this be |
||
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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.