Skip to content

Commit

Permalink
refactor: storage construction via trait
Browse files Browse the repository at this point in the history
Allow an abstract bitmap store to construct itself through the
abstraction.
  • Loading branch information
domodwyer committed Dec 13, 2024
1 parent 4c545fe commit dbe50b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/bitmap/compressed_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ impl Bitmap for CompressedBitmap {
fn or(&self, other: &Self) -> Self {
self.or(other)
}

fn new_with_capacity(max_key: usize) -> Self {
Self::new(max_key)
}
}

#[cfg(test)]
Expand Down
20 changes: 14 additions & 6 deletions src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use std::marker::PhantomData;
/// A trait to abstract bit storage for use in a [`Bloom2`](crate::Bloom2)
/// filter.
pub trait Bitmap {
/// Construct a new [`Bitmap`] impl with capacity to hold at least `max_key`
/// number of bits.
fn new_with_capacity(max_key: usize) -> Self;

/// Set bit indexed by `key` to `value`.
fn set(&mut self, key: usize, value: bool);

Expand Down Expand Up @@ -99,12 +103,7 @@ where
_key_type: PhantomData,
}
}
}

impl<H> BloomFilterBuilder<H, CompressedBitmap>
where
H: BuildHasher,
{
/// Control the in-memory size and false-positive probability of the filter.
///
/// Setting the bitmap size replaces the current `Bitmap` instance with a
Expand All @@ -114,11 +113,16 @@ where
pub fn size(self, size: FilterSize) -> Self {
Self {
key_size: size,
bitmap: CompressedBitmap::new(key_size_to_bits(size)),
bitmap: B::new_with_capacity(key_size_to_bits(size)),
..self
}
}
}

impl<H> BloomFilterBuilder<H, CompressedBitmap>
where
H: BuildHasher,
{
/// Initialise a `BloomFilterBuilder` that unless changed, will construct a
/// `Bloom2` instance using a [2 byte key] and use the specified hasher.
///
Expand Down Expand Up @@ -362,6 +366,10 @@ mod tests {
fn or(&self, _other: &Self) -> Self {
unreachable!()
}

fn new_with_capacity(_max_key: usize) -> Self {
Self::default()
}
}

fn new_test_bloom<T: Hash>() -> Bloom2<MockHasher, MockBitmap, T> {
Expand Down

0 comments on commit dbe50b7

Please sign in to comment.