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/>.
19 * @file util/container_multihashmap32.c
20 * @brief a version of hash map implemented in container_multihashmap.c but with
22 * @author Christian Grothoff
23 * @author Sree Harsha Totakura
27 #include "gnunet_container_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
33 * Maximum recursion depth for callbacks of
34 * #GNUNET_CONTAINER_multihashmap_get_multiple() themselves
35 * again calling #GNUNET_CONTAINER_multihashmap_get_multiple().
36 * Should be totally excessive, but if violated we die.
38 #define NEXT_CACHE_SIZE 16
41 * An entry in the hash map.
57 * If there is a hash collision, we create a linked list.
59 struct MapEntry *next;
64 * Internal representation of the hash map.
66 struct GNUNET_CONTAINER_MultiHashMap32
72 struct MapEntry **map;
75 * Number of entries in the map.
80 * Length of the @e map array.
82 unsigned int map_length;
85 * Counts the destructive modifications (grow, remove)
86 * to the map, so that iterators can check if they are still valid.
88 unsigned int modification_counter;
91 * Map entries indicating iteration positions currently
92 * in use by #GNUNET_CONTAINER_multihashmap_get_multiple().
93 * Only used up to @e next_cache_off.
95 struct MapEntry *next_cache[NEXT_CACHE_SIZE];
98 * Offset of @e next_cache entries in use, must be smaller
99 * than #NEXT_CACHE_SIZE.
101 unsigned int next_cache_off;
106 * Cursor into a multihashmap.
107 * Allows to enumerate elements asynchronously.
109 struct GNUNET_CONTAINER_MultiHashMap32Iterator
112 * Position in the bucket @e idx
117 * Current bucket index.
122 * Modification counter as observed on the map when the iterator
125 unsigned int modification_counter;
128 * Map that we are iterating over.
130 const struct GNUNET_CONTAINER_MultiHashMap32 *map;
135 * Create a multi hash map.
137 * @param len initial size (map will grow as needed)
138 * @return NULL on error
140 struct GNUNET_CONTAINER_MultiHashMap32 *
141 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
143 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
145 GNUNET_assert (len > 0);
146 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
147 ret->map = GNUNET_new_array (len,
149 ret->map_length = len;
155 * Destroy a hash map. Will not free any values
156 * stored in the hash map!
161 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32 *map)
165 for (unsigned int i = 0; i < map->map_length; i++)
167 while (NULL != (e = map->map[i]))
169 map->map[i] = e->next;
173 GNUNET_free (map->map);
179 * Compute the index of the bucket for the given key.
181 * @param m hash map for which to compute the index
182 * @param key what key should the index be computed for
183 * @return offset into the "map" array of "m"
186 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
189 GNUNET_assert (NULL != m);
190 return ((unsigned int) key) % m->map_length;
195 * Get the number of key-value pairs in the map.
198 * @return the number of key value pairs
201 GNUNET_CONTAINER_multihashmap32_size (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
208 * Given a key find a value in the map matching the key.
211 * @param key what to look for
212 * @return NULL if no value was found; note that
213 * this is indistinguishable from values that just
214 * happen to be NULL; use "contains" to test for
215 * key-value pairs with value NULL
218 GNUNET_CONTAINER_multihashmap32_get (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
223 e = map->map[idx_of (map, key)];
235 * Iterate over all entries in the map.
238 * @param it function to call on each entry
239 * @param it_cls extra argument to @a it
240 * @return the number of key value pairs processed,
241 * #GNUNET_SYSERR if it aborted iteration
244 GNUNET_CONTAINER_multihashmap32_iterate (struct GNUNET_CONTAINER_MultiHashMap32 *map,
245 GNUNET_CONTAINER_HashMapIterator32 it,
249 struct MapEntry **ce;
252 GNUNET_assert (NULL != map);
253 ce = &map->next_cache[map->next_cache_off];
254 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
255 for (unsigned int i = 0; i < map->map_length; i++)
260 while (NULL != (e = *ce))
265 if (GNUNET_OK != it (it_cls,
269 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
270 return GNUNET_SYSERR;
276 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
282 * We are about to free() the @a bme, make sure it is not in
283 * the list of next values for any iterator in the @a map's next_cache.
285 * @param map the map to check
286 * @param bme the entry that is about to be free'd
289 update_next_cache (struct GNUNET_CONTAINER_MultiHashMap32 *map,
290 const struct MapEntry *me)
292 for (unsigned int i=0;i<map->next_cache_off;i++)
293 if (map->next_cache[i] == me)
294 map->next_cache[i] = me->next;
299 * Remove the given key-value pair from the map. Note that if the
300 * key-value pair is in the map multiple times, only one of the pairs
304 * @param key key of the key-value pair
305 * @param value value of the key-value pair
306 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
310 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32 *map,
318 map->modification_counter++;
320 i = idx_of (map, key);
325 if ( (key == e->key) && (value == e->value) )
328 map->map[i] = e->next;
331 update_next_cache (map,
345 * Remove all entries for the given key from the map.
346 * Note that the values would not be "freed".
349 * @param key identifies values to be removed
350 * @return number of values removed
353 GNUNET_CONTAINER_multihashmap32_remove_all (struct GNUNET_CONTAINER_MultiHashMap32 *map,
361 map->modification_counter++;
364 i = idx_of (map, key);
372 map->map[i] = e->next;
375 update_next_cache (map,
396 * Check if the map contains any value under the given
397 * key (including values that are NULL).
400 * @param key the key to test if a value exists for it
401 * @return #GNUNET_YES if such a value exists,
405 GNUNET_CONTAINER_multihashmap32_contains (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
410 e = map->map[idx_of (map, key)];
422 * Check if the map contains the given value under the given
426 * @param key the key to test if a value exists for it
427 * @param value value to test for
428 * @return #GNUNET_YES if such a value exists,
432 GNUNET_CONTAINER_multihashmap32_contains_value (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
438 e = map->map[idx_of (map, key)];
441 if ( (key == e->key) && (e->value == value) )
450 * Grow the given map to a more appropriate size.
452 * @param map the hash map to grow
455 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
457 struct MapEntry **old_map;
458 struct MapEntry **new_map;
460 unsigned int old_len;
461 unsigned int new_len;
464 map->modification_counter++;
467 old_len = map->map_length;
468 new_len = old_len * 2;
469 new_map = GNUNET_new_array (new_len,
471 map->map_length = new_len;
473 for (unsigned int i = 0; i < old_len; i++)
475 while (NULL != (e = old_map[i]))
477 old_map[i] = e->next;
478 idx = idx_of (map, e->key);
479 e->next = new_map[idx];
483 GNUNET_free (old_map);
488 * Store a key-value pair in the map.
491 * @param key key to use
492 * @param value value to use
493 * @param opt options for put
494 * @return #GNUNET_OK on success,
495 * #GNUNET_NO if a value was replaced (with REPLACE)
496 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
497 * value already exists
500 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
501 *map, uint32_t key, void *value,
502 enum GNUNET_CONTAINER_MultiHashMapOption
508 i = idx_of (map, key);
509 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
510 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
517 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
518 return GNUNET_SYSERR;
525 if (map->size / 3 >= map->map_length / 4)
528 i = idx_of (map, key);
530 e = GNUNET_new (struct MapEntry);
533 e->next = map->map[i];
541 * Iterate over all entries in the map that match a particular key.
544 * @param key key that the entries must correspond to
545 * @param it function to call on each entry
546 * @param it_cls extra argument to @a it
547 * @return the number of key value pairs processed,
548 * GNUNET_SYSERR if it aborted iteration
551 GNUNET_CONTAINER_multihashmap32_get_multiple (struct GNUNET_CONTAINER_MultiHashMap32 *map,
553 GNUNET_CONTAINER_HashMapIterator32 it,
558 struct MapEntry **ce;
561 ce = &map->next_cache[map->next_cache_off];
562 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
564 *ce = map->map[idx_of (map, key)];
565 while (NULL != (e = *ce))
571 (GNUNET_OK != it (it_cls,
575 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
576 return GNUNET_SYSERR;
580 GNUNET_assert (--map->next_cache_off < NEXT_CACHE_SIZE);
586 * Create an iterator for a multihashmap.
587 * The iterator can be used to retrieve all the elements in the multihashmap
588 * one by one, without having to handle all elements at once (in contrast to
589 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
590 * used anymore if elements have been removed from 'map' after the creation of
591 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
592 * result in skipped or repeated elements.
594 * @param map the map to create an iterator for
595 * @return an iterator over the given multihashmap @a map
597 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
598 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
600 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
602 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
604 iter->modification_counter = map->modification_counter;
605 iter->me = map->map[0];
611 * Retrieve the next element from the hash map at the iterator's position.
612 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
614 * This operation is only allowed if no elements have been removed from the
615 * multihashmap since the creation of 'iter', and the map has not been destroyed.
616 * Adding elements may result in repeating or skipping elements.
618 * @param iter the iterator to get the next element from
619 * @param key pointer to store the key in, can be NULL
620 * @param value pointer to store the value in, can be NULL
621 * @return #GNUNET_YES we returned an element,
622 * #GNUNET_NO if we are out of elements
625 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
629 /* make sure the map has not been modified */
630 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
632 /* look for the next entry, skipping empty buckets */
635 if (iter->idx >= iter->map->map_length)
637 if (NULL != iter->me)
640 *key = iter->me->key;
642 *value = iter->me->value;
643 iter->me = iter->me->next;
647 if (iter->idx < iter->map->map_length)
648 iter->me = iter->map->map[iter->idx];
654 * Destroy a multihashmap iterator.
656 * @param iter the iterator to destroy
659 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
665 /* end of container_multihashmap.c */