collision detection for IBF, timing for test tool
[oweals/gnunet.git] / src / consensus / ibf.h
1 /*
2       This file is part of GNUnet
3       (C) 2012 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 2, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file consensus/ibf.h
23  * @brief invertible bloom filter
24  * @author Florian Dold
25  */
26
27 #ifndef GNUNET_CONSENSUS_IBF_H
28 #define GNUNET_CONSENSUS_IBF_H
29
30 #include "platform.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * Opaque handle to an invertible bloom filter (IBF).
45  *
46  * An IBF is a counting bloom filter that has the ability to restore
47  * the hashes of its stored elements with high probability.
48  */
49 struct InvertibleBloomFilter;
50
51
52 /**
53  * Create an invertible bloom filter.
54  *
55  * @param size number of IBF buckets
56  * @param hash_num number of buckets one element is hashed in
57  * @param salt salt for mingling hashes, different salt may
58  *        result in less (or more) collisions
59  * @return the newly created invertible bloom filter
60  */
61 struct InvertibleBloomFilter *
62 ibf_create(unsigned int size, unsigned int hash_num, uint32_t salt);
63
64
65 /**
66  * Insert an element into an IBF.
67  *
68  * @param ibf the IBF
69  * @param id the element's hash code
70  */
71 void
72 ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *id);
73
74
75 /**
76  * Subtract ibf2 from ibf1, storing the result in ibf1.
77  * The two IBF's must have the same parameters size and hash_num.
78  *
79  * @param ibf1 IBF that is subtracted from
80  * @param ibf2 IBF that will be subtracted from ibf1
81  */
82 void
83 ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2);
84
85
86 /**
87  * Decode and remove an element from the IBF, if possible.
88  *
89  * @param ibf the invertible bloom filter to decode
90  * @param side sign of the cell's count where the decoded element came from.
91  *             A negative sign indicates that the element was recovered resides in an IBF
92  *             that was previously subtracted from.
93  * @param ret_id the hash code of the decoded element, if successful
94  * @return GNUNET_YES if decoding an element was successful, GNUNET_NO if the IBF is empty,
95  *         GNUNET_SYSERR if the decoding has faile
96  */
97 int
98 ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct GNUNET_HashCode *ret_id);
99
100
101 /**
102  * Create a copy of an IBF, the copy has to be destroyed properly.
103  *
104  * @param ibf the IBF to copy
105  */
106 struct InvertibleBloomFilter *
107 ibf_dup (struct InvertibleBloomFilter *ibf);
108
109
110 /*
111 ibf_hton ();
112
113 ibf_ntoh ();
114
115 ibf_get_nbo_size ();
116 */
117
118 /**
119  * Destroy all resources associated with the invertible bloom filter.
120  * No more ibf_*-functions may be called on ibf after calling destroy.
121  *
122  * @param ibf the intertible bloom filter to destroy
123  */
124 void
125 ibf_destroy (struct InvertibleBloomFilter *ibf);
126
127
128 #if 0                           /* keep Emacsens' auto-indent happy */
129 {
130 #endif
131 #ifdef __cplusplus
132 }
133 #endif
134
135 #endif
136