2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_crypto_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 * An entry in the hash map.
52 * If there is a hash collision, we create a linked list.
54 struct MapEntry *next;
59 * Internal representation of the hash map.
61 struct GNUNET_CONTAINER_MultiHashMap32
67 struct MapEntry **map;
70 * Number of entries in the map.
75 * Length of the "map" array.
77 unsigned int map_length;
82 * Create a multi hash map.
84 * @param len initial size (map will grow as needed)
85 * @return NULL on error
87 struct GNUNET_CONTAINER_MultiHashMap32 *
88 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
90 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
92 GNUNET_assert (len > 0);
93 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap32));
94 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
95 ret->map_length = len;
101 * Destroy a hash map. Will not free any values
102 * stored in the hash map!
107 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
113 for (i = 0; i < map->map_length; i++)
115 while (NULL != (e = map->map[i]))
117 map->map[i] = e->next;
121 GNUNET_free (map->map);
127 * Compute the index of the bucket for the given key.
129 * @param m hash map for which to compute the index
130 * @param key what key should the index be computed for
131 * @return offset into the "map" array of "m"
134 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
137 GNUNET_assert (m != NULL);
138 return ((unsigned int) key) % m->map_length;
143 * Get the number of key-value pairs in the map.
146 * @return the number of key value pairs
149 GNUNET_CONTAINER_multihashmap32_size (const struct
150 GNUNET_CONTAINER_MultiHashMap32 *map)
157 * Given a key find a value in the map matching the key.
160 * @param key what to look for
161 * @return NULL if no value was found; note that
162 * this is indistinguishable from values that just
163 * happen to be NULL; use "contains" to test for
164 * key-value pairs with value NULL
167 GNUNET_CONTAINER_multihashmap32_get (const struct
168 GNUNET_CONTAINER_MultiHashMap32 *map,
173 e = map->map[idx_of (map, key)];
185 * Iterate over all entries in the map.
188 * @param it function to call on each entry
189 * @param it_cls extra argument to it
190 * @return the number of key value pairs processed,
191 * GNUNET_SYSERR if it aborted iteration
194 GNUNET_CONTAINER_multihashmap32_iterate (const struct
195 GNUNET_CONTAINER_MultiHashMap32 *map,
196 GNUNET_CONTAINER_HashMapIterator32 it,
205 GNUNET_assert (map != NULL);
206 for (i = 0; i < map->map_length; i++)
209 while (NULL != (e = n))
214 if (GNUNET_OK != it (it_cls, e->key, e->value))
215 return GNUNET_SYSERR;
225 * Remove the given key-value pair from the map. Note that if the
226 * key-value pair is in the map multiple times, only one of the pairs
230 * @param key key of the key-value pair
231 * @param value value of the key-value pair
232 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
236 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
238 uint32_t key, const void *value)
244 i = idx_of (map, key);
249 if ( (key == e->key) && (value == e->value) )
252 map->map[i] = e->next;
267 * Remove all entries for the given key from the map.
268 * Note that the values would not be "freed".
271 * @param key identifies values to be removed
272 * @return number of values removed
275 GNUNET_CONTAINER_multihashmap32_remove_all (struct
276 GNUNET_CONTAINER_MultiHashMap32
286 i = idx_of (map, key);
294 map->map[i] = e->next;
316 * Check if the map contains any value under the given
317 * key (including values that are NULL).
320 * @param key the key to test if a value exists for it
321 * @return GNUNET_YES if such a value exists,
325 GNUNET_CONTAINER_multihashmap32_contains (const struct
326 GNUNET_CONTAINER_MultiHashMap32 *map,
331 e = map->map[idx_of (map, key)];
343 * Check if the map contains the given value under the given
347 * @param key the key to test if a value exists for it
348 * @param value value to test for
349 * @return GNUNET_YES if such a value exists,
353 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
354 GNUNET_CONTAINER_MultiHashMap32
361 e = map->map[idx_of (map, key)];
364 if ( (key == e->key) && (e->value == value) )
373 * Grow the given map to a more appropriate size.
375 * @param map the hash map to grow
378 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
380 struct MapEntry **old_map;
381 struct MapEntry **new_map;
383 unsigned int old_len;
384 unsigned int new_len;
389 old_len = map->map_length;
390 new_len = old_len * 2;
391 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
392 map->map_length = new_len;
394 for (i = 0; i < old_len; i++)
396 while (NULL != (e = old_map[i]))
398 old_map[i] = e->next;
399 idx = idx_of (map, e->key);
400 e->next = new_map[idx];
404 GNUNET_free (old_map);
409 * Store a key-value pair in the map.
412 * @param key key to use
413 * @param value value to use
414 * @param opt options for put
415 * @return GNUNET_OK on success,
416 * GNUNET_NO if a value was replaced (with REPLACE)
417 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
418 * value already exists
421 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
422 *map, uint32_t key, void *value,
423 enum GNUNET_CONTAINER_MultiHashMapOption
429 i = idx_of (map, key);
430 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
431 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
438 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
439 return GNUNET_SYSERR;
446 if (map->size / 3 >= map->map_length / 4)
449 i = idx_of (map, key);
451 e = GNUNET_malloc (sizeof (struct MapEntry));
454 e->next = map->map[i];
462 * Iterate over all entries in the map that match a particular key.
465 * @param key key that the entries must correspond to
466 * @param it function to call on each entry
467 * @param it_cls extra argument to it
468 * @return the number of key value pairs processed,
469 * GNUNET_SYSERR if it aborted iteration
472 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
473 GNUNET_CONTAINER_MultiHashMap32
475 GNUNET_CONTAINER_HashMapIterator32
483 n = map->map[idx_of (map, key)];
484 while (NULL != (e = n))
489 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
490 return GNUNET_SYSERR;
497 /* end of container_multihashmap.c */