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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
mod bucket;
mod util;
extern crate rand;
extern crate byteorder;
use bucket::{Bucket, Fingerprint, BUCKET_SIZE};
use util::{get_fai, get_alt_index, FaI};
use rand::Rng;
use std::iter::repeat;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hasher, Hash};
use std::marker::PhantomData;
use std::mem;
pub const MAX_REBUCKET: u32 = 500;
pub const DEFAULT_CAPACITY: u64 = (1 << 20) - 1;
pub struct CuckooFilter<H> {
buckets: Box<[Bucket]>,
len: u64,
_hasher: std::marker::PhantomData<H>,
}
impl Default for CuckooFilter<DefaultHasher> {
fn default() -> Self {
CuckooFilter::new()
}
}
impl CuckooFilter<DefaultHasher> {
pub fn new() -> CuckooFilter<DefaultHasher> {
Self::with_capacity(DEFAULT_CAPACITY)
}
}
impl<H> CuckooFilter<H>
where H: Hasher + Default
{
pub fn with_capacity(cap: u64) -> CuckooFilter<H> {
let capacity = match cap.next_power_of_two() / BUCKET_SIZE as u64 {
0 => 1,
cap => cap,
};
CuckooFilter {
buckets: repeat(Bucket::new())
.take(capacity as usize)
.collect::<Vec<_>>()
.into_boxed_slice(),
len: 0,
_hasher: PhantomData,
}
}
pub fn contains<T: ?Sized + Hash>(&self, data: &T) -> bool {
let FaI { fp, i1, i2 } = get_fai::<T, H>(data);
let len = self.buckets.len();
self.buckets[i1 % len]
.get_fingerprint_index(fp)
.or(self.buckets[i2 % len].get_fingerprint_index(fp))
.is_some()
}
pub fn add<T: ?Sized + Hash>(&mut self, data: &T) -> bool {
let fai = get_fai::<T, H>(data);
if self.put(fai.fp, fai.i1) || self.put(fai.fp, fai.i2) {
return true;
}
let len = self.buckets.len();
let mut rng = rand::thread_rng();
let mut i = fai.random_index(&mut rng);
let mut fp = fai.fp;
for _ in 0..MAX_REBUCKET {
let other_fp;
{
let loc = &mut self.buckets[i % len].buffer[rng.gen_range(0, BUCKET_SIZE)];
other_fp = *loc;
*loc = fp;
i = get_alt_index::<H>(other_fp, i);
}
if self.put(other_fp, i) {
return true;
}
fp = other_fp;
}
panic!("Map is full, could not insert item");
}
pub fn test_and_add<T: ?Sized + Hash>(&mut self, data: &T) -> bool {
if self.contains(data) {
false
} else {
self.add(data)
}
}
pub fn len(&self) -> u64 {
self.len
}
pub fn memory_usage(&self) -> usize {
mem::size_of_val(self) + self.buckets.len() * mem::size_of::<Bucket>()
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn delete<T: ?Sized + Hash>(&mut self, data: &T) -> bool {
let FaI { fp, i1, i2 } = get_fai::<T, H>(data);
self.remove(fp, i1) || self.remove(fp, i2)
}
fn remove(&mut self, fp: Fingerprint, i: usize) -> bool {
let len = self.buckets.len();
if self.buckets[i % len].delete(fp) {
self.len -= 1;
true
} else {
false
}
}
fn put(&mut self, fp: Fingerprint, i: usize) -> bool {
let len = self.buckets.len();
if self.buckets[i % len].insert(fp) {
self.len += 1;
true
} else {
false
}
}
}