MinGW
[oweals/gnunet.git] / src / util / container_multihashmap.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file util/container_multihashmap.c
22  * @brief hash map where the same key may be present multiple times
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
30
31 /**
32  * An entry in the hash map.
33  */
34 struct MapEntry
35 {
36
37   /**
38    * Key for the entry.
39    */
40   GNUNET_HashCode key;
41
42   /**
43    * Value of the entry.
44    */
45   void *value;
46
47   /**
48    * If there is a hash collision, we create a linked list.
49    */
50   struct MapEntry *next;
51
52 };
53
54 /**
55  * Internal representation of the hash map.
56  */
57 struct GNUNET_CONTAINER_MultiHashMap
58 {
59
60   /**
61    * All of our buckets.
62    */
63   struct MapEntry **map;
64
65   /**
66    * Number of entries in the map.
67    */
68   unsigned int size;
69
70   /**
71    * Length of the "map" array.
72    */
73   unsigned int map_length;
74 };
75
76
77 /**
78  * Create a multi hash map.
79  *
80  * @param len initial size (map will grow as needed)
81  * @return NULL on error
82  */
83 struct GNUNET_CONTAINER_MultiHashMap *
84 GNUNET_CONTAINER_multihashmap_create (unsigned int len)
85 {
86   struct GNUNET_CONTAINER_MultiHashMap *ret;
87
88   GNUNET_assert (len > 0);
89   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
90   ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
91   ret->map_length = len;
92   return ret;
93 }
94
95
96 /**
97  * Destroy a hash map.  Will not free any values
98  * stored in the hash map!
99  *
100  * @param map the map
101  */
102 void
103 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
104                                        *map)
105 {
106   unsigned int i;
107   struct MapEntry *e;
108
109   for (i = 0; i < map->map_length; i++)
110     {
111       while (NULL != (e = map->map[i]))
112         {
113           map->map[i] = e->next;
114           GNUNET_free (e);
115         }
116     }
117   GNUNET_free (map->map);
118   GNUNET_free (map);
119 }
120
121
122 /**
123  * Compute the index of the bucket for the given key.
124  *
125  * @param m hash map for which to compute the index
126  * @param key what key should the index be computed for
127  * @return offset into the "map" array of "m"
128  */
129 static unsigned int
130 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
131         const GNUNET_HashCode * key)
132 {
133   return (*(unsigned int *) key) % m->map_length;
134 }
135
136
137 /**
138  * Get the number of key-value pairs in the map.
139  *
140  * @param map the map
141  * @return the number of key value pairs
142  */
143 unsigned int
144 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
145                                     *map)
146 {
147   return map->size;
148 }
149
150
151 /**
152  * Given a key find a value in the map matching the key.
153  *
154  * @param map the map
155  * @param key what to look for
156  * @return NULL if no value was found; note that
157  *   this is indistinguishable from values that just
158  *   happen to be NULL; use "contains" to test for
159  *   key-value pairs with value NULL
160  */
161 void *
162 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
163                                    *map, const GNUNET_HashCode * key)
164 {
165   struct MapEntry *e;
166
167   e = map->map[idx_of (map, key)];
168   while (e != NULL)
169     {
170       if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
171         return e->value;
172       e = e->next;
173     }
174   return NULL;
175 }
176
177
178 /**
179  * Iterate over all entries in the map.
180  *
181  * @param map the map
182  * @param it function to call on each entry
183  * @param it_cls extra argument to it
184  * @return the number of key value pairs processed,
185  *         GNUNET_SYSERR if it aborted iteration
186  */
187 int
188 GNUNET_CONTAINER_multihashmap_iterate (const struct
189                                        GNUNET_CONTAINER_MultiHashMap *map,
190                                        GNUNET_CONTAINER_HashMapIterator it,
191                                        void *it_cls)
192 {
193   int count;
194   unsigned int i;
195   struct MapEntry *e;
196   struct MapEntry *n;
197   GNUNET_HashCode kc;
198
199   count = 0;
200   for (i = 0; i < map->map_length; i++)
201     {
202       n = map->map[i];
203       while (NULL != (e = n))
204         {
205           n = e->next;
206           if (NULL != it)
207             {
208               kc = e->key;
209               if (GNUNET_OK != it (it_cls, &kc, e->value))
210                 return GNUNET_SYSERR;
211             }
212           count++;
213         }
214     }
215   return count;
216 }
217
218
219 /**
220  * Remove the given key-value pair from the map.  Note that if the
221  * key-value pair is in the map multiple times, only one of the pairs
222  * will be removed.
223  *
224  * @param map the map
225  * @param key key of the key-value pair
226  * @param value value of the key-value pair
227  * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
228  *  is not in the map
229  */
230 int
231 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
232                                       *map, const GNUNET_HashCode * key,
233                                       void *value)
234 {
235   struct MapEntry *e;
236   struct MapEntry *p;
237   unsigned int i;
238
239   i = idx_of (map, key);
240   p = NULL;
241   e = map->map[i];
242   while (e != NULL)
243     {
244       if ((0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
245           (value == e->value))
246         {
247           if (p == NULL)
248             map->map[i] = e->next;
249           else
250             p->next = e->next;
251           GNUNET_free (e);
252           map->size--;
253           return GNUNET_YES;
254         }
255       p = e;
256       e = e->next;
257     }
258   return GNUNET_NO;
259 }
260
261
262 /**
263  * Remove all entries for the given key from the map.
264  * Note that the values would not be "freed".
265  *
266  * @param map the map
267  * @param key identifies values to be removed
268  * @return number of values removed
269  */
270 int
271 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
272                                           *map, const GNUNET_HashCode * key)
273 {
274   struct MapEntry *e;
275   struct MapEntry *p;
276   unsigned int i;
277   int ret;
278
279   ret = 0;
280   i = idx_of (map, key);
281   p = NULL;
282   e = map->map[i];
283   while (e != NULL)
284     {
285       if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
286         {
287           if (p == NULL)
288             map->map[i] = e->next;
289           else
290             p->next = e->next;
291           GNUNET_free (e);
292           map->size--;
293           if (p == NULL)
294             e = map->map[i];
295           else
296             e = p->next;
297           ret++;
298         }
299       else
300         {
301           p = e;
302           e = e->next;
303         }
304     }
305   return ret;
306 }
307
308
309 /**
310  * Check if the map contains any value under the given
311  * key (including values that are NULL).
312  *
313  * @param map the map
314  * @param key the key to test if a value exists for it
315  * @return GNUNET_YES if such a value exists,
316  *         GNUNET_NO if not
317  */
318 int
319 GNUNET_CONTAINER_multihashmap_contains (const struct
320                                         GNUNET_CONTAINER_MultiHashMap *map,
321                                         const GNUNET_HashCode * key)
322 {
323   struct MapEntry *e;
324
325   e = map->map[idx_of (map, key)];
326   while (e != NULL)
327     {
328       if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
329         return GNUNET_YES;
330       e = e->next;
331     }
332   return GNUNET_NO;
333 }
334
335
336 /**
337  * Grow the given map to a more appropriate size.
338  *
339  * @param map the hash map to grow
340  */
341 static void
342 grow (struct GNUNET_CONTAINER_MultiHashMap *map)
343 {
344   struct MapEntry **old_map;
345   struct MapEntry **new_map;
346   struct MapEntry *e;
347   unsigned int old_len;
348   unsigned int new_len;
349   unsigned int idx;
350   unsigned int i;
351
352   old_map = map->map;
353   old_len = map->map_length;
354   new_len = old_len * 2;
355   new_map = GNUNET_malloc (sizeof (struct MapEntry *) * new_len);
356   map->map_length = new_len;
357   map->map = new_map;
358   for (i = 0; i < old_len; i++)
359     {
360       while (NULL != (e = old_map[i]))
361         {
362           old_map[i] = e->next;
363           idx = idx_of (map, &e->key);
364           e->next = new_map[idx];
365           new_map[idx] = e;
366         }
367     }
368   GNUNET_free (old_map);
369 }
370
371
372 /**
373  * Store a key-value pair in the map.
374  *
375  * @param map the map
376  * @param key key to use
377  * @param value value to use
378  * @param opt options for put
379  * @return GNUNET_OK on success,
380  *         GNUNET_NO if a value was replaced (with REPLACE)
381  *         GNUNET_SYSERR if UNIQUE_ONLY was the option and the
382  *                       value already exists
383  */
384 int
385 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
386                                    const GNUNET_HashCode * key,
387                                    void *value,
388                                    enum GNUNET_CONTAINER_MultiHashMapOption
389                                    opt)
390 {
391   struct MapEntry *e;
392   unsigned int i;
393
394   i = idx_of (map, key);
395   if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
396       (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
397     {
398       e = map->map[i];
399       while (e != NULL)
400         {
401           if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
402             {
403               if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
404                 return GNUNET_SYSERR;
405               e->value = value;
406               return GNUNET_NO;
407             }
408           e = e->next;
409         }
410     }
411   if (map->size / 3 >= map->map_length / 4)
412     {
413       grow (map);
414       i = idx_of (map, key);
415     }
416   e = GNUNET_malloc (sizeof (struct MapEntry));
417   e->key = *key;
418   e->value = value;
419   e->next = map->map[i];
420   map->map[i] = e;
421   map->size++;
422   return GNUNET_OK;
423 }
424
425
426 /**
427  * Iterate over all entries in the map that match a particular key.
428  *
429  * @param map the map
430  * @param key key that the entries must correspond to
431  * @param it function to call on each entry
432  * @param it_cls extra argument to it
433  * @return the number of key value pairs processed,
434  *         GNUNET_SYSERR if it aborted iteration
435  */
436 int
437 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
438                                             GNUNET_CONTAINER_MultiHashMap
439                                             *map, const GNUNET_HashCode * key,
440                                             GNUNET_CONTAINER_HashMapIterator
441                                             it, void *it_cls)
442 {
443   int count;
444   struct MapEntry *e;
445   struct MapEntry *n;
446
447   count = 0;
448   n = map->map[idx_of (map, key)];
449   while (NULL != (e = n))
450     {
451       n = e->next;
452       if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
453         continue;
454       if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
455         return GNUNET_SYSERR;
456       count++;
457     }
458   return count;
459 }
460
461
462 /* end of container_multihashmap.c */