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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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,...) GNUNET_log_from (kind, "util-container-multihashmap32", __VA_ARGS__)
34 * An entry in the hash map.
50 * If there is a hash collision, we create a linked list.
52 struct MapEntry *next;
57 * Internal representation of the hash map.
59 struct GNUNET_CONTAINER_MultiHashMap32
65 struct MapEntry **map;
68 * Number of entries in the map.
73 * Length of the "map" array.
75 unsigned int map_length;
78 * Counts the destructive modifications (grow, remove)
79 * to the map, so that iterators can check if they are still valid.
81 unsigned int modification_counter;
86 * Cursor into a multihashmap.
87 * Allows to enumerate elements asynchronously.
89 struct GNUNET_CONTAINER_MultiHashMap32Iterator
92 * Position in the bucket 'idx'
97 * Current bucket index.
102 * Modification counter as observed on the map when the iterator
105 unsigned int modification_counter;
108 * Map that we are iterating over.
110 const struct GNUNET_CONTAINER_MultiHashMap32 *map;
115 * Create a multi hash map.
117 * @param len initial size (map will grow as needed)
118 * @return NULL on error
120 struct GNUNET_CONTAINER_MultiHashMap32 *
121 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
123 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
125 GNUNET_assert (len > 0);
126 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
127 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
128 ret->map_length = len;
134 * Destroy a hash map. Will not free any values
135 * stored in the hash map!
140 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
146 for (i = 0; i < map->map_length; i++)
148 while (NULL != (e = map->map[i]))
150 map->map[i] = e->next;
154 GNUNET_free (map->map);
160 * Compute the index of the bucket for the given key.
162 * @param m hash map for which to compute the index
163 * @param key what key should the index be computed for
164 * @return offset into the "map" array of "m"
167 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
170 GNUNET_assert (m != NULL);
171 return ((unsigned int) key) % m->map_length;
176 * Get the number of key-value pairs in the map.
179 * @return the number of key value pairs
182 GNUNET_CONTAINER_multihashmap32_size (const struct
183 GNUNET_CONTAINER_MultiHashMap32 *map)
190 * Given a key find a value in the map matching the key.
193 * @param key what to look for
194 * @return NULL if no value was found; note that
195 * this is indistinguishable from values that just
196 * happen to be NULL; use "contains" to test for
197 * key-value pairs with value NULL
200 GNUNET_CONTAINER_multihashmap32_get (const struct
201 GNUNET_CONTAINER_MultiHashMap32 *map,
206 e = map->map[idx_of (map, key)];
218 * Iterate over all entries in the map.
221 * @param it function to call on each entry
222 * @param it_cls extra argument to @a it
223 * @return the number of key value pairs processed,
224 * #GNUNET_SYSERR if it aborted iteration
227 GNUNET_CONTAINER_multihashmap32_iterate (const struct
228 GNUNET_CONTAINER_MultiHashMap32 *map,
229 GNUNET_CONTAINER_HashMapIterator32 it,
238 GNUNET_assert (NULL != map);
239 for (i = 0; i < map->map_length; i++)
242 while (NULL != (e = n))
247 if (GNUNET_OK != it (it_cls, e->key, e->value))
248 return GNUNET_SYSERR;
258 * Remove the given key-value pair from the map. Note that if the
259 * key-value pair is in the map multiple times, only one of the pairs
263 * @param key key of the key-value pair
264 * @param value value of the key-value pair
265 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
269 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
271 uint32_t key, const void *value)
277 map->modification_counter++;
279 i = idx_of (map, key);
284 if ( (key == e->key) && (value == e->value) )
287 map->map[i] = e->next;
302 * Remove all entries for the given key from the map.
303 * Note that the values would not be "freed".
306 * @param key identifies values to be removed
307 * @return number of values removed
310 GNUNET_CONTAINER_multihashmap32_remove_all (struct
311 GNUNET_CONTAINER_MultiHashMap32
320 map->modification_counter++;
323 i = idx_of (map, key);
331 map->map[i] = e->next;
353 * Check if the map contains any value under the given
354 * key (including values that are NULL).
357 * @param key the key to test if a value exists for it
358 * @return GNUNET_YES if such a value exists,
362 GNUNET_CONTAINER_multihashmap32_contains (const struct
363 GNUNET_CONTAINER_MultiHashMap32 *map,
368 e = map->map[idx_of (map, key)];
380 * Check if the map contains the given value under the given
384 * @param key the key to test if a value exists for it
385 * @param value value to test for
386 * @return GNUNET_YES if such a value exists,
390 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
391 GNUNET_CONTAINER_MultiHashMap32
398 e = map->map[idx_of (map, key)];
401 if ( (key == e->key) && (e->value == value) )
410 * Grow the given map to a more appropriate size.
412 * @param map the hash map to grow
415 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
417 struct MapEntry **old_map;
418 struct MapEntry **new_map;
420 unsigned int old_len;
421 unsigned int new_len;
425 map->modification_counter++;
428 old_len = map->map_length;
429 new_len = old_len * 2;
430 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
431 map->map_length = new_len;
433 for (i = 0; i < old_len; i++)
435 while (NULL != (e = old_map[i]))
437 old_map[i] = e->next;
438 idx = idx_of (map, e->key);
439 e->next = new_map[idx];
443 GNUNET_free (old_map);
448 * Store a key-value pair in the map.
451 * @param key key to use
452 * @param value value to use
453 * @param opt options for put
454 * @return GNUNET_OK on success,
455 * GNUNET_NO if a value was replaced (with REPLACE)
456 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
457 * value already exists
460 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
461 *map, uint32_t key, void *value,
462 enum GNUNET_CONTAINER_MultiHashMapOption
468 i = idx_of (map, key);
469 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
470 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
477 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
478 return GNUNET_SYSERR;
485 if (map->size / 3 >= map->map_length / 4)
488 i = idx_of (map, key);
490 e = GNUNET_new (struct MapEntry);
493 e->next = map->map[i];
501 * Iterate over all entries in the map that match a particular key.
504 * @param key key that the entries must correspond to
505 * @param it function to call on each entry
506 * @param it_cls extra argument to it
507 * @return the number of key value pairs processed,
508 * GNUNET_SYSERR if it aborted iteration
511 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
512 GNUNET_CONTAINER_MultiHashMap32
514 GNUNET_CONTAINER_HashMapIterator32
522 n = map->map[idx_of (map, key)];
523 while (NULL != (e = n))
528 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
529 return GNUNET_SYSERR;
537 * Create an iterator for a multihashmap.
538 * The iterator can be used to retrieve all the elements in the multihashmap
539 * one by one, without having to handle all elements at once (in contrast to
540 * GNUNET_CONTAINER_multihashmap_iterate()). Note that the iterator can not be
541 * used anymore if elements have been removed from 'map' after the creation of
542 * the iterator, or 'map' has been destroyed. Adding elements to 'map' may
543 * result in skipped or repeated elements.
545 * @param map the map to create an iterator for
546 * @return an iterator over the given multihashmap 'map'
548 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
549 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map)
551 struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter;
553 iter = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32Iterator);
555 iter->modification_counter = map->modification_counter;
556 iter->me = map->map[0];
562 * Retrieve the next element from the hash map at the iterator's position.
563 * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
565 * This operation is only allowed if no elements have been removed from the
566 * multihashmap since the creation of 'iter', and the map has not been destroyed.
567 * Adding elements may result in repeating or skipping elements.
569 * @param iter the iterator to get the next element from
570 * @param key pointer to store the key in, can be NULL
571 * @param value pointer to store the value in, can be NULL
572 * @return #GNUNET_YES we returned an element,
573 * #GNUNET_NO if we are out of elements
576 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
580 /* make sure the map has not been modified */
581 GNUNET_assert (iter->modification_counter == iter->map->modification_counter);
583 /* look for the next entry, skipping empty buckets */
586 if (iter->idx >= iter->map->map_length)
588 if (NULL != iter->me)
591 *key = iter->me->key;
593 *value = iter->me->value;
594 iter->me = iter->me->next;
598 if (iter->idx < iter->map->map_length)
599 iter->me = iter->map->map[iter->idx];
605 * Destroy a multihashmap iterator.
607 * @param iter the iterator to destroy
610 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
616 /* end of container_multihashmap.c */