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_multihashmap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
27 #include "gnunet_container_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __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_HashCode 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_HashCode *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_MultiHashMap
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 multihashmap.
132 * Allows to enumerate elements asynchronously.
134 struct GNUNET_CONTAINER_MultiHashMapIterator
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_MultiHashMap *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_MultiHashMap *
175 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
176 int do_not_copy_keys)
178 struct GNUNET_CONTAINER_MultiHashMap *map;
180 GNUNET_assert (len > 0);
181 map = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap);
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_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
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_MultiHashMap *map,
246 const struct GNUNET_HashCode *key)
248 GNUNET_assert (map != NULL);
249 return (*(unsigned int *) key) % map->map_length;
254 * Get the number of key-value pairs in the map.
257 * @return the number of key value pairs
260 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
268 * Given a key find a value in the map matching the key.
271 * @param key what to look for
272 * @return NULL if no value was found; note that
273 * this is indistinguishable from values that just
274 * happen to be NULL; use "contains" to test for
275 * key-value pairs with value NULL
278 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map,
279 const struct GNUNET_HashCode *key)
283 me = map->map[idx_of (map, key)];
284 if (map->use_small_entries)
286 struct SmallMapEntry *sme;
288 for (sme = me.sme; NULL != sme; sme = sme->next)
289 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
294 struct BigMapEntry *bme;
296 for (bme = me.bme; NULL != bme; bme = bme->next)
297 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
305 * Iterate over all entries in the map.
308 * @param it function to call on each entry
309 * @param it_cls extra argument to @a it
310 * @return the number of key value pairs processed,
311 * #GNUNET_SYSERR if it aborted iteration
314 GNUNET_CONTAINER_multihashmap_iterate (const struct
315 GNUNET_CONTAINER_MultiHashMap *map,
316 GNUNET_CONTAINER_HashMapIterator it,
322 struct GNUNET_HashCode kc;
325 GNUNET_assert (NULL != map);
326 for (i = 0; i < map->map_length; i++)
329 if (map->use_small_entries)
331 struct SmallMapEntry *sme;
332 struct SmallMapEntry *nxt;
335 while (NULL != (sme = nxt))
340 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
341 return GNUNET_SYSERR;
348 struct BigMapEntry *bme;
349 struct BigMapEntry *nxt;
352 while (NULL != (bme = nxt))
358 if (GNUNET_OK != it (it_cls, &kc, bme->value))
359 return GNUNET_SYSERR;
370 * Remove the given key-value pair from the map. Note that if the
371 * key-value pair is in the map multiple times, only one of the pairs
375 * @param key key of the key-value pair
376 * @param value value of the key-value pair
377 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
381 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
382 const struct GNUNET_HashCode *key,
388 map->modification_counter++;
390 i = idx_of (map, key);
392 if (map->use_small_entries)
394 struct SmallMapEntry *sme;
395 struct SmallMapEntry *p;
398 for (sme = me.sme; NULL != sme; sme = sme->next)
400 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
401 (value == sme->value))
404 map->map[i].sme = sme->next;
416 struct BigMapEntry *bme;
417 struct BigMapEntry *p;
420 for (bme = me.bme; NULL != bme; bme = bme->next)
422 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
423 (value == bme->value))
426 map->map[i].bme = bme->next;
441 * Remove all entries for the given key from the map.
442 * Note that the values would not be "freed".
445 * @param key identifies values to be removed
446 * @return number of values removed
449 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map,
450 const struct GNUNET_HashCode *key)
456 map->modification_counter++;
459 i = idx_of (map, key);
461 if (map->use_small_entries)
463 struct SmallMapEntry *sme;
464 struct SmallMapEntry *p;
470 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
473 map->map[i].sme = sme->next;
479 sme = map->map[i].sme;
493 struct BigMapEntry *bme;
494 struct BigMapEntry *p;
500 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
503 map->map[i].bme = bme->next;
509 bme = map->map[i].bme;
526 * Callback used to remove all entries from the map.
528 * @param cls the `struct GNUNET_CONTAINER_MultiHashMap`
530 * @param value the value
531 * @return #GNUNET_OK (continue to iterate)
534 remove_all (void *cls,
535 const struct GNUNET_HashCode *key,
538 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
540 GNUNET_CONTAINER_multihashmap_remove (map,
549 * Remove all entries from the map.
550 * Note that the values would not be "freed".
553 * @return number of values removed
556 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map)
561 GNUNET_CONTAINER_multihashmap_iterate (map,
569 * Check if the map contains any value under the given
570 * key (including values that are NULL).
573 * @param key the key to test if a value exists for it
574 * @return #GNUNET_YES if such a value exists,
578 GNUNET_CONTAINER_multihashmap_contains (const struct
579 GNUNET_CONTAINER_MultiHashMap *map,
580 const struct GNUNET_HashCode *key)
584 me = map->map[idx_of (map, key)];
585 if (map->use_small_entries)
587 struct SmallMapEntry *sme;
589 for (sme = me.sme; NULL != sme; sme = sme->next)
590 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
595 struct BigMapEntry *bme;
597 for (bme = me.bme; NULL != bme; bme = bme->next)
598 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
606 * Check if the map contains the given value under the given
610 * @param key the key to test if a value exists for it
611 * @param value value to test for
612 * @return #GNUNET_YES if such a value exists,
616 GNUNET_CONTAINER_multihashmap_contains_value (const struct
617 GNUNET_CONTAINER_MultiHashMap
618 *map, const struct GNUNET_HashCode *key,
623 me = map->map[idx_of (map, key)];
624 if (map->use_small_entries)
626 struct SmallMapEntry *sme;
628 for (sme = me.sme; NULL != sme; sme = sme->next)
629 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
630 (sme->value == value) )
635 struct BigMapEntry *bme;
637 for (bme = me.bme; NULL != bme; bme = bme->next)
638 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
639 (bme->value == value) )
647 * Grow the given map to a more appropriate size.
649 * @param map the hash map to grow
652 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
654 union MapEntry *old_map;
655 union MapEntry *new_map;
656 unsigned int old_len;
657 unsigned int new_len;
661 map->modification_counter++;
664 old_len = map->map_length;
665 new_len = old_len * 2;
666 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
667 map->map_length = new_len;
669 for (i = 0; i < old_len; i++)
671 if (map->use_small_entries)
673 struct SmallMapEntry *sme;
675 while (NULL != (sme = old_map[i].sme))
677 old_map[i].sme = sme->next;
678 idx = idx_of (map, sme->key);
679 sme->next = new_map[idx].sme;
680 new_map[idx].sme = sme;
685 struct BigMapEntry *bme;
687 while (NULL != (bme = old_map[i].bme))
689 old_map[i].bme = bme->next;
690 idx = idx_of (map, &bme->key);
691 bme->next = new_map[idx].bme;
692 new_map[idx].bme = bme;
696 GNUNET_free (old_map);
701 * Store a key-value pair in the map.
704 * @param key key to use
705 * @param value value to use
706 * @param opt options for put
707 * @return #GNUNET_OK on success,
708 * #GNUNET_NO if a value was replaced (with REPLACE)
709 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
710 * value already exists
713 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
714 const struct GNUNET_HashCode *key,
716 enum GNUNET_CONTAINER_MultiHashMapOption opt)
721 i = idx_of (map, key);
722 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
723 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
726 if (map->use_small_entries)
728 struct SmallMapEntry *sme;
730 for (sme = me.sme; NULL != sme; sme = sme->next)
731 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
733 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
734 return GNUNET_SYSERR;
741 struct BigMapEntry *bme;
743 for (bme = me.bme; NULL != bme; bme = bme->next)
744 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
746 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
747 return GNUNET_SYSERR;
753 if (map->size / 3 >= map->map_length / 4)
756 i = idx_of (map, key);
758 if (map->use_small_entries)
760 struct SmallMapEntry *sme;
762 sme = GNUNET_new (struct SmallMapEntry);
765 sme->next = map->map[i].sme;
766 map->map[i].sme = sme;
770 struct BigMapEntry *bme;
772 bme = GNUNET_new (struct BigMapEntry);
775 bme->next = map->map[i].bme;
776 map->map[i].bme = bme;
784 * Iterate over all entries in the map that match a particular key.
787 * @param key key that the entries must correspond to
788 * @param it function to call on each entry
789 * @param it_cls extra argument to it
790 * @return the number of key value pairs processed,
791 * #GNUNET_SYSERR if it aborted iteration
794 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
795 GNUNET_CONTAINER_MultiHashMap *map,
796 const struct GNUNET_HashCode *key,
797 GNUNET_CONTAINER_HashMapIterator it,
804 me = map->map[idx_of (map, key)];
805 if (map->use_small_entries)
807 struct SmallMapEntry *sme;
808 struct SmallMapEntry *nxt;
811 while (NULL != (sme = nxt))
814 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
816 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
817 return GNUNET_SYSERR;
823 struct BigMapEntry *bme;
824 struct BigMapEntry *nxt;
827 while (NULL != (bme = nxt))
830 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
832 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
833 return GNUNET_SYSERR;
843 * Call @a it on a random value from the map, or not at all
844 * if the map is empty. Note that this function has linear
845 * complexity (in the size of the map).
848 * @param it function to call on a random entry
849 * @param it_cls extra argument to @a it
850 * @return the number of key value pairs processed, zero or one.
853 GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map,
854 GNUNET_CONTAINER_HashMapIterator it,
865 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
867 for (idx = 0; idx < map->map_length; idx++)
870 if (map->use_small_entries)
872 struct SmallMapEntry *sme;
873 struct SmallMapEntry *nxt;
876 while (NULL != (sme = nxt))
881 if (GNUNET_OK != it (it_cls,
884 return GNUNET_SYSERR;
892 struct BigMapEntry *bme;
893 struct BigMapEntry *nxt;
896 while (NULL != (bme = nxt))
901 if (GNUNET_OK != it (it_cls,
902 &bme->key, bme->value))
903 return GNUNET_SYSERR;
911 return GNUNET_SYSERR;
916 * Create an iterator for a multihashmap.
917 * The iterator can be used to retrieve all the elements in the multihashmap
918 * one by one, without having to handle all elements at once (in contrast to
919 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
920 * used anymore if elements have been removed from 'map' after the creation of
921 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
922 * result in skipped or repeated elements.
924 * @param map the map to create an iterator for
925 * @return an iterator over the given multihashmap 'map'
927 struct GNUNET_CONTAINER_MultiHashMapIterator *
928 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
930 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
932 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
934 iter->modification_counter = map->modification_counter;
935 iter->me = map->map[0];
941 * Retrieve the next element from the hash map at the iterator's position.
942 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
944 * This operation is only allowed if no elements have been removed from the
945 * multihashmap since the creation of 'iter', and the map has not been destroyed.
946 * Adding elements may result in repeating or skipping elements.
948 * @param iter the iterator to get the next element from
949 * @param key pointer to store the key in, can be NULL
950 * @param value pointer to store the value in, can be NULL
951 * @return #GNUNET_YES we returned an element,
952 * #GNUNET_NO if we are out of elements
955 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
956 struct GNUNET_HashCode *key,
959 /* make sure the map has not been modified */
960 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
962 /* look for the next entry, skipping empty buckets */
965 if (iter->idx >= iter->map->map_length)
967 if (GNUNET_YES == iter->map->use_small_entries)
969 if (NULL != iter->me.sme)
972 *key = *iter->me.sme->key;
974 *value = iter->me.sme->value;
975 iter->me.sme = iter->me.sme->next;
981 if (NULL != iter->me.bme)
984 *key = iter->me.bme->key;
986 *value = iter->me.bme->value;
987 iter->me.bme = iter->me.bme->next;
992 if (iter->idx < iter->map->map_length)
993 iter->me = iter->map->map[iter->idx];
999 * Destroy a multihashmap iterator.
1001 * @param iter the iterator to destroy
1004 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
1010 /* end of container_multihashmap.c */