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