diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-10-04 08:17:52 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-10-04 08:17:52 +0000 |
commit | a9e1af3613a7fff3edde2362cb235acb63cfd350 (patch) | |
tree | f10428560bad378e7f1143f6ff7905c066ed5d9e /src/util/container_bloomfilter.c | |
parent | 5fc9d7e6702ff300f5bce13352811962145b0383 (diff) | |
download | gnunet-a9e1af3613a7fff3edde2362cb235acb63cfd350.tar.gz gnunet-a9e1af3613a7fff3edde2362cb235acb63cfd350.zip |
improved bf api
Diffstat (limited to 'src/util/container_bloomfilter.c')
-rw-r--r-- | src/util/container_bloomfilter.c | 47 |
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 | */ | ||
85 | size_t | ||
86 | GNUNET_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 | */ |
289 | typedef void (*BitIterator) (void *cls, | 306 | typedef 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 | */ |
302 | static void | 319 | static void |
303 | iterateBits (struct GNUNET_CONTAINER_BloomFilter *bf, | 320 | iterateBits (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 | */ |
344 | static void | 361 | static void |
345 | incrementBitCallback (void *cls, | 362 | incrementBitCallback (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 | */ |
359 | static void | 377 | static void |
360 | decrementBitCallback (void *cls, | 378 | decrementBitCallback (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 | */ |
374 | static void | 393 | static void |
375 | testBitCallback (void *cls, | 394 | testBitCallback (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 | */ |
540 | int | 560 | int |
541 | GNUNET_CONTAINER_bloomfilter_get_raw_data (struct GNUNET_CONTAINER_BloomFilter | 561 | GNUNET_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 | */ |
596 | int | 615 | int |
597 | GNUNET_CONTAINER_bloomfilter_test (struct GNUNET_CONTAINER_BloomFilter *bf, | 616 | GNUNET_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 | /** |