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_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __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;
80 * Create a multi hash map.
82 * @param len initial size (map will grow as needed)
83 * @return NULL on error
85 struct GNUNET_CONTAINER_MultiHashMap32 *
86 GNUNET_CONTAINER_multihashmap32_create (unsigned int len)
88 struct GNUNET_CONTAINER_MultiHashMap32 *ret;
90 GNUNET_assert (len > 0);
91 ret = GNUNET_new (struct GNUNET_CONTAINER_MultiHashMap32);
92 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
93 ret->map_length = len;
99 * Destroy a hash map. Will not free any values
100 * stored in the hash map!
105 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
111 for (i = 0; i < map->map_length; i++)
113 while (NULL != (e = map->map[i]))
115 map->map[i] = e->next;
119 GNUNET_free (map->map);
125 * Compute the index of the bucket for the given key.
127 * @param m hash map for which to compute the index
128 * @param key what key should the index be computed for
129 * @return offset into the "map" array of "m"
132 idx_of (const struct GNUNET_CONTAINER_MultiHashMap32 *m,
135 GNUNET_assert (m != NULL);
136 return ((unsigned int) key) % m->map_length;
141 * Get the number of key-value pairs in the map.
144 * @return the number of key value pairs
147 GNUNET_CONTAINER_multihashmap32_size (const struct
148 GNUNET_CONTAINER_MultiHashMap32 *map)
155 * Given a key find a value in the map matching the key.
158 * @param key what to look for
159 * @return NULL if no value was found; note that
160 * this is indistinguishable from values that just
161 * happen to be NULL; use "contains" to test for
162 * key-value pairs with value NULL
165 GNUNET_CONTAINER_multihashmap32_get (const struct
166 GNUNET_CONTAINER_MultiHashMap32 *map,
171 e = map->map[idx_of (map, key)];
183 * Iterate over all entries in the map.
186 * @param it function to call on each entry
187 * @param it_cls extra argument to it
188 * @return the number of key value pairs processed,
189 * GNUNET_SYSERR if it aborted iteration
192 GNUNET_CONTAINER_multihashmap32_iterate (const struct
193 GNUNET_CONTAINER_MultiHashMap32 *map,
194 GNUNET_CONTAINER_HashMapIterator32 it,
203 GNUNET_assert (map != NULL);
204 for (i = 0; i < map->map_length; i++)
207 while (NULL != (e = n))
212 if (GNUNET_OK != it (it_cls, e->key, e->value))
213 return GNUNET_SYSERR;
223 * Remove the given key-value pair from the map. Note that if the
224 * key-value pair is in the map multiple times, only one of the pairs
228 * @param key key of the key-value pair
229 * @param value value of the key-value pair
230 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
234 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32
236 uint32_t key, const void *value)
242 i = idx_of (map, key);
247 if ( (key == e->key) && (value == e->value) )
250 map->map[i] = e->next;
265 * Remove all entries for the given key from the map.
266 * Note that the values would not be "freed".
269 * @param key identifies values to be removed
270 * @return number of values removed
273 GNUNET_CONTAINER_multihashmap32_remove_all (struct
274 GNUNET_CONTAINER_MultiHashMap32
284 i = idx_of (map, key);
292 map->map[i] = e->next;
314 * Check if the map contains any value under the given
315 * key (including values that are NULL).
318 * @param key the key to test if a value exists for it
319 * @return GNUNET_YES if such a value exists,
323 GNUNET_CONTAINER_multihashmap32_contains (const struct
324 GNUNET_CONTAINER_MultiHashMap32 *map,
329 e = map->map[idx_of (map, key)];
341 * Check if the map contains the given value under the given
345 * @param key the key to test if a value exists for it
346 * @param value value to test for
347 * @return GNUNET_YES if such a value exists,
351 GNUNET_CONTAINER_multihashmap32_contains_value (const struct
352 GNUNET_CONTAINER_MultiHashMap32
359 e = map->map[idx_of (map, key)];
362 if ( (key == e->key) && (e->value == value) )
371 * Grow the given map to a more appropriate size.
373 * @param map the hash map to grow
376 grow (struct GNUNET_CONTAINER_MultiHashMap32 *map)
378 struct MapEntry **old_map;
379 struct MapEntry **new_map;
381 unsigned int old_len;
382 unsigned int new_len;
387 old_len = map->map_length;
388 new_len = old_len * 2;
389 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
390 map->map_length = new_len;
392 for (i = 0; i < old_len; i++)
394 while (NULL != (e = old_map[i]))
396 old_map[i] = e->next;
397 idx = idx_of (map, e->key);
398 e->next = new_map[idx];
402 GNUNET_free (old_map);
407 * Store a key-value pair in the map.
410 * @param key key to use
411 * @param value value to use
412 * @param opt options for put
413 * @return GNUNET_OK on success,
414 * GNUNET_NO if a value was replaced (with REPLACE)
415 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
416 * value already exists
419 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32
420 *map, uint32_t key, void *value,
421 enum GNUNET_CONTAINER_MultiHashMapOption
427 i = idx_of (map, key);
428 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
429 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
436 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
437 return GNUNET_SYSERR;
444 if (map->size / 3 >= map->map_length / 4)
447 i = idx_of (map, key);
449 e = GNUNET_new (struct MapEntry);
452 e->next = map->map[i];
460 * Iterate over all entries in the map that match a particular key.
463 * @param key key that the entries must correspond to
464 * @param it function to call on each entry
465 * @param it_cls extra argument to it
466 * @return the number of key value pairs processed,
467 * GNUNET_SYSERR if it aborted iteration
470 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct
471 GNUNET_CONTAINER_MultiHashMap32
473 GNUNET_CONTAINER_HashMapIterator32
481 n = map->map[idx_of (map, key)];
482 while (NULL != (e = n))
487 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
488 return GNUNET_SYSERR;
495 /* end of container_multihashmap.c */