implemented the invertible bloom filter
[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 void
80 ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2);
81
82
83 /**
84  * Decode and remove an element from the IBF, if possible.
85  *
86  * @param ibf the invertible bloom filter to decode
87  * @param ret_id the hash code of the decoded element, if successful
88  * @param side sign of the cell's count where the decoded element came from.
89  *             A negative sign indicates that the element was recovered resides in an IBF
90  *             that was previously subtracted from.
91  * @return GNUNET_YES if decoding an element was successful, GNUNET_NO if the IBF is empty,
92  *         GNUNET_SYSERR if the decoding has faile
93  */
94 int
95 ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct GNUNET_HashCode *ret_id);
96
97
98 /**
99  * Create a copy of an IBF, the copy has to be destroyed properly.
100  *
101  * @param ibf the IBF to copy
102  */
103 struct InvertibleBloomFilter *
104 ibf_dup (struct InvertibleBloomFilter *ibf);
105
106
107 /*
108 ibf_hton ();
109
110 ibf_ntoh ();
111
112 ibf_get_nbo_size ();
113 */
114
115 /**
116  * Destroy all resources associated with the invertible bloom filter.
117  * No more ibf_*-functions may be called on ibf after calling destroy.
118  *
119  * @param ibf the intertible bloom filter to destroy
120  */
121 void
122 ibf_destroy (struct InvertibleBloomFilter *ibf);
123
124
125 #if 0                           /* keep Emacsens' auto-indent happy */
126 {
127 #endif
128 #ifdef __cplusplus
129 }
130 #endif
131
132 #endif
133