aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/container_multiuuidmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/container_multiuuidmap.c')
-rw-r--r--src/lib/util/container_multiuuidmap.c902
1 files changed, 902 insertions, 0 deletions
diff --git a/src/lib/util/container_multiuuidmap.c b/src/lib/util/container_multiuuidmap.c
new file mode 100644
index 000000000..3e957d47b
--- /dev/null
+++ b/src/lib/util/container_multiuuidmap.c
@@ -0,0 +1,902 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2008, 2012 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/container_multiuuidmap.c
22 * @brief hash map for UUIDs where the same key may be present multiple times
23 * @author Christian Grothoff
24 */
25
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30#define LOG(kind, ...) \
31 GNUNET_log_from (kind, "util-container-multiuuidmap", __VA_ARGS__)
32
33/**
34 * Maximum recursion depth for callbacks of
35 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves s
36 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
37 * Should be totally excessive, but if violated we die.
38 */
39#define NEXT_CACHE_SIZE 16
40
41
42/**
43 * An entry in the hash map with the full key.
44 */
45struct BigMapEntry
46{
47 /**
48 * Value of the entry.
49 */
50 void *value;
51
52 /**
53 * If there is a hash collision, we create a linked list.
54 */
55 struct BigMapEntry *next;
56
57 /**
58 * Key for the entry.
59 */
60 struct GNUNET_Uuid key;
61};
62
63
64/**
65 * An entry in the hash map with just a pointer to the key.
66 */
67struct SmallMapEntry
68{
69 /**
70 * Value of the entry.
71 */
72 void *value;
73
74 /**
75 * If there is a hash collision, we create a linked list.
76 */
77 struct SmallMapEntry *next;
78
79 /**
80 * Key for the entry.
81 */
82 const struct GNUNET_Uuid *key;
83};
84
85
86/**
87 * Entry in the map.
88 */
89union MapEntry
90{
91 /**
92 * Variant used if map entries only contain a pointer to the key.
93 */
94 struct SmallMapEntry *sme;
95
96 /**
97 * Variant used if map entries contain the full key.
98 */
99 struct BigMapEntry *bme;
100};
101
102
103/**
104 * Internal representation of the hash map.
105 */
106struct GNUNET_CONTAINER_MultiUuidmap
107{
108 /**
109 * All of our buckets.
110 */
111 union MapEntry *map;
112
113 /**
114 * Number of entries in the map.
115 */
116 unsigned int size;
117
118 /**
119 * Length of the "map" array.
120 */
121 unsigned int map_length;
122
123 /**
124 * #GNUNET_NO if the map entries are of type 'struct BigMapEntry',
125 * #GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
126 */
127 int use_small_entries;
128
129 /**
130 * Counts the destructive modifications (grow, remove)
131 * to the map, so that iterators can check if they are still valid.
132 */
133 unsigned int modification_counter;
134
135 /**
136 * Map entries indicating iteration positions currently
137 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
138 * Only used up to @e next_cache_off.
139 */
140 union MapEntry next_cache[NEXT_CACHE_SIZE];
141
142 /**
143 * Offset of @e next_cache entries in use, must be smaller
144 * than #NEXT_CACHE_SIZE.
145 */
146 unsigned int next_cache_off;
147};
148
149
150/**
151 * Cursor into a multiuuidmap.
152 * Allows to enumerate elements asynchronously.
153 */
154struct GNUNET_CONTAINER_MultiUuidmapIterator
155{
156 /**
157 * Position in the bucket 'idx'
158 */
159 union MapEntry me;
160
161 /**
162 * Current bucket index.
163 */
164 unsigned int idx;
165
166 /**
167 * Modification counter as observed on the map when the iterator
168 * was created.
169 */
170 unsigned int modification_counter;
171
172 /**
173 * Map that we are iterating over.
174 */
175 const struct GNUNET_CONTAINER_MultiUuidmap *map;
176};
177
178
179/**
180 * Create a multi hash map.
181 *
182 * @param len initial size (map will grow as needed)
183 * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
184 * #GNUNET_YES means that on 'put', the 'key' does not have
185 * to be copied as the destination of the pointer is
186 * guaranteed to be life as long as the value is stored in
187 * the hashmap. This can significantly reduce memory
188 * consumption, but of course is also a recipe for
189 * heap corruption if the assumption is not true. Only
190 * use this if (1) memory use is important in this case and
191 * (2) you have triple-checked that the invariant holds
192 * @return NULL on error
193 */
194struct GNUNET_CONTAINER_MultiUuidmap *
195GNUNET_CONTAINER_multiuuidmap_create (unsigned int len, int do_not_copy_keys)
196{
197 struct GNUNET_CONTAINER_MultiUuidmap *map;
198
199 GNUNET_assert (len > 0);
200 map = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmap);
201 map->map = GNUNET_malloc_large (len * sizeof(union MapEntry));
202 if (NULL == map->map)
203 {
204 GNUNET_free (map);
205 return NULL;
206 }
207 map->map_length = len;
208 map->use_small_entries = do_not_copy_keys;
209 return map;
210}
211
212
213void
214GNUNET_CONTAINER_multiuuidmap_destroy (
215 struct GNUNET_CONTAINER_MultiUuidmap *map)
216{
217 GNUNET_assert (0 == map->next_cache_off);
218 for (unsigned int i = 0; i < map->map_length; i++)
219 {
220 union MapEntry me;
221
222 me = map->map[i];
223 if (map->use_small_entries)
224 {
225 struct SmallMapEntry *sme;
226 struct SmallMapEntry *nxt;
227
228 nxt = me.sme;
229 while (NULL != (sme = nxt))
230 {
231 nxt = sme->next;
232 GNUNET_free (sme);
233 }
234 me.sme = NULL;
235 }
236 else
237 {
238 struct BigMapEntry *bme;
239 struct BigMapEntry *nxt;
240
241 nxt = me.bme;
242 while (NULL != (bme = nxt))
243 {
244 nxt = bme->next;
245 GNUNET_free (bme);
246 }
247 me.bme = NULL;
248 }
249 }
250 GNUNET_free (map->map);
251 GNUNET_free (map);
252}
253
254
255/**
256 * Compute the index of the bucket for the given key.
257 *
258 * @param map hash map for which to compute the index
259 * @param key what key should the index be computed for
260 * @return offset into the "map" array of "map"
261 */
262static unsigned int
263idx_of (const struct GNUNET_CONTAINER_MultiUuidmap *map,
264 const struct GNUNET_Uuid *key)
265{
266 unsigned int kx;
267
268 GNUNET_assert (NULL != map);
269 GNUNET_memcpy (&kx, key, sizeof(kx));
270 return kx % map->map_length;
271}
272
273
274unsigned int
275GNUNET_CONTAINER_multiuuidmap_size (
276 const struct GNUNET_CONTAINER_MultiUuidmap *map)
277{
278 return map->size;
279}
280
281
282void *
283GNUNET_CONTAINER_multiuuidmap_get (
284 const struct GNUNET_CONTAINER_MultiUuidmap *map,
285 const struct GNUNET_Uuid *key)
286{
287 union MapEntry me;
288
289 me = map->map[idx_of (map, key)];
290 if (map->use_small_entries)
291 {
292 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
293 if (0 == GNUNET_memcmp (key, sme->key))
294 return sme->value;
295 }
296 else
297 {
298 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
299 if (0 == GNUNET_memcmp (key, &bme->key))
300 return bme->value;
301 }
302 return NULL;
303}
304
305
306enum GNUNET_GenericReturnValue
307GNUNET_CONTAINER_multiuuidmap_iterate (
308 struct GNUNET_CONTAINER_MultiUuidmap *map,
309 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
310 void *it_cls)
311{
312 int count;
313 union MapEntry me;
314 union MapEntry *ce;
315 struct GNUNET_Uuid kc;
316
317 count = 0;
318 GNUNET_assert (NULL != map);
319 ce = &map->next_cache[map->next_cache_off];
320 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
321 for (unsigned int i = 0; i < map->map_length; i++)
322 {
323 me = map->map[i];
324 if (map->use_small_entries)
325 {
326 struct SmallMapEntry *sme;
327
328 ce->sme = me.sme;
329 while (NULL != (sme = ce->sme))
330 {
331 ce->sme = sme->next;
332 if ((NULL != it) && (GNUNET_OK != it (it_cls, sme->key, sme->value)))
333 {
334 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
335 return GNUNET_SYSERR;
336 }
337 count++;
338 }
339 }
340 else
341 {
342 struct BigMapEntry *bme;
343
344 ce->bme = me.bme;
345 while (NULL != (bme = ce->bme))
346 {
347 ce->bme = bme->next;
348 if (NULL != it)
349 {
350 kc = bme->key;
351 if (GNUNET_OK != it (it_cls, &kc, bme->value))
352 {
353 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
354 return GNUNET_SYSERR;
355 }
356 }
357 count++;
358 }
359 }
360 }
361 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
362 return count;
363}
364
365
366/**
367 * We are about to free() the @a bme, make sure it is not in
368 * the list of next values for any iterator in the @a map's next_cache.
369 *
370 * @param map the map to check
371 * @param bme the entry that is about to be free'd
372 */
373static void
374update_next_cache_bme (struct GNUNET_CONTAINER_MultiUuidmap *map,
375 const struct BigMapEntry *bme)
376{
377 for (unsigned int i = 0; i < map->next_cache_off; i++)
378 if (map->next_cache[i].bme == bme)
379 map->next_cache[i].bme = bme->next;
380}
381
382
383/**
384 * We are about to free() the @a sme, make sure it is not in
385 * the list of next values for any iterator in the @a map's next_cache.
386 *
387 * @param map the map to check
388 * @param sme the entry that is about to be free'd
389 */
390static void
391update_next_cache_sme (struct GNUNET_CONTAINER_MultiUuidmap *map,
392 const struct SmallMapEntry *sme)
393{
394 for (unsigned int i = 0; i < map->next_cache_off; i++)
395 if (map->next_cache[i].sme == sme)
396 map->next_cache[i].sme = sme->next;
397}
398
399
400enum GNUNET_GenericReturnValue
401GNUNET_CONTAINER_multiuuidmap_remove (struct GNUNET_CONTAINER_MultiUuidmap *map,
402 const struct GNUNET_Uuid *key,
403 const void *value)
404{
405 union MapEntry me;
406 unsigned int i;
407
408 map->modification_counter++;
409 i = idx_of (map, key);
410 me = map->map[i];
411 if (map->use_small_entries)
412 {
413 struct SmallMapEntry *p = NULL;
414
415 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
416 {
417 if ((0 == GNUNET_memcmp (key, sme->key)) && (value == sme->value))
418 {
419 if (NULL == p)
420 map->map[i].sme = sme->next;
421 else
422 p->next = sme->next;
423 update_next_cache_sme (map, sme);
424 GNUNET_free (sme);
425 map->size--;
426 return GNUNET_YES;
427 }
428 p = sme;
429 }
430 }
431 else
432 {
433 struct BigMapEntry *p = NULL;
434
435 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
436 {
437 if ((0 == GNUNET_memcmp (key, &bme->key)) && (value == bme->value))
438 {
439 if (NULL == p)
440 map->map[i].bme = bme->next;
441 else
442 p->next = bme->next;
443 update_next_cache_bme (map, bme);
444 GNUNET_free (bme);
445 map->size--;
446 return GNUNET_YES;
447 }
448 p = bme;
449 }
450 }
451 return GNUNET_NO;
452}
453
454
455int
456GNUNET_CONTAINER_multiuuidmap_remove_all (
457 struct GNUNET_CONTAINER_MultiUuidmap *map,
458 const struct GNUNET_Uuid *key)
459{
460 union MapEntry me;
461 unsigned int i;
462 int ret;
463
464 map->modification_counter++;
465
466 ret = 0;
467 i = idx_of (map, key);
468 me = map->map[i];
469 if (map->use_small_entries)
470 {
471 struct SmallMapEntry *sme;
472 struct SmallMapEntry *p;
473
474 p = NULL;
475 sme = me.sme;
476 while (NULL != sme)
477 {
478 if (0 == GNUNET_memcmp (key, sme->key))
479 {
480 if (NULL == p)
481 map->map[i].sme = sme->next;
482 else
483 p->next = sme->next;
484 update_next_cache_sme (map, sme);
485 GNUNET_free (sme);
486 map->size--;
487 if (NULL == p)
488 sme = map->map[i].sme;
489 else
490 sme = p->next;
491 ret++;
492 }
493 else
494 {
495 p = sme;
496 sme = sme->next;
497 }
498 }
499 }
500 else
501 {
502 struct BigMapEntry *bme;
503 struct BigMapEntry *p;
504
505 p = NULL;
506 bme = me.bme;
507 while (NULL != bme)
508 {
509 if (0 == GNUNET_memcmp (key, &bme->key))
510 {
511 if (NULL == p)
512 map->map[i].bme = bme->next;
513 else
514 p->next = bme->next;
515 update_next_cache_bme (map, bme);
516 GNUNET_free (bme);
517 map->size--;
518 if (NULL == p)
519 bme = map->map[i].bme;
520 else
521 bme = p->next;
522 ret++;
523 }
524 else
525 {
526 p = bme;
527 bme = bme->next;
528 }
529 }
530 }
531 return ret;
532}
533
534
535enum GNUNET_GenericReturnValue
536GNUNET_CONTAINER_multiuuidmap_contains (
537 const struct GNUNET_CONTAINER_MultiUuidmap *map,
538 const struct GNUNET_Uuid *key)
539{
540 union MapEntry me;
541
542 me = map->map[idx_of (map, key)];
543 if (map->use_small_entries)
544 {
545 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
546 if (0 == GNUNET_memcmp (key, sme->key))
547 return GNUNET_YES;
548 }
549 else
550 {
551 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
552 if (0 == GNUNET_memcmp (key, &bme->key))
553 return GNUNET_YES;
554 }
555 return GNUNET_NO;
556}
557
558
559enum GNUNET_GenericReturnValue
560GNUNET_CONTAINER_multiuuidmap_contains_value (
561 const struct GNUNET_CONTAINER_MultiUuidmap *map,
562 const struct GNUNET_Uuid *key,
563 const void *value)
564{
565 union MapEntry me;
566
567 me = map->map[idx_of (map, key)];
568 if (map->use_small_entries)
569 {
570 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
571 if ((0 == GNUNET_memcmp (key, sme->key)) && (sme->value == value))
572 return GNUNET_YES;
573 }
574 else
575 {
576 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
577 if ((0 == GNUNET_memcmp (key, &bme->key)) && (bme->value == value))
578 return GNUNET_YES;
579 }
580 return GNUNET_NO;
581}
582
583
584/**
585 * Grow the given map to a more appropriate size.
586 *
587 * @param map the hash map to grow
588 */
589static void
590grow (struct GNUNET_CONTAINER_MultiUuidmap *map)
591{
592 union MapEntry *old_map;
593 union MapEntry *new_map;
594 unsigned int old_len;
595 unsigned int new_len;
596 unsigned int idx;
597
598 old_map = map->map;
599 old_len = map->map_length;
600 new_len = old_len * 2;
601 if (0 == new_len) /* 2^31 * 2 == 0 */
602 new_len = old_len; /* never use 0 */
603 if (new_len == old_len)
604 return; /* nothing changed */
605 new_map = GNUNET_malloc_large (new_len * sizeof(union MapEntry));
606 if (NULL == new_map)
607 return; /* grow not possible */
608 map->modification_counter++;
609 map->map_length = new_len;
610 map->map = new_map;
611 for (unsigned int i = 0; i < old_len; i++)
612 {
613 if (map->use_small_entries)
614 {
615 struct SmallMapEntry *sme;
616
617 while (NULL != (sme = old_map[i].sme))
618 {
619 old_map[i].sme = sme->next;
620 idx = idx_of (map, sme->key);
621 sme->next = new_map[idx].sme;
622 new_map[idx].sme = sme;
623 }
624 }
625 else
626 {
627 struct BigMapEntry *bme;
628
629 while (NULL != (bme = old_map[i].bme))
630 {
631 old_map[i].bme = bme->next;
632 idx = idx_of (map, &bme->key);
633 bme->next = new_map[idx].bme;
634 new_map[idx].bme = bme;
635 }
636 }
637 }
638 GNUNET_free (old_map);
639}
640
641
642enum GNUNET_GenericReturnValue
643GNUNET_CONTAINER_multiuuidmap_put (struct GNUNET_CONTAINER_MultiUuidmap *map,
644 const struct GNUNET_Uuid *key,
645 void *value,
646 enum GNUNET_CONTAINER_MultiHashMapOption opt)
647{
648 union MapEntry me;
649 unsigned int i;
650
651 i = idx_of (map, key);
652 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
653 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
654 {
655 me = map->map[i];
656 if (map->use_small_entries)
657 {
658 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
659 if (0 == GNUNET_memcmp (key, sme->key))
660 {
661 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
662 return GNUNET_SYSERR;
663 sme->value = value;
664 return GNUNET_NO;
665 }
666 }
667 else
668 {
669 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
670 if (0 == GNUNET_memcmp (key, &bme->key))
671 {
672 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
673 return GNUNET_SYSERR;
674 bme->value = value;
675 return GNUNET_NO;
676 }
677 }
678 }
679 if (map->size / 3 >= map->map_length / 4)
680 {
681 grow (map);
682 i = idx_of (map, key);
683 }
684 if (map->use_small_entries)
685 {
686 struct SmallMapEntry *sme;
687
688 sme = GNUNET_new (struct SmallMapEntry);
689 sme->key = key;
690 sme->value = value;
691 sme->next = map->map[i].sme;
692 map->map[i].sme = sme;
693 }
694 else
695 {
696 struct BigMapEntry *bme;
697
698 bme = GNUNET_new (struct BigMapEntry);
699 bme->key = *key;
700 bme->value = value;
701 bme->next = map->map[i].bme;
702 map->map[i].bme = bme;
703 }
704 map->size++;
705 return GNUNET_OK;
706}
707
708
709/**
710 * Iterate over all entries in the map that match a particular key.
711 *
712 * @param map the map
713 * @param key key that the entries must correspond to
714 * @param it function to call on each entry
715 * @param it_cls extra argument to @a it
716 * @return the number of key value pairs processed,
717 * #GNUNET_SYSERR if it aborted iteration
718 */
719int
720GNUNET_CONTAINER_multiuuidmap_get_multiple (
721 struct GNUNET_CONTAINER_MultiUuidmap *map,
722 const struct GNUNET_Uuid *key,
723 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
724 void *it_cls)
725{
726 int count;
727 union MapEntry me;
728 union MapEntry *ce;
729
730 ce = &map->next_cache[map->next_cache_off];
731 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
732 count = 0;
733 me = map->map[idx_of (map, key)];
734 if (map->use_small_entries)
735 {
736 struct SmallMapEntry *sme;
737
738 ce->sme = me.sme;
739 while (NULL != (sme = ce->sme))
740 {
741 ce->sme = sme->next;
742 if (0 != GNUNET_memcmp (key, sme->key))
743 continue;
744 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, sme->value)))
745 {
746 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
747 return GNUNET_SYSERR;
748 }
749 count++;
750 }
751 }
752 else
753 {
754 struct BigMapEntry *bme;
755
756 ce->bme = me.bme;
757 while (NULL != (bme = ce->bme))
758 {
759 ce->bme = bme->next;
760 if (0 != GNUNET_memcmp (key, &bme->key))
761 continue;
762 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, bme->value)))
763 {
764 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
765 return GNUNET_SYSERR;
766 }
767 count++;
768 }
769 }
770 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
771 return count;
772}
773
774
775/**
776 * @ingroup hashmap
777 * Call @a it on a random value from the map, or not at all
778 * if the map is empty. Note that this function has linear
779 * complexity (in the size of the map).
780 *
781 * @param map the map
782 * @param it function to call on a random entry
783 * @param it_cls extra argument to @a it
784 * @return the number of key value pairs processed, zero or one.
785 */
786unsigned int
787GNUNET_CONTAINER_multiuuidmap_get_random (
788 const struct GNUNET_CONTAINER_MultiUuidmap *map,
789 GNUNET_CONTAINER_MultiUuidmapIteratorCallback it,
790 void *it_cls)
791{
792 unsigned int off;
793 union MapEntry me;
794
795 if (0 == map->size)
796 return 0;
797 if (NULL == it)
798 return 1;
799 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, map->size);
800 for (unsigned int idx = 0; idx < map->map_length; idx++)
801 {
802 me = map->map[idx];
803 if (map->use_small_entries)
804 {
805 for (struct SmallMapEntry *sme = me.sme; NULL != sme; sme = sme->next)
806 {
807 if (0 == off)
808 {
809 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
810 return GNUNET_SYSERR;
811 return 1;
812 }
813 off--;
814 }
815 }
816 else
817 {
818 for (struct BigMapEntry *bme = me.bme; NULL != bme; bme = bme->next)
819 {
820 if (0 == off)
821 {
822 if (GNUNET_OK != it (it_cls, &bme->key, bme->value))
823 return GNUNET_SYSERR;
824 return 1;
825 }
826 off--;
827 }
828 }
829 }
830 GNUNET_break (0);
831 return GNUNET_SYSERR;
832}
833
834
835struct GNUNET_CONTAINER_MultiUuidmapIterator *
836GNUNET_CONTAINER_multiuuidmap_iterator_create (
837 const struct GNUNET_CONTAINER_MultiUuidmap *map)
838{
839 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter;
840
841 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiUuidmapIterator);
842 iter->map = map;
843 iter->modification_counter = map->modification_counter;
844 iter->me = map->map[0];
845 return iter;
846}
847
848
849enum GNUNET_GenericReturnValue
850GNUNET_CONTAINER_multiuuidmap_iterator_next (
851 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter,
852 struct GNUNET_Uuid *key,
853 const void **value)
854{
855 /* make sure the map has not been modified */
856 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
857
858 /* look for the next entry, skipping empty buckets */
859 while (1)
860 {
861 if (iter->idx >= iter->map->map_length)
862 return GNUNET_NO;
863 if (GNUNET_YES == iter->map->use_small_entries)
864 {
865 if (NULL != iter->me.sme)
866 {
867 if (NULL != key)
868 *key = *iter->me.sme->key;
869 if (NULL != value)
870 *value = iter->me.sme->value;
871 iter->me.sme = iter->me.sme->next;
872 return GNUNET_YES;
873 }
874 }
875 else
876 {
877 if (NULL != iter->me.bme)
878 {
879 if (NULL != key)
880 *key = iter->me.bme->key;
881 if (NULL != value)
882 *value = iter->me.bme->value;
883 iter->me.bme = iter->me.bme->next;
884 return GNUNET_YES;
885 }
886 }
887 iter->idx += 1;
888 if (iter->idx < iter->map->map_length)
889 iter->me = iter->map->map[iter->idx];
890 }
891}
892
893
894void
895GNUNET_CONTAINER_multiuuidmap_iterator_destroy (
896 struct GNUNET_CONTAINER_MultiUuidmapIterator *iter)
897{
898 GNUNET_free (iter);
899}
900
901
902/* end of container_multiuuidmap.c */