-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlbf.c
166 lines (129 loc) · 4.37 KB
/
dlbf.c
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
/* dlbf.c
Simple implementation of a deletable Bloom filter interface.
Copyright 2020 Owen Niles <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include "dlbf.h"
#define META (3 * sizeof (unsigned))
/* Macros that can be used to extract a particular filter's metadata. */
#define __M(filt) (((unsigned *) filt)[0])
#define __K(filt) (((unsigned *) filt)[1])
#define __R(filt) (((unsigned *) filt)[2])
#define getbit(filt,i) \
(((filt)[(i) / 8 + META] & (1 << (i) % 8)) == 1 << (i) % 8)
/* Monotonically set the specified bit to the specified value by increasing its
value if possible. */
#define incbit(filt,i,b) ((filt)[(i) / 8 + META] |= (b) << (i) % 8)
/* Monotonically set the specified bit to the specified value by decreasing its
value if possible. */
#define decbit(filt,i,b) ((filt)[(i) / 8 + META] &= ~((!b) << (i) % 8))
/* Computes the the hash function H_i on input x. */
static uint32_t
hash (int i, int x)
{
uint64_t concat = ((uint64_t) i << 32) | x;
uint64_t product = concat * 2654435769;
uint32_t lower = product;
uint32_t upper = product >> 32;
return lower + upper;
}
/* Allocates a new deletable Bloom filter that is m bits long, applies k
discrete hash functions to each key, and tracks collision-freeness for r
regions. */
uint8_t *
dlbf_alloc (unsigned m, unsigned k, unsigned r)
{
unsigned *filt;
if (m < 1 || k < 1 || r < 1 || r > m)
return NULL;
filt = calloc (META + (m + r - 1) / 8 + 1, 1);
if (filt == NULL)
return NULL;
filt[0] = m;
filt[1] = k;
filt[2] = r;
return (uint8_t *) filt;
}
void
dlbf_free (uint8_t *filt)
{
free (filt);
}
/* Inserts the specified key into the Bloom filter. Returns the number of hash
functions that will output a bit offset that falls within a non collision-
free zone when applied to the specified key following its insertion. */
int
dlbf_insert (uint8_t *filt, int x)
{
if (filt == NULL)
return -1;
unsigned m = __M (filt);
unsigned k = __K (filt);
unsigned r = __R (filt);
int collisions = 0;
/* The bits that are used to represent this particular key. */
uint32_t offsets[k];
for (int i = 0; i < (int) k; ++i)
{
uint8_t collision;
uint32_t h = hash (i, x) % m;
offsets[i] = h + r;
collision = getbit (filt, offsets[i]) | getbit (filt, h * r / m);
incbit (filt, h * r / m, collision);
collisions += collision;
}
/* We have to do this in two steps because if there exists any pair i, j such
that H_i (x) == H_j (x), we will detect a false collision. Therefore, in
the most naive implementation, we calculate collisions before inserting
the key. */
for (int i = 0; i < (int) k; ++i)
incbit (filt, offsets[i], 1);
return collisions;
}
/* Queries the Bloom filter for the specified key. Returns 1 if the the key is
found and 0 if not. */
int
dlbf_query (uint8_t *filt, int x)
{
if (filt == NULL)
return -1;
for (int i = 0; i < (int) __K (filt); ++i)
{
if (getbit (filt, hash (i, x) % __M (filt) + __R (filt)) == 0)
return 0;
}
return 1;
}
/* Removes the specified key from the Bloom filter. Returns the number of hash
functions that output a bit offset that lies within a non collision free
zone when applied to the specified key. */
int
dlbf_remove (uint8_t *filt, int x)
{
if (filt == NULL || dlbf_query (filt, x) == 0)
return -1;
unsigned m = __M (filt);
unsigned k = __K (filt);
unsigned r = __R (filt);
int collisions = 0;
for (int i = 0; i < (int) k; ++i)
{
uint32_t h = hash (i, x) % m;
uint8_t collision = getbit (filt, h * r / m);
collisions += collision;
decbit (filt, h + r, collision);
}
assert (collisions <= (int) k);
return collisions;
}