2 This file is part of GNUnet.
3 (C) 2008, 2012 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"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 * An entry in the hash map with the full key.
45 * If there is a hash collision, we create a linked list.
47 struct BigMapEntry *next;
52 struct GNUNET_HashCode key;
58 * An entry in the hash map with just a pointer to the key.
69 * If there is a hash collision, we create a linked list.
71 struct SmallMapEntry *next;
76 const struct GNUNET_HashCode *key;
87 * Variant used if map entries only contain a pointer to the key.
89 struct SmallMapEntry *sme;
92 * Variant used if map entries contain the full key.
94 struct BigMapEntry *bme;
99 * Internal representation of the hash map.
101 struct GNUNET_CONTAINER_MultiHashMap
105 * All of our buckets.
110 * Number of entries in the map.
115 * Length of the "map" array.
117 unsigned int map_length;
120 * GNUNET_NO if the map entries are of type 'struct BigMapEntry',
121 * GNUNET_YES if the map entries are of type 'struct SmallMapEntry'.
123 int use_small_entries;
128 * Create a multi hash map.
130 * @param len initial size (map will grow as needed)
131 * @param do_not_copy_keys GNUNET_NO is always safe and should be used by default;
132 * GNUNET_YES means that on 'put', the 'key' does not have
133 * to be copied as the destination of the pointer is
134 * guaranteed to be life as long as the value is stored in
135 * the hashmap. This can significantly reduce memory
136 * consumption, but of course is also a recipie for
137 * heap corruption if the assumption is not true. Only
138 * use this if (1) memory use is important in this case and
139 * (2) you have triple-checked that the invariant holds
140 * @return NULL on error
142 struct GNUNET_CONTAINER_MultiHashMap *
143 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
144 int do_not_copy_keys)
146 struct GNUNET_CONTAINER_MultiHashMap *map;
148 GNUNET_assert (len > 0);
149 map = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
150 map->map = GNUNET_malloc (len * sizeof (union MapEntry));
151 map->map_length = len;
152 map->use_small_entries = do_not_copy_keys;
158 * Destroy a hash map. Will not free any values
159 * stored in the hash map!
164 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
170 for (i = 0; i < map->map_length; i++)
173 if (map->use_small_entries)
175 struct SmallMapEntry *sme;
176 struct SmallMapEntry *nxt;
179 while (NULL != (sme = nxt))
188 struct BigMapEntry *bme;
189 struct BigMapEntry *nxt;
192 while (NULL != (bme = nxt))
200 GNUNET_free (map->map);
206 * Compute the index of the bucket for the given key.
208 * @param map hash map for which to compute the index
209 * @param key what key should the index be computed for
210 * @return offset into the "map" array of "map"
213 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *map,
214 const struct GNUNET_HashCode *key)
216 GNUNET_assert (map != NULL);
217 return (*(unsigned int *) key) % map->map_length;
222 * Get the number of key-value pairs in the map.
225 * @return the number of key value pairs
228 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
236 * Given a key find a value in the map matching the key.
239 * @param key what to look for
240 * @return NULL if no value was found; note that
241 * this is indistinguishable from values that just
242 * happen to be NULL; use "contains" to test for
243 * key-value pairs with value NULL
246 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
247 *map, const struct GNUNET_HashCode *key)
251 me = map->map[idx_of (map, key)];
252 if (map->use_small_entries)
254 struct SmallMapEntry *sme;
256 for (sme = me.sme; NULL != sme; sme = sme->next)
257 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
262 struct BigMapEntry *bme;
264 for (bme = me.bme; NULL != bme; bme = bme->next)
265 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
273 * Iterate over all entries in the map.
276 * @param it function to call on each entry
277 * @param it_cls extra argument to it
278 * @return the number of key value pairs processed,
279 * GNUNET_SYSERR if it aborted iteration
282 GNUNET_CONTAINER_multihashmap_iterate (const struct
283 GNUNET_CONTAINER_MultiHashMap *map,
284 GNUNET_CONTAINER_HashMapIterator it,
290 struct GNUNET_HashCode kc;
293 GNUNET_assert (NULL != map);
294 for (i = 0; i < map->map_length; i++)
297 if (map->use_small_entries)
299 struct SmallMapEntry *sme;
300 struct SmallMapEntry *nxt;
303 while (NULL != (sme = nxt))
308 if (GNUNET_OK != it (it_cls, sme->key, sme->value))
309 return GNUNET_SYSERR;
316 struct BigMapEntry *bme;
317 struct BigMapEntry *nxt;
320 while (NULL != (bme = nxt))
326 if (GNUNET_OK != it (it_cls, &kc, bme->value))
327 return GNUNET_SYSERR;
338 * Remove the given key-value pair from the map. Note that if the
339 * key-value pair is in the map multiple times, only one of the pairs
343 * @param key key of the key-value pair
344 * @param value value of the key-value pair
345 * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
349 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
350 const struct GNUNET_HashCode *key,
356 i = idx_of (map, key);
358 if (map->use_small_entries)
360 struct SmallMapEntry *sme;
361 struct SmallMapEntry *p;
364 for (sme = me.sme; NULL != sme; sme = sme->next)
366 if ((0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
367 (value == sme->value))
370 map->map[i].sme = sme->next;
382 struct BigMapEntry *bme;
383 struct BigMapEntry *p;
386 for (bme = me.bme; NULL != bme; bme = bme->next)
388 if ((0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
389 (value == bme->value))
392 map->map[i].bme = bme->next;
407 * Remove all entries for the given key from the map.
408 * Note that the values would not be "freed".
411 * @param key identifies values to be removed
412 * @return number of values removed
415 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
416 *map, const struct GNUNET_HashCode *key)
423 i = idx_of (map, key);
425 if (map->use_small_entries)
427 struct SmallMapEntry *sme;
428 struct SmallMapEntry *p;
434 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
437 map->map[i].sme = sme->next;
443 sme = map->map[i].sme;
457 struct BigMapEntry *bme;
458 struct BigMapEntry *p;
464 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
467 map->map[i].bme = bme->next;
473 bme = map->map[i].bme;
490 * Check if the map contains any value under the given
491 * key (including values that are NULL).
494 * @param key the key to test if a value exists for it
495 * @return GNUNET_YES if such a value exists,
499 GNUNET_CONTAINER_multihashmap_contains (const struct
500 GNUNET_CONTAINER_MultiHashMap *map,
501 const struct GNUNET_HashCode *key)
505 me = map->map[idx_of (map, key)];
506 if (map->use_small_entries)
508 struct SmallMapEntry *sme;
510 for (sme = me.sme; NULL != sme; sme = sme->next)
511 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
516 struct BigMapEntry *bme;
518 for (bme = me.bme; NULL != bme; bme = bme->next)
519 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
527 * Check if the map contains the given value under the given
531 * @param key the key to test if a value exists for it
532 * @param value value to test for
533 * @return GNUNET_YES if such a value exists,
537 GNUNET_CONTAINER_multihashmap_contains_value (const struct
538 GNUNET_CONTAINER_MultiHashMap
539 *map, const struct GNUNET_HashCode *key,
544 me = map->map[idx_of (map, key)];
545 if (map->use_small_entries)
547 struct SmallMapEntry *sme;
549 for (sme = me.sme; NULL != sme; sme = sme->next)
550 if ( (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode))) &&
551 (sme->value == value) )
556 struct BigMapEntry *bme;
558 for (bme = me.bme; NULL != bme; bme = bme->next)
559 if ( (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode))) &&
560 (bme->value == value) )
568 * Grow the given map to a more appropriate size.
570 * @param map the hash map to grow
573 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
575 union MapEntry *old_map;
576 union MapEntry *new_map;
577 unsigned int old_len;
578 unsigned int new_len;
583 old_len = map->map_length;
584 new_len = old_len * 2;
585 new_map = GNUNET_malloc (sizeof (union MapEntry) * new_len);
586 map->map_length = new_len;
588 for (i = 0; i < old_len; i++)
590 if (map->use_small_entries)
592 struct SmallMapEntry *sme;
594 while (NULL != (sme = old_map[i].sme))
596 old_map[i].sme = sme->next;
597 idx = idx_of (map, sme->key);
598 sme->next = new_map[idx].sme;
599 new_map[idx].sme = sme;
604 struct BigMapEntry *bme;
606 while (NULL != (bme = old_map[i].bme))
608 old_map[i].bme = bme->next;
609 idx = idx_of (map, &bme->key);
610 bme->next = new_map[idx].bme;
611 new_map[idx].bme = bme;
615 GNUNET_free (old_map);
620 * Store a key-value pair in the map.
623 * @param key key to use
624 * @param value value to use
625 * @param opt options for put
626 * @return GNUNET_OK on success,
627 * GNUNET_NO if a value was replaced (with REPLACE)
628 * GNUNET_SYSERR if UNIQUE_ONLY was the option and the
629 * value already exists
632 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
633 const struct GNUNET_HashCode *key,
635 enum GNUNET_CONTAINER_MultiHashMapOption opt)
640 i = idx_of (map, key);
641 if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
642 (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
645 if (map->use_small_entries)
647 struct SmallMapEntry *sme;
649 for (sme = me.sme; NULL != sme; sme = sme->next)
650 if (0 == memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
652 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
653 return GNUNET_SYSERR;
660 struct BigMapEntry *bme;
662 for (bme = me.bme; NULL != bme; bme = bme->next)
663 if (0 == memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
665 if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
666 return GNUNET_SYSERR;
672 if (map->size / 3 >= map->map_length / 4)
675 i = idx_of (map, key);
677 if (map->use_small_entries)
679 struct SmallMapEntry *sme;
681 sme = GNUNET_malloc (sizeof (struct SmallMapEntry));
684 sme->next = map->map[i].sme;
685 map->map[i].sme = sme;
689 struct BigMapEntry *bme;
691 bme = GNUNET_malloc (sizeof (struct BigMapEntry));
694 bme->next = map->map[i].bme;
695 map->map[i].bme = bme;
703 * Iterate over all entries in the map that match a particular key.
706 * @param key key that the entries must correspond to
707 * @param it function to call on each entry
708 * @param it_cls extra argument to it
709 * @return the number of key value pairs processed,
710 * GNUNET_SYSERR if it aborted iteration
713 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
714 GNUNET_CONTAINER_MultiHashMap *map,
715 const struct GNUNET_HashCode *key,
716 GNUNET_CONTAINER_HashMapIterator it,
723 me = map->map[idx_of (map, key)];
724 if (map->use_small_entries)
726 struct SmallMapEntry *sme;
727 struct SmallMapEntry *nxt;
730 while (NULL != (sme = nxt))
733 if (0 != memcmp (key, sme->key, sizeof (struct GNUNET_HashCode)))
735 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, sme->value)))
736 return GNUNET_SYSERR;
742 struct BigMapEntry *bme;
743 struct BigMapEntry *nxt;
746 while (NULL != (bme = nxt))
749 if (0 != memcmp (key, &bme->key, sizeof (struct GNUNET_HashCode)))
751 if ((it != NULL) && (GNUNET_OK != it (it_cls, key, bme->value)))
752 return GNUNET_SYSERR;
760 /* end of container_multihashmap.c */