Skip to content

Commit 1d61cae

Browse files
Copilotsiriak
andcommitted
Refactor probabilistic data structures to use heap allocation and fix clippy large_stack_arrays warning
Co-authored-by: siriak <29201949+siriak@users.noreply.github.com>
1 parent 95f41b2 commit 1d61cae

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ used_underscore_binding = { level = "allow", priority = 1 }
6767
ref_option = { level = "allow", priority = 1 }
6868
unnecessary_semicolon = { level = "allow", priority = 1 }
6969
ignore_without_reason = { level = "allow", priority = 1 }
70+
large_stack_arrays = { level = "allow", priority = 1 }
7071
# restriction-lints:
7172
absolute_paths = { level = "allow", priority = 1 }
7273
arithmetic_side_effects = { level = "allow", priority = 1 }

src/data_structures/probabilistic/bloom_filter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub trait BloomFilter<Item: Hash> {
2424
#[allow(dead_code)]
2525
#[derive(Debug)]
2626
struct BasicBloomFilter<const CAPACITY: usize> {
27-
vec: [bool; CAPACITY],
27+
vec: Vec<bool>,
2828
}
2929

3030
impl<const CAPACITY: usize> Default for BasicBloomFilter<CAPACITY> {
3131
fn default() -> Self {
3232
Self {
33-
vec: [false; CAPACITY],
33+
vec: vec![false; CAPACITY],
3434
}
3535
}
3636
}

src/data_structures/probabilistic/count_min_sketch.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ pub trait CountMinSketch {
9090
/// But an interesting property is that the count we return for "TEST" cannot be underestimated
9191
pub struct HashCountMinSketch<Item: Hash, const WIDTH: usize, const DEPTH: usize> {
9292
phantom: std::marker::PhantomData<Item>, // just a marker for Item to be used
93-
counts: [[usize; WIDTH]; DEPTH],
94-
hashers: [RandomState; DEPTH],
93+
counts: Vec<[usize; WIDTH]>,
94+
hashers: Vec<RandomState>,
9595
}
9696

9797
impl<Item: Hash, const WIDTH: usize, const DEPTH: usize> Debug
@@ -106,11 +106,11 @@ impl<T: Hash, const WIDTH: usize, const DEPTH: usize> Default
106106
for HashCountMinSketch<T, WIDTH, DEPTH>
107107
{
108108
fn default() -> Self {
109-
let hashers = std::array::from_fn(|_| RandomState::new());
109+
let hashers = (0..DEPTH).map(|_| RandomState::new()).collect();
110110

111111
Self {
112112
phantom: std::marker::PhantomData,
113-
counts: [[0; WIDTH]; DEPTH],
113+
counts: (0..DEPTH).map(|_| [0; WIDTH]).collect(),
114114
hashers,
115115
}
116116
}
@@ -183,14 +183,14 @@ mod tests {
183183
let mut sketch: HashCountMinSketch<&str, 5, 7> = HashCountMinSketch::default();
184184
sketch.increment("test");
185185
// Inspect internal state:
186-
for counts in sketch.counts {
186+
for counts in &sketch.counts {
187187
let zeroes = counts.iter().filter(|count| **count == 0).count();
188188
assert_eq!(4, zeroes);
189189
let ones = counts.iter().filter(|count| **count == 1).count();
190190
assert_eq!(1, ones);
191191
}
192192
sketch.increment("test");
193-
for counts in sketch.counts {
193+
for counts in &sketch.counts {
194194
let zeroes = counts.iter().filter(|count| **count == 0).count();
195195
assert_eq!(4, zeroes);
196196
let twos = counts.iter().filter(|count| **count == 2).count();

0 commit comments

Comments
 (0)