-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom-filter.h
112 lines (93 loc) · 3.22 KB
/
bloom-filter.h
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
/* bloom-filter.h
*
* Copyright (C) 2012 Christian Hergert <[email protected]>
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file 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
* Lesser 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 <http://www.gnu.org/licenses/>.
*/
#ifndef BLOOM_FILTER_H
#define BLOOM_FILTER_H
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _BloomFilter BloomFilter;
struct _BloomFilter
{
/*< private >*/
gint ref_count;
GPtrArray *hash_funcs;
gsize width;
gint key_len;
guint8 data[0];
};
typedef guint32 (*BloomHashFunc) (gconstpointer key, int len, guint32 seed);
void bloom_filter_remove_all (BloomFilter *filter);
GType bloom_filter_get_type (void) G_GNUC_CONST;
BloomFilter *bloom_filter_new_full (gsize width,
gint key_len,
guint n_hash_funcs,
BloomHashFunc first_hash_func,
...);
BloomFilter *bloom_filter_new_murmur (gsize width,
gint key_len,
guint n_hash_funcs);
BloomFilter *bloom_filter_ref (BloomFilter *filter);
void bloom_filter_unref (BloomFilter *filter);
G_INLINE_FUNC gboolean
bloom_filter_get_bit (BloomFilter *filter,
gsize bit)
{
g_return_val_if_fail(filter != NULL, FALSE);
g_return_val_if_fail(bit < filter->width, FALSE);
return !!(filter->data[bit >> 3] & (1 << (bit & 0x7)));
}
G_INLINE_FUNC void
bloom_filter_set_bit (BloomFilter *filter,
gsize bit)
{
g_return_if_fail(filter != NULL);
g_return_if_fail(bit < filter->width);
filter->data[bit >> 3] |= (1 << (bit & 0x7));
}
G_INLINE_FUNC void
bloom_filter_insert (BloomFilter *filter,
gconstpointer key)
{
BloomHashFunc hash_func;
guint i;
guint32 seed = 0;
g_return_if_fail(filter);
for (i = 0; i < filter->hash_funcs->len; i++) {
hash_func = g_ptr_array_index(filter->hash_funcs, i);
seed = hash_func(key, filter->key_len, seed);
bloom_filter_set_bit(filter, seed % filter->width);
}
}
G_INLINE_FUNC gboolean
bloom_filter_contains (BloomFilter *filter,
gconstpointer key)
{
BloomHashFunc hash_func;
guint i;
guint32 seed = 0;
g_return_val_if_fail(filter, FALSE);
for (i = 0; i < filter->hash_funcs->len; i++) {
hash_func = g_ptr_array_index(filter->hash_funcs, i);
seed = hash_func(key, filter->key_len, seed);
if (!bloom_filter_get_bit(filter, seed % filter->width)) {
return FALSE;
}
}
return TRUE;
}
G_END_DECLS
#endif /* BLOOM_FILTER_H */