Skip to content

Commit

Permalink
del: unstable contains/insert generic API
Browse files Browse the repository at this point in the history
Signed-off-by: qjerome <qjerome@rawsec.lu>
  • Loading branch information
qjerome committed Apr 29, 2024
1 parent 00a647c commit 0fdb19a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
23 changes: 4 additions & 19 deletions poppy/src/bloom/v1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
hash::Hasher,
io::{self, BufWriter, Read, Write},
slice,
};

use crate::{
Expand Down Expand Up @@ -221,13 +220,6 @@ impl BloomFilter {
.collect::<Vec<bool>>()
}

#[inline(always)]
pub fn insert<S: Sized>(&mut self, value: S) -> Result<bool, Error> {
self.insert_bytes(unsafe {
slice::from_raw_parts(&value as *const S as *const u8, core::mem::size_of::<S>())
})
}

/// inserts a value into the bloom filter, as bloom filters are not easily
/// growable an error is returned if we try to insert too many entries
#[inline(always)]
Expand Down Expand Up @@ -294,13 +286,6 @@ impl BloomFilter {
ret
}

#[inline(always)]
pub fn contains<S: Sized>(&self, value: S) -> bool {
self.contains_bytes(unsafe {
slice::from_raw_parts(&value as *const S as *const u8, core::mem::size_of::<S>())
})
}

/// counts all the set bits in the bloom filter
#[inline(always)]
pub fn count_ones(&self) -> usize {
Expand Down Expand Up @@ -544,13 +529,13 @@ mod test {
assert!(b.bit_size.is_power_of_two());

for i in 0..n {
b.insert(i).unwrap();
assert!(b.contains(i));
b.insert_bytes(i.to_le_bytes()).unwrap();
assert!(b.contains_bytes(i.to_le_bytes()));
}

let mut s = Stats::new();
for i in n..n * 2 {
if b.contains(i) {
if b.contains_bytes(i.to_le_bytes()) {
s.inc_fp()
} else {
s.inc_tn()
Expand All @@ -564,7 +549,7 @@ mod test {
#[test]
fn test_contains_on_empty() {
let b = bloom!(0, 0.001);
assert!(!b.contains(42))
assert!(!b.contains_bytes("42"))
}

#[test]
Expand Down
17 changes: 1 addition & 16 deletions poppy/src/bloom/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
hash::Hasher,
io::{self, BufWriter, Read, Write},
marker::PhantomData,
slice,
};

use crate::{
Expand Down Expand Up @@ -331,13 +330,6 @@ impl BloomFilter {
Ok(new)
}

#[inline(always)]
pub fn insert<S: Sized>(&mut self, value: S) -> Result<bool, Error> {
self.insert_bytes(unsafe {
slice::from_raw_parts(&value as *const S as *const u8, core::mem::size_of::<S>())
})
}

#[inline]
pub fn contains_bytes<D: AsRef<[u8]>>(&self, data: D) -> bool {
if self.capacity == 0 {
Expand Down Expand Up @@ -378,13 +370,6 @@ impl BloomFilter {
true
}

#[inline(always)]
pub fn contains<S: Sized>(&self, value: S) -> bool {
self.contains_bytes(unsafe {
slice::from_raw_parts(&value as *const S as *const u8, core::mem::size_of::<S>())
})
}

/// counts all the set bits in the bloom filter
#[inline]
pub fn count_ones(&self) -> usize {
Expand Down Expand Up @@ -852,7 +837,7 @@ mod test {
#[test]
fn test_contains_on_empty() {
let b = bloom!(0, 0.001);
assert!(!b.contains(42))
assert!(!b.contains_bytes("toto"))
}

#[test]
Expand Down

0 comments on commit 0fdb19a

Please sign in to comment.