1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use super::*;
/// Core operations for [`BlockedBloomFilter`].
impl BlockedBloomFilter {
/// Constructs an optimally sized [`BlockedBloomFilter`] for `expected_items` and target `false_positive_rate`.
///
/// # Arguments
///
/// - `usize` - Expected number of distinct elements to insert (`>= 1`).
/// - `f64` - Target false positive probability in `(0.0, 1.0)` (clamped to `[1e-9, 0.5]`).
///
/// # Returns
///
/// - `Self` - Sized cache-line-blocked Bloom filter.
pub fn with_rate(expected_items: usize, false_positive_rate: f64) -> Self {
let items: f64 = (expected_items.max(1)) as f64;
let fpr: f64 = false_positive_rate.clamp(1e-9, 0.5);
let ln2_sq: f64 = LN_2 * LN_2;
let bits: f64 = (-items * fpr.ln() / ln2_sq).ceil();
let block_count: usize =
((bits as usize).saturating_add(BLOCK_BITS - 1) / BLOCK_BITS).max(1);
let actual_bits: f64 = (block_count * BLOCK_BITS) as f64;
let k: u32 = ((actual_bits / items) * LN_2).round().clamp(1.0, 16.0) as u32;
Self::with_blocks(block_count, k)
}
/// Constructs a [`BlockedBloomFilter`] with explicit `block_count` and per-block probe count `hash_count`.
///
/// # Arguments
///
/// - `usize` - Number of 512-bit cache-line blocks (`>= 1`).
/// - `u32` - Number of bit probes per element (`1..=16`).
///
/// # Returns
///
/// - `Self` - Fresh empty blocked Bloom filter.
pub fn with_blocks(block_count: usize, hash_count: u32) -> Self {
let count: usize = block_count.max(1);
let k: u32 = hash_count.clamp(1, 16);
Self {
blocks: vec![BloomBlock::default(); count],
hash_count: k,
}
}
/// Inserts `item` into the filter and returns `true` if at least one bit changed from `0` to `1`.
///
/// # Arguments
///
/// - `&[u8]` - Element bytes to insert.
///
/// # Returns
///
/// - `bool` - `true` when `item` was definitely not previously present.
#[inline]
pub fn insert(&mut self, item: &[u8]) -> bool {
let (h1, h2): (u64, u64) = hash_pair(item);
let Self { blocks, hash_count } = self;
let block_idx: usize = (h1 as usize) % blocks.len();
let block: &mut BloomBlock = &mut blocks[block_idx];
let mut changed: bool = false;
let mut probe: u64 = h2;
for _ in 0..*hash_count {
let bit: usize = (probe as usize) & (BLOCK_BITS - 1);
let word_idx: usize = bit >> 6;
let mask: u64 = 1u64 << (bit & 63);
if (block.words[word_idx] & mask) == 0 {
block.words[word_idx] |= mask;
changed = true;
}
probe = probe.wrapping_add(h1.rotate_left(17) | 1);
}
changed
}
/// Tests whether `item` may be present in the filter (zero false negatives).
///
/// # Arguments
///
/// - `&[u8]` - Element bytes to query.
///
/// # Returns
///
/// - `bool` - `false` if definitely absent, `true` if probably present.
#[inline]
pub fn contains(&self, item: &[u8]) -> bool {
let (h1, h2): (u64, u64) = hash_pair(item);
let Self { blocks, hash_count } = self;
let block_idx: usize = (h1 as usize) % blocks.len();
let block: &BloomBlock = &blocks[block_idx];
let mut probe: u64 = h2;
for _ in 0..*hash_count {
let bit: usize = (probe as usize) & (BLOCK_BITS - 1);
let word_idx: usize = bit >> 6;
let mask: u64 = 1u64 << (bit & 63);
if (block.words[word_idx] & mask) == 0 {
return false;
}
probe = probe.wrapping_add(h1.rotate_left(17) | 1);
}
true
}
/// Clears all bits to zero while keeping allocated capacity.
pub fn clear(&mut self) {
let Self { blocks, .. } = self;
for block in blocks.iter_mut() {
block.words = [0u64; BLOCK_WORDS];
}
}
/// Returns total bit capacity (`block_count * 512`).
///
/// # Returns
///
/// - `usize` - Total bits stored.
#[inline(always)]
pub fn bit_len(&self) -> usize {
let Self { blocks, .. } = self;
blocks.len() * BLOCK_BITS
}
/// Returns the number of configured hash probes per element.
///
/// # Returns
///
/// - `u32` - Probe count `k`.
#[inline(always)]
pub fn hash_count(&self) -> u32 {
let Self { hash_count, .. } = self;
*hash_count
}
/// Counts total set bits across all blocks.
///
/// # Returns
///
/// - `usize` - Number of `1` bits currently set.
pub fn count_ones(&self) -> usize {
let Self { blocks, .. } = self;
let mut ones: usize = 0;
for block in blocks {
for &word in &block.words {
ones += word.count_ones() as usize;
}
}
ones
}
/// Estimates the number of distinct inserted elements from bit density.
///
/// # Returns
///
/// - `f64` - Estimated cardinality.
pub fn estimated_count(&self) -> f64 {
let m: f64 = self.bit_len() as f64;
let x: f64 = self.count_ones() as f64;
if x == 0.0 || x >= m {
return x;
}
let k: f64 = f64::from(self.hash_count());
-(m / k) * (1.0 - x / m).ln()
}
/// Computes bitwise union (`self |= other`) when dimensions and hash counts match.
///
/// # Arguments
///
/// - `&Self` - Other filter with identical dimensions.
///
/// # Returns
///
/// - `bool` - `true` if merged, `false` if dimensions or hash counts differed.
pub fn union_with(&mut self, other: &Self) -> bool {
let Self { blocks, hash_count } = self;
let Self {
blocks: other_blocks,
hash_count: other_k,
} = other;
if blocks.len() != other_blocks.len() || *hash_count != *other_k {
return false;
}
for (dst, src) in blocks.iter_mut().zip(other_blocks.iter()) {
for (d, s) in dst.words.iter_mut().zip(src.words.iter()) {
*d |= *s;
}
}
true
}
}