Clarify ChunkSize
invariants.
`ChunkedBitSet::is_empty` currently does an unnecessary check. This commit removes that check and adds clarifying comments and an assertion that demonstrate why it's unnecessary.
This commit is contained in:
parent
6b6a867ae9
commit
15b24c46b4
1 changed files with 8 additions and 4 deletions
|
@ -369,9 +369,11 @@ pub struct ChunkedBitSet<T> {
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
enum Chunk {
|
enum Chunk {
|
||||||
/// A chunk that is all zeros; we don't represent the zeros explicitly.
|
/// A chunk that is all zeros; we don't represent the zeros explicitly.
|
||||||
|
/// The `ChunkSize` is always non-zero.
|
||||||
Zeros(ChunkSize),
|
Zeros(ChunkSize),
|
||||||
|
|
||||||
/// A chunk that is all ones; we don't represent the ones explicitly.
|
/// A chunk that is all ones; we don't represent the ones explicitly.
|
||||||
|
/// `ChunkSize` is always non-zero.
|
||||||
Ones(ChunkSize),
|
Ones(ChunkSize),
|
||||||
|
|
||||||
/// A chunk that has a mix of zeros and ones, which are represented
|
/// A chunk that has a mix of zeros and ones, which are represented
|
||||||
|
@ -384,8 +386,10 @@ enum Chunk {
|
||||||
/// words are always be zero, as are any excess bits in the final in-use
|
/// words are always be zero, as are any excess bits in the final in-use
|
||||||
/// word.
|
/// word.
|
||||||
///
|
///
|
||||||
/// The second field is the count of 1s set in the chunk, and must satisfy
|
/// The first `ChunkSize` field is always non-zero.
|
||||||
/// `0 < count < chunk_domain_size`.
|
///
|
||||||
|
/// The second `ChunkSize` field is the count of 1s set in the chunk, and
|
||||||
|
/// must satisfy `0 < count < chunk_domain_size`.
|
||||||
///
|
///
|
||||||
/// The words are within an `Rc` because it's surprisingly common to
|
/// The words are within an `Rc` because it's surprisingly common to
|
||||||
/// duplicate an entire chunk, e.g. in `ChunkedBitSet::clone_from()`, or
|
/// duplicate an entire chunk, e.g. in `ChunkedBitSet::clone_from()`, or
|
||||||
|
@ -461,7 +465,7 @@ impl<T: Idx> ChunkedBitSet<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..) | Chunk::Ones(0)))
|
self.chunks.iter().all(|chunk| matches!(chunk, Chunk::Zeros(..)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if `self` contains `elem`.
|
/// Returns `true` if `self` contains `elem`.
|
||||||
|
@ -1005,7 +1009,7 @@ impl Chunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new(chunk_domain_size: usize, is_empty: bool) -> Self {
|
fn new(chunk_domain_size: usize, is_empty: bool) -> Self {
|
||||||
debug_assert!(chunk_domain_size <= CHUNK_BITS);
|
debug_assert!(0 < chunk_domain_size && chunk_domain_size <= CHUNK_BITS);
|
||||||
let chunk_domain_size = chunk_domain_size as ChunkSize;
|
let chunk_domain_size = chunk_domain_size as ChunkSize;
|
||||||
if is_empty { Zeros(chunk_domain_size) } else { Ones(chunk_domain_size) }
|
if is_empty { Zeros(chunk_domain_size) } else { Ones(chunk_domain_size) }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue