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"
38 struct MapEntry *next;
44 struct GNUNET_CONTAINER_MultiHashMap
47 struct MapEntry **map;
51 unsigned int map_length;
55 struct GNUNET_CONTAINER_MultiHashMap *
56 GNUNET_CONTAINER_multihashmap_create (unsigned int len)
58 struct GNUNET_CONTAINER_MultiHashMap *ret;
60 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
62 ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
63 memset (ret->map, 0, len * sizeof (struct MapEntry *));
64 ret->map_length = len;
70 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
76 for (i = 0; i < map->map_length; i++)
78 while (NULL != (e = map->map[i]))
80 map->map[i] = e->next;
84 GNUNET_free (map->map);
89 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
90 const GNUNET_HashCode * key)
92 return (*(unsigned int *) key) % m->map_length;
97 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
105 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
106 *map, const GNUNET_HashCode * key)
110 e = map->map[idx_of (map, key)];
113 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
122 GNUNET_CONTAINER_multihashmap_iterate (const struct
123 GNUNET_CONTAINER_MultiHashMap *map,
124 GNUNET_CONTAINER_HashMapIterator it,
132 for (i = 0; i < map->map_length; i++)
137 if ((NULL != it) && (GNUNET_OK != it (cls, &e->key, e->value)))
138 return GNUNET_SYSERR;
148 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
149 *map, const GNUNET_HashCode * key,
156 i = idx_of (map, key);
161 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
165 map->map[i] = e->next;
180 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
181 *map, const GNUNET_HashCode * key)
189 i = idx_of (map, key);
194 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
197 map->map[i] = e->next;
219 GNUNET_CONTAINER_multihashmap_contains (const struct
220 GNUNET_CONTAINER_MultiHashMap *map,
221 const GNUNET_HashCode * key)
226 i = idx_of (map, key);
230 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
239 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
241 struct MapEntry **old;
248 map->map_length *= 2;
249 map->map = GNUNET_malloc (sizeof (struct MapEntry *) * map->map_length);
250 memset (map->map, 0, sizeof (struct MapEntry *) * map->map_length);
251 for (i = 0; i < l; i++)
253 while (NULL != (e = old[i]))
256 e->next = map->map[idx_of (map, &e->key)];
257 map->map[idx_of (map, &e->key)] = e;
265 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
266 const GNUNET_HashCode * key,
268 enum GNUNET_CONTAINER_MultiHashMapOption
274 i = idx_of (map, key);
275 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
276 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
281 if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
284 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
285 return GNUNET_SYSERR;
292 if (map->size / 3 > map->map_length / 4)
294 e = GNUNET_malloc (sizeof (struct MapEntry));
297 e->next = map->map[i];
305 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
306 GNUNET_CONTAINER_MultiHashMap
307 *map, const GNUNET_HashCode * key,
308 GNUNET_CONTAINER_HashMapIterator
315 e = map->map[idx_of (map, key)];
318 if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
320 if ((it != NULL) && (GNUNET_OK != it (&e->key, e->value, cls)))
321 return GNUNET_SYSERR;
331 GNUNET_CONTAINER_multihashmap_get_random (const struct
332 GNUNET_CONTAINER_MultiHashMap *map)
344 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
352 /* end of container_multihashmap.c */