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 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., 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_util_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
279 *map, 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
450 *map, 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 * Check if the map contains any value under the given
527 * key (including values that are NULL).
530 * @param key the key to test if a value exists for it
531 * @return #GNUNET_YES if such a value exists,
535 GNUNET_CONTAINER_multihashmap_contains (const struct
536 GNUNET_CONTAINER_MultiHashMap *map,
537 const struct GNUNET_HashCode *key)
541 me = map->map[idx_of (map, key)];
542 if (map->use_small_entries)
544 struct SmallMapEntry *sme;
546 for (sme = me.sme; NULL != sme; sme = sme->next)
547 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
552 struct BigMapEntry *bme;
554 for (bme = me.bme; NULL != bme; bme = bme->next)
555 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
563 * Check if the map contains the given value under the given
567 * @param key the key to test if a value exists for it
568 * @param value value to test for
569 * @return #GNUNET_YES if such a value exists,
573 GNUNET_CONTAINER_multihashmap_contains_value (const struct
574 GNUNET_CONTAINER_MultiHashMap
575 *map, const struct GNUNET_HashCode *key,
580 me = map->map[idx_of (map, key)];
581 if (map->use_small_entries)
583 struct SmallMapEntry *sme;
585 for (sme = me.sme; NULL != sme; sme = sme->next)
586 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
587 (sme->value == value) )
592 struct BigMapEntry *bme;
594 for (bme = me.bme; NULL != bme; bme = bme->next)
595 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
596 (bme->value == value) )
604 * Grow the given map to a more appropriate size.
606 * @param map the hash map to grow
609 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
611 union MapEntry *old_map;
612 union MapEntry *new_map;
613 unsigned int old_len;
614 unsigned int new_len;
618 map->modification_counter++;
621 old_len = map->map_length;
622 new_len = old_len * 2;
623 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
624 map->map_length = new_len;
626 for (i = 0; i < old_len; i++)
628 if (map->use_small_entries)
630 struct SmallMapEntry *sme;
632 while (NULL != (sme = old_map[i].sme))
634 old_map[i].sme = sme->next;
635 idx = idx_of (map, sme->key);
636 sme->next = new_map[idx].sme;
637 new_map[idx].sme = sme;
642 struct BigMapEntry *bme;
644 while (NULL != (bme = old_map[i].bme))
646 old_map[i].bme = bme->next;
647 idx = idx_of (map, &bme->key);
648 bme->next = new_map[idx].bme;
649 new_map[idx].bme = bme;
653 GNUNET_free (old_map);
658 * Store a key-value pair in the map.
661 * @param key key to use
662 * @param value value to use
663 * @param opt options for put
664 * @return #GNUNET_OK on success,
665 * #GNUNET_NO if a value was replaced (with REPLACE)
666 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
667 * value already exists
670 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
671 const struct GNUNET_HashCode *key,
673 enum GNUNET_CONTAINER_MultiHashMapOption opt)
678 i = idx_of (map, key);
679 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
680 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
683 if (map->use_small_entries)
685 struct SmallMapEntry *sme;
687 for (sme = me.sme; NULL != sme; sme = sme->next)
688 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
690 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
691 return GNUNET_SYSERR;
698 struct BigMapEntry *bme;
700 for (bme = me.bme; NULL != bme; bme = bme->next)
701 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
703 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
704 return GNUNET_SYSERR;
710 if (map->size / 3 >= map->map_length / 4)
713 i = idx_of (map, key);
715 if (map->use_small_entries)
717 struct SmallMapEntry *sme;
719 sme = GNUNET_new (struct SmallMapEntry);
722 sme->next = map->map[i].sme;
723 map->map[i].sme = sme;
727 struct BigMapEntry *bme;
729 bme = GNUNET_new (struct BigMapEntry);
732 bme->next = map->map[i].bme;
733 map->map[i].bme = bme;
741 * Iterate over all entries in the map that match a particular key.
744 * @param key key that the entries must correspond to
745 * @param it function to call on each entry
746 * @param it_cls extra argument to it
747 * @return the number of key value pairs processed,
748 * #GNUNET_SYSERR if it aborted iteration
751 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
752 GNUNET_CONTAINER_MultiHashMap *map,
753 const struct GNUNET_HashCode *key,
754 GNUNET_CONTAINER_HashMapIterator it,
761 me = map->map[idx_of (map, key)];
762 if (map->use_small_entries)
764 struct SmallMapEntry *sme;
765 struct SmallMapEntry *nxt;
768 while (NULL != (sme = nxt))
771 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
773 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
774 return GNUNET_SYSERR;
780 struct BigMapEntry *bme;
781 struct BigMapEntry *nxt;
784 while (NULL != (bme = nxt))
787 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
789 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
790 return GNUNET_SYSERR;
799 * Create an iterator for a multihashmap.
800 * The iterator can be used to retrieve all the elements in the multihashmap
801 * one by one, without having to handle all elements at once (in contrast to
802 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
803 * used anymore if elements have been removed from 'map' after the creation of
804 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
805 * result in skipped or repeated elements.
807 * @param map the map to create an iterator for
808 * @return an iterator over the given multihashmap 'map'
810 struct GNUNET_CONTAINER_MultiHashMapIterator *
811 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map)
813 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
815 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMapIterator);
817 iter->modification_counter = map->modification_counter;
818 iter->me = map->map[0];
824 * Retrieve the next element from the hash map at the iterator's position.
825 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
827 * This operation is only allowed if no elements have been removed from the
828 * multihashmap since the creation of 'iter', and the map has not been destroyed.
829 * Adding elements may result in repeating or skipping elements.
831 * @param iter the iterator to get the next element from
832 * @param key pointer to store the key in, can be NULL
833 * @param value pointer to store the value in, can be NULL
834 * @return #GNUNET_YES we returned an element,
835 * #GNUNET_NO if we are out of elements
838 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
839 struct GNUNET_HashCode *key,
842 /* make sure the map has not been modified */
843 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
845 /* look for the next entry, skipping empty buckets */
848 if (iter->idx >= iter->map->map_length)
850 if (GNUNET_YES == iter->map->use_small_entries)
852 if (NULL != iter->me.sme)
855 *key = *iter->me.sme->key;
857 *value = iter->me.sme->value;
858 iter->me.sme = iter->me.sme->next;
864 if (NULL != iter->me.bme)
867 *key = iter->me.bme->key;
869 *value = iter->me.bme->value;
870 iter->me.bme = iter->me.bme->next;
875 if (iter->idx < iter->map->map_length)
876 iter->me = iter->map->map[iter->idx];
882 * Destroy a multihashmap iterator.
884 * @param iter the iterator to destroy
887 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
893 /* end of container_multihashmap.c */