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 2, 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_multihashmap.c
22 * @brief hash map where the same key may be present multiple times
23 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
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_MultiHashMap
63 struct MapEntry **map;
66 * Number of entries in the map.
71 * Length of the "map" array.
73 unsigned int map_length;
78 * Create a multi hash map.
80 * @param len initial size (map will grow as needed)
81 * @return NULL on error
83 struct GNUNET_CONTAINER_MultiHashMap *
84 GNUNET_CONTAINER_multihashmap_create (unsigned int len)
86 struct GNUNET_CONTAINER_MultiHashMap *ret;
88 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
89 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
90 ret->map_length = len;
96 * Destroy a hash map. Will not free any values
97 * stored in the hash map!
102 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
108 for (i = 0; i < map->map_length; i++)
110 while (NULL != (e = map->map[i]))
112 map->map[i] = e->next;
116 GNUNET_free (map->map);
122 * Compute the index of the bucket for the given key.
124 * @param m hash map for which to compute the index
125 * @param key what key should the index be computed for
126 * @return offset into the "map" array of "m"
129 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
130 const GNUNET_HashCode * key)
132 return (*(unsigned int *) key) % m->map_length;
137 * Get the number of key-value pairs in the map.
140 * @return the number of key value pairs
143 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
151 * Given a key find a value in the map matching the key.
154 * @param key what to look for
155 * @return NULL if no value was found; note that
156 * this is indistinguishable from values that just
157 * happen to be NULL; use "contains" to test for
158 * key-value pairs with value NULL
161 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
162 *map, const GNUNET_HashCode * key)
166 e = map->map[idx_of (map, key)];
169 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
178 * Iterate over all entries in the map.
181 * @param it function to call on each entry
182 * @param it_cls extra argument to it
183 * @return the number of key value pairs processed,
184 * GNUNET_SYSERR if it aborted iteration
187 GNUNET_CONTAINER_multihashmap_iterate (const struct
188 GNUNET_CONTAINER_MultiHashMap *map,
189 GNUNET_CONTAINER_HashMapIterator it,
198 for (i = 0; i < map->map_length; i++)
201 while (NULL != (e = n))
204 if ((NULL != it) && (GNUNET_OK != it (it_cls, &e->key, e->value)))
205 return GNUNET_SYSERR;
214 * Remove the given key-value pair from the map. Note that if the
215 * key-value pair is in the map multiple times, only one of the pairs
219 * @param key key of the key-value pair
220 * @param value value of the key-value pair
221 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
225 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
226 *map, const GNUNET_HashCode * key,
233 i = idx_of (map, key);
238 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
242 map->map[i] = e->next;
257 * Remove all entries for the given key from the map.
258 * Note that the values would not be "freed".
261 * @param key identifies values to be removed
262 * @return number of values removed
265 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
266 *map, const GNUNET_HashCode * key)
274 i = idx_of (map, key);
279 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
282 map->map[i] = e->next;
304 * Check if the map contains any value under the given
305 * key (including values that are NULL).
308 * @param key the key to test if a value exists for it
309 * @return GNUNET_YES if such a value exists,
313 GNUNET_CONTAINER_multihashmap_contains (const struct
314 GNUNET_CONTAINER_MultiHashMap *map,
315 const GNUNET_HashCode * key)
319 e = map->map[idx_of (map, key)];
322 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
331 * Grow the given map to a more appropriate size.
333 * @param map the hash map to grow
336 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
338 struct MapEntry **old_map;
339 struct MapEntry **new_map;
341 unsigned int old_len;
342 unsigned int new_len;
347 old_len = map->map_length;
348 new_len = old_len * 2;
349 new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
350 map->map_length = new_len;
352 for (i = 0; i < old_len; i++)
354 while (NULL != (e = old_map[i]))
356 old_map[i] = e->next;
357 idx = idx_of (map, &e->key);
358 e->next = new_map[idx];
362 GNUNET_free (old_map);
367 * Store a key-value pair in the map.
370 * @param key key to use
371 * @param value value to use
372 * @param opt options for put
373 * @return GNUNET_OK on success,
374 * GNUNET_NO if a value was replaced (with REPLACE)
375 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
376 * value already exists
379 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
380 const GNUNET_HashCode * key,
382 enum GNUNET_CONTAINER_MultiHashMapOption
388 i = idx_of (map, key);
389 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
390 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
395 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
397 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
398 return GNUNET_SYSERR;
405 if (map->size / 3 >= map->map_length / 4)
408 i = idx_of (map, key);
410 e = GNUNET_malloc (sizeof (struct MapEntry));
413 e->next = map->map[i];
421 * Iterate over all entries in the map that match a particular key.
424 * @param key key that the entries must correspond to
425 * @param it function to call on each entry
426 * @param it_cls extra argument to it
427 * @return the number of key value pairs processed,
428 * GNUNET_SYSERR if it aborted iteration
431 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
432 GNUNET_CONTAINER_MultiHashMap
433 *map, const GNUNET_HashCode * key,
434 GNUNET_CONTAINER_HashMapIterator
442 n = map->map[idx_of (map, key)];
443 while (NULL != (e = n))
446 if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
448 if ((it != NULL) && (GNUNET_OK != it (it_cls, &e->key, e->value)))
449 return GNUNET_SYSERR;
456 /* end of container_multihashmap.c */