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__)
32 * An entry in the hash map.
48 * If there is a hash collision, we create a linked list.
50 struct MapEntry *next;
55 * Internal representation of the hash map.
57 struct GNUNET_CONTAINER_MultiHashMap32
63 struct MapEntry **map;
66 * Number of entries in the map.
71 * Length of the "map" array.
73 unsigned int map_length;
76 * Counts the destructive modifications (grow, remove)
77 * to the map, so that iterators can check if they are still valid.
79 unsigned int modification_counter;
84 * Cursor into a multihashmap.
85 * Allows to enumerate elements asynchronously.
87 struct GNUNET_CONTAINER_MultiHashMap32Iterator
90 * Position in the bucket 'idx'
95 * Current bucket index.
100 * Modification counter as observed on the map when the iterator
103 unsigned int modification_counter;
106 * Map that we are iterating over.
108 const struct GNUNET_CONTAINER_MultiHashMap32 *map;
113 * Create a multi hash map.
115 * @param len initial size (map will grow as needed)
116 * @return NULL on error
118 struct GNUNET_CONTAINER_MultiHashMap32 *
119 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
121 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
123 GNUNET_assert (len > 0);
124 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
125 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
126 ret->map_length = len;
132 * Destroy a hash map. Will not free any values
133 * stored in the hash map!
138 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
144 for (i = 0; i < map->map_length; i++)
146 while (NULL != (e = map->map[i]))
148 map->map[i] = e->next;
152 GNUNET_free (map->map);
158 * Compute the index of the bucket for the given key.
160 * @param m hash map for which to compute the index
161 * @param key what key should the index be computed for
162 * @return offset into the "map" array of "m"
165 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
168 GNUNET_assert (m != NULL);
169 return ((unsigned int) key) % m->map_length;
174 * Get the number of key-value pairs in the map.
177 * @return the number of key value pairs
180 GNUNET_CONTAINER_multihashmap32_size (const struct
181 GNUNET_CONTAINER_MultiHashMap32 *map)
188 * Given a key find a value in the map matching the key.
191 * @param key what to look for
192 * @return NULL if no value was found; note that
193 * this is indistinguishable from values that just
194 * happen to be NULL; use "contains" to test for
195 * key-value pairs with value NULL
198 GNUNET_CONTAINER_multihashmap32_get (const struct
199 GNUNET_CONTAINER_MultiHashMap32 *map,
204 e = map->map[idx_of (map, key)];
216 * Iterate over all entries in the map.
219 * @param it function to call on each entry
220 * @param it_cls extra argument to @a it
221 * @return the number of key value pairs processed,
222 * #GNUNET_SYSERR if it aborted iteration
225 GNUNET_CONTAINER_multihashmap32_iterate (const struct
226 GNUNET_CONTAINER_MultiHashMap32 *map,
227 GNUNET_CONTAINER_HashMapIterator32 it,
236 GNUNET_assert (NULL != map);
237 for (i = 0; i < map->map_length; i++)
240 while (NULL != (e = n))
245 if (GNUNET_OK != it (it_cls, e->key, e->value))
246 return GNUNET_SYSERR;
256 * Remove the given key-value pair from the map. Note that if the
257 * key-value pair is in the map multiple times, only one of the pairs
261 * @param key key of the key-value pair
262 * @param value value of the key-value pair
263 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
267 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
269 uint32_t key, const void *value)
275 map->modification_counter++;
277 i = idx_of (map, key);
282 if ( (key == e->key) && (value == e->value) )
285 map->map[i] = e->next;
300 * Remove all entries for the given key from the map.
301 * Note that the values would not be "freed".
304 * @param key identifies values to be removed
305 * @return number of values removed
308 GNUNET_CONTAINER_multihashmap32_remove_all (struct
309 GNUNET_CONTAINER_MultiHashMap32
318 map->modification_counter++;
321 i = idx_of (map, key);
329 map->map[i] = e->next;
351 * Check if the map contains any value under the given
352 * key (including values that are NULL).
355 * @param key the key to test if a value exists for it
356 * @return GNUNET_YES if such a value exists,
360 GNUNET_CONTAINER_multihashmap32_contains (const struct
361 GNUNET_CONTAINER_MultiHashMap32 *map,
366 e = map->map[idx_of (map, key)];
378 * Check if the map contains the given value under the given
382 * @param key the key to test if a value exists for it
383 * @param value value to test for
384 * @return GNUNET_YES if such a value exists,
388 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
389 GNUNET_CONTAINER_MultiHashMap32
396 e = map->map[idx_of (map, key)];
399 if ( (key == e->key) && (e->value == value) )
408 * Grow the given map to a more appropriate size.
410 * @param map the hash map to grow
413 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
415 struct MapEntry **old_map;
416 struct MapEntry **new_map;
418 unsigned int old_len;
419 unsigned int new_len;
423 map->modification_counter++;
426 old_len = map->map_length;
427 new_len = old_len * 2;
428 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
429 map->map_length = new_len;
431 for (i = 0; i < old_len; i++)
433 while (NULL != (e = old_map[i]))
435 old_map[i] = e->next;
436 idx = idx_of (map, e->key);
437 e->next = new_map[idx];
441 GNUNET_free (old_map);
446 * Store a key-value pair in the map.
449 * @param key key to use
450 * @param value value to use
451 * @param opt options for put
452 * @return GNUNET_OK on success,
453 * GNUNET_NO if a value was replaced (with REPLACE)
454 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
455 * value already exists
458 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
459 *map, uint32_t key, void *value,
460 enum GNUNET_CONTAINER_MultiHashMapOption
466 i = idx_of (map, key);
467 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
468 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
475 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
476 return GNUNET_SYSERR;
483 if (map->size / 3 >= map->map_length / 4)
486 i = idx_of (map, key);
488 e = GNUNET_new (struct MapEntry);
491 e->next = map->map[i];
499 * Iterate over all entries in the map that match a particular key.
502 * @param key key that the entries must correspond to
503 * @param it function to call on each entry
504 * @param it_cls extra argument to it
505 * @return the number of key value pairs processed,
506 * GNUNET_SYSERR if it aborted iteration
509 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
510 GNUNET_CONTAINER_MultiHashMap32
512 GNUNET_CONTAINER_HashMapIterator32
520 n = map->map[idx_of (map, key)];
521 while (NULL != (e = n))
526 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
527 return GNUNET_SYSERR;
535 * Create an iterator for a multihashmap.
536 * The iterator can be used to retrieve all the elements in the multihashmap
537 * one by one, without having to handle all elements at once (in contrast to
538 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
539 * used anymore if elements have been removed from 'map' after the creation of
540 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
541 * result in skipped or repeated elements.
543 * @param map the map to create an iterator for
544 * @return an iterator over the given multihashmap 'map'
546 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
547 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
549 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
551 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
553 iter->modification_counter = map->modification_counter;
554 iter->me = map->map[0];
560 * Retrieve the next element from the hash map at the iterator's position.
561 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
563 * This operation is only allowed if no elements have been removed from the
564 * multihashmap since the creation of 'iter', and the map has not been destroyed.
565 * Adding elements may result in repeating or skipping elements.
567 * @param iter the iterator to get the next element from
568 * @param key pointer to store the key in, can be NULL
569 * @param value pointer to store the value in, can be NULL
570 * @return #GNUNET_YES we returned an element,
571 * #GNUNET_NO if we are out of elements
574 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
578 /* make sure the map has not been modified */
579 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
581 /* look for the next entry, skipping empty buckets */
584 if (iter->idx >= iter->map->map_length)
586 if (NULL != iter->me)
589 *key = iter->me->key;
591 *value = iter->me->value;
592 iter->me = iter->me->next;
596 if (iter->idx < iter->map->map_length)
597 iter->me = iter->map->map[iter->idx];
603 * Destroy a multihashmap iterator.
605 * @param iter the iterator to destroy
608 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
614 /* end of container_multihashmap.c */