2 This file is part of GNUnet.
3 Copyright (C) 2008, 2012 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/container_multipeermap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multipeermap", __VA_ARGS__)
32 * An entry in the hash map with the full key.
43 * If there is a hash collision, we create a linked list.
45 struct BigMapEntry *next;
50 struct GNUNET_PeerIdentity key;
56 * An entry in the hash map with just a pointer to the key.
67 * If there is a hash collision, we create a linked list.
69 struct SmallMapEntry *next;
74 const struct GNUNET_PeerIdentity *key;
85 * Variant used if map entries only contain a pointer to the key.
87 struct SmallMapEntry *sme;
90 * Variant used if map entries contain the full key.
92 struct BigMapEntry *bme;
97 * Internal representation of the hash map.
99 struct GNUNET_CONTAINER_MultiPeerMap
102 * All of our buckets.
107 * Number of entries in the map.
112 * Length of the "map" array.
114 unsigned int map_length;
117 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
118 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
120 int use_small_entries;
123 * Counts the destructive modifications (grow, remove)
124 * to the map, so that iterators can check if they are still valid.
126 unsigned int modification_counter;
131 * Cursor into a multipeermap.
132 * Allows to enumerate elements asynchronously.
134 struct GNUNET_CONTAINER_MultiPeerMapIterator
137 * Position in the bucket 'idx'
142 * Current bucket index.
147 * Modification counter as observed on the map when the iterator
150 unsigned int modification_counter;
153 * Map that we are iterating over.
155 const struct GNUNET_CONTAINER_MultiPeerMap *map;
160 * Create a multi hash map.
162 * @param len initial size (map will grow as needed)
163 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
164 * GNUNET_YES means that on 'put', the 'key' does not have
165 * to be copied as the destination of the pointer is
166 * guaranteed to be life as long as the value is stored in
167 * the hashmap. This can significantly reduce memory
168 * consumption, but of course is also a recipie for
169 * heap corruption if the assumption is not true. Only
170 * use this if (1) memory use is important in this case and
171 * (2) you have triple-checked that the invariant holds
172 * @return NULL on error
174 struct GNUNET_CONTAINER_MultiPeerMap *
175 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
176 int do_not_copy_keys)
178 struct GNUNET_CONTAINER_MultiPeerMap *map;
180 GNUNET_assert (len > 0);
181 map = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMap);
182 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
183 map->map_length = len;
184 map->use_small_entries = do_not_copy_keys;
190 * Destroy a hash map. Will not free any values
191 * stored in the hash map!
196 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap
202 for (i = 0; i < map->map_length; i++)
205 if (map->use_small_entries)
207 struct SmallMapEntry *sme;
208 struct SmallMapEntry *nxt;
211 while (NULL != (sme = nxt))
220 struct BigMapEntry *bme;
221 struct BigMapEntry *nxt;
224 while (NULL != (bme = nxt))
232 GNUNET_free (map->map);
238 * Compute the index of the bucket for the given key.
240 * @param map hash map for which to compute the index
241 * @param key what key should the index be computed for
242 * @return offset into the "map" array of "map"
245 idx_of (const struct GNUNET_CONTAINER_MultiPeerMap *map,
246 const struct GNUNET_PeerIdentity *key)
250 GNUNET_assert (NULL != map);
251 GNUNET_memcpy (&kx, key, sizeof (kx));
252 return kx % map->map_length;
257 * Get the number of key-value pairs in the map.
260 * @return the number of key value pairs
263 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap
271 * Given a key find a value in the map matching the key.
274 * @param key what to look for
275 * @return NULL if no value was found; note that
276 * this is indistinguishable from values that just
277 * happen to be NULL; use "contains" to test for
278 * key-value pairs with value NULL
281 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap
282 *map, const struct GNUNET_PeerIdentity *key)
286 me = map->map[idx_of (map, key)];
287 if (map->use_small_entries)
289 struct SmallMapEntry *sme;
291 for (sme = me.sme; NULL != sme; sme = sme->next)
292 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
297 struct BigMapEntry *bme;
299 for (bme = me.bme; NULL != bme; bme = bme->next)
300 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
308 * Iterate over all entries in the map.
311 * @param it function to call on each entry
312 * @param it_cls extra argument to @a it
313 * @return the number of key value pairs processed,
314 * #GNUNET_SYSERR if it aborted iteration
317 GNUNET_CONTAINER_multipeermap_iterate (const struct
318 GNUNET_CONTAINER_MultiPeerMap *map,
319 GNUNET_CONTAINER_PeerMapIterator it,
325 struct GNUNET_PeerIdentity kc;
328 GNUNET_assert (NULL != map);
329 for (i = 0; i < map->map_length; i++)
332 if (map->use_small_entries)
334 struct SmallMapEntry *sme;
335 struct SmallMapEntry *nxt;
338 while (NULL != (sme = nxt))
343 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
344 return GNUNET_SYSERR;
351 struct BigMapEntry *bme;
352 struct BigMapEntry *nxt;
355 while (NULL != (bme = nxt))
361 if (GNUNET_OK != it (it_cls, &kc, bme->value))
362 return GNUNET_SYSERR;
373 * Remove the given key-value pair from the map. Note that if the
374 * key-value pair is in the map multiple times, only one of the pairs
378 * @param key key of the key-value pair
379 * @param value value of the key-value pair
380 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
384 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
385 const struct GNUNET_PeerIdentity *key,
391 map->modification_counter++;
393 i = idx_of (map, key);
395 if (map->use_small_entries)
397 struct SmallMapEntry *sme;
398 struct SmallMapEntry *p;
401 for (sme = me.sme; NULL != sme; sme = sme->next)
403 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
404 (value == sme->value))
407 map->map[i].sme = sme->next;
419 struct BigMapEntry *bme;
420 struct BigMapEntry *p;
423 for (bme = me.bme; NULL != bme; bme = bme->next)
425 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
426 (value == bme->value))
429 map->map[i].bme = bme->next;
444 * Remove all entries for the given key from the map.
445 * Note that the values would not be "freed".
448 * @param key identifies values to be removed
449 * @return number of values removed
452 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap
453 *map, const struct GNUNET_PeerIdentity *key)
459 map->modification_counter++;
462 i = idx_of (map, key);
464 if (map->use_small_entries)
466 struct SmallMapEntry *sme;
467 struct SmallMapEntry *p;
473 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
476 map->map[i].sme = sme->next;
482 sme = map->map[i].sme;
496 struct BigMapEntry *bme;
497 struct BigMapEntry *p;
503 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
506 map->map[i].bme = bme->next;
512 bme = map->map[i].bme;
529 * Check if the map contains any value under the given
530 * key (including values that are NULL).
533 * @param key the key to test if a value exists for it
534 * @return GNUNET_YES if such a value exists,
538 GNUNET_CONTAINER_multipeermap_contains (const struct
539 GNUNET_CONTAINER_MultiPeerMap *map,
540 const struct GNUNET_PeerIdentity *key)
544 me = map->map[idx_of (map, key)];
545 if (map->use_small_entries)
547 struct SmallMapEntry *sme;
549 for (sme = me.sme; NULL != sme; sme = sme->next)
550 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
555 struct BigMapEntry *bme;
557 for (bme = me.bme; NULL != bme; bme = bme->next)
558 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
566 * Check if the map contains the given value under the given
570 * @param key the key to test if a value exists for it
571 * @param value value to test for
572 * @return GNUNET_YES if such a value exists,
576 GNUNET_CONTAINER_multipeermap_contains_value (const struct
577 GNUNET_CONTAINER_MultiPeerMap
578 *map, const struct GNUNET_PeerIdentity *key,
583 me = map->map[idx_of (map, key)];
584 if (map->use_small_entries)
586 struct SmallMapEntry *sme;
588 for (sme = me.sme; NULL != sme; sme = sme->next)
589 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity))) &&
590 (sme->value == value) )
595 struct BigMapEntry *bme;
597 for (bme = me.bme; NULL != bme; bme = bme->next)
598 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity))) &&
599 (bme->value == value) )
607 * Grow the given map to a more appropriate size.
609 * @param map the hash map to grow
612 grow (struct GNUNET_CONTAINER_MultiPeerMap *map)
614 union MapEntry *old_map;
615 union MapEntry *new_map;
616 unsigned int old_len;
617 unsigned int new_len;
621 map->modification_counter++;
624 old_len = map->map_length;
625 new_len = old_len * 2;
626 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
627 map->map_length = new_len;
629 for (i = 0; i < old_len; i++)
631 if (map->use_small_entries)
633 struct SmallMapEntry *sme;
635 while (NULL != (sme = old_map[i].sme))
637 old_map[i].sme = sme->next;
638 idx = idx_of (map, sme->key);
639 sme->next = new_map[idx].sme;
640 new_map[idx].sme = sme;
645 struct BigMapEntry *bme;
647 while (NULL != (bme = old_map[i].bme))
649 old_map[i].bme = bme->next;
650 idx = idx_of (map, &bme->key);
651 bme->next = new_map[idx].bme;
652 new_map[idx].bme = bme;
656 GNUNET_free (old_map);
661 * Store a key-value pair in the map.
664 * @param key key to use
665 * @param value value to use
666 * @param opt options for put
667 * @return #GNUNET_OK on success,
668 * #GNUNET_NO if a value was replaced (with REPLACE)
669 * #GNUNET_SYSERR if GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
670 * value already exists
673 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
674 const struct GNUNET_PeerIdentity *key,
676 enum GNUNET_CONTAINER_MultiHashMapOption opt)
681 i = idx_of (map, key);
682 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
683 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
686 if (map->use_small_entries)
688 struct SmallMapEntry *sme;
690 for (sme = me.sme; NULL != sme; sme = sme->next)
691 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
693 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
694 return GNUNET_SYSERR;
701 struct BigMapEntry *bme;
703 for (bme = me.bme; NULL != bme; bme = bme->next)
704 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
706 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
707 return GNUNET_SYSERR;
713 if (map->size / 3 >= map->map_length / 4)
716 i = idx_of (map, key);
718 if (map->use_small_entries)
720 struct SmallMapEntry *sme;
722 sme = GNUNET_new (struct SmallMapEntry);
725 sme->next = map->map[i].sme;
726 map->map[i].sme = sme;
730 struct BigMapEntry *bme;
732 bme = GNUNET_new (struct BigMapEntry);
735 bme->next = map->map[i].bme;
736 map->map[i].bme = bme;
744 * Iterate over all entries in the map that match a particular key.
747 * @param key key that the entries must correspond to
748 * @param it function to call on each entry
749 * @param it_cls extra argument to @a it
750 * @return the number of key value pairs processed,
751 * #GNUNET_SYSERR if it aborted iteration
754 GNUNET_CONTAINER_multipeermap_get_multiple (const struct GNUNET_CONTAINER_MultiPeerMap *map,
755 const struct GNUNET_PeerIdentity *key,
756 GNUNET_CONTAINER_PeerMapIterator it,
763 me = map->map[idx_of (map, key)];
764 if (map->use_small_entries)
766 struct SmallMapEntry *sme;
767 struct SmallMapEntry *nxt;
770 while (NULL != (sme = nxt))
773 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_PeerIdentity)))
775 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
776 return GNUNET_SYSERR;
782 struct BigMapEntry *bme;
783 struct BigMapEntry *nxt;
786 while (NULL != (bme = nxt))
789 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_PeerIdentity)))
791 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
792 return GNUNET_SYSERR;
802 * Call @a it on a random value from the map, or not at all
803 * if the map is empty. Note that this function has linear
804 * complexity (in the size of the map).
807 * @param it function to call on a random entry
808 * @param it_cls extra argument to @a it
809 * @return the number of key value pairs processed, zero or one.
812 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
813 GNUNET_CONTAINER_PeerMapIterator it,
824 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
826 for (idx = 0; idx < map->map_length; idx++)
829 if (map->use_small_entries)
831 struct SmallMapEntry *sme;
832 struct SmallMapEntry *nxt;
835 while (NULL != (sme = nxt))
840 if (GNUNET_OK != it (it_cls,
843 return GNUNET_SYSERR;
851 struct BigMapEntry *bme;
852 struct BigMapEntry *nxt;
855 while (NULL != (bme = nxt))
860 if (GNUNET_OK != it (it_cls,
861 &bme->key, bme->value))
862 return GNUNET_SYSERR;
870 return GNUNET_SYSERR;
875 * Create an iterator for a multipeermap.
876 * The iterator can be used to retrieve all the elements in the multipeermap
877 * one by one, without having to handle all elements at once (in contrast to
878 * #GNUNET_CONTAINER_multipeermap_iterate). Note that the iterator can not be
879 * used anymore if elements have been removed from 'map' after the creation of
880 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
881 * result in skipped or repeated elements.
883 * @param map the map to create an iterator for
884 * @return an iterator over the given multipeermap 'map'
886 struct GNUNET_CONTAINER_MultiPeerMapIterator *
887 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map)
889 struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
891 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiPeerMapIterator);
893 iter->modification_counter = map->modification_counter;
894 iter->me = map->map[0];
900 * Retrieve the next element from the hash map at the iterator's position.
901 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
903 * This operation is only allowed if no elements have been removed from the
904 * multipeermap since the creation of 'iter', and the map has not been destroyed.
905 * Adding elements may result in repeating or skipping elements.
907 * @param iter the iterator to get the next element from
908 * @param key pointer to store the key in, can be NULL
909 * @param value pointer to store the value in, can be NULL
910 * @return #GNUNET_YES we returned an element,
911 * #GNUNET_NO if we are out of elements
914 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
915 struct GNUNET_PeerIdentity *key, const void **value)
917 /* make sure the map has not been modified */
918 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
920 /* look for the next entry, skipping empty buckets */
923 if (iter->idx >= iter->map->map_length)
925 if (GNUNET_YES == iter->map->use_small_entries)
927 if (NULL != iter->me.sme)
930 *key = *iter->me.sme->key;
932 *value = iter->me.sme->value;
933 iter->me.sme = iter->me.sme->next;
939 if (NULL != iter->me.bme)
942 *key = iter->me.bme->key;
944 *value = iter->me.bme->value;
945 iter->me.bme = iter->me.bme->next;
950 if (iter->idx < iter->map->map_length)
951 iter->me = iter->map->map[iter->idx];
957 * Destroy a multipeermap iterator.
959 * @param iter the iterator to destroy
962 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter)
968 /* end of container_multipeermap.c */