-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcuckoofilter_c.zig
293 lines (249 loc) · 10.3 KB
/
cuckoofilter_c.zig
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// C interface for cucoofilter.zig
//
const cuckoo = @import("./cuckoofilter.zig");
// CONSTS
export const CF_OK: c_int = 0;
export const CF_TOOFULL: c_int = 1;
export const CF_BROKEN: c_int = 2;
export const CF_BADLEN: c_int = 3;
export const CF_ALIGN8: usize = cuckoo.Filter8.Align;
export const CF_ALIGN16: usize = cuckoo.Filter16.Align;
export const CF_ALIGN32: usize = cuckoo.Filter32.Align;
export const CF_MAXERR8: f32 = cuckoo.Filter8.MaxError;
export const CF_MAXERR16: f32 = cuckoo.Filter16.MaxError;
export const CF_MAXERR32: f32 = cuckoo.Filter32.MaxError;
export const Filter8 = extern struct { cf: [@sizeOf(cuckoo.Filter8)]u8 };
export const Filter16 = extern struct { cf: [@sizeOf(cuckoo.Filter16)]u8 };
export const Filter32 = extern struct { cf: [@sizeOf(cuckoo.Filter32)]u8 };
// seed_default_prng
export fn seed_default_prng(seed: u64) void {
cuckoo.seed_default_prng(seed);
}
// get_default_rng_state
export fn get_default_prng_state() [2]u64 {
return cuckoo.get_default_prng_state();
}
// set_default_rng_state
export fn set_default_prng_state(s: [2]u64) void {
cuckoo.set_default_prng_state(s);
}
// size_for
export fn cf_size_for8(min_capacity: usize) usize {
return cuckoo.Filter8.size_for(min_capacity);
}
export fn cf_size_for16(min_capacity: usize) usize {
return cuckoo.Filter16.size_for(min_capacity);
}
export fn cf_size_for32(min_capacity: usize) usize {
return cuckoo.Filter32.size_for(min_capacity);
}
// size_for_exactly
export fn cf_size_for_exactly8(min_capacity: usize) usize {
return cuckoo.Filter8.size_for_exactly(min_capacity);
}
export fn cf_size_for_exactly16(min_capacity: usize) usize {
return cuckoo.Filter16.size_for_exactly(min_capacity);
}
export fn cf_size_for_exactly32(min_capacity: usize) usize {
return cuckoo.Filter32.size_for_exactly(min_capacity);
}
// capacity
export fn cf_capacity8(size: usize) usize {
return cuckoo.Filter8.capacity(size);
}
export fn cf_capacity16(size: usize) usize {
return cuckoo.Filter16.capacity(size);
}
export fn cf_capacity32(size: usize) usize {
return cuckoo.Filter32.capacity(size);
}
// init
export fn cf_init8(memory: [*]u8, size: usize, cf: *Filter8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
cf_ptr.* = cuckoo.Filter8.init(@alignCast(cuckoo.Filter8.Align, memory)[0..size]) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}
export fn cf_init16(memory: [*]u8, size: usize, cf: *Filter16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
cf_ptr.* = cuckoo.Filter16.init(@alignCast(cuckoo.Filter16.Align, memory)[0..size]) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}
export fn cf_init32(memory: [*]u8, size: usize, cf: *Filter32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
cf_ptr.* = cuckoo.Filter32.init(@alignCast(cuckoo.Filter32.Align, memory)[0..size]) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}
// count
export fn cf_count8(cf: *Filter8, res: *usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
res.* = cf_ptr.count() catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_count16(cf: *Filter16, res: *usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
res.* = cf_ptr.count() catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_count32(cf: *Filter32, res: *usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
res.* = cf_ptr.count() catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
// maybe_contains
export fn cf_maybe_contains8(cf: *Filter8, hash: u64, fp: u8, res: *c_int) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
res.* = @boolToInt(cf_ptr.maybe_contains(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
});
return CF_OK;
}
export fn cf_maybe_contains16(cf: *Filter16, hash: u64, fp: u16, res: *c_int) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
res.* = @boolToInt(cf_ptr.maybe_contains(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
});
return CF_OK;
}
export fn cf_maybe_contains32(cf: *Filter32, hash: u64, fp: u32, res: *c_int) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
res.* = @boolToInt(cf_ptr.maybe_contains(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
});
return CF_OK;
}
// remove
export fn cf_remove8(cf: *Filter8, hash: u64, fp: u8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
cf_ptr.remove(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_remove16(cf: *Filter16, hash: u64, fp: u16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
cf_ptr.remove(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_remove32(cf: *Filter32, hash: u64, fp: u32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
cf_ptr.remove(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
// add
export fn cf_add8(cf: *Filter8, hash: u64, fp: u8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
cf_ptr.add(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
error.TooFull => return CF_TOOFULL,
};
return CF_OK;
}
export fn cf_add16(cf: *Filter16, hash: u64, fp: u16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
cf_ptr.add(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
error.TooFull => return CF_TOOFULL,
};
return CF_OK;
}
export fn cf_add32(cf: *Filter32, hash: u64, fp: u32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
cf_ptr.add(hash, fp) catch |err| switch (err) {
error.Broken => return CF_BROKEN,
error.TooFull => return CF_TOOFULL,
};
return CF_OK;
}
// is_broken
export fn cf_is_broken8(cf: *Filter8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_broken());
}
export fn cf_is_broken16(cf: *Filter16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_broken());
}
export fn cf_is_broken32(cf: *Filter32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_broken());
}
// is_toofull
export fn cf_is_toofull8(cf: *Filter8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_toofull());
}
export fn cf_is_toofull16(cf: *Filter16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_toofull());
}
export fn cf_is_toofull32(cf: *Filter32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
return @boolToInt(cf_ptr.is_toofull());
}
// fix_toofull
export fn cf_fix_toofull8(cf: *Filter8) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
cf_ptr.fix_toofull() catch |err| switch (err) {
error.TooFull => return CF_TOOFULL,
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_fix_toofull16(cf: *Filter16) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
cf_ptr.fix_toofull() catch |err| switch (err) {
error.TooFull => return CF_TOOFULL,
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
export fn cf_fix_toofull32(cf: *Filter32) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
cf_ptr.fix_toofull() catch |err| switch (err) {
error.TooFull => return CF_TOOFULL,
error.Broken => return CF_BROKEN,
};
return CF_OK;
}
// To persist a filter you simply need to save the struct's bytes and the its relative buckets
// (referred to as `memory` throughout the documentation). The struct contains a pointer
// to its `memory` which would not match when loading the filter back again, so use this
// function to properly restore it.
export fn cf_restore_memory8(cf: *Filter8, memory: [*]u8, memory_len: usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter8, @alignCast(@alignOf(cuckoo.Filter8), &(cf.*.cf)));
cf_ptr.buckets = cuckoo.Filter8.bytesToBuckets(@alignCast(CF_ALIGN8, memory[0..memory_len])) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}
export fn cf_restore_memory16(cf: *Filter16, memory: [*]u8, memory_len: usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter16, @alignCast(@alignOf(cuckoo.Filter16), &(cf.*.cf)));
cf_ptr.buckets = cuckoo.Filter16.bytesToBuckets(@alignCast(CF_ALIGN16, memory[0..memory_len])) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}
export fn cf_restore_memory32(cf: *Filter32, memory: [*]u8, memory_len: usize) c_int {
var cf_ptr = @ptrCast(*cuckoo.Filter32, @alignCast(@alignOf(cuckoo.Filter32), &(cf.*.cf)));
cf_ptr.buckets = cuckoo.Filter32.bytesToBuckets(@alignCast(CF_ALIGN32, memory[0..memory_len])) catch |err| switch (err) {
error.BadLength => return CF_BADLEN,
};
return CF_OK;
}