2 This file is part of GNUnet.
3 Copyright (C) 2008 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/container_multihashmap32.c
22 * @brief a version of hash map implemented in container_multihashmap.c but with
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
29 #include "gnunet_container_lib.h"
31 #define LOG(kind, ...) \
32 GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
36 * Maximum recursion depth for callbacks of
37 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves
38 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
39 * Should be totally excessive, but if violated we die.
41 #define NEXT_CACHE_SIZE 16
44 * An entry in the hash map.
60 * If there is a hash collision, we create a linked list.
62 struct MapEntry *next;
66 * Internal representation of the hash map.
68 struct GNUNET_CONTAINER_MultiHashMap32
74 struct MapEntry **map;
77 * Number of entries in the map.
82 * Length of the @e map array.
84 unsigned int map_length;
87 * Counts the destructive modifications (grow, remove)
88 * to the map, so that iterators can check if they are still valid.
90 unsigned int modification_counter;
93 * Map entries indicating iteration positions currently
94 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
95 * Only used up to @e next_cache_off.
97 struct MapEntry *next_cache[NEXT_CACHE_SIZE];
100 * Offset of @e next_cache entries in use, must be smaller
101 * than #NEXT_CACHE_SIZE.
103 unsigned int next_cache_off;
108 * Cursor into a multihashmap.
109 * Allows to enumerate elements asynchronously.
111 struct GNUNET_CONTAINER_MultiHashMap32Iterator
114 * Position in the bucket @e idx
119 * Current bucket index.
124 * Modification counter as observed on the map when the iterator
127 unsigned int modification_counter;
130 * Map that we are iterating over.
132 const struct GNUNET_CONTAINER_MultiHashMap32 *map;
137 * Create a multi hash map.
139 * @param len initial size (map will grow as needed)
140 * @return NULL on error
142 struct GNUNET_CONTAINER_MultiHashMap32 *
143 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
145 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
147 GNUNET_assert (len > 0);
148 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
149 ret->map = GNUNET_malloc_large (len * sizeof (struct MapEntry *));
150 if (NULL == ret->map)
155 ret->map_length = len;
161 * Destroy a hash map. Will not free any values
162 * stored in the hash map!
167 GNUNET_CONTAINER_multihashmap32_destroy (
168 struct GNUNET_CONTAINER_MultiHashMap32 *map)
172 for (unsigned int i = 0; i < map->map_length; i++)
174 while (NULL != (e = map->map[i]))
176 map->map[i] = e->next;
180 GNUNET_free (map->map);
186 * Compute the index of the bucket for the given key.
188 * @param m hash map for which to compute the index
189 * @param key what key should the index be computed for
190 * @return offset into the "map" array of "m"
193 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m, const uint32_t key)
195 GNUNET_assert (NULL != m);
196 return ((unsigned int) key) % m->map_length;
201 * Get the number of key-value pairs in the map.
204 * @return the number of key value pairs
207 GNUNET_CONTAINER_multihashmap32_size (
208 const struct GNUNET_CONTAINER_MultiHashMap32 *map)
215 * Given a key find a value in the map matching the key.
218 * @param key what to look for
219 * @return NULL if no value was found; note that
220 * this is indistinguishable from values that just
221 * happen to be NULL; use "contains" to test for
222 * key-value pairs with value NULL
225 GNUNET_CONTAINER_multihashmap32_get (
226 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
231 e = map->map[idx_of (map, key)];
243 * Iterate over all entries in the map.
246 * @param it function to call on each entry
247 * @param it_cls extra argument to @a it
248 * @return the number of key value pairs processed,
249 * #GNUNET_SYSERR if it aborted iteration
252 GNUNET_CONTAINER_multihashmap32_iterate (
253 struct GNUNET_CONTAINER_MultiHashMap32 *map,
254 GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
258 struct MapEntry **ce;
261 GNUNET_assert (NULL != map);
262 ce = &map->next_cache[map->next_cache_off];
263 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
264 for (unsigned int i = 0; i < map->map_length; i++)
269 while (NULL != (e = *ce))
274 if (GNUNET_OK != it (it_cls, e->key, e->value))
276 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
277 return GNUNET_SYSERR;
283 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
289 * We are about to free() the @a bme, make sure it is not in
290 * the list of next values for any iterator in the @a map's next_cache.
292 * @param map the map to check
293 * @param bme the entry that is about to be free'd
296 update_next_cache (struct GNUNET_CONTAINER_MultiHashMap32 *map,
297 const struct MapEntry *me)
299 for (unsigned int i = 0; i < map->next_cache_off; i++)
300 if (map->next_cache[i] == me)
301 map->next_cache[i] = me->next;
306 * Remove the given key-value pair from the map. Note that if the
307 * key-value pair is in the map multiple times, only one of the pairs
311 * @param key key of the key-value pair
312 * @param value value of the key-value pair
313 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
317 GNUNET_CONTAINER_multihashmap32_remove (
318 struct GNUNET_CONTAINER_MultiHashMap32 *map,
326 map->modification_counter++;
328 i = idx_of (map, key);
333 if ((key == e->key) && (value == e->value))
336 map->map[i] = e->next;
339 update_next_cache (map, e);
352 * Remove all entries for the given key from the map.
353 * Note that the values would not be "freed".
356 * @param key identifies values to be removed
357 * @return number of values removed
360 GNUNET_CONTAINER_multihashmap32_remove_all (
361 struct GNUNET_CONTAINER_MultiHashMap32 *map,
369 map->modification_counter++;
372 i = idx_of (map, key);
380 map->map[i] = e->next;
383 update_next_cache (map, e);
403 * Check if the map contains any value under the given
404 * key (including values that are NULL).
407 * @param key the key to test if a value exists for it
408 * @return #GNUNET_YES if such a value exists,
412 GNUNET_CONTAINER_multihashmap32_contains (
413 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
418 e = map->map[idx_of (map, key)];
430 * Check if the map contains the given value under the given
434 * @param key the key to test if a value exists for it
435 * @param value value to test for
436 * @return #GNUNET_YES if such a value exists,
440 GNUNET_CONTAINER_multihashmap32_contains_value (
441 const struct GNUNET_CONTAINER_MultiHashMap32 *map,
447 e = map->map[idx_of (map, key)];
450 if ((key == e->key) && (e->value == value))
459 * Grow the given map to a more appropriate size.
461 * @param map the hash map to grow
464 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
466 struct MapEntry **old_map;
467 struct MapEntry **new_map;
469 unsigned int old_len;
470 unsigned int new_len;
474 old_len = map->map_length;
475 new_len = old_len * 2;
476 if (0 == new_len) /* 2^31 * 2 == 0 */
477 new_len = old_len; /* never use 0 */
478 if (new_len == old_len)
479 return; /* nothing changed */
480 new_map = GNUNET_malloc_large (new_len * sizeof (struct MapEntry *));
482 return; /* grow not possible */
483 map->modification_counter++;
484 map->map_length = new_len;
486 for (unsigned int i = 0; i < old_len; i++)
488 while (NULL != (e = old_map[i]))
490 old_map[i] = e->next;
491 idx = idx_of (map, e->key);
492 e->next = new_map[idx];
496 GNUNET_free (old_map);
501 * Store a key-value pair in the map.
504 * @param key key to use
505 * @param value value to use
506 * @param opt options for put
507 * @return #GNUNET_OK on success,
508 * #GNUNET_NO if a value was replaced (with REPLACE)
509 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
510 * value already exists
513 GNUNET_CONTAINER_multihashmap32_put (
514 struct GNUNET_CONTAINER_MultiHashMap32 *map,
517 enum GNUNET_CONTAINER_MultiHashMapOption opt)
522 i = idx_of (map, key);
523 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
524 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
531 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
532 return GNUNET_SYSERR;
539 if (map->size / 3 >= map->map_length / 4)
542 i = idx_of (map, key);
544 e = GNUNET_new (struct MapEntry);
547 e->next = map->map[i];
555 * Iterate over all entries in the map that match a particular key.
558 * @param key key that the entries must correspond to
559 * @param it function to call on each entry
560 * @param it_cls extra argument to @a it
561 * @return the number of key value pairs processed,
562 * GNUNET_SYSERR if it aborted iteration
565 GNUNET_CONTAINER_multihashmap32_get_multiple (
566 struct GNUNET_CONTAINER_MultiHashMap32 *map,
568 GNUNET_CONTAINER_MulitHashMapIterator32Callback it,
573 struct MapEntry **ce;
576 ce = &map->next_cache[map->next_cache_off];
577 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
579 *ce = map->map[idx_of (map, key)];
580 while (NULL != (e = *ce))
585 if ((NULL != it) && (GNUNET_OK != it (it_cls, key, e->value)))
587 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
588 return GNUNET_SYSERR;
592 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
598 * Create an iterator for a multihashmap.
599 * The iterator can be used to retrieve all the elements in the multihashmap
600 * one by one, without having to handle all elements at once (in contrast to
601 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
602 * used anymore if elements have been removed from 'map' after the creation of
603 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
604 * result in skipped or repeated elements.
606 * @param map the map to create an iterator for
607 * @return an iterator over the given multihashmap @a map
609 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
610 GNUNET_CONTAINER_multihashmap32_iterator_create (
611 const struct GNUNET_CONTAINER_MultiHashMap32 *map)
613 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
615 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
617 iter->modification_counter = map->modification_counter;
618 iter->me = map->map[0];
624 * Retrieve the next element from the hash map at the iterator's position.
625 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
627 * This operation is only allowed if no elements have been removed from the
628 * multihashmap since the creation of 'iter', and the map has not been destroyed.
629 * Adding elements may result in repeating or skipping elements.
631 * @param iter the iterator to get the next element from
632 * @param key pointer to store the key in, can be NULL
633 * @param value pointer to store the value in, can be NULL
634 * @return #GNUNET_YES we returned an element,
635 * #GNUNET_NO if we are out of elements
638 GNUNET_CONTAINER_multihashmap32_iterator_next (
639 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
643 /* make sure the map has not been modified */
644 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
646 /* look for the next entry, skipping empty buckets */
649 if (iter->idx >= iter->map->map_length)
651 if (NULL != iter->me)
654 *key = iter->me->key;
656 *value = iter->me->value;
657 iter->me = iter->me->next;
661 if (iter->idx < iter->map->map_length)
662 iter->me = iter->map->map[iter->idx];
668 * Destroy a multihashmap iterator.
670 * @param iter the iterator to destroy
673 GNUNET_CONTAINER_multihashmap32_iterator_destroy (
674 struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
680 /* end of container_multihashmap.c */