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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file util/container_multishortmap.c
20 * @brief hash map where the same key may be present multiple times
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multishortmap", __VA_ARGS__)
30 * An entry in the hash map with the full key.
41 * If there is a hash collision, we create a linked list.
43 struct BigMapEntry *next;
48 struct GNUNET_ShortHashCode key;
54 * An entry in the hash map with just a pointer to the key.
65 * If there is a hash collision, we create a linked list.
67 struct SmallMapEntry *next;
72 const struct GNUNET_ShortHashCode *key;
83 * Variant used if map entries only contain a pointer to the key.
85 struct SmallMapEntry *sme;
88 * Variant used if map entries contain the full key.
90 struct BigMapEntry *bme;
95 * Internal representation of the hash map.
97 struct GNUNET_CONTAINER_MultiShortmap
100 * All of our buckets.
105 * Number of entries in the map.
110 * Length of the "map" array.
112 unsigned int map_length;
115 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
116 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
118 int use_small_entries;
121 * Counts the destructive modifications (grow, remove)
122 * to the map, so that iterators can check if they are still valid.
124 unsigned int modification_counter;
129 * Cursor into a multishortmap.
130 * Allows to enumerate elements asynchronously.
132 struct GNUNET_CONTAINER_MultiShortmapIterator
135 * Position in the bucket 'idx'
140 * Current bucket index.
145 * Modification counter as observed on the map when the iterator
148 unsigned int modification_counter;
151 * Map that we are iterating over.
153 const struct GNUNET_CONTAINER_MultiShortmap *map;
158 * Create a multi hash map.
160 * @param len initial size (map will grow as needed)
161 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
162 * GNUNET_YES means that on 'put', the 'key' does not have
163 * to be copied as the destination of the pointer is
164 * guaranteed to be life as long as the value is stored in
165 * the hashmap. This can significantly reduce memory
166 * consumption, but of course is also a recipie for
167 * heap corruption if the assumption is not true. Only
168 * use this if (1) memory use is important in this case and
169 * (2) you have triple-checked that the invariant holds
170 * @return NULL on error
172 struct GNUNET_CONTAINER_MultiShortmap *
173 GNUNET_CONTAINER_multishortmap_create (unsigned int len,
174 int do_not_copy_keys)
176 struct GNUNET_CONTAINER_MultiShortmap *map;
178 GNUNET_assert (len > 0);
179 map = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmap);
180 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
181 map->map_length = len;
182 map->use_small_entries = do_not_copy_keys;
188 * Destroy a hash map. Will not free any values
189 * stored in the hash map!
194 GNUNET_CONTAINER_multishortmap_destroy (struct GNUNET_CONTAINER_MultiShortmap
200 for (i = 0; i < map->map_length; i++)
203 if (map->use_small_entries)
205 struct SmallMapEntry *sme;
206 struct SmallMapEntry *nxt;
209 while (NULL != (sme = nxt))
218 struct BigMapEntry *bme;
219 struct BigMapEntry *nxt;
222 while (NULL != (bme = nxt))
230 GNUNET_free (map->map);
236 * Compute the index of the bucket for the given key.
238 * @param map hash map for which to compute the index
239 * @param key what key should the index be computed for
240 * @return offset into the "map" array of "map"
243 idx_of (const struct GNUNET_CONTAINER_MultiShortmap *map,
244 const struct GNUNET_ShortHashCode *key)
248 GNUNET_assert (NULL != map);
249 GNUNET_memcpy (&kx, key, sizeof (kx));
250 return kx % map->map_length;
255 * Get the number of key-value pairs in the map.
258 * @return the number of key value pairs
261 GNUNET_CONTAINER_multishortmap_size (const struct GNUNET_CONTAINER_MultiShortmap *map)
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_multishortmap_get (const struct GNUNET_CONTAINER_MultiShortmap *map,
279 const struct GNUNET_ShortHashCode *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_ShortHashCode)))
294 struct BigMapEntry *bme;
296 for (bme = me.bme; NULL != bme; bme = bme->next)
297 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
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_multishortmap_iterate (const struct GNUNET_CONTAINER_MultiShortmap *map,
315 GNUNET_CONTAINER_ShortmapIterator it,
321 struct GNUNET_ShortHashCode kc;
324 GNUNET_assert (NULL != map);
325 for (i = 0; i < map->map_length; i++)
328 if (map->use_small_entries)
330 struct SmallMapEntry *sme;
331 struct SmallMapEntry *nxt;
334 while (NULL != (sme = nxt))
339 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
340 return GNUNET_SYSERR;
347 struct BigMapEntry *bme;
348 struct BigMapEntry *nxt;
351 while (NULL != (bme = nxt))
357 if (GNUNET_OK != it (it_cls, &kc, bme->value))
358 return GNUNET_SYSERR;
369 * Remove the given key-value pair from the map. Note that if the
370 * key-value pair is in the map multiple times, only one of the pairs
374 * @param key key of the key-value pair
375 * @param value value of the key-value pair
376 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
380 GNUNET_CONTAINER_multishortmap_remove (struct GNUNET_CONTAINER_MultiShortmap *map,
381 const struct GNUNET_ShortHashCode *key,
387 map->modification_counter++;
389 i = idx_of (map, key);
391 if (map->use_small_entries)
393 struct SmallMapEntry *sme;
394 struct SmallMapEntry *p;
397 for (sme = me.sme; NULL != sme; sme = sme->next)
399 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode))) &&
400 (value == sme->value))
403 map->map[i].sme = sme->next;
415 struct BigMapEntry *bme;
416 struct BigMapEntry *p;
419 for (bme = me.bme; NULL != bme; bme = bme->next)
421 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode))) &&
422 (value == bme->value))
425 map->map[i].bme = bme->next;
440 * Remove all entries for the given key from the map.
441 * Note that the values would not be "freed".
444 * @param key identifies values to be removed
445 * @return number of values removed
448 GNUNET_CONTAINER_multishortmap_remove_all (struct GNUNET_CONTAINER_MultiShortmap *map,
449 const struct GNUNET_ShortHashCode *key)
455 map->modification_counter++;
458 i = idx_of (map, key);
460 if (map->use_small_entries)
462 struct SmallMapEntry *sme;
463 struct SmallMapEntry *p;
469 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
472 map->map[i].sme = sme->next;
478 sme = map->map[i].sme;
492 struct BigMapEntry *bme;
493 struct BigMapEntry *p;
499 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
502 map->map[i].bme = bme->next;
508 bme = map->map[i].bme;
525 * Check if the map contains any value under the given
526 * key (including values that are NULL).
529 * @param key the key to test if a value exists for it
530 * @return #GNUNET_YES if such a value exists,
534 GNUNET_CONTAINER_multishortmap_contains (const struct GNUNET_CONTAINER_MultiShortmap *map,
535 const struct GNUNET_ShortHashCode *key)
539 me = map->map[idx_of (map, key)];
540 if (map->use_small_entries)
542 struct SmallMapEntry *sme;
544 for (sme = me.sme; NULL != sme; sme = sme->next)
545 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
550 struct BigMapEntry *bme;
552 for (bme = me.bme; NULL != bme; bme = bme->next)
553 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
561 * Check if the map contains the given value under the given
565 * @param key the key to test if a value exists for it
566 * @param value value to test for
567 * @return #GNUNET_YES if such a value exists,
571 GNUNET_CONTAINER_multishortmap_contains_value (const struct GNUNET_CONTAINER_MultiShortmap *map,
572 const struct GNUNET_ShortHashCode *key,
577 me = map->map[idx_of (map, key)];
578 if (map->use_small_entries)
580 struct SmallMapEntry *sme;
582 for (sme = me.sme; NULL != sme; sme = sme->next)
583 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode))) &&
584 (sme->value == value) )
589 struct BigMapEntry *bme;
591 for (bme = me.bme; NULL != bme; bme = bme->next)
592 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode))) &&
593 (bme->value == value) )
601 * Grow the given map to a more appropriate size.
603 * @param map the hash map to grow
606 grow (struct GNUNET_CONTAINER_MultiShortmap *map)
608 union MapEntry *old_map;
609 union MapEntry *new_map;
610 unsigned int old_len;
611 unsigned int new_len;
615 map->modification_counter++;
618 old_len = map->map_length;
619 new_len = old_len * 2;
620 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
621 map->map_length = new_len;
623 for (i = 0; i < old_len; i++)
625 if (map->use_small_entries)
627 struct SmallMapEntry *sme;
629 while (NULL != (sme = old_map[i].sme))
631 old_map[i].sme = sme->next;
632 idx = idx_of (map, sme->key);
633 sme->next = new_map[idx].sme;
634 new_map[idx].sme = sme;
639 struct BigMapEntry *bme;
641 while (NULL != (bme = old_map[i].bme))
643 old_map[i].bme = bme->next;
644 idx = idx_of (map, &bme->key);
645 bme->next = new_map[idx].bme;
646 new_map[idx].bme = bme;
650 GNUNET_free (old_map);
655 * Store a key-value pair in the map.
658 * @param key key to use
659 * @param value value to use
660 * @param opt options for put
661 * @return #GNUNET_OK on success,
662 * #GNUNET_NO if a value was replaced (with REPLACE)
663 * #GNUNET_SYSERR if GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
664 * value already exists
667 GNUNET_CONTAINER_multishortmap_put (struct GNUNET_CONTAINER_MultiShortmap *map,
668 const struct GNUNET_ShortHashCode *key,
670 enum GNUNET_CONTAINER_MultiHashMapOption opt)
675 i = idx_of (map, key);
676 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
677 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
680 if (map->use_small_entries)
682 struct SmallMapEntry *sme;
684 for (sme = me.sme; NULL != sme; sme = sme->next)
685 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
687 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
688 return GNUNET_SYSERR;
695 struct BigMapEntry *bme;
697 for (bme = me.bme; NULL != bme; bme = bme->next)
698 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
700 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
701 return GNUNET_SYSERR;
707 if (map->size / 3 >= map->map_length / 4)
710 i = idx_of (map, key);
712 if (map->use_small_entries)
714 struct SmallMapEntry *sme;
716 sme = GNUNET_new (struct SmallMapEntry);
719 sme->next = map->map[i].sme;
720 map->map[i].sme = sme;
724 struct BigMapEntry *bme;
726 bme = GNUNET_new (struct BigMapEntry);
729 bme->next = map->map[i].bme;
730 map->map[i].bme = bme;
738 * Iterate over all entries in the map that match a particular key.
741 * @param key key that the entries must correspond to
742 * @param it function to call on each entry
743 * @param it_cls extra argument to @a it
744 * @return the number of key value pairs processed,
745 * #GNUNET_SYSERR if it aborted iteration
748 GNUNET_CONTAINER_multishortmap_get_multiple (const struct GNUNET_CONTAINER_MultiShortmap *map,
749 const struct GNUNET_ShortHashCode *key,
750 GNUNET_CONTAINER_ShortmapIterator it,
757 me = map->map[idx_of (map, key)];
758 if (map->use_small_entries)
760 struct SmallMapEntry *sme;
761 struct SmallMapEntry *nxt;
764 while (NULL != (sme = nxt))
767 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_ShortHashCode)))
769 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
770 return GNUNET_SYSERR;
776 struct BigMapEntry *bme;
777 struct BigMapEntry *nxt;
780 while (NULL != (bme = nxt))
783 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_ShortHashCode)))
785 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
786 return GNUNET_SYSERR;
796 * Call @a it on a random value from the map, or not at all
797 * if the map is empty. Note that this function has linear
798 * complexity (in the size of the map).
801 * @param it function to call on a random entry
802 * @param it_cls extra argument to @a it
803 * @return the number of key value pairs processed, zero or one.
806 GNUNET_CONTAINER_multishortmap_get_random (const struct GNUNET_CONTAINER_MultiShortmap *map,
807 GNUNET_CONTAINER_ShortmapIterator it,
818 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
820 for (idx = 0; idx < map->map_length; idx++)
823 if (map->use_small_entries)
825 struct SmallMapEntry *sme;
826 struct SmallMapEntry *nxt;
829 while (NULL != (sme = nxt))
834 if (GNUNET_OK != it (it_cls,
837 return GNUNET_SYSERR;
845 struct BigMapEntry *bme;
846 struct BigMapEntry *nxt;
849 while (NULL != (bme = nxt))
854 if (GNUNET_OK != it (it_cls,
855 &bme->key, bme->value))
856 return GNUNET_SYSERR;
864 return GNUNET_SYSERR;
869 * Create an iterator for a multishortmap.
870 * The iterator can be used to retrieve all the elements in the multishortmap
871 * one by one, without having to handle all elements at once (in contrast to
872 * #GNUNET_CONTAINER_multishortmap_iterate). Note that the iterator can not be
873 * used anymore if elements have been removed from 'map' after the creation of
874 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
875 * result in skipped or repeated elements.
877 * @param map the map to create an iterator for
878 * @return an iterator over the given multishortmap 'map'
880 struct GNUNET_CONTAINER_MultiShortmapIterator *
881 GNUNET_CONTAINER_multishortmap_iterator_create (const struct GNUNET_CONTAINER_MultiShortmap *map)
883 struct GNUNET_CONTAINER_MultiShortmapIterator *iter;
885 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiShortmapIterator);
887 iter->modification_counter = map->modification_counter;
888 iter->me = map->map[0];
894 * Retrieve the next element from the hash map at the iterator's position.
895 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
897 * This operation is only allowed if no elements have been removed from the
898 * multishortmap since the creation of 'iter', and the map has not been destroyed.
899 * Adding elements may result in repeating or skipping elements.
901 * @param iter the iterator to get the next element from
902 * @param key pointer to store the key in, can be NULL
903 * @param value pointer to store the value in, can be NULL
904 * @return #GNUNET_YES we returned an element,
905 * #GNUNET_NO if we are out of elements
908 GNUNET_CONTAINER_multishortmap_iterator_next (struct GNUNET_CONTAINER_MultiShortmapIterator *iter,
909 struct GNUNET_ShortHashCode *key,
912 /* make sure the map has not been modified */
913 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
915 /* look for the next entry, skipping empty buckets */
918 if (iter->idx >= iter->map->map_length)
920 if (GNUNET_YES == iter->map->use_small_entries)
922 if (NULL != iter->me.sme)
925 *key = *iter->me.sme->key;
927 *value = iter->me.sme->value;
928 iter->me.sme = iter->me.sme->next;
934 if (NULL != iter->me.bme)
937 *key = iter->me.bme->key;
939 *value = iter->me.bme->value;
940 iter->me.bme = iter->me.bme->next;
945 if (iter->idx < iter->map->map_length)
946 iter->me = iter->map->map[iter->idx];
952 * Destroy a multishortmap iterator.
954 * @param iter the iterator to destroy
957 GNUNET_CONTAINER_multishortmap_iterator_destroy (struct GNUNET_CONTAINER_MultiShortmapIterator *iter)
963 /* end of container_multishortmap.c */