-fixing build issues in fs/set related to #3047
[oweals/gnunet.git] / src / set / 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 3, 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 set/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_util_lib.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41
42 struct IBF_Key
43 {
44   uint64_t key_val;
45 };
46
47 struct IBF_KeyHash
48 {
49   uint32_t key_hash_val;
50 };
51
52 struct IBF_Count
53 {
54   int8_t count_val;
55 };
56
57 /**
58  * Size of one ibf bucket in bytes
59  */
60 #define IBF_BUCKET_SIZE (sizeof (struct IBF_Count) + sizeof (struct IBF_Key) + \
61     sizeof (struct IBF_KeyHash))
62
63 /**
64  * Invertible bloom filter (IBF).
65  *
66  * An IBF is a counting bloom filter that has the ability to restore
67  * the hashes of its stored elements with high probability.
68  */
69 struct InvertibleBloomFilter
70 {
71   /**
72    * How many cells does this IBF have?
73    */
74   uint32_t size;
75
76   /**
77    * In how many cells do we hash one element?
78    * Usually 4 or 3.
79    */
80   uint8_t hash_num;
81
82   /**
83    * Xor sums of the elements' keys, used to identify the elements.
84    * Array of 'size' elements.
85    */
86   struct IBF_Key *key_sum;
87
88   /**
89    * Xor sums of the hashes of the keys of inserted elements.
90    * Array of 'size' elements.
91    */
92   struct IBF_KeyHash *key_hash_sum;
93
94   /**
95    * How many times has a bucket been hit?
96    * Can be negative, as a result of IBF subtraction.
97    * Array of 'size' elements.
98    */
99   struct IBF_Count *count;
100 };
101
102
103 /**
104  * Write buckets from an ibf to a buffer.
105  * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
106  * 
107  * @param ibf the ibf to write
108  * @param start with which bucket to start
109  * @param count how many buckets to write
110  * @param buf buffer to write the data to
111  */
112 void
113 ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf);
114
115
116 /**
117  * Read buckets from a buffer into an ibf.
118  *
119  * @param buf pointer to the buffer to read from
120  * @param start which bucket to start at
121  * @param count how many buckets to read
122  * @param ibf the ibf to read from
123  */
124 void
125 ibf_read_slice (const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf);
126
127
128 /**
129  * Create a key from a hashcode.
130  *
131  * @param hash the hashcode
132  * @return a key
133  */
134 struct IBF_Key
135 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
136
137
138 /**
139  * Create a hashcode from a key, by replicating the key
140  * until the hascode is filled
141  *
142  * @param key the key
143  * @param dst hashcode to store the result in
144  */
145 void
146 ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
147
148
149 /**
150  * Create an invertible bloom filter.
151  *
152  * @param size number of IBF buckets
153  * @param hash_num number of buckets one element is hashed in, usually 3 or 4
154  * @return the newly created invertible bloom filter
155  */
156 struct InvertibleBloomFilter *
157 ibf_create (uint32_t size, uint8_t hash_num);
158
159
160 /**
161  * Insert an element into an IBF.
162  *
163  * @param ibf the IBF
164  * @param key the element's hash code
165  */
166 void
167 ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
168
169
170 /**
171  * Subtract ibf2 from ibf1, storing the result in ibf1.
172  * The two IBF's must have the same parameters size and hash_num.
173  *
174  * @param ibf1 IBF that is subtracted from
175  * @param ibf2 IBF that will be subtracted from ibf1
176  */
177 void
178 ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2);
179
180
181 /**
182  * Decode and remove an element from the IBF, if possible.
183  *
184  * @param ibf the invertible bloom filter to decode
185  * @param ret_side sign of the cell's count where the decoded element came from.
186  *                 A negative sign indicates that the element was recovered
187  *                 resides in an IBF that was previously subtracted from.
188  * @param ret_id receives the hash code of the decoded element, if successful
189  * @return GNUNET_YES if decoding an element was successful,
190  *         GNUNET_NO if the IBF is empty,
191  *         GNUNET_SYSERR if the decoding has failed
192  */
193 int
194 ibf_decode (struct InvertibleBloomFilter *ibf, int *ret_side, struct IBF_Key *ret_id);
195
196
197 /**
198  * Create a copy of an IBF, the copy has to be destroyed properly.
199  *
200  * @param ibf the IBF to copy
201  */
202 struct InvertibleBloomFilter *
203 ibf_dup (const struct InvertibleBloomFilter *ibf);
204
205 /**
206  * Destroy all resources associated with the invertible bloom filter.
207  * No more ibf_*-functions may be called on ibf after calling destroy.
208  *
209  * @param ibf the intertible bloom filter to destroy
210  */
211 void
212 ibf_destroy (struct InvertibleBloomFilter *ibf);
213
214
215 #if 0                           /* keep Emacsens' auto-indent happy */
216 {
217 #endif
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif
223