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_multishortmap.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-multishortmap", __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_ShortHashCode 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_ShortHashCode *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_MultiShortmap
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 multishortmap.
132 * Allows to enumerate elements asynchronously.
134 struct GNUNET_CONTAINER_MultiShortmapIterator
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_MultiShortmap *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_MultiShortmap *
175 GNUNET_CONTAINER_multishortmap_create (unsigned int len,
176 int do_not_copy_keys)
178 struct GNUNET_CONTAINER_MultiShortmap *map;
180 GNUNET_assert (len > 0);
181 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
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_multishortmap_destroy (struct GNUNET_CONTAINER_MultiShortmap
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_MultiShortmap *map,
246 const struct GNUNET_ShortHashCode *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_multishortmap_size (const struct GNUNET_CONTAINER_MultiShortmap *map)
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_multishortmap_get (const struct GNUNET_CONTAINER_MultiShortmap *map,
281 const struct GNUNET_ShortHashCode *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_ShortHashCode)))
296 struct BigMapEntry *bme;
298 for (bme = me.bme; NULL != bme; bme = bme->next)
299 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
307 * Iterate over all entries in the map.
310 * @param it function to call on each entry
311 * @param it_cls extra argument to @a it
312 * @return the number of key value pairs processed,
313 * #GNUNET_SYSERR if it aborted iteration
316 GNUNET_CONTAINER_multishortmap_iterate (const struct GNUNET_CONTAINER_MultiShortmap *map,
317 GNUNET_CONTAINER_ShortmapIterator it,
323 struct GNUNET_ShortHashCode kc;
326 GNUNET_assert (NULL != map);
327 for (i = 0; i < map->map_length; i++)
330 if (map->use_small_entries)
332 struct SmallMapEntry *sme;
333 struct SmallMapEntry *nxt;
336 while (NULL != (sme = nxt))
341 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
342 return GNUNET_SYSERR;
349 struct BigMapEntry *bme;
350 struct BigMapEntry *nxt;
353 while (NULL != (bme = nxt))
359 if (GNUNET_OK != it (it_cls, &kc, bme->value))
360 return GNUNET_SYSERR;
371 * Remove the given key-value pair from the map. Note that if the
372 * key-value pair is in the map multiple times, only one of the pairs
376 * @param key key of the key-value pair
377 * @param value value of the key-value pair
378 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
382 GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *map,
383 const struct GNUNET_ShortHashCode *key,
389 map->modification_counter++;
391 i = idx_of (map, key);
393 if (map->use_small_entries)
395 struct SmallMapEntry *sme;
396 struct SmallMapEntry *p;
399 for (sme = me.sme; NULL != sme; sme = sme->next)
401 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode))) &&
402 (value == sme->value))
405 map->map[i].sme = sme->next;
417 struct BigMapEntry *bme;
418 struct BigMapEntry *p;
421 for (bme = me.bme; NULL != bme; bme = bme->next)
423 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode))) &&
424 (value == bme->value))
427 map->map[i].bme = bme->next;
442 * Remove all entries for the given key from the map.
443 * Note that the values would not be "freed".
446 * @param key identifies values to be removed
447 * @return number of values removed
450 GNUNET_CONTAINER_multishortmap_remove_all (struct GNUNET_CONTAINER_MultiShortmap *map,
451 const struct GNUNET_ShortHashCode *key)
457 map->modification_counter++;
460 i = idx_of (map, key);
462 if (map->use_small_entries)
464 struct SmallMapEntry *sme;
465 struct SmallMapEntry *p;
471 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
474 map->map[i].sme = sme->next;
480 sme = map->map[i].sme;
494 struct BigMapEntry *bme;
495 struct BigMapEntry *p;
501 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
504 map->map[i].bme = bme->next;
510 bme = map->map[i].bme;
527 * Check if the map contains any value under the given
528 * key (including values that are NULL).
531 * @param key the key to test if a value exists for it
532 * @return #GNUNET_YES if such a value exists,
536 GNUNET_CONTAINER_multishortmap_contains (const struct GNUNET_CONTAINER_MultiShortmap *map,
537 const struct GNUNET_ShortHashCode *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_ShortHashCode)))
552 struct BigMapEntry *bme;
554 for (bme = me.bme; NULL != bme; bme = bme->next)
555 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
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_multishortmap_contains_value (const struct GNUNET_CONTAINER_MultiShortmap *map,
574 const struct GNUNET_ShortHashCode *key,
579 me = map->map[idx_of (map, key)];
580 if (map->use_small_entries)
582 struct SmallMapEntry *sme;
584 for (sme = me.sme; NULL != sme; sme = sme->next)
585 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode))) &&
586 (sme->value == value) )
591 struct BigMapEntry *bme;
593 for (bme = me.bme; NULL != bme; bme = bme->next)
594 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode))) &&
595 (bme->value == value) )
603 * Grow the given map to a more appropriate size.
605 * @param map the hash map to grow
608 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
610 union MapEntry *old_map;
611 union MapEntry *new_map;
612 unsigned int old_len;
613 unsigned int new_len;
617 map->modification_counter++;
620 old_len = map->map_length;
621 new_len = old_len * 2;
622 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
623 map->map_length = new_len;
625 for (i = 0; i < old_len; i++)
627 if (map->use_small_entries)
629 struct SmallMapEntry *sme;
631 while (NULL != (sme = old_map[i].sme))
633 old_map[i].sme = sme->next;
634 idx = idx_of (map, sme->key);
635 sme->next = new_map[idx].sme;
636 new_map[idx].sme = sme;
641 struct BigMapEntry *bme;
643 while (NULL != (bme = old_map[i].bme))
645 old_map[i].bme = bme->next;
646 idx = idx_of (map, &bme->key);
647 bme->next = new_map[idx].bme;
648 new_map[idx].bme = bme;
652 GNUNET_free (old_map);
657 * Store a key-value pair in the map.
660 * @param key key to use
661 * @param value value to use
662 * @param opt options for put
663 * @return #GNUNET_OK on success,
664 * #GNUNET_NO if a value was replaced (with REPLACE)
665 * #GNUNET_SYSERR if GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
666 * value already exists
669 GNUNET_CONTAINER_multishortmap_put (struct GNUNET_CONTAINER_MultiShortmap *map,
670 const struct GNUNET_ShortHashCode *key,
672 enum GNUNET_CONTAINER_MultiHashMapOption opt)
677 i = idx_of (map, key);
678 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
679 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
682 if (map->use_small_entries)
684 struct SmallMapEntry *sme;
686 for (sme = me.sme; NULL != sme; sme = sme->next)
687 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
689 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
690 return GNUNET_SYSERR;
697 struct BigMapEntry *bme;
699 for (bme = me.bme; NULL != bme; bme = bme->next)
700 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
702 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
703 return GNUNET_SYSERR;
709 if (map->size / 3 >= map->map_length / 4)
712 i = idx_of (map, key);
714 if (map->use_small_entries)
716 struct SmallMapEntry *sme;
718 sme = GNUNET_new (struct SmallMapEntry);
721 sme->next = map->map[i].sme;
722 map->map[i].sme = sme;
726 struct BigMapEntry *bme;
728 bme = GNUNET_new (struct BigMapEntry);
731 bme->next = map->map[i].bme;
732 map->map[i].bme = bme;
740 * Iterate over all entries in the map that match a particular key.
743 * @param key key that the entries must correspond to
744 * @param it function to call on each entry
745 * @param it_cls extra argument to @a it
746 * @return the number of key value pairs processed,
747 * #GNUNET_SYSERR if it aborted iteration
750 GNUNET_CONTAINER_multishortmap_get_multiple (const struct GNUNET_CONTAINER_MultiShortmap *map,
751 const struct GNUNET_ShortHashCode *key,
752 GNUNET_CONTAINER_ShortmapIterator it,
759 me = map->map[idx_of (map, key)];
760 if (map->use_small_entries)
762 struct SmallMapEntry *sme;
763 struct SmallMapEntry *nxt;
766 while (NULL != (sme = nxt))
769 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
771 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
772 return GNUNET_SYSERR;
778 struct BigMapEntry *bme;
779 struct BigMapEntry *nxt;
782 while (NULL != (bme = nxt))
785 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
787 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
788 return GNUNET_SYSERR;
798 * Call @a it on a random value from the map, or not at all
799 * if the map is empty. Note that this function has linear
800 * complexity (in the size of the map).
803 * @param it function to call on a random entry
804 * @param it_cls extra argument to @a it
805 * @return the number of key value pairs processed, zero or one.
808 GNUNET_CONTAINER_multishortmap_get_random (const struct GNUNET_CONTAINER_MultiShortmap *map,
809 GNUNET_CONTAINER_ShortmapIterator it,
820 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
822 for (idx = 0; idx < map->map_length; idx++)
825 if (map->use_small_entries)
827 struct SmallMapEntry *sme;
828 struct SmallMapEntry *nxt;
831 while (NULL != (sme = nxt))
836 if (GNUNET_OK != it (it_cls,
839 return GNUNET_SYSERR;
847 struct BigMapEntry *bme;
848 struct BigMapEntry *nxt;
851 while (NULL != (bme = nxt))
856 if (GNUNET_OK != it (it_cls,
857 &bme->key, bme->value))
858 return GNUNET_SYSERR;
866 return GNUNET_SYSERR;
871 * Create an iterator for a multishortmap.
872 * The iterator can be used to retrieve all the elements in the multishortmap
873 * one by one, without having to handle all elements at once (in contrast to
874 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
875 * used anymore if elements have been removed from 'map' after the creation of
876 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
877 * result in skipped or repeated elements.
879 * @param map the map to create an iterator for
880 * @return an iterator over the given multishortmap 'map'
882 struct GNUNET_CONTAINER_MultiShortmapIterator *
883 GNUNET_CONTAINER_multishortmap_iterator_create (const struct GNUNET_CONTAINER_MultiShortmap *map)
885 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
887 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
889 iter->modification_counter = map->modification_counter;
890 iter->me = map->map[0];
896 * Retrieve the next element from the hash map at the iterator's position.
897 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
899 * This operation is only allowed if no elements have been removed from the
900 * multishortmap since the creation of 'iter', and the map has not been destroyed.
901 * Adding elements may result in repeating or skipping elements.
903 * @param iter the iterator to get the next element from
904 * @param key pointer to store the key in, can be NULL
905 * @param value pointer to store the value in, can be NULL
906 * @return #GNUNET_YES we returned an element,
907 * #GNUNET_NO if we are out of elements
910 GNUNET_CONTAINER_multishortmap_iterator_next (struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
911 struct GNUNET_ShortHashCode *key,
914 /* make sure the map has not been modified */
915 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
917 /* look for the next entry, skipping empty buckets */
920 if (iter->idx >= iter->map->map_length)
922 if (GNUNET_YES == iter->map->use_small_entries)
924 if (NULL != iter->me.sme)
927 *key = *iter->me.sme->key;
929 *value = iter->me.sme->value;
930 iter->me.sme = iter->me.sme->next;
936 if (NULL != iter->me.bme)
939 *key = iter->me.bme->key;
941 *value = iter->me.bme->value;
942 iter->me.bme = iter->me.bme->next;
947 if (iter->idx < iter->map->map_length)
948 iter->me = iter->map->map[iter->idx];
954 * Destroy a multishortmap iterator.
956 * @param iter the iterator to destroy
959 GNUNET_CONTAINER_multishortmap_iterator_destroy (struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
965 /* end of container_multishortmap.c */