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 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.
16 * @file util/container_multihashmap32.c
17 * @brief a version of hash map implemented in container_multihashmap.c but with
19 * @author Christian Grothoff
20 * @author Sree Harsha Totakura
24 #include "gnunet_container_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
29 * An entry in the hash map.
45 * If there is a hash collision, we create a linked list.
47 struct MapEntry *next;
52 * Internal representation of the hash map.
54 struct GNUNET_CONTAINER_MultiHashMap32
60 struct MapEntry **map;
63 * Number of entries in the map.
68 * Length of the "map" array.
70 unsigned int map_length;
73 * Counts the destructive modifications (grow, remove)
74 * to the map, so that iterators can check if they are still valid.
76 unsigned int modification_counter;
81 * Cursor into a multihashmap.
82 * Allows to enumerate elements asynchronously.
84 struct GNUNET_CONTAINER_MultiHashMap32Iterator
87 * Position in the bucket 'idx'
92 * Current bucket index.
97 * Modification counter as observed on the map when the iterator
100 unsigned int modification_counter;
103 * Map that we are iterating over.
105 const struct GNUNET_CONTAINER_MultiHashMap32 *map;
110 * Create a multi hash map.
112 * @param len initial size (map will grow as needed)
113 * @return NULL on error
115 struct GNUNET_CONTAINER_MultiHashMap32 *
116 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
118 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
120 GNUNET_assert (len > 0);
121 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
122 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
123 ret->map_length = len;
129 * Destroy a hash map. Will not free any values
130 * stored in the hash map!
135 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
141 for (i = 0; i < map->map_length; i++)
143 while (NULL != (e = map->map[i]))
145 map->map[i] = e->next;
149 GNUNET_free (map->map);
155 * Compute the index of the bucket for the given key.
157 * @param m hash map for which to compute the index
158 * @param key what key should the index be computed for
159 * @return offset into the "map" array of "m"
162 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
165 GNUNET_assert (m != NULL);
166 return ((unsigned int) key) % m->map_length;
171 * Get the number of key-value pairs in the map.
174 * @return the number of key value pairs
177 GNUNET_CONTAINER_multihashmap32_size (const struct
178 GNUNET_CONTAINER_MultiHashMap32 *map)
185 * Given a key find a value in the map matching the key.
188 * @param key what to look for
189 * @return NULL if no value was found; note that
190 * this is indistinguishable from values that just
191 * happen to be NULL; use "contains" to test for
192 * key-value pairs with value NULL
195 GNUNET_CONTAINER_multihashmap32_get (const struct
196 GNUNET_CONTAINER_MultiHashMap32 *map,
201 e = map->map[idx_of (map, key)];
213 * Iterate over all entries in the map.
216 * @param it function to call on each entry
217 * @param it_cls extra argument to @a it
218 * @return the number of key value pairs processed,
219 * #GNUNET_SYSERR if it aborted iteration
222 GNUNET_CONTAINER_multihashmap32_iterate (const struct
223 GNUNET_CONTAINER_MultiHashMap32 *map,
224 GNUNET_CONTAINER_HashMapIterator32 it,
233 GNUNET_assert (NULL != map);
234 for (i = 0; i < map->map_length; i++)
237 while (NULL != (e = n))
242 if (GNUNET_OK != it (it_cls, e->key, e->value))
243 return GNUNET_SYSERR;
253 * Remove the given key-value pair from the map. Note that if the
254 * key-value pair is in the map multiple times, only one of the pairs
258 * @param key key of the key-value pair
259 * @param value value of the key-value pair
260 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
264 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
266 uint32_t key, const void *value)
272 map->modification_counter++;
274 i = idx_of (map, key);
279 if ( (key == e->key) && (value == e->value) )
282 map->map[i] = e->next;
297 * Remove all entries for the given key from the map.
298 * Note that the values would not be "freed".
301 * @param key identifies values to be removed
302 * @return number of values removed
305 GNUNET_CONTAINER_multihashmap32_remove_all (struct
306 GNUNET_CONTAINER_MultiHashMap32
315 map->modification_counter++;
318 i = idx_of (map, key);
326 map->map[i] = e->next;
348 * Check if the map contains any value under the given
349 * key (including values that are NULL).
352 * @param key the key to test if a value exists for it
353 * @return GNUNET_YES if such a value exists,
357 GNUNET_CONTAINER_multihashmap32_contains (const struct
358 GNUNET_CONTAINER_MultiHashMap32 *map,
363 e = map->map[idx_of (map, key)];
375 * Check if the map contains the given value under the given
379 * @param key the key to test if a value exists for it
380 * @param value value to test for
381 * @return GNUNET_YES if such a value exists,
385 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
386 GNUNET_CONTAINER_MultiHashMap32
393 e = map->map[idx_of (map, key)];
396 if ( (key == e->key) && (e->value == value) )
405 * Grow the given map to a more appropriate size.
407 * @param map the hash map to grow
410 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
412 struct MapEntry **old_map;
413 struct MapEntry **new_map;
415 unsigned int old_len;
416 unsigned int new_len;
420 map->modification_counter++;
423 old_len = map->map_length;
424 new_len = old_len * 2;
425 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
426 map->map_length = new_len;
428 for (i = 0; i < old_len; i++)
430 while (NULL != (e = old_map[i]))
432 old_map[i] = e->next;
433 idx = idx_of (map, e->key);
434 e->next = new_map[idx];
438 GNUNET_free (old_map);
443 * Store a key-value pair in the map.
446 * @param key key to use
447 * @param value value to use
448 * @param opt options for put
449 * @return GNUNET_OK on success,
450 * GNUNET_NO if a value was replaced (with REPLACE)
451 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
452 * value already exists
455 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
456 *map, uint32_t key, void *value,
457 enum GNUNET_CONTAINER_MultiHashMapOption
463 i = idx_of (map, key);
464 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
465 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
472 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
473 return GNUNET_SYSERR;
480 if (map->size / 3 >= map->map_length / 4)
483 i = idx_of (map, key);
485 e = GNUNET_new (struct MapEntry);
488 e->next = map->map[i];
496 * Iterate over all entries in the map that match a particular key.
499 * @param key key that the entries must correspond to
500 * @param it function to call on each entry
501 * @param it_cls extra argument to it
502 * @return the number of key value pairs processed,
503 * GNUNET_SYSERR if it aborted iteration
506 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
507 GNUNET_CONTAINER_MultiHashMap32
509 GNUNET_CONTAINER_HashMapIterator32
517 n = map->map[idx_of (map, key)];
518 while (NULL != (e = n))
523 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
524 return GNUNET_SYSERR;
532 * Create an iterator for a multihashmap.
533 * The iterator can be used to retrieve all the elements in the multihashmap
534 * one by one, without having to handle all elements at once (in contrast to
535 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
536 * used anymore if elements have been removed from 'map' after the creation of
537 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
538 * result in skipped or repeated elements.
540 * @param map the map to create an iterator for
541 * @return an iterator over the given multihashmap 'map'
543 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
544 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
546 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
548 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
550 iter->modification_counter = map->modification_counter;
551 iter->me = map->map[0];
557 * Retrieve the next element from the hash map at the iterator's position.
558 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
560 * This operation is only allowed if no elements have been removed from the
561 * multihashmap since the creation of 'iter', and the map has not been destroyed.
562 * Adding elements may result in repeating or skipping elements.
564 * @param iter the iterator to get the next element from
565 * @param key pointer to store the key in, can be NULL
566 * @param value pointer to store the value in, can be NULL
567 * @return #GNUNET_YES we returned an element,
568 * #GNUNET_NO if we are out of elements
571 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
575 /* make sure the map has not been modified */
576 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
578 /* look for the next entry, skipping empty buckets */
581 if (iter->idx >= iter->map->map_length)
583 if (NULL != iter->me)
586 *key = iter->me->key;
588 *value = iter->me->value;
589 iter->me = iter->me->next;
593 if (iter->idx < iter->map->map_length)
594 iter->me = iter->map->map[iter->idx];
600 * Destroy a multihashmap iterator.
602 * @param iter the iterator to destroy
605 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
611 /* end of container_multihashmap.c */