-more datacache integration work
[oweals/gnunet.git] / src / include / gnunet_container_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2015 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 3, 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 /**
22  * @file include/gnunet_container_lib.h
23  * @brief container classes for GNUnet
24  * @author Christian Grothoff
25  * @author Nils Durner
26  * @defgroup hashmap multi hash-map
27  * @defgroup heap min- or max-heap with arbitrary element removal
28  * @defgroup bloomfilter Bloom filter (probabilistic set tests)
29  * @defgroup dll Doubly-linked list
30  * @defgroup metadata Meta data (GNU libextractor key-value pairs)
31  */
32
33 #ifndef GNUNET_CONTAINER_LIB_H
34 #define GNUNET_CONTAINER_LIB_H
35
36 /* add error and config prototypes */
37 #include "gnunet_crypto_lib.h"
38 #include <extractor.h>
39
40 #ifndef EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
41 /* hack for LE < 0.6.3 */
42 #define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME 180
43 #endif
44
45 #ifdef __cplusplus
46 extern "C"
47 {
48 #if 0                           /* keep Emacsens' auto-indent happy */
49 }
50 #endif
51 #endif
52
53
54 /* ******************* bloomfilter ***************** */
55
56 /**
57  * @brief bloomfilter representation (opaque)
58  * @ingroup bloomfilter
59  */
60 struct GNUNET_CONTAINER_BloomFilter;
61
62
63 /**
64  * @ingroup bloomfilter
65  * Iterator over `struct GNUNET_HashCode`.
66  *
67  * @param cls closure
68  * @param next set to the next hash code
69  * @return #GNUNET_YES if next was updated
70  *         #GNUNET_NO if there are no more entries
71  */
72 typedef int
73 (*GNUNET_CONTAINER_HashCodeIterator) (void *cls,
74                                       struct GNUNET_HashCode *next);
75
76
77 /**
78  * @ingroup bloomfilter
79  * Load a Bloom filter from a file.
80  *
81  * @param filename the name of the file (or the prefix)
82  * @param size the size of the bloom-filter (number of
83  *        bytes of storage space to use); will be rounded up
84  *        to next power of 2
85  * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
86  *        element (number of bits set per element in the set)
87  * @return the bloomfilter
88  */
89 struct GNUNET_CONTAINER_BloomFilter *
90 GNUNET_CONTAINER_bloomfilter_load (const char *filename,
91                                    size_t size,
92                                    unsigned int k);
93
94
95 /**
96  * @ingroup bloomfilter
97  * Create a Bloom filter from raw bits.
98  *
99  * @param data the raw bits in memory (maybe NULL,
100  *        in which case all bits should be considered
101  *        to be zero).
102  * @param size the size of the bloom-filter (number of
103  *        bytes of storage space to use); also size of @a data
104  *        -- unless data is NULL.  Must be a power of 2.
105  * @param k the number of #GNUNET_CRYPTO_hash-functions to apply per
106  *        element (number of bits set per element in the set)
107  * @return the bloomfilter
108  */
109 struct GNUNET_CONTAINER_BloomFilter *
110 GNUNET_CONTAINER_bloomfilter_init (const char *data,
111                                    size_t size,
112                                    unsigned int k);
113
114
115 /**
116  * @ingroup bloomfilter
117  * Copy the raw data of this Bloom filter into
118  * the given data array.
119  *
120  * @param data where to write the data
121  * @param size the size of the given @a data array
122  * @return #GNUNET_SYSERR if the data array of the wrong size
123  */
124 int
125 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct GNUNET_CONTAINER_BloomFilter *bf,
126                                            char *data,
127                                            size_t size);
128
129
130 /**
131  * @ingroup bloomfilter
132  * Test if an element is in the filter.
133  *
134  * @param e the element
135  * @param bf the filter
136  * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
137  */
138 int
139 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
140                                    const struct GNUNET_HashCode *e);
141
142
143 /**
144  * @ingroup bloomfilter
145  * Add an element to the filter.
146  *
147  * @param bf the filter
148  * @param e the element
149  */
150 void
151 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
152                                   const struct GNUNET_HashCode *e);
153
154
155 /**
156  * @ingroup bloomfilter
157  * Remove an element from the filter.
158  *
159  * @param bf the filter
160  * @param e the element to remove
161  */
162 void
163 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
164                                      const struct GNUNET_HashCode *e);
165
166
167 /**
168  * @ingroup bloomfilter
169  * Create a copy of a bloomfilter.
170  *
171  * @param bf the filter
172  * @return copy of bf
173  */
174 struct GNUNET_CONTAINER_BloomFilter *
175 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter *bf);
176
177
178
179 /**
180  * @ingroup bloomfilter
181  * Free the space associcated with a filter
182  * in memory, flush to drive if needed (do not
183  * free the space on the drive).
184  *
185  * @param bf the filter
186  */
187 void
188 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf);
189
190
191 /**
192  * Get the number of the addresses set per element in the bloom filter.
193  *
194  * @param bf the filter
195  * @return addresses set per element in the bf
196  */
197 size_t
198 GNUNET_CONTAINER_bloomfilter_get_element_addresses (const struct GNUNET_CONTAINER_BloomFilter *bf);
199
200
201 /**
202  * @ingroup bloomfilter
203  * Get size of the bloom filter.
204  *
205  * @param bf the filter
206  * @return number of bytes used for the data of the bloom filter
207  */
208 size_t
209 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter *bf);
210
211
212 /**
213  * @ingroup bloomfilter
214  * Reset a Bloom filter to empty.
215  *
216  * @param bf the filter
217  */
218 void
219 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf);
220
221
222 /**
223  * @ingroup bloomfilter
224  * "or" the entries of the given raw data array with the
225  * data of the given Bloom filter.  Assumes that
226  * the @a size of the @a data array and the current filter
227  * match.
228  *
229  * @param bf the filter
230  * @param data data to OR-in
231  * @param size size of @a data
232  * @return #GNUNET_OK on success
233  */
234 int
235 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
236                                  const char *data, size_t size);
237
238
239 /**
240  * @ingroup bloomfilter
241  * "or" the entries of the given raw data array with the
242  * data of the given Bloom filter.  Assumes that
243  * the size of the two filters matches.
244  *
245  * @param bf the filter
246  * @param to_or the bloomfilter to or-in
247  * @return #GNUNET_OK on success
248  */
249 int
250 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
251                                   const struct GNUNET_CONTAINER_BloomFilter *to_or);
252
253
254 /**
255  * @ingroup bloomfilter
256  * Resize a bloom filter.  Note that this operation
257  * is pretty costly.  Essentially, the Bloom filter
258  * needs to be completely re-build.
259  *
260  * @param bf the filter
261  * @param iterator an iterator over all elements stored in the BF
262  * @param iterator_cls closure for @a iterator
263  * @param size the new size for the filter
264  * @param k the new number of #GNUNET_CRYPTO_hash-function to apply per element
265  */
266 void
267 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
268                                      GNUNET_CONTAINER_HashCodeIterator iterator,
269                                      void *iterator_cls,
270                                      size_t size,
271                                      unsigned int k);
272
273
274 /* ****************** metadata ******************* */
275
276 /**
277  * @ingroup metadata
278  * Meta data to associate with a file, directory or namespace.
279  */
280 struct GNUNET_CONTAINER_MetaData;
281
282
283 /**
284  * @ingroup metadata
285  * Create a fresh meta data container.
286  *
287  * @return empty meta-data container
288  */
289 struct GNUNET_CONTAINER_MetaData *
290 GNUNET_CONTAINER_meta_data_create (void);
291
292
293 /**
294  * @ingroup metadata
295  * Duplicate a MetaData token.
296  *
297  * @param md what to duplicate
298  * @return duplicate meta-data container
299  */
300 struct GNUNET_CONTAINER_MetaData *
301 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData *md);
302
303
304 /**
305  * @ingroup metadata
306  * Free meta data.
307  *
308  * @param md what to free
309  */
310 void
311 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md);
312
313
314 /**
315  * @ingroup metadata
316  * Test if two MDs are equal. We consider them equal if
317  * the meta types, formats and content match (we do not
318  * include the mime types and plugins names in this
319  * consideration).
320  *
321  * @param md1 first value to check
322  * @param md2 other value to check
323  * @return #GNUNET_YES if they are equal
324  */
325 int
326 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData *md1,
327                                        const struct GNUNET_CONTAINER_MetaData *md2);
328
329
330 /**
331  * @ingroup metadata
332  * Extend metadata.
333  *
334  * @param md metadata to extend
335  * @param plugin_name name of the plugin that produced this value;
336  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
337  *        used in the main libextractor library and yielding
338  *        meta data).
339  * @param type libextractor-type describing the meta data
340  * @param format basic format information about data
341  * @param data_mime_type mime-type of data (not of the original file);
342  *        can be NULL (if mime-type is not known)
343  * @param data actual meta-data found
344  * @param data_size number of bytes in data
345  * @return #GNUNET_OK on success, #GNUNET_SYSERR if this entry already exists
346  *         data_mime_type and plugin_name are not considered for "exists" checks
347  */
348 int
349 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
350                                    const char *plugin_name,
351                                    enum EXTRACTOR_MetaType type,
352                                    enum EXTRACTOR_MetaFormat format,
353                                    const char *data_mime_type,
354                                    const char *data,
355                                    size_t data_size);
356
357
358 /**
359  * @ingroup metadata
360  * Extend metadata.  Merges the meta data from the second argument
361  * into the first, discarding duplicate key-value pairs.
362  *
363  * @param md metadata to extend
364  * @param in metadata to merge
365  */
366 void
367 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
368                                   const struct GNUNET_CONTAINER_MetaData *in);
369
370
371 /**
372  * @ingroup metadata
373  * Remove an item.
374  *
375  * @param md metadata to manipulate
376  * @param type type of the item to remove
377  * @param data specific value to remove, NULL to remove all
378  *        entries of the given type
379  * @param data_size number of bytes in data
380  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md
381  */
382 int
383 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
384                                    enum EXTRACTOR_MetaType type,
385                                    const char *data,
386                                    size_t data_size);
387
388
389 /**
390  * @ingroup metadata
391  * Remove all items in the container.
392  *
393  * @param md metadata to manipulate
394  */
395 void
396 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md);
397
398
399 /**
400  * @ingroup metadata
401  * Add the current time as the publication date
402  * to the meta-data.
403  *
404  * @param md metadata to modify
405  */
406 void
407 GNUNET_CONTAINER_meta_data_add_publication_date (struct GNUNET_CONTAINER_MetaData *md);
408
409
410 /**
411  * @ingroup metadata
412  * Iterate over MD entries.
413  *
414  * @param md metadata to inspect
415  * @param iter function to call on each entry, return 0 to continue to iterate
416  *             and 1 to abort iteration in this function (GNU libextractor API!)
417  * @param iter_cls closure for @a iter
418  * @return number of entries
419  */
420 int
421 GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
422                                     EXTRACTOR_MetaDataProcessor iter,
423                                     void *iter_cls);
424
425
426 /**
427  * @ingroup metadata
428  * Get the first MD entry of the given type.  Caller
429  * is responsible for freeing the return value.
430  * Also, only meta data items that are strings (0-terminated)
431  * are returned by this function.
432  *
433  * @param md metadata to inspect
434  * @param type type to look for
435  * @return NULL if no entry was found
436  */
437 char *
438 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData *md,
439                                         enum EXTRACTOR_MetaType type);
440
441
442 /**
443  * @ingroup metadata
444  * Get the first matching MD entry of the given types. Caller is
445  * responsible for freeing the return value.  Also, only meta data
446  * items that are strings (0-terminated) are returned by this
447  * function.
448  *
449  * @param md metadata to inspect
450  * @param ... -1-terminated list of types
451  * @return NULL if we do not have any such entry,
452  *  otherwise client is responsible for freeing the value!
453  */
454 char *
455 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct GNUNET_CONTAINER_MetaData *md,
456                                                ...);
457
458 /**
459  * @ingroup metadata
460  * Get a thumbnail from the meta-data (if present).  Only matches meta
461  * data with mime type "image" and binary format.
462  *
463  * @param md metadata to inspect
464  * @param thumb will be set to the thumbnail data.  Must be
465  *        freed by the caller!
466  * @return number of bytes in thumbnail, 0 if not available
467  */
468 size_t
469 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData *md,
470                                           unsigned char **thumb);
471
472
473
474 /**
475  * @ingroup metadata
476  * Options for metadata serialization.
477  */
478 enum GNUNET_CONTAINER_MetaDataSerializationOptions
479 {
480   /**
481    * @ingroup metadata
482    * Serialize all of the data.
483    */
484   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
485
486   /**
487    * @ingroup metadata
488    * If not enough space is available, it is acceptable
489    * to only serialize some of the metadata.
490    */
491   GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
492
493   /**
494    * @ingroup metadata
495    * Speed is of the essence, do not allow compression.
496    */
497   GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
498 };
499
500
501 /**
502  * @ingroup metadata
503  * Serialize meta-data to target.
504  *
505  * @param md metadata to serialize
506  * @param target where to write the serialized metadata;
507  *         *target can be NULL, in which case memory is allocated
508  * @param max maximum number of bytes available
509  * @param opt is it ok to just write SOME of the
510  *        meta-data to match the size constraint,
511  *        possibly discarding some data?
512  * @return number of bytes written on success,
513  *         -1 on error (typically: not enough
514  *         space)
515  */
516 ssize_t
517 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData *md,
518                                       char **target,
519                                       size_t max,
520                                       enum GNUNET_CONTAINER_MetaDataSerializationOptions opt);
521
522
523 /**
524  * @ingroup metadata
525  * Get the size of the full meta-data in serialized form.
526  *
527  * @param md metadata to inspect
528  * @return number of bytes needed for serialization, -1 on error
529  */
530 ssize_t
531 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct GNUNET_CONTAINER_MetaData *md);
532
533
534 /**
535  * @ingroup metadata
536  * Deserialize meta-data.  Initializes md.
537  *
538  * @param input serialized meta-data.
539  * @param size number of bytes available
540  * @return MD on success, NULL on error (i.e.
541  *         bad format)
542  */
543 struct GNUNET_CONTAINER_MetaData *
544 GNUNET_CONTAINER_meta_data_deserialize (const char *input,
545                                         size_t size);
546
547
548 /* ******************************* HashMap **************************** */
549
550 /**
551  * @ingroup hashmap
552  * Opaque handle for a HashMap.
553  */
554 struct GNUNET_CONTAINER_MultiHashMap;
555
556 /**
557  * @ingroup hashmap
558  * Opaque handle to an iterator over
559  * a multihashmap.
560  */
561 struct GNUNET_CONTAINER_MultiHashMapIterator;
562
563 /**
564  * @ingroup hashmap
565  * Options for storing values in the HashMap.
566  */
567 enum GNUNET_CONTAINER_MultiHashMapOption
568 {
569
570   /**
571    * @ingroup hashmap
572    * If a value with the given key exists, replace it.  Note that the
573    * old value would NOT be freed by replace (the application has to
574    * make sure that this happens if required).
575    */
576   GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
577
578   /**
579    * @ingroup hashmap
580    * Allow multiple values with the same key.
581    */
582   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
583
584   /**
585    * @ingroup hashmap
586    * There must only be one value per key; storing a value should fail
587    * if a value under the same key already exists.
588    */
589   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
590
591   /**
592    * @ingroup hashmap There must only be one value per key, but don't
593    * bother checking if a value already exists (faster than
594    * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY; implemented
595    * just like #GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE but this
596    * option documents better what is intended if
597    * #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY is what is
598    * desired).
599    */
600   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
601 };
602
603
604 /**
605  * @ingroup hashmap
606  * Iterator over hash map entries.
607  *
608  * @param cls closure
609  * @param key current key code
610  * @param value value in the hash map
611  * @return #GNUNET_YES if we should continue to
612  *         iterate,
613  *         #GNUNET_NO if not.
614  */
615 typedef int
616 (*GNUNET_CONTAINER_HashMapIterator) (void *cls,
617                                      const struct GNUNET_HashCode *key,
618                                      void *value);
619
620
621 /**
622  * @ingroup hashmap
623  * Create a multi hash map.
624  *
625  * @param len initial size (map will grow as needed)
626  * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
627  *                         #GNUNET_YES means that on 'put', the 'key' does not have
628  *                         to be copied as the destination of the pointer is
629  *                         guaranteed to be life as long as the value is stored in
630  *                         the hashmap.  This can significantly reduce memory
631  *                         consumption, but of course is also a recipie for
632  *                         heap corruption if the assumption is not true.  Only
633  *                         use this if (1) memory use is important in this case and
634  *                         (2) you have triple-checked that the invariant holds
635  * @return NULL on error
636  */
637 struct GNUNET_CONTAINER_MultiHashMap *
638 GNUNET_CONTAINER_multihashmap_create (unsigned int len,
639                                       int do_not_copy_keys);
640
641
642 /**
643  * @ingroup hashmap
644  * Destroy a hash map.  Will not free any values
645  * stored in the hash map!
646  *
647  * @param map the map
648  */
649 void
650 GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap *map);
651
652
653 /**
654  * @ingroup hashmap
655  * Given a key find a value in the map matching the key.
656  *
657  * @param map the map
658  * @param key what to look for
659  * @return NULL if no value was found; note that
660  *   this is indistinguishable from values that just
661  *   happen to be NULL; use "contains" to test for
662  *   key-value pairs with value NULL
663  */
664 void *
665 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap *map,
666                                    const struct GNUNET_HashCode *key);
667
668
669 /**
670  * @ingroup hashmap
671  * Remove the given key-value pair from the map.  Note that if the
672  * key-value pair is in the map multiple times, only one of the pairs
673  * will be removed.
674  *
675  * @param map the map
676  * @param key key of the key-value pair
677  * @param value value of the key-value pair
678  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
679  *  is not in the map
680  */
681 int
682 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
683                                       const struct GNUNET_HashCode *key,
684                                       const void *value);
685
686 /**
687  * @ingroup hashmap
688  * Remove all entries for the given key from the map.
689  * Note that the values would not be "freed".
690  *
691  * @param map the map
692  * @param key identifies values to be removed
693  * @return number of values removed
694  */
695 int
696 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap *map,
697                                           const struct GNUNET_HashCode *key);
698
699
700 /**
701  * @ingroup hashmap
702  * Remove all entries from the map.
703  * Note that the values would not be "freed".
704  *
705  * @param map the map
706  * @return number of values removed
707  */
708 unsigned int
709 GNUNET_CONTAINER_multihashmap_clear (struct GNUNET_CONTAINER_MultiHashMap *map);
710
711
712 /**
713  * @ingroup hashmap
714  * Check if the map contains any value under the given
715  * key (including values that are NULL).
716  *
717  * @param map the map
718  * @param key the key to test if a value exists for it
719  * @return #GNUNET_YES if such a value exists,
720  *         #GNUNET_NO if not
721  */
722 int
723 GNUNET_CONTAINER_multihashmap_contains (const struct GNUNET_CONTAINER_MultiHashMap *map,
724                                         const struct GNUNET_HashCode * key);
725
726
727 /**
728  * @ingroup hashmap
729  * Check if the map contains the given value under the given
730  * key.
731  *
732  * @param map the map
733  * @param key the key to test if a value exists for it
734  * @param value value to test for
735  * @return #GNUNET_YES if such a value exists,
736  *         #GNUNET_NO if not
737  */
738 int
739 GNUNET_CONTAINER_multihashmap_contains_value (const struct GNUNET_CONTAINER_MultiHashMap *map,
740                                               const struct GNUNET_HashCode *key,
741                                               const void *value);
742
743
744 /**
745  * @ingroup hashmap
746  * Store a key-value pair in the map.
747  *
748  * @param map the map
749  * @param key key to use
750  * @param value value to use
751  * @param opt options for put
752  * @return #GNUNET_OK on success,
753  *         #GNUNET_NO if a value was replaced (with REPLACE)
754  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
755  *                       value already exists
756  */
757 int
758 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
759                                    const struct GNUNET_HashCode *key,
760                                    void *value,
761                                    enum GNUNET_CONTAINER_MultiHashMapOption
762                                    opt);
763
764 /**
765  * @ingroup hashmap
766  * Get the number of key-value pairs in the map.
767  *
768  * @param map the map
769  * @return the number of key value pairs
770  */
771 unsigned int
772 GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap *map);
773
774
775 /**
776  * @ingroup hashmap
777  * Iterate over all entries in the map.
778  *
779  * @param map the map
780  * @param it function to call on each entry
781  * @param it_cls extra argument to @a it
782  * @return the number of key value pairs processed,
783  *         #GNUNET_SYSERR if it aborted iteration
784  */
785 int
786 GNUNET_CONTAINER_multihashmap_iterate (const struct GNUNET_CONTAINER_MultiHashMap *map,
787                                        GNUNET_CONTAINER_HashMapIterator it,
788                                        void *it_cls);
789
790
791 /**
792  * @ingroup hashmap
793  * Create an iterator for a multihashmap.
794  * The iterator can be used to retrieve all the elements in the multihashmap
795  * one by one, without having to handle all elements at once (in contrast to
796  * #GNUNET_CONTAINER_multihashmap_iterate).  Note that the iterator can not be
797  * used anymore if elements have been removed from 'map' after the creation of
798  * the iterator, or 'map' has been destroyed.  Adding elements to 'map' may
799  * result in skipped or repeated elements.
800  *
801  * @param map the map to create an iterator for
802  * @return an iterator over the given multihashmap @a map
803  */
804 struct GNUNET_CONTAINER_MultiHashMapIterator *
805 GNUNET_CONTAINER_multihashmap_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap *map);
806
807
808 /**
809  * @ingroup hashmap
810  * Retrieve the next element from the hash map at the iterator's
811  * position.  If there are no elements left, #GNUNET_NO is returned,
812  * and @a key and @a value are not modified.  This operation is only
813  * allowed if no elements have been removed from the multihashmap
814  * since the creation of @a iter, and the map has not been destroyed.
815  * Adding elements may result in repeating or skipping elements.
816  *
817  * @param iter the iterator to get the next element from
818  * @param key pointer to store the key in, can be NULL
819  * @param value pointer to store the value in, can be NULL
820  * @return #GNUNET_YES we returned an element,
821  *         #GNUNET_NO if we are out of elements
822  */
823 int
824 GNUNET_CONTAINER_multihashmap_iterator_next (struct GNUNET_CONTAINER_MultiHashMapIterator *iter,
825                                              struct GNUNET_HashCode *key,
826                                              const void **value);
827
828
829 /**
830  * @ingroup hashmap
831  * Destroy a multihashmap iterator.
832  *
833  * @param iter the iterator to destroy
834  */
835 void
836 GNUNET_CONTAINER_multihashmap_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
837
838
839 /**
840  * @ingroup hashmap
841  * Iterate over all entries in the map that match a particular key.
842  *
843  * @param map the map
844  * @param key key that the entries must correspond to
845  * @param it function to call on each entry
846  * @param it_cls extra argument to @a it
847  * @return the number of key value pairs processed,
848  *         #GNUNET_SYSERR if it aborted iteration
849  */
850 int
851 GNUNET_CONTAINER_multihashmap_get_multiple (const struct GNUNET_CONTAINER_MultiHashMap *map,
852                                             const struct GNUNET_HashCode *key,
853                                             GNUNET_CONTAINER_HashMapIterator it,
854                                             void *it_cls);
855
856
857 /**
858  * @ingroup hashmap
859  * Call @a it on a random value from the map, or not at all
860  * if the map is empty.  Note that this function has linear
861  * complexity (in the size of the map).
862  *
863  * @param map the map
864  * @param it function to call on a random entry
865  * @param it_cls extra argument to @a it
866  * @return the number of key value pairs processed, zero or one.
867  */
868 unsigned int
869 GNUNET_CONTAINER_multihashmap_get_random (const struct GNUNET_CONTAINER_MultiHashMap *map,
870                                           GNUNET_CONTAINER_HashMapIterator it,
871                                           void *it_cls);
872
873
874 /* ***************** Version of Multihashmap for peer identities ****************** */
875
876 /**
877  * @ingroup hashmap
878  * Iterator over hash map entries.
879  *
880  * @param cls closure
881  * @param key current public key
882  * @param value value in the hash map
883  * @return #GNUNET_YES if we should continue to
884  *         iterate,
885  *         #GNUNET_NO if not.
886  */
887 typedef int
888 (*GNUNET_CONTAINER_PeerMapIterator) (void *cls,
889                                      const struct GNUNET_PeerIdentity *key,
890                                      void *value);
891
892
893 struct GNUNET_CONTAINER_MultiPeerMap;
894 /**
895  * @ingroup hashmap
896  * Create a multi peer map (hash map for public keys of peers).
897  *
898  * @param len initial size (map will grow as needed)
899  * @param do_not_copy_keys #GNUNET_NO is always safe and should be used by default;
900  *                         #GNUNET_YES means that on 'put', the 'key' does not have
901  *                         to be copied as the destination of the pointer is
902  *                         guaranteed to be life as long as the value is stored in
903  *                         the hashmap.  This can significantly reduce memory
904  *                         consumption, but of course is also a recipie for
905  *                         heap corruption if the assumption is not true.  Only
906  *                         use this if (1) memory use is important in this case and
907  *                         (2) you have triple-checked that the invariant holds
908  * @return NULL on error
909  */
910 struct GNUNET_CONTAINER_MultiPeerMap *
911 GNUNET_CONTAINER_multipeermap_create (unsigned int len,
912                                       int do_not_copy_keys);
913
914
915 /**
916  * @ingroup hashmap
917  * Destroy a hash map.  Will not free any values
918  * stored in the hash map!
919  *
920  * @param map the map
921  */
922 void
923 GNUNET_CONTAINER_multipeermap_destroy (struct GNUNET_CONTAINER_MultiPeerMap *map);
924
925
926 /**
927  * @ingroup hashmap
928  * Given a key find a value in the map matching the key.
929  *
930  * @param map the map
931  * @param key what to look for
932  * @return NULL if no value was found; note that
933  *   this is indistinguishable from values that just
934  *   happen to be NULL; use "contains" to test for
935  *   key-value pairs with value NULL
936  */
937 void *
938 GNUNET_CONTAINER_multipeermap_get (const struct GNUNET_CONTAINER_MultiPeerMap *map,
939                                    const struct GNUNET_PeerIdentity *key);
940
941
942 /**
943  * @ingroup hashmap
944  * Remove the given key-value pair from the map.  Note that if the
945  * key-value pair is in the map multiple times, only one of the pairs
946  * will be removed.
947  *
948  * @param map the map
949  * @param key key of the key-value pair
950  * @param value value of the key-value pair
951  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
952  *  is not in the map
953  */
954 int
955 GNUNET_CONTAINER_multipeermap_remove (struct GNUNET_CONTAINER_MultiPeerMap *map,
956                                       const struct GNUNET_PeerIdentity * key,
957                                       const void *value);
958
959 /**
960  * @ingroup hashmap
961  * Remove all entries for the given key from the map.
962  * Note that the values would not be "freed".
963  *
964  * @param map the map
965  * @param key identifies values to be removed
966  * @return number of values removed
967  */
968 int
969 GNUNET_CONTAINER_multipeermap_remove_all (struct GNUNET_CONTAINER_MultiPeerMap *map,
970                                           const struct GNUNET_PeerIdentity *key);
971
972
973 /**
974  * @ingroup hashmap
975  * Check if the map contains any value under the given
976  * key (including values that are NULL).
977  *
978  * @param map the map
979  * @param key the key to test if a value exists for it
980  * @return #GNUNET_YES if such a value exists,
981  *         #GNUNET_NO if not
982  */
983 int
984 GNUNET_CONTAINER_multipeermap_contains (const struct GNUNET_CONTAINER_MultiPeerMap *map,
985                                         const struct GNUNET_PeerIdentity *key);
986
987
988 /**
989  * @ingroup hashmap
990  * Check if the map contains the given value under the given
991  * key.
992  *
993  * @param map the map
994  * @param key the key to test if a value exists for it
995  * @param value value to test for
996  * @return #GNUNET_YES if such a value exists,
997  *         #GNUNET_NO if not
998  */
999 int
1000 GNUNET_CONTAINER_multipeermap_contains_value (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1001                                               const struct GNUNET_PeerIdentity * key,
1002                                               const void *value);
1003
1004
1005 /**
1006  * @ingroup hashmap
1007  * Store a key-value pair in the map.
1008  *
1009  * @param map the map
1010  * @param key key to use
1011  * @param value value to use
1012  * @param opt options for put
1013  * @return #GNUNET_OK on success,
1014  *         #GNUNET_NO if a value was replaced (with REPLACE)
1015  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
1016  *                       value already exists
1017  */
1018 int
1019 GNUNET_CONTAINER_multipeermap_put (struct GNUNET_CONTAINER_MultiPeerMap *map,
1020                                    const struct GNUNET_PeerIdentity *key,
1021                                    void *value,
1022                                    enum GNUNET_CONTAINER_MultiHashMapOption opt);
1023
1024
1025 /**
1026  * @ingroup hashmap
1027  * Get the number of key-value pairs in the map.
1028  *
1029  * @param map the map
1030  * @return the number of key value pairs
1031  */
1032 unsigned int
1033 GNUNET_CONTAINER_multipeermap_size (const struct GNUNET_CONTAINER_MultiPeerMap *map);
1034
1035
1036 /**
1037  * @ingroup hashmap
1038  * Iterate over all entries in the map.
1039  *
1040  * @param map the map
1041  * @param it function to call on each entry
1042  * @param it_cls extra argument to @a it
1043  * @return the number of key value pairs processed,
1044  *         #GNUNET_SYSERR if it aborted iteration
1045  */
1046 int
1047 GNUNET_CONTAINER_multipeermap_iterate (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1048                                        GNUNET_CONTAINER_PeerMapIterator it,
1049                                        void *it_cls);
1050
1051
1052 struct GNUNET_CONTAINER_MultiPeerMapIterator;
1053 /**
1054  * @ingroup hashmap
1055  * Create an iterator for a multihashmap.
1056  * The iterator can be used to retrieve all the elements in the multihashmap
1057  * one by one, without having to handle all elements at once (in contrast to
1058  * #GNUNET_CONTAINER_multipeermap_iterate).  Note that the iterator can not be
1059  * used anymore if elements have been removed from @a map after the creation of
1060  * the iterator, or 'map' has been destroyed.  Adding elements to @a map may
1061  * result in skipped or repeated elements.
1062  *
1063  * @param map the map to create an iterator for
1064  * @return an iterator over the given multihashmap @a map
1065  */
1066 struct GNUNET_CONTAINER_MultiPeerMapIterator *
1067 GNUNET_CONTAINER_multipeermap_iterator_create (const struct GNUNET_CONTAINER_MultiPeerMap *map);
1068
1069
1070 /**
1071  * @ingroup hashmap
1072  * Retrieve the next element from the hash map at the iterator's
1073  * position.  If there are no elements left, #GNUNET_NO is returned,
1074  * and @a key and @a value are not modified.  This operation is only
1075  * allowed if no elements have been removed from the multihashmap
1076  * since the creation of @a iter, and the map has not been destroyed.
1077  * Adding elements may result in repeating or skipping elements.
1078  *
1079  * @param iter the iterator to get the next element from
1080  * @param key pointer to store the key in, can be NULL
1081  * @param value pointer to store the value in, can be NULL
1082  * @return #GNUNET_YES we returned an element,
1083  *         #GNUNET_NO if we are out of elements
1084  */
1085 int
1086 GNUNET_CONTAINER_multipeermap_iterator_next (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter,
1087                                              struct GNUNET_PeerIdentity *key,
1088                                              const void **value);
1089
1090
1091 /**
1092  * @ingroup hashmap
1093  * Destroy a multipeermap iterator.
1094  *
1095  * @param iter the iterator to destroy
1096  */
1097 void
1098 GNUNET_CONTAINER_multipeermap_iterator_destroy (struct GNUNET_CONTAINER_MultiPeerMapIterator *iter);
1099
1100
1101 /**
1102  * @ingroup hashmap
1103  * Iterate over all entries in the map that match a particular key.
1104  *
1105  * @param map the map
1106  * @param key public key that the entries must correspond to
1107  * @param it function to call on each entry
1108  * @param it_cls extra argument to @a it
1109  * @return the number of key value pairs processed,
1110  *         #GNUNET_SYSERR if it aborted iteration
1111  */
1112 int
1113 GNUNET_CONTAINER_multipeermap_get_multiple (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1114                                             const struct GNUNET_PeerIdentity *key,
1115                                             GNUNET_CONTAINER_PeerMapIterator it,
1116                                             void *it_cls);
1117
1118
1119 /**
1120  * @ingroup hashmap
1121  * Call @a it on a random value from the map, or not at all
1122  * if the map is empty.  Note that this function has linear
1123  * complexity (in the size of the map).
1124  *
1125  * @param map the map
1126  * @param it function to call on a random entry
1127  * @param it_cls extra argument to @a it
1128  * @return the number of key value pairs processed, zero or one.
1129  */
1130 unsigned int
1131 GNUNET_CONTAINER_multipeermap_get_random (const struct GNUNET_CONTAINER_MultiPeerMap *map,
1132                                           GNUNET_CONTAINER_PeerMapIterator it,
1133                                           void *it_cls);
1134
1135
1136 /* Version of multihashmap with 32 bit keys */
1137
1138 /**
1139  * @ingroup hashmap
1140  * Opaque handle for the 32-bit key HashMap.
1141  */
1142 struct GNUNET_CONTAINER_MultiHashMap32;
1143
1144
1145 /**
1146  * @ingroup hashmap
1147  * Opaque handle to an iterator over
1148  * a 32-bit key multihashmap.
1149  */
1150 struct GNUNET_CONTAINER_MultiHashMap32Iterator;
1151
1152
1153 /**
1154  * @ingroup hashmap
1155  * Iterator over hash map entries.
1156  *
1157  * @param cls closure
1158  * @param key current key code
1159  * @param value value in the hash map
1160  * @return #GNUNET_YES if we should continue to
1161  *         iterate,
1162  *         #GNUNET_NO if not.
1163  */
1164 typedef int (*GNUNET_CONTAINER_HashMapIterator32) (void *cls,
1165                                                    uint32_t key,
1166                                                    void *value);
1167
1168
1169 /**
1170  * @ingroup hashmap
1171  * Create a 32-bit key multi hash map.
1172  *
1173  * @param len initial size (map will grow as needed)
1174  * @return NULL on error
1175  */
1176 struct GNUNET_CONTAINER_MultiHashMap32 *
1177 GNUNET_CONTAINER_multihashmap32_create (unsigned int len);
1178
1179
1180 /**
1181  * @ingroup hashmap
1182  * Destroy a 32-bit key hash map.  Will not free any values
1183  * stored in the hash map!
1184  *
1185  * @param map the map
1186  */
1187 void
1188 GNUNET_CONTAINER_multihashmap32_destroy (struct GNUNET_CONTAINER_MultiHashMap32
1189                                          *map);
1190
1191
1192 /**
1193  * @ingroup hashmap
1194  * Get the number of key-value pairs in the map.
1195  *
1196  * @param map the map
1197  * @return the number of key value pairs
1198  */
1199 unsigned int
1200 GNUNET_CONTAINER_multihashmap32_size (const struct
1201                                       GNUNET_CONTAINER_MultiHashMap32 *map);
1202
1203
1204 /**
1205  * @ingroup hashmap
1206  * Given a key find a value in the map matching the key.
1207  *
1208  * @param map the map
1209  * @param key what to look for
1210  * @return NULL if no value was found; note that
1211  *   this is indistinguishable from values that just
1212  *   happen to be NULL; use "contains" to test for
1213  *   key-value pairs with value NULL
1214  */
1215 void *
1216 GNUNET_CONTAINER_multihashmap32_get (const struct
1217                                      GNUNET_CONTAINER_MultiHashMap32 *map,
1218                                      uint32_t key);
1219
1220
1221 /**
1222  * @ingroup hashmap
1223  * Iterate over all entries in the map.
1224  *
1225  * @param map the map
1226  * @param it function to call on each entry
1227  * @param it_cls extra argument to @a it
1228  * @return the number of key value pairs processed,
1229  *         #GNUNET_SYSERR if it aborted iteration
1230  */
1231 int
1232 GNUNET_CONTAINER_multihashmap32_iterate (const struct
1233                                          GNUNET_CONTAINER_MultiHashMap32 *map,
1234                                          GNUNET_CONTAINER_HashMapIterator32 it,
1235                                          void *it_cls);
1236
1237
1238 /**
1239  * @ingroup hashmap
1240  * Remove the given key-value pair from the map.  Note that if the
1241  * key-value pair is in the map multiple times, only one of the pairs
1242  * will be removed.
1243  *
1244  * @param map the map
1245  * @param key key of the key-value pair
1246  * @param value value of the key-value pair
1247  * @return #GNUNET_YES on success, #GNUNET_NO if the key-value pair
1248  *  is not in the map
1249  */
1250 int
1251 GNUNET_CONTAINER_multihashmap32_remove (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1252                                         uint32_t key,
1253                                         const void *value);
1254
1255
1256 /**
1257  * @ingroup hashmap
1258  * Remove all entries for the given key from the map.
1259  * Note that the values would not be "freed".
1260  *
1261  * @param map the map
1262  * @param key identifies values to be removed
1263  * @return number of values removed
1264  */
1265 int
1266 GNUNET_CONTAINER_multihashmap32_remove_all (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1267                                             uint32_t key);
1268
1269
1270 /**
1271  * @ingroup hashmap
1272  * Check if the map contains any value under the given
1273  * key (including values that are NULL).
1274  *
1275  * @param map the map
1276  * @param key the key to test if a value exists for it
1277  * @return #GNUNET_YES if such a value exists,
1278  *         #GNUNET_NO if not
1279  */
1280 int
1281 GNUNET_CONTAINER_multihashmap32_contains (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1282                                           uint32_t key);
1283
1284
1285 /**
1286  * @ingroup hashmap
1287  * Check if the map contains the given value under the given
1288  * key.
1289  *
1290  * @param map the map
1291  * @param key the key to test if a value exists for it
1292  * @param value value to test for
1293  * @return #GNUNET_YES if such a value exists,
1294  *         #GNUNET_NO if not
1295  */
1296 int
1297 GNUNET_CONTAINER_multihashmap32_contains_value (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1298                                                 uint32_t key,
1299                                                 const void *value);
1300
1301
1302 /**
1303  * @ingroup hashmap
1304  * Store a key-value pair in the map.
1305  *
1306  * @param map the map
1307  * @param key key to use
1308  * @param value value to use
1309  * @param opt options for put
1310  * @return #GNUNET_OK on success,
1311  *         #GNUNET_NO if a value was replaced (with REPLACE)
1312  *         #GNUNET_SYSERR if #GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY was the option and the
1313  *                       value already exists
1314  */
1315 int
1316 GNUNET_CONTAINER_multihashmap32_put (struct GNUNET_CONTAINER_MultiHashMap32 *map,
1317                                      uint32_t key,
1318                                      void *value,
1319                                      enum GNUNET_CONTAINER_MultiHashMapOption opt);
1320
1321
1322 /**
1323  * @ingroup hashmap
1324  * Iterate over all entries in the map that match a particular key.
1325  *
1326  * @param map the map
1327  * @param key key that the entries must correspond to
1328  * @param it function to call on each entry
1329  * @param it_cls extra argument to @a it
1330  * @return the number of key value pairs processed,
1331  *         #GNUNET_SYSERR if it aborted iteration
1332  */
1333 int
1334 GNUNET_CONTAINER_multihashmap32_get_multiple (const struct GNUNET_CONTAINER_MultiHashMap32 *map,
1335                                               uint32_t key,
1336                                               GNUNET_CONTAINER_HashMapIterator32 it,
1337                                               void *it_cls);
1338
1339
1340 /**
1341  * Create an iterator for a 32-bit multihashmap.
1342  * The iterator can be used to retrieve all the elements in the multihashmap
1343  * one by one, without having to handle all elements at once (in contrast to
1344  * #GNUNET_CONTAINER_multihashmap32_iterate).  Note that the iterator can not be
1345  * used anymore if elements have been removed from 'map' after the creation of
1346  * the iterator, or 'map' has been destroyed.  Adding elements to 'map' may
1347  * result in skipped or repeated elements.
1348  *
1349  * @param map the map to create an iterator for
1350  * @return an iterator over the given multihashmap map
1351  */
1352 struct GNUNET_CONTAINER_MultiHashMap32Iterator *
1353 GNUNET_CONTAINER_multihashmap32_iterator_create (const struct GNUNET_CONTAINER_MultiHashMap32 *map);
1354
1355
1356 /**
1357  * Retrieve the next element from the hash map at the iterator's position.
1358  * If there are no elements left, GNUNET_NO is returned, and 'key' and 'value'
1359  * are not modified.
1360  * This operation is only allowed if no elements have been removed from the
1361  * multihashmap since the creation of 'iter', and the map has not been destroyed.
1362  * Adding elements may result in repeating or skipping elements.
1363  *
1364  * @param iter the iterator to get the next element from
1365  * @param key pointer to store the key in, can be NULL
1366  * @param value pointer to store the value in, can be NULL
1367  * @return #GNUNET_YES we returned an element,
1368  *         #GNUNET_NO if we are out of elements
1369  */
1370 int
1371 GNUNET_CONTAINER_multihashmap32_iterator_next (struct GNUNET_CONTAINER_MultiHashMap32Iterator *iter,
1372                                                uint32_t *key,
1373                                                const void **value);
1374
1375
1376 /**
1377  * Destroy a 32-bit multihashmap iterator.
1378  *
1379  * @param iter the iterator to destroy
1380  */
1381 void
1382 GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiHashMapIterator *iter);
1383
1384
1385 /* ******************** doubly-linked list *************** */
1386 /* To avoid mistakes: head->prev == tail->next == NULL     */
1387
1388 /**
1389  * @ingroup dll
1390  * Insert an element at the head of a DLL. Assumes that head, tail and
1391  * element are structs with prev and next fields.
1392  *
1393  * @param head pointer to the head of the DLL
1394  * @param tail pointer to the tail of the DLL
1395  * @param element element to insert
1396  */
1397 #define GNUNET_CONTAINER_DLL_insert(head,tail,element) do { \
1398   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1399   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1400   (element)->next = (head); \
1401   (element)->prev = NULL; \
1402   if ((tail) == NULL) \
1403     (tail) = element; \
1404   else \
1405     (head)->prev = element; \
1406   (head) = (element); } while (0)
1407
1408
1409 /**
1410  * @ingroup dll
1411  * Insert an element at the tail of a DLL. Assumes that head, tail and
1412  * element are structs with prev and next fields.
1413  *
1414  * @param head pointer to the head of the DLL
1415  * @param tail pointer to the tail of the DLL
1416  * @param element element to insert
1417  */
1418 #define GNUNET_CONTAINER_DLL_insert_tail(head,tail,element) do { \
1419   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1420   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1421   (element)->prev = (tail); \
1422   (element)->next = NULL; \
1423   if ((head) == NULL) \
1424     (head) = element; \
1425   else \
1426     (tail)->next = element; \
1427   (tail) = (element); } while (0)
1428
1429
1430 /**
1431  * @ingroup dll
1432  * Insert an element into a DLL after the given other element.  Insert
1433  * at the head if the other element is NULL.
1434  *
1435  * @param head pointer to the head of the DLL
1436  * @param tail pointer to the tail of the DLL
1437  * @param other prior element, NULL for insertion at head of DLL
1438  * @param element element to insert
1439  */
1440 #define GNUNET_CONTAINER_DLL_insert_after(head,tail,other,element) do { \
1441   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1442   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1443   (element)->prev = (other); \
1444   if (NULL == other) \
1445     { \
1446       (element)->next = (head); \
1447       (head) = (element); \
1448     } \
1449   else \
1450     { \
1451       (element)->next = (other)->next; \
1452       (other)->next = (element); \
1453     } \
1454   if (NULL == (element)->next) \
1455     (tail) = (element); \
1456   else \
1457     (element)->next->prev = (element); } while (0)
1458
1459
1460 /**
1461  * @ingroup dll
1462  * Insert an element into a DLL before the given other element.  Insert
1463  * at the tail if the other element is NULL.
1464  *
1465  * @param head pointer to the head of the DLL
1466  * @param tail pointer to the tail of the DLL
1467  * @param other prior element, NULL for insertion at head of DLL
1468  * @param element element to insert
1469  */
1470 #define GNUNET_CONTAINER_DLL_insert_before(head,tail,other,element) do { \
1471   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
1472   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
1473   (element)->next = (other); \
1474   if (NULL == other) \
1475     { \
1476       (element)->prev = (tail); \
1477       (tail) = (element); \
1478     } \
1479   else \
1480     { \
1481       (element)->prev = (other)->prev; \
1482       (other)->prev = (element); \
1483     } \
1484   if (NULL == (element)->prev) \
1485     (head) = (element); \
1486   else \
1487     (element)->prev->next = (element); } while (0)
1488
1489
1490 /**
1491  * @ingroup dll
1492  * Remove an element from a DLL. Assumes that head, tail and
1493  * element point to structs with prev and next fields.
1494  *
1495  * Using the head or tail pointer as the element
1496  * argument does NOT work with this macro.
1497  * Make sure to store head/tail in another pointer
1498  * and use it to remove the head or tail of the list.
1499  *
1500  * @param head pointer to the head of the DLL
1501  * @param tail pointer to the tail of the DLL
1502  * @param element element to remove
1503  */
1504 #define GNUNET_CONTAINER_DLL_remove(head,tail,element) do { \
1505   GNUNET_assert ( ( (element)->prev != NULL) || ((head) == (element))); \
1506   GNUNET_assert ( ( (element)->next != NULL) || ((tail) == (element))); \
1507   if ((element)->prev == NULL) \
1508     (head) = (element)->next;  \
1509   else \
1510     (element)->prev->next = (element)->next; \
1511   if ((element)->next == NULL) \
1512     (tail) = (element)->prev;  \
1513   else \
1514     (element)->next->prev = (element)->prev; \
1515   (element)->next = NULL; \
1516   (element)->prev = NULL; } while (0)
1517
1518
1519 /* ************ Multi-DLL interface, allows DLL elements to be
1520    in multiple lists at the same time *********************** */
1521
1522 /**
1523  * @ingroup dll
1524  * Insert an element at the head of a MDLL. Assumes that head, tail and
1525  * element are structs with prev and next fields.
1526  *
1527  * @param mdll suffix name for the next and prev pointers in the element
1528  * @param head pointer to the head of the MDLL
1529  * @param tail pointer to the tail of the MDLL
1530  * @param element element to insert
1531  */
1532 #define GNUNET_CONTAINER_MDLL_insert(mdll,head,tail,element) do {       \
1533   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1534   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1535   (element)->next_##mdll = (head); \
1536   (element)->prev_##mdll = NULL; \
1537   if ((tail) == NULL) \
1538     (tail) = element; \
1539   else \
1540     (head)->prev_##mdll = element; \
1541   (head) = (element); } while (0)
1542
1543
1544 /**
1545  * @ingroup dll
1546  * Insert an element at the tail of a MDLL. Assumes that head, tail and
1547  * element are structs with prev and next fields.
1548  *
1549  * @param mdll suffix name for the next and prev pointers in the element
1550  * @param head pointer to the head of the MDLL
1551  * @param tail pointer to the tail of the MDLL
1552  * @param element element to insert
1553  */
1554 #define GNUNET_CONTAINER_MDLL_insert_tail(mdll,head,tail,element) do {  \
1555   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1556   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1557   (element)->prev_##mdll = (tail); \
1558   (element)->next_##mdll = NULL; \
1559   if ((head) == NULL) \
1560     (head) = element; \
1561   else \
1562     (tail)->next_##mdll = element; \
1563   (tail) = (element); } while (0)
1564
1565
1566 /**
1567  * @ingroup dll
1568  * Insert an element into a MDLL after the given other element.  Insert
1569  * at the head if the other element is NULL.
1570  *
1571  * @param mdll suffix name for the next and prev pointers in the element
1572  * @param head pointer to the head of the MDLL
1573  * @param tail pointer to the tail of the MDLL
1574  * @param other prior element, NULL for insertion at head of MDLL
1575  * @param element element to insert
1576  */
1577 #define GNUNET_CONTAINER_MDLL_insert_after(mdll,head,tail,other,element) do { \
1578   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1579   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1580   (element)->prev_##mdll = (other); \
1581   if (NULL == other) \
1582     { \
1583       (element)->next_##mdll = (head); \
1584       (head) = (element); \
1585     } \
1586   else \
1587     { \
1588       (element)->next_##mdll = (other)->next_##mdll; \
1589       (other)->next_##mdll = (element); \
1590     } \
1591   if (NULL == (element)->next_##mdll) \
1592     (tail) = (element); \
1593   else \
1594     (element)->next->prev_##mdll = (element); } while (0)
1595
1596
1597 /**
1598  * @ingroup dll
1599  * Insert an element into a MDLL before the given other element.  Insert
1600  * at the tail if the other element is NULL.
1601  *
1602  * @param mdll suffix name for the next and prev pointers in the element
1603  * @param head pointer to the head of the MDLL
1604  * @param tail pointer to the tail of the MDLL
1605  * @param other prior element, NULL for insertion at head of MDLL
1606  * @param element element to insert
1607  */
1608 #define GNUNET_CONTAINER_MDLL_insert_before(mdll,head,tail,other,element) do { \
1609   GNUNET_assert ( ( (element)->prev_##mdll == NULL) && ((head) != (element))); \
1610   GNUNET_assert ( ( (element)->next_##mdll == NULL) && ((tail) != (element))); \
1611   (element)->next_##mdll = (other); \
1612   if (NULL == other) \
1613     { \
1614       (element)->prev = (tail); \
1615       (tail) = (element); \
1616     } \
1617   else \
1618     { \
1619       (element)->prev_##mdll = (other)->prev_##mdll; \
1620       (other)->prev_##mdll = (element); \
1621     } \
1622   if (NULL == (element)->prev_##mdll) \
1623     (head) = (element); \
1624   else \
1625     (element)->prev_##mdll->next_##mdll = (element); } while (0)
1626
1627
1628 /**
1629  * @ingroup dll
1630  * Remove an element from a MDLL. Assumes
1631  * that head, tail and element are structs
1632  * with prev and next fields.
1633  *
1634  * @param mdll suffix name for the next and prev pointers in the element
1635  * @param head pointer to the head of the MDLL
1636  * @param tail pointer to the tail of the MDLL
1637  * @param element element to remove
1638  */
1639 #define GNUNET_CONTAINER_MDLL_remove(mdll,head,tail,element) do {       \
1640   GNUNET_assert ( ( (element)->prev_##mdll != NULL) || ((head) == (element))); \
1641   GNUNET_assert ( ( (element)->next_##mdll != NULL) || ((tail) == (element))); \
1642   if ((element)->prev_##mdll == NULL) \
1643     (head) = (element)->next_##mdll;  \
1644   else \
1645     (element)->prev_##mdll->next_##mdll = (element)->next_##mdll; \
1646   if ((element)->next_##mdll == NULL) \
1647     (tail) = (element)->prev_##mdll;  \
1648   else \
1649     (element)->next_##mdll->prev_##mdll = (element)->prev_##mdll; \
1650   (element)->next_##mdll = NULL; \
1651   (element)->prev_##mdll = NULL; } while (0)
1652
1653
1654
1655 /* ******************** Heap *************** */
1656
1657
1658 /**
1659  * @ingroup heap
1660  * Cost by which elements in a heap can be ordered.
1661  */
1662 typedef uint64_t GNUNET_CONTAINER_HeapCostType;
1663
1664
1665 /**
1666  * @ingroup heap
1667  * Heap type, either max or min.
1668  */
1669 enum GNUNET_CONTAINER_HeapOrder
1670 {
1671   /**
1672    * @ingroup heap
1673    * Heap with the maximum cost at the root.
1674    */
1675   GNUNET_CONTAINER_HEAP_ORDER_MAX,
1676
1677   /**
1678    * @ingroup heap
1679    * Heap with the minimum cost at the root.
1680    */
1681   GNUNET_CONTAINER_HEAP_ORDER_MIN
1682 };
1683
1684
1685 /**
1686  * @ingroup heap
1687  * Handle to a Heap.
1688  */
1689 struct GNUNET_CONTAINER_Heap;
1690
1691
1692 /**
1693  * @ingroup heap
1694  * Handle to a node in a heap.
1695  */
1696 struct GNUNET_CONTAINER_HeapNode;
1697
1698
1699 /**
1700  * @ingroup heap
1701  * Create a new heap.
1702  *
1703  * @param order how should the heap be sorted?
1704  * @return handle to the heap
1705  */
1706 struct GNUNET_CONTAINER_Heap *
1707 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order);
1708
1709
1710 /**
1711  * @ingroup heap
1712  * Destroys the heap.  Only call on a heap that
1713  * is already empty.
1714  *
1715  * @param heap heap to destroy
1716  */
1717 void
1718 GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap);
1719
1720
1721 /**
1722  * @ingroup heap
1723  * Get element stored at the root of @a heap.
1724  *
1725  * @param heap  Heap to inspect.
1726  * @return Element at the root, or NULL if heap is empty.
1727  */
1728 void *
1729 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap);
1730
1731
1732 /**
1733  * Get @a element and @a cost stored at the root of @a heap.
1734  *
1735  * @param[in]  heap     Heap to inspect.
1736  * @param[out] element  Root element is returned here.
1737  * @param[out] cost     Cost of @a element is returned here.
1738  * @return #GNUNET_YES if an element is returned,
1739  *         #GNUNET_NO  if the heap is empty.
1740  */
1741 int
1742 GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap,
1743                              void **element,
1744                              GNUNET_CONTAINER_HeapCostType *cost);
1745
1746
1747 /**
1748  * @ingroup heap
1749  * Get the current size of the heap
1750  *
1751  * @param heap the heap to get the size of
1752  * @return number of elements stored
1753  */
1754 unsigned int
1755 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap);
1756
1757
1758 /**
1759  * @ingroup heap
1760  * Get the current cost of the node
1761  *
1762  * @param node the node to get the cost of
1763  * @return cost of the node
1764  */
1765 GNUNET_CONTAINER_HeapCostType
1766 GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
1767                                      *node);
1768
1769 /**
1770  * @ingroup heap
1771  * Iterator for heap
1772  *
1773  * @param cls closure
1774  * @param node internal node of the heap
1775  * @param element value stored at the node
1776  * @param cost cost associated with the node
1777  * @return #GNUNET_YES if we should continue to iterate,
1778  *         #GNUNET_NO if not.
1779  */
1780 typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls,
1781                                               struct GNUNET_CONTAINER_HeapNode *
1782                                               node, void *element,
1783                                               GNUNET_CONTAINER_HeapCostType
1784                                               cost);
1785
1786
1787 /**
1788  * @ingroup heap
1789  * Iterate over all entries in the heap.
1790  *
1791  * @param heap the heap
1792  * @param iterator function to call on each entry
1793  * @param iterator_cls closure for @a iterator
1794  */
1795 void
1796 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
1797                                GNUNET_CONTAINER_HeapIterator iterator,
1798                                void *iterator_cls);
1799
1800 /**
1801  * @ingroup heap
1802  * Perform a random walk of the tree.  The walk is biased
1803  * towards elements closer to the root of the tree (since
1804  * each walk starts at the root and ends at a random leaf).
1805  * The heap internally tracks the current position of the
1806  * walk.
1807  *
1808  * @param heap heap to walk
1809  * @return data stored at the next random node in the walk;
1810  *         NULL if the tree is empty.
1811  */
1812 void *
1813 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap);
1814
1815
1816 /**
1817  * @ingroup heap
1818  * Inserts a new element into the heap.
1819  *
1820  * @param heap heap to modify
1821  * @param element element to insert
1822  * @param cost cost for the element
1823  * @return node for the new element
1824  */
1825 struct GNUNET_CONTAINER_HeapNode *
1826 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
1827                               GNUNET_CONTAINER_HeapCostType cost);
1828
1829
1830 /**
1831  * @ingroup heap
1832  * Remove root of the heap.
1833  *
1834  * @param heap heap to modify
1835  * @return element data stored at the root node
1836  */
1837 void *
1838 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
1839
1840
1841 /**
1842  * @ingroup heap
1843  * Removes a node from the heap.
1844  *
1845  * @param node node to remove
1846  * @return element data stored at the node, NULL if heap is empty
1847  */
1848 void *
1849 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node);
1850
1851
1852 /**
1853  * @ingroup heap
1854  * Updates the cost of any node in the tree
1855  *
1856  * @param heap heap to modify
1857  * @param node node for which the cost is to be changed
1858  * @param new_cost new cost for the node
1859  */
1860 void
1861 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
1862                                    struct GNUNET_CONTAINER_HeapNode *node,
1863                                    GNUNET_CONTAINER_HeapCostType new_cost);
1864
1865
1866 #if 0                           /* keep Emacsens' auto-indent happy */
1867 {
1868 #endif
1869 #ifdef __cplusplus
1870 }
1871 #endif
1872
1873
1874 /* ifndef GNUNET_CONTAINER_LIB_H */
1875 #endif
1876 /* end of gnunet_container_lib.h */