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,
268 return GNUNET_SYSERR;
278 * We are about to free() the @a bme, make sure it is not in
279 * the list of next values for any iterator in the @a map's next_cache.
281 * @param map the map to check
282 * @param bme the entry that is about to be free'd
285 update_next_cache (struct GNUNET_CONTAINER_MultiHashMap32 *map,
286 const struct MapEntry *me)
288 for (unsigned int i=0;i<map->next_cache_off;i++)
289 if (map->next_cache[i] == me)
290 map->next_cache[i] = me->next;
295 * Remove the given key-value pair from the map. Note that if the
296 * key-value pair is in the map multiple times, only one of the pairs
300 * @param key key of the key-value pair
301 * @param value value of the key-value pair
302 * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
306 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32 *map,
314 map->modification_counter++;
316 i = idx_of (map, key);
321 if ( (key == e->key) && (value == e->value) )
324 map->map[i] = e->next;
327 update_next_cache (map,
341 * Remove all entries for the given key from the map.
342 * Note that the values would not be "freed".
345 * @param key identifies values to be removed
346 * @return number of values removed
349 GNUNET_CONTAINER_multihashmap32_remove_all (struct GNUNET_CONTAINER_MultiHashMap32 *map,
357 map->modification_counter++;
360 i = idx_of (map, key);
368 map->map[i] = e->next;
371 update_next_cache (map,
392 * Check if the map contains any value under the given
393 * key (including values that are NULL).
396 * @param key the key to test if a value exists for it
397 * @return #GNUNET_YES if such a value exists,
401 GNUNET_CONTAINER_multihashmap32_contains (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
406 e = map->map[idx_of (map, key)];
418 * Check if the map contains the given value under the given
422 * @param key the key to test if a value exists for it
423 * @param value value to test for
424 * @return #GNUNET_YES if such a value exists,
428 GNUNET_CONTAINER_multihashmap32_contains_value (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
434 e = map->map[idx_of (map, key)];
437 if ( (key == e->key) && (e->value == value) )
446 * Grow the given map to a more appropriate size.
448 * @param map the hash map to grow
451 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
453 struct MapEntry **old_map;
454 struct MapEntry **new_map;
456 unsigned int old_len;
457 unsigned int new_len;
460 map->modification_counter++;
463 old_len = map->map_length;
464 new_len = old_len * 2;
465 new_map = GNUNET_new_array (new_len,
467 map->map_length = new_len;
469 for (unsigned int i = 0; i < old_len; i++)
471 while (NULL != (e = old_map[i]))
473 old_map[i] = e->next;
474 idx = idx_of (map, e->key);
475 e->next = new_map[idx];
479 GNUNET_free (old_map);
484 * Store a key-value pair in the map.
487 * @param key key to use
488 * @param value value to use
489 * @param opt options for put
490 * @return #GNUNET_OK on success,
491 * #GNUNET_NO if a value was replaced (with REPLACE)
492 * #GNUNET_SYSERR if UNIQUE_ONLY was the option and the
493 * value already exists
496 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
497 *map, uint32_t key, void *value,
498 enum GNUNET_CONTAINER_MultiHashMapOption
504 i = idx_of (map, key);
505 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
506 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
513 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
514 return GNUNET_SYSERR;
521 if (map->size / 3 >= map->map_length / 4)
524 i = idx_of (map, key);
526 e = GNUNET_new (struct MapEntry);
529 e->next = map->map[i];
537 * Iterate over all entries in the map that match a particular key.
540 * @param key key that the entries must correspond to
541 * @param it function to call on each entry
542 * @param it_cls extra argument to @a it
543 * @return the number of key value pairs processed,
544 * GNUNET_SYSERR if it aborted iteration
547 GNUNET_CONTAINER_multihashmap32_get_multiple (struct GNUNET_CONTAINER_MultiHashMap32 *map,
549 GNUNET_CONTAINER_HashMapIterator32 it,
554 struct MapEntry **ce;
557 ce = &map->next_cache[map->next_cache_off];
558 GNUNET_assert (++map->next_cache_off < NEXT_CACHE_SIZE);
560 *ce = map->map[idx_of (map, key)];
561 while (NULL != (e = *ce))
567 (GNUNET_OK != it (it_cls,
570 return GNUNET_SYSERR;
578 * Create an iterator for a multihashmap.
579 * The iterator can be used to retrieve all the elements in the multihashmap
580 * one by one, without having to handle all elements at once (in contrast to
581 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
582 * used anymore if elements have been removed from 'map' after the creation of
583 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
584 * result in skipped or repeated elements.
586 * @param map the map to create an iterator for
587 * @return an iterator over the given multihashmap @a map
589 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
590 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
592 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
594 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
596 iter->modification_counter = map->modification_counter;
597 iter->me = map->map[0];
603 * Retrieve the next element from the hash map at the iterator's position.
604 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
606 * This operation is only allowed if no elements have been removed from the
607 * multihashmap since the creation of 'iter', and the map has not been destroyed.
608 * Adding elements may result in repeating or skipping elements.
610 * @param iter the iterator to get the next element from
611 * @param key pointer to store the key in, can be NULL
612 * @param value pointer to store the value in, can be NULL
613 * @return #GNUNET_YES we returned an element,
614 * #GNUNET_NO if we are out of elements
617 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
621 /* make sure the map has not been modified */
622 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
624 /* look for the next entry, skipping empty buckets */
627 if (iter->idx >= iter->map->map_length)
629 if (NULL != iter->me)
632 *key = iter->me->key;
634 *value = iter->me->value;
635 iter->me = iter->me->next;
639 if (iter->idx < iter->map->map_length)
640 iter->me = iter->map->map[iter->idx];
646 * Destroy a multihashmap iterator.
648 * @param iter the iterator to destroy
651 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
657 /* end of container_multihashmap.c */