spider_lib/utils/
bloom_filter.rs1use std::collections::hash_map::DefaultHasher;
8use std::hash::{Hash, Hasher};
9use std::sync::RwLock;
10
11pub struct BloomFilter {
13 bit_array: RwLock<Vec<bool>>,
14 bit_array_size: usize,
15 hash_functions: usize,
16}
17
18impl BloomFilter {
19 pub fn new(size: usize, hash_functions: usize) -> Self {
21 Self {
22 bit_array: RwLock::new(vec![false; size]),
23 bit_array_size: size,
24 hash_functions,
25 }
26 }
27
28 pub fn add<T: Hash>(&self, item: &T) {
30 let mut bit_array = self.bit_array.write().unwrap();
31 for i in 0..self.hash_functions {
32 let hash = self.hash_with_seed(item, i);
33 let index = hash % self.bit_array_size;
34 bit_array[index] = true;
35 }
36 }
37
38 pub fn might_contain<T: Hash>(&self, item: &T) -> bool {
41 let bit_array = self.bit_array.read().unwrap();
42 for i in 0..self.hash_functions {
43 let hash = self.hash_with_seed(item, i);
44 let index = hash % self.bit_array_size;
45 if !bit_array[index] {
46 return false;
47 }
48 }
49 true
50 }
51
52 fn hash_with_seed<T: Hash>(&self, item: &T, seed: usize) -> usize {
54 let mut hasher = DefaultHasher::new();
55 item.hash(&mut hasher);
56 (hasher.finish() as usize) ^ seed
57 }
58}
59