2 This file is part of GNUnet.
3 (C) 2008, 2012 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 * An entry in the hash map with the full key.
45 * If there is a hash collision, we create a linked list.
47 struct BigMapEntry *next;
52 struct GNUNET_HashCode key;
58 * An entry in the hash map with just a pointer to the key.
69 * If there is a hash collision, we create a linked list.
71 struct SmallMapEntry *next;
76 const struct GNUNET_HashCode *key;
87 * Variant used if map entries only contain a pointer to the key.
89 struct SmallMapEntry *sme;
92 * Variant used if map entries contain the full key.
94 struct BigMapEntry *bme;
99 * Internal representation of the hash map.
101 struct GNUNET_CONTAINER_MultiHashMap
104 * All of our buckets.
109 * Number of entries in the map.
114 * Length of the "map" array.
116 unsigned int map_length;
119 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
120 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
122 int use_small_entries;
125 * Counts the destructive modifications (grow, remove)
126 * to the map, so that iterators can check if they are still valid.
128 unsigned int modification_counter;
133 * Cursor into a multihashmap.
134 * Allows to enumerate elements asynchronously.
136 struct GNUNET_CONTAINER_MultiHashMapIterator
139 * Position in the bucket 'idx'
144 * Current bucket index.
149 * Modification counter as observed on the map when the iterator
152 unsigned int modification_counter;
155 * Map that we are iterating over.
157 const struct GNUNET_CONTAINER_MultiHashMap *map;
162 * Create a multi hash map.
164 * @param len initial size (map will grow as needed)
165 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
166 * GNUNET_YES means that on 'put', the 'key' does not have
167 * to be copied as the destination of the pointer is
168 * guaranteed to be life as long as the value is stored in
169 * the hashmap. This can significantly reduce memory
170 * consumption, but of course is also a recipie for
171 * heap corruption if the assumption is not true. Only
172 * use this if (1) memory use is important in this case and
173 * (2) you have triple-checked that the invariant holds
174 * @return NULL on error
176 struct GNUNET_CONTAINER_MultiHashMap *
177 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
178 int do_not_copy_keys)
180 struct GNUNET_CONTAINER_MultiHashMap *map;
182 GNUNET_assert (len > 0);
183 map = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
184 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
185 map->map_length = len;
186 map->use_small_entries = do_not_copy_keys;
192 * Destroy a hash map. Will not free any values
193 * stored in the hash map!
198 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
204 for (i = 0; i < map->map_length; i++)
207 if (map->use_small_entries)
209 struct SmallMapEntry *sme;
210 struct SmallMapEntry *nxt;
213 while (NULL != (sme = nxt))
222 struct BigMapEntry *bme;
223 struct BigMapEntry *nxt;
226 while (NULL != (bme = nxt))
234 GNUNET_free (map->map);
240 * Compute the index of the bucket for the given key.
242 * @param map hash map for which to compute the index
243 * @param key what key should the index be computed for
244 * @return offset into the "map" array of "map"
247 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
248 const struct GNUNET_HashCode *key)
250 GNUNET_assert (map != NULL);
251 return (*(unsigned int *) key) % map->map_length;
256 * Get the number of key-value pairs in the map.
259 * @return the number of key value pairs
262 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
270 * Given a key find a value in the map matching the key.
273 * @param key what to look for
274 * @return NULL if no value was found; note that
275 * this is indistinguishable from values that just
276 * happen to be NULL; use "contains" to test for
277 * key-value pairs with value NULL
280 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
281 *map, const struct GNUNET_HashCode *key)
285 me = map->map[idx_of (map, key)];
286 if (map->use_small_entries)
288 struct SmallMapEntry *sme;
290 for (sme = me.sme; NULL != sme; sme = sme->next)
291 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
296 struct BigMapEntry *bme;
298 for (bme = me.bme; NULL != bme; bme = bme->next)
299 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
307 * Iterate over all entries in the map.
310 * @param it function to call on each entry
311 * @param it_cls extra argument to it
312 * @return the number of key value pairs processed,
313 * GNUNET_SYSERR if it aborted iteration
316 GNUNET_CONTAINER_multihashmap_iterate (const struct
317 GNUNET_CONTAINER_MultiHashMap *map,
318 GNUNET_CONTAINER_HashMapIterator it,
324 struct GNUNET_HashCode kc;
327 GNUNET_assert (NULL != map);
328 for (i = 0; i < map->map_length; i++)
331 if (map->use_small_entries)
333 struct SmallMapEntry *sme;
334 struct SmallMapEntry *nxt;
337 while (NULL != (sme = nxt))
342 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
343 return GNUNET_SYSERR;
350 struct BigMapEntry *bme;
351 struct BigMapEntry *nxt;
354 while (NULL != (bme = nxt))
360 if (GNUNET_OK != it (it_cls, &kc, bme->value))
361 return GNUNET_SYSERR;
372 * Remove the given key-value pair from the map. Note that if the
373 * key-value pair is in the map multiple times, only one of the pairs
377 * @param key key of the key-value pair
378 * @param value value of the key-value pair
379 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
383 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
384 const struct GNUNET_HashCode *key,
390 map->modification_counter++;
392 i = idx_of (map, key);
394 if (map->use_small_entries)
396 struct SmallMapEntry *sme;
397 struct SmallMapEntry *p;
400 for (sme = me.sme; NULL != sme; sme = sme->next)
402 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
403 (value == sme->value))
406 map->map[i].sme = sme->next;
418 struct BigMapEntry *bme;
419 struct BigMapEntry *p;
422 for (bme = me.bme; NULL != bme; bme = bme->next)
424 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
425 (value == bme->value))
428 map->map[i].bme = bme->next;
443 * Remove all entries for the given key from the map.
444 * Note that the values would not be "freed".
447 * @param key identifies values to be removed
448 * @return number of values removed
451 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
452 *map, const struct GNUNET_HashCode *key)
458 map->modification_counter++;
461 i = idx_of (map, key);
463 if (map->use_small_entries)
465 struct SmallMapEntry *sme;
466 struct SmallMapEntry *p;
472 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
475 map->map[i].sme = sme->next;
481 sme = map->map[i].sme;
495 struct BigMapEntry *bme;
496 struct BigMapEntry *p;
502 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
505 map->map[i].bme = bme->next;
511 bme = map->map[i].bme;
528 * Check if the map contains any value under the given
529 * key (including values that are NULL).
532 * @param key the key to test if a value exists for it
533 * @return GNUNET_YES if such a value exists,
537 GNUNET_CONTAINER_multihashmap_contains (const struct
538 GNUNET_CONTAINER_MultiHashMap *map,
539 const struct GNUNET_HashCode *key)
543 me = map->map[idx_of (map, key)];
544 if (map->use_small_entries)
546 struct SmallMapEntry *sme;
548 for (sme = me.sme; NULL != sme; sme = sme->next)
549 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
554 struct BigMapEntry *bme;
556 for (bme = me.bme; NULL != bme; bme = bme->next)
557 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
565 * Check if the map contains the given value under the given
569 * @param key the key to test if a value exists for it
570 * @param value value to test for
571 * @return GNUNET_YES if such a value exists,
575 GNUNET_CONTAINER_multihashmap_contains_value (const struct
576 GNUNET_CONTAINER_MultiHashMap
577 *map, const struct GNUNET_HashCode *key,
582 me = map->map[idx_of (map, key)];
583 if (map->use_small_entries)
585 struct SmallMapEntry *sme;
587 for (sme = me.sme; NULL != sme; sme = sme->next)
588 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
589 (sme->value == value) )
594 struct BigMapEntry *bme;
596 for (bme = me.bme; NULL != bme; bme = bme->next)
597 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
598 (bme->value == value) )
606 * Grow the given map to a more appropriate size.
608 * @param map the hash map to grow
611 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
613 union MapEntry *old_map;
614 union MapEntry *new_map;
615 unsigned int old_len;
616 unsigned int new_len;
620 map->modification_counter++;
623 old_len = map->map_length;
624 new_len = old_len * 2;
625 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
626 map->map_length = new_len;
628 for (i = 0; i < old_len; i++)
630 if (map->use_small_entries)
632 struct SmallMapEntry *sme;
634 while (NULL != (sme = old_map[i].sme))
636 old_map[i].sme = sme->next;
637 idx = idx_of (map, sme->key);
638 sme->next = new_map[idx].sme;
639 new_map[idx].sme = sme;
644 struct BigMapEntry *bme;
646 while (NULL != (bme = old_map[i].bme))
648 old_map[i].bme = bme->next;
649 idx = idx_of (map, &bme->key);
650 bme->next = new_map[idx].bme;
651 new_map[idx].bme = bme;
655 GNUNET_free (old_map);
660 * Store a key-value pair in the map.
663 * @param key key to use
664 * @param value value to use
665 * @param opt options for put
666 * @return GNUNET_OK on success,
667 * GNUNET_NO if a value was replaced (with REPLACE)
668 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
669 * value already exists
672 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
673 const struct GNUNET_HashCode *key,
675 enum GNUNET_CONTAINER_MultiHashMapOption opt)
680 i = idx_of (map, key);
681 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
682 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
685 if (map->use_small_entries)
687 struct SmallMapEntry *sme;
689 for (sme = me.sme; NULL != sme; sme = sme->next)
690 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
692 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
693 return GNUNET_SYSERR;
700 struct BigMapEntry *bme;
702 for (bme = me.bme; NULL != bme; bme = bme->next)
703 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
705 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
706 return GNUNET_SYSERR;
712 if (map->size / 3 >= map->map_length / 4)
715 i = idx_of (map, key);
717 if (map->use_small_entries)
719 struct SmallMapEntry *sme;
721 sme = GNUNET_malloc (sizeof (struct SmallMapEntry));
724 sme->next = map->map[i].sme;
725 map->map[i].sme = sme;
729 struct BigMapEntry *bme;
731 bme = GNUNET_malloc (sizeof (struct BigMapEntry));
734 bme->next = map->map[i].bme;
735 map->map[i].bme = bme;
743 * Iterate over all entries in the map that match a particular key.
746 * @param key key that the entries must correspond to
747 * @param it function to call on each entry
748 * @param it_cls extra argument to it
749 * @return the number of key value pairs processed,
750 * GNUNET_SYSERR if it aborted iteration
753 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
754 GNUNET_CONTAINER_MultiHashMap *map,
755 const struct GNUNET_HashCode *key,
756 GNUNET_CONTAINER_HashMapIterator 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_HashCode)))
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_HashCode)))
791 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
792 return GNUNET_SYSERR;
801 * Create an iterator for a multihashmap.
802 * The iterator can be used to retrieve all the elements in the multihashmap
803 * one by one, without having to handle all elements at once (in contrast to
804 * 'GNUNET_CONTAINER_multihashmap_iterate'). Note that the iterator can not be
805 * used anymore if elements have been removed from 'map' after the creation of
806 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
807 * result in skipped or repeated elements.
809 * @param map the map to create an iterator for
810 * @return an iterator over the given multihashmap 'map'
812 struct GNUNET_CONTAINER_MultiHashMapIterator *
813 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
815 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
817 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
819 iter->modification_counter = map->modification_counter;
820 iter->me = map->map[0];
826 * Retrieve the next element from the hash map at the iterator's position.
827 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
829 * This operation is only allowed if no elements have been removed from the
830 * multihashmap since the creation of 'iter', and the map has not been destroyed.
831 * Adding elements may result in repeating or skipping elements.
833 * @param iter the iterator to get the next element from
834 * @param key pointer to store the key in, can be NULL
835 * @param value pointer to store the value in, can be NULL
836 * @return GNUNET_YES we returned an element,
837 * GNUNET_NO if we are out of elements
840 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
841 struct GNUNET_HashCode *key, void **value)
843 /* make sure nobody modified the map */
844 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
846 /* look for the next entry, skipping empty buckets */
849 if (iter->idx >= iter->map->map_length)
851 if (GNUNET_YES == iter->map->use_small_entries)
853 if (NULL != iter->me.sme)
856 *key = *iter->me.sme->key;
858 *value = iter->me.sme->value;
859 iter->me.sme = iter->me.sme->next;
865 if (NULL != iter->me.bme)
868 *key = iter->me.bme->key;
870 *value = iter->me.bme->value;
871 iter->me.bme = iter->me.bme->next;
881 * Destroy a multihashmap iterator.
883 * @param iter the iterator to destroy
886 GNUNET_CONTAINER_multihashmap_enumerator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
892 /* end of container_multihashmap.c */