aboutsummaryrefslogtreecommitdiff
path: root/src/util/container_bloomfilter.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-10-04 08:17:52 +0000
committerChristian Grothoff <christian@grothoff.org>2010-10-04 08:17:52 +0000
commita9e1af3613a7fff3edde2362cb235acb63cfd350 (patch)
treef10428560bad378e7f1143f6ff7905c066ed5d9e /src/util/container_bloomfilter.c
parent5fc9d7e6702ff300f5bce13352811962145b0383 (diff)
downloadgnunet-a9e1af3613a7fff3edde2362cb235acb63cfd350.tar.gz
gnunet-a9e1af3613a7fff3edde2362cb235acb63cfd350.zip
improved bf api
Diffstat (limited to 'src/util/container_bloomfilter.c')
-rw-r--r--src/util/container_bloomfilter.c47
1 files changed, 33 insertions, 14 deletions
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index 3902e4318..d9cadb945 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -75,6 +75,23 @@ struct GNUNET_CONTAINER_BloomFilter
75}; 75};
76 76
77 77
78
79/**
80 * Get size of the bloom filter.
81 *
82 * @param bf the filter
83 * @return number of bytes used for the data of the bloom filter
84 */
85size_t
86GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
87 *bf)
88{
89 if (bf == NULL)
90 return 0;
91 return bf->bitArraySize;
92}
93
94
78/** 95/**
79 * Sets a bit active in the bitArray. Increment bit-specific 96 * Sets a bit active in the bitArray. Increment bit-specific
80 * usage counter on disk only if below 4bit max (==15). 97 * usage counter on disk only if below 4bit max (==15).
@@ -287,7 +304,7 @@ makeEmptyFile (const struct GNUNET_DISK_FileHandle *fh, size_t size)
287 * @param bit the current bit 304 * @param bit the current bit
288 */ 305 */
289typedef void (*BitIterator) (void *cls, 306typedef void (*BitIterator) (void *cls,
290 struct GNUNET_CONTAINER_BloomFilter * bf, 307 const struct GNUNET_CONTAINER_BloomFilter * bf,
291 unsigned int bit); 308 unsigned int bit);
292 309
293/** 310/**
@@ -300,7 +317,7 @@ typedef void (*BitIterator) (void *cls,
300 * @param key the key for which we iterate over the BF bits 317 * @param key the key for which we iterate over the BF bits
301 */ 318 */
302static void 319static void
303iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf, 320iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
304 BitIterator callback, void *arg, const GNUNET_HashCode * key) 321 BitIterator callback, void *arg, const GNUNET_HashCode * key)
305{ 322{
306 GNUNET_HashCode tmp[2]; 323 GNUNET_HashCode tmp[2];
@@ -337,31 +354,33 @@ iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf,
337/** 354/**
338 * Callback: increment bit 355 * Callback: increment bit
339 * 356 *
340 * @param cls not used 357 * @param cls pointer to writeable form of bf
341 * @param bf the filter to manipulate 358 * @param bf the filter to manipulate
342 * @param bit the bit to increment 359 * @param bit the bit to increment
343 */ 360 */
344static void 361static void
345incrementBitCallback (void *cls, 362incrementBitCallback (void *cls,
346 struct GNUNET_CONTAINER_BloomFilter *bf, 363 const struct GNUNET_CONTAINER_BloomFilter *bf,
347 unsigned int bit) 364 unsigned int bit)
348{ 365{
349 incrementBit (bf->bitArray, bit, bf->fh); 366 struct GNUNET_CONTAINER_BloomFilter *b = cls;
367 incrementBit (b->bitArray, bit, bf->fh);
350} 368}
351 369
352/** 370/**
353 * Callback: decrement bit 371 * Callback: decrement bit
354 * 372 *
355 * @param cls not used 373 * @param cls pointer to writeable form of bf
356 * @param bf the filter to manipulate 374 * @param bf the filter to manipulate
357 * @param bit the bit to decrement 375 * @param bit the bit to decrement
358 */ 376 */
359static void 377static void
360decrementBitCallback (void *cls, 378decrementBitCallback (void *cls,
361 struct GNUNET_CONTAINER_BloomFilter *bf, 379 const struct GNUNET_CONTAINER_BloomFilter *bf,
362 unsigned int bit) 380 unsigned int bit)
363{ 381{
364 decrementBit (bf->bitArray, bit, bf->fh); 382 struct GNUNET_CONTAINER_BloomFilter *b = cls;
383 decrementBit (b->bitArray, bit, bf->fh);
365} 384}
366 385
367/** 386/**
@@ -373,7 +392,8 @@ decrementBitCallback (void *cls,
373 */ 392 */
374static void 393static void
375testBitCallback (void *cls, 394testBitCallback (void *cls,
376 struct GNUNET_CONTAINER_BloomFilter *bf, unsigned int bit) 395 const struct GNUNET_CONTAINER_BloomFilter *bf,
396 unsigned int bit)
377{ 397{
378 int *arg = cls; 398 int *arg = cls;
379 if (GNUNET_NO == testBit (bf->bitArray, bit)) 399 if (GNUNET_NO == testBit (bf->bitArray, bit))
@@ -538,12 +558,11 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data,
538 * @return GNUNET_SYSERR if the data array is not big enough 558 * @return GNUNET_SYSERR if the data array is not big enough
539 */ 559 */
540int 560int
541GNUNET_CONTAINER_bloomfilter_get_raw_data (struct GNUNET_CONTAINER_BloomFilter 561GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct GNUNET_CONTAINER_BloomFilter
542 *bf, char *data, size_t size) 562 *bf, char *data, size_t size)
543{ 563{
544 if (NULL == bf) 564 if (NULL == bf)
545 return GNUNET_SYSERR; 565 return GNUNET_SYSERR;
546
547 if (bf->bitArraySize != size) 566 if (bf->bitArraySize != size)
548 return GNUNET_SYSERR; 567 return GNUNET_SYSERR;
549 memcpy (data, bf->bitArray, size); 568 memcpy (data, bf->bitArray, size);
@@ -594,7 +613,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
594 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not 613 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
595 */ 614 */
596int 615int
597GNUNET_CONTAINER_bloomfilter_test (struct GNUNET_CONTAINER_BloomFilter *bf, 616GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
598 const GNUNET_HashCode * e) 617 const GNUNET_HashCode * e)
599{ 618{
600 int res; 619 int res;
@@ -619,7 +638,7 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
619 638
620 if (NULL == bf) 639 if (NULL == bf)
621 return; 640 return;
622 iterateBits (bf, &incrementBitCallback, NULL, e); 641 iterateBits (bf, &incrementBitCallback, bf, e);
623} 642}
624 643
625 644
@@ -706,7 +725,7 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
706 return; 725 return;
707 if (bf->filename == NULL) 726 if (bf->filename == NULL)
708 return; 727 return;
709 iterateBits (bf, &decrementBitCallback, NULL, e); 728 iterateBits (bf, &decrementBitCallback, bf, e);
710} 729}
711 730
712/** 731/**