SHUTDOWN
[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,
66                                         GNUNET_HashCode * next);
67
68
69 /**
70  * Load a bloom-filter from a file.
71  *
72  * @param filename the name of the file (or the prefix)
73  * @param size the size of the bloom-filter (number of
74  *        bytes of storage space to use)
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
81                                    char
82                                    *filename,
83                                    size_t
84                                    size,
85                                    unsigned
86                                    int
87                                    k);
88
89
90 /**
91  * Create a bloom filter from raw bits.
92  *
93  * @param data the raw bits in memory (maybe NULL,
94  *        in which case all bits should be considered
95  *        to be zero).
96  * @param size the size of the bloom-filter (number of
97  *        bytes of storage space to use); also size of data
98  *        -- unless data is NULL.  Must be a power of 2.
99  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
100  *        element (number of bits set per element in the set)
101  * @return the bloomfilter
102  */
103 struct GNUNET_CONTAINER_BloomFilter *
104 GNUNET_CONTAINER_bloomfilter_init (const
105                                    char
106                                    *data,
107                                    size_t
108                                    size,
109                                    unsigned
110                                    int
111                                    k);
112
113
114 /**
115  * Copy the raw data of this bloomfilter into
116  * the given data array.
117  *
118  * @param data where to write the data
119  * @param size the size of the given data array
120  * @return GNUNET_SYSERR if the data array of the wrong size
121  */
122 int GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
123                                                GNUNET_CONTAINER_BloomFilter
124                                                *bf, char *data,
125                                                size_t size);
126
127
128 /**
129  * Test if an element is in the filter.
130  * @param e the element
131  * @param bf the filter
132  * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
133  */
134 int GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
135                                        *bf, const GNUNET_HashCode * e);
136
137
138 /**
139  * Add an element to the filter
140  * @param bf the filter
141  * @param e the element
142  */
143 void GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter
144                                        *bf, const GNUNET_HashCode * e);
145
146
147 /**
148  * Remove an element from the filter.
149  * @param bf the filter
150  * @param e the element to remove
151  */
152 void GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter
153                                           *bf, const GNUNET_HashCode * e);
154
155
156 /**
157  * Create a copy of a bloomfilter.
158  *
159  * @param bf the filter
160  * @return copy of bf
161  */
162 struct GNUNET_CONTAINER_BloomFilter *
163 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter *bf);
164
165
166
167 /**
168  * Free the space associcated with a filter
169  * in memory, flush to drive if needed (do not
170  * free the space on the drive)
171  * @param bf the filter
172  */
173 void GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter
174                                         *bf);
175
176
177 /**
178  * Get size of the bloom filter.
179  *
180  * @param bf the filter
181  * @return number of bytes used for the data of the bloom filter
182  */
183 size_t 
184 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
185                                        *bf);
186
187
188 /**
189  * Reset a bloom filter to empty.
190  * @param bf the filter
191  */
192 void GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter
193                                          *bf);
194
195 /**
196  * Or the entries of the given raw data array with the
197  * data of the given bloom filter.  Assumes that
198  * the size of the data array and the current filter
199  * match.
200  *
201  * @param bf the filter
202  * @param data data to OR-in
203  * @param size size of data
204  * @return GNUNET_OK on success
205  */
206 int GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
207                                      const char *data, size_t size);
208
209 /**
210  * Or the entries of the given raw data array with the
211  * data of the given bloom filter.  Assumes that
212  * the size of the data array and the current filter
213  * match.
214  *
215  * @param bf the filter
216  * @param to_or the bloomfilter to or-in
217  * @param size number of bytes in data
218  */
219 int
220 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
221                                   const struct GNUNET_CONTAINER_BloomFilter *to_or,
222                                   size_t size);
223
224 /**
225  * Resize a bloom filter.  Note that this operation
226  * is pretty costly.  Essentially, the bloom filter
227  * needs to be completely re-build.
228  *
229  * @param bf the filter
230  * @param iterator an iterator over all elements stored in the BF
231  * @param iterator_cls closure for iterator
232  * @param size the new size for the filter
233  * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
234  */
235 void GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter
236                                           *bf,
237                                           GNUNET_HashCodeIterator iterator,
238                                           void *iterator_cls,
239                                           size_t size, unsigned int k);
240
241 /* ****************** metadata ******************* */
242
243 /**
244  * Meta data to associate with a file, directory or namespace.
245  */
246 struct GNUNET_CONTAINER_MetaData;
247
248 /**
249  * Create a fresh MetaData token.
250  * 
251  * @return empty meta-data container
252  */
253 struct GNUNET_CONTAINER_MetaData *
254 GNUNET_CONTAINER_meta_data_create (void);
255
256 /**
257  * Duplicate a MetaData token.
258  * 
259  * @param md what to duplicate
260  * @return duplicate meta-data container
261  */
262 struct GNUNET_CONTAINER_MetaData *
263 GNUNET_CONTAINER_meta_data_duplicate (const struct 
264                                       GNUNET_CONTAINER_MetaData *md);
265
266 /**
267  * Free meta data.
268  *
269  * @param md what to free
270  */
271 void 
272 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md);
273
274 /**
275  * Test if two MDs are equal. We consider them equal if
276  * the meta types, formats and content match (we do not
277  * include the mime types and plugins names in this
278  * consideration).
279  *
280  * @param md1 first value to check
281  * @param md2 other value to check
282  * @return GNUNET_YES if they are equal
283  */
284 int 
285 GNUNET_CONTAINER_meta_data_test_equal (const struct
286                                        GNUNET_CONTAINER_MetaData *md1,
287                                        const struct
288                                        GNUNET_CONTAINER_MetaData *md2);
289
290
291 /**
292  * Extend metadata.
293  *
294  * @param md metadata to extend
295  * @param plugin_name name of the plugin that produced this value;
296  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
297  *        used in the main libextractor library and yielding
298  *        meta data).
299  * @param type libextractor-type describing the meta data
300  * @param format basic format information about data 
301  * @param data_mime_type mime-type of data (not of the original file);
302  *        can be NULL (if mime-type is not known)
303  * @param data actual meta-data found
304  * @param data_len number of bytes in data
305  * @return GNUNET_OK on success, GNUNET_SYSERR if this entry already exists
306  *         data_mime_type and plugin_name are not considered for "exists" checks
307  */
308 int 
309 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
310                                    const char *plugin_name,
311                                    enum EXTRACTOR_MetaType type,
312                                    enum EXTRACTOR_MetaFormat format,
313                                    const char *data_mime_type,
314                                    const char *data,
315                                    size_t data_len);
316
317
318 /**
319  * Extend metadata.  Merges the meta data from the second argument
320  * into the first, discarding duplicate key-value pairs.
321  *
322  * @param md metadata to extend
323  * @param in metadata to merge
324  */
325 void 
326 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
327                                   const struct GNUNET_CONTAINER_MetaData *in);
328
329
330 /**
331  * Remove an item.
332  *
333  * @param md metadata to manipulate
334  * @param type type of the item to remove
335  * @param data specific value to remove, NULL to remove all
336  *        entries of the given type
337  * @param data_len number of bytes in data
338  * @return GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md
339  */
340 int 
341 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
342                                    enum EXTRACTOR_MetaType type,
343                                    const char *data,
344                                    size_t data_len);
345
346
347 /**
348  * Remove all items in the container.
349  *
350  * @param md metadata to manipulate
351  */
352 void 
353 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md);
354
355
356 /**
357  * Add the current time as the publication date
358  * to the meta-data.
359  *
360  * @param md metadata to modify
361  */
362 void 
363 GNUNET_CONTAINER_meta_data_add_publication_date (struct
364                                                  GNUNET_CONTAINER_MetaData
365                                                  *md);
366
367
368 /**
369  * Iterate over MD entries.
370  *
371  * @param md metadata to inspect
372  * @param iter function to call on each entry
373  * @param iter_cls closure for iterator
374  * @return number of entries
375  */
376 int GNUNET_CONTAINER_meta_data_iterate (const struct
377                                         GNUNET_CONTAINER_MetaData *md,
378                                         EXTRACTOR_MetaDataProcessor
379                                         iter, void *iter_cls);
380
381 /**
382  * Get the first MD entry of the given type.  Caller
383  * is responsible for freeing the return value.
384  * Also, only meta data items that are strings (0-terminated)
385  * are returned by this function.
386  *
387  * @param md metadata to inspect
388  * @param type type to look for
389  * @return NULL if no entry was found
390  */
391 char *
392 GNUNET_CONTAINER_meta_data_get_by_type (const struct
393                                         GNUNET_CONTAINER_MetaData *md,
394                                         enum EXTRACTOR_MetaType type);
395
396
397 /**
398  * Get the first matching MD entry of the given types. Caller is
399  * responsible for freeing the return value.  Also, only meta data
400  * items that are strings (0-terminated) are returned by this
401  * function.
402  *
403  * @param md metadata to inspect
404  * @param ... -1-terminated list of types
405  * @return NULL if we do not have any such entry,
406  *  otherwise client is responsible for freeing the value!
407  */
408 char *
409 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
410                                                GNUNET_CONTAINER_MetaData
411                                                *md, ...);
412
413 /**
414  * Get a thumbnail from the meta-data (if present).  Only matches meta
415  * data with mime type "image" and binary format.
416  *
417  * @param md metadata to inspect
418  * @param thumb will be set to the thumbnail data.  Must be
419  *        freed by the caller!
420  * @return number of bytes in thumbnail, 0 if not available
421  */
422 size_t 
423 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
424                                           GNUNET_CONTAINER_MetaData
425                                           *md, unsigned char **thumb);
426
427
428
429 /**
430  * Options for metadata serialization.
431  */
432 enum GNUNET_CONTAINER_MetaDataSerializationOptions
433 {
434   /**
435    * Serialize all of the data.
436    */
437   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
438
439   /**
440    * If not enough space is available, it is acceptable
441    * to only serialize some of the metadata.
442    */
443   GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
444
445   /**
446    * Speed is of the essence, do not allow compression.
447    */
448   GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
449 };
450
451
452 /**
453  * Serialize meta-data to target.
454  *
455  * @param md metadata to serialize
456  * @param target where to write the serialized metadata;
457  *         *target can be NULL, in which case memory is allocated
458  * @param max maximum number of bytes available
459  * @param opt is it ok to just write SOME of the
460  *        meta-data to match the size constraint,
461  *        possibly discarding some data?
462  * @return number of bytes written on success,
463  *         -1 on error (typically: not enough
464  *         space)
465  */
466 ssize_t 
467 GNUNET_CONTAINER_meta_data_serialize (const struct
468                                       GNUNET_CONTAINER_MetaData *md,
469                                       char **target, 
470                                       size_t max,
471                                       enum
472                                       GNUNET_CONTAINER_MetaDataSerializationOptions
473                                       opt);
474
475
476 /**
477  * Get the size of the full meta-data in serialized form.
478  *
479  * @param md metadata to inspect
480  * @return number of bytes needed for serialization, -1 on error
481  */
482 ssize_t 
483 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
484                                                 GNUNET_CONTAINER_MetaData
485                                                 *md);
486
487
488 /**
489  * Deserialize meta-data.  Initializes md.
490  *
491  * @param input serialized meta-data.
492  * @param size number of bytes available
493  * @return MD on success, NULL on error (i.e.
494  *         bad format)
495  */
496 struct GNUNET_CONTAINER_MetaData *
497 GNUNET_CONTAINER_meta_data_deserialize (const char *input,
498                                         size_t size);
499
500
501 /* ******************************* HashMap **************************** */
502
503 /**
504  * Opaque handle for a HashMap.
505  */
506 struct GNUNET_CONTAINER_MultiHashMap;
507
508 /**
509  * Options for storing values in the HashMap.
510  */
511 enum GNUNET_CONTAINER_MultiHashMapOption
512 {
513
514   /**
515    * If a value with the given key exists, replace it.  Note that the
516    * old value would NOT be freed by replace (the application has to
517    * make sure that this happens if required).
518    */
519   GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
520
521   /**
522    * Allow multiple values with the same key.
523    */
524   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
525
526   /**
527    * There must only be one value per key; storing a value should fail
528    * if a value under the same key already exists.
529    */
530   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
531
532   /**
533    * There must only be one value per key, but don't bother checking
534    * if a value already exists (faster than UNIQUE_ONLY; implemented
535    * just like MULTIPLE but this option documents better what is
536    * intended if UNIQUE is what is desired).
537    */
538   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
539 };
540
541
542 /**
543  * Iterator over hash map entries.
544  *
545  * @param cls closure
546  * @param key current key code
547  * @param value value in the hash map
548  * @return GNUNET_YES if we should continue to
549  *         iterate,
550  *         GNUNET_NO if not.
551  */
552 typedef int (*GNUNET_CONTAINER_HashMapIterator) (void *cls,
553                                                  const GNUNET_HashCode * key,
554                                                  void *value);
555
556
557 /**
558  * Create a multi hash map.
559  *
560  * @param len initial size (map will grow as needed)
561  * @return NULL on error
562  */
563 struct GNUNET_CONTAINER_MultiHashMap
564   *GNUNET_CONTAINER_multihashmap_create (unsigned int len);
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 GNUNET_CONTAINER_multihashmap_destroy (struct
574                                             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 *GNUNET_CONTAINER_multihashmap_get (const struct
589                                          GNUNET_CONTAINER_MultiHashMap *map,
590                                          const 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 GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
605                                           *map, const GNUNET_HashCode * key,
606                                           void *value);
607
608 /**
609  * Remove all entries for the given key from the map.
610  * Note that the values would not be "freed".
611  *
612  * @param map the map
613  * @param key identifies values to be removed
614  * @return number of values removed
615  */
616 int GNUNET_CONTAINER_multihashmap_remove_all (struct
617                                               GNUNET_CONTAINER_MultiHashMap
618                                               *map,
619                                               const 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 GNUNET_CONTAINER_multihashmap_contains (const struct
632                                             GNUNET_CONTAINER_MultiHashMap
633                                             *map,
634                                             const 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 GNUNET_CONTAINER_multihashmap_contains_value (const struct
648                                                   GNUNET_CONTAINER_MultiHashMap
649                                                   *map,
650                                                   const 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 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap
667                                        *map, const GNUNET_HashCode * key,
668                                        void *value,
669                                        enum
670                                        GNUNET_CONTAINER_MultiHashMapOption
671                                        opt);
672
673 /**
674  * Get the number of key-value pairs in the map.
675  *
676  * @param map the map
677  * @return the number of key value pairs
678  */
679 unsigned int GNUNET_CONTAINER_multihashmap_size (const struct
680                                                  GNUNET_CONTAINER_MultiHashMap
681                                                  *map);
682
683
684 /**
685  * Iterate over all entries in the map.
686  *
687  * @param map the map
688  * @param it function to call on each entry
689  * @param it_cls extra argument to it
690  * @return the number of key value pairs processed,
691  *         GNUNET_SYSERR if it aborted iteration
692  */
693 int GNUNET_CONTAINER_multihashmap_iterate (const struct
694                                            GNUNET_CONTAINER_MultiHashMap *map,
695                                            GNUNET_CONTAINER_HashMapIterator
696                                            it, void *it_cls);
697
698
699 /**
700  * Iterate over all entries in the map that match a particular key.
701  *
702  * @param map the map
703  * @param key key that the entries must correspond to
704  * @param it function to call on each entry
705  * @param it_cls extra argument to it
706  * @return the number of key value pairs processed,
707  *         GNUNET_SYSERR if it aborted iteration
708  */
709 int GNUNET_CONTAINER_multihashmap_get_multiple (const struct
710                                                 GNUNET_CONTAINER_MultiHashMap
711                                                 *map,
712                                                 const GNUNET_HashCode * key,
713                                                 GNUNET_CONTAINER_HashMapIterator
714                                                 it, void *it_cls);
715
716
717 /* ******************** doubly-linked list *************** */
718
719 /**
720  * Insert an element at the head of a DLL. Assumes that head, tail and
721  * element are structs with prev and next fields.
722  *
723  * @param head pointer to the head of the DLL
724  * @param tail pointer to the tail of the DLL
725  * @param element element to insert
726  */
727 #define GNUNET_CONTAINER_DLL_insert(head,tail,element) do { \
728   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
729   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
730   (element)->next = (head); \
731   (element)->prev = NULL; \
732   if ((tail) == NULL) \
733     (tail) = element; \
734   else \
735     (head)->prev = element; \
736   (head) = (element); } while (0)
737
738
739 /**
740  * Insert an element at the tail of a DLL. Assumes that head, tail and
741  * element are structs with prev and next fields.
742  *
743  * @param head pointer to the head of the DLL
744  * @param tail pointer to the tail of the DLL
745  * @param element element to insert
746  */
747 #define GNUNET_CONTAINER_DLL_insert_tail(head,tail,element) do { \
748   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
749   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
750   (element)->prev = (tail); \
751   (element)->next = NULL; \
752   if ((head) == NULL) \
753     (head) = element; \
754   else \
755     (tail)->next = element; \
756   (tail) = (element); } while (0)
757
758
759 /**
760  * Insert an element into a DLL after the given other element.  Insert
761  * at the head if the other element is NULL.
762  *
763  * @param head pointer to the head of the DLL
764  * @param tail pointer to the tail of the DLL
765  * @param other prior element, NULL for insertion at head of DLL
766  * @param element element to insert
767  */
768 #define GNUNET_CONTAINER_DLL_insert_after(head,tail,other,element) do { \
769   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
770   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
771   (element)->prev = (other); \
772   if (NULL == other) \
773     { \
774       (element)->next = (head); \
775       (head) = (element); \
776     } \
777   else \
778     { \
779       (element)->next = (other)->next; \
780       (other)->next = (element); \
781     } \
782   if (NULL == (element)->next) \
783     (tail) = (element); \
784   else \
785     (element)->next->prev = (element); } while (0)
786
787
788 /**
789  * Insert an element into a DLL before the given other element.  Insert
790  * at the tail if the other element is NULL.
791  *
792  * @param head pointer to the head of the DLL
793  * @param tail pointer to the tail of the DLL
794  * @param other prior element, NULL for insertion at head of DLL
795  * @param element element to insert
796  */
797 #define GNUNET_CONTAINER_DLL_insert_before(head,tail,other,element) do { \
798   GNUNET_assert ( ( (element)->prev == NULL) && ((head) != (element))); \
799   GNUNET_assert ( ( (element)->next == NULL) && ((tail) != (element))); \
800   (element)->next = (other); \
801   if (NULL == other) \
802     { \
803       (element)->prev = (tail); \
804       (tail) = (element); \
805     } \
806   else \
807     { \
808       (element)->prev = (other)->prev; \
809       (other)->prev = (element); \
810     } \
811   if (NULL == (element)->prev) \
812     (head) = (element); \
813   else \
814     (element)->prev->next = (element); } while (0)
815
816
817 /**
818  * Remove an element from a DLL. Assumes
819  * that head, tail and element are structs
820  * with prev and next fields.
821  *
822  * @param head pointer to the head of the DLL
823  * @param tail pointer to the tail of the DLL
824  * @param element element to remove
825  */
826 #define GNUNET_CONTAINER_DLL_remove(head,tail,element) do { \
827   GNUNET_assert ( ( (element)->prev != NULL) || ((head) == (element))); \
828   GNUNET_assert ( ( (element)->next != NULL) || ((tail) == (element))); \
829   if ((element)->prev == NULL) \
830     (head) = (element)->next;  \
831   else \
832     (element)->prev->next = (element)->next; \
833   if ((element)->next == NULL) \
834     (tail) = (element)->prev;  \
835   else \
836     (element)->next->prev = (element)->prev; \
837   (element)->next = NULL; \
838   (element)->prev = NULL; } while (0)
839
840
841
842 /* ******************** Heap *************** */
843
844
845 /**
846  * Cost by which elements in a heap can be ordered.
847  */
848 typedef uint64_t GNUNET_CONTAINER_HeapCostType;
849
850
851 /*
852  * Heap type, either max or min.  Hopefully makes the
853  * implementation more useful.
854  */
855 enum GNUNET_CONTAINER_HeapOrder
856 {
857   /**
858    * Heap with the maximum cost at the root.
859    */
860   GNUNET_CONTAINER_HEAP_ORDER_MAX,
861
862   /**
863    * Heap with the minimum cost at the root.
864    */
865   GNUNET_CONTAINER_HEAP_ORDER_MIN
866 };
867
868
869 /**
870  * Handle to a Heap.
871  */
872 struct GNUNET_CONTAINER_Heap;
873
874
875
876 /**
877  * Handle to a node in a heap.
878  */
879 struct GNUNET_CONTAINER_HeapNode;
880
881
882 /**
883  * Create a new heap.
884  *
885  * @param order how should the heap be sorted?
886  * @return handle to the heap
887  */
888 struct GNUNET_CONTAINER_Heap *
889 GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order);
890
891
892 /**
893  * Destroys the heap.  Only call on a heap that
894  * is already empty.
895  *
896  * @param heap heap to destroy
897  */
898 void GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap);
899
900
901 /**
902  * Get element stored at root of heap.
903  *
904  * @param heap heap to inspect
905  * @return NULL if heap is empty
906  */
907 void *
908 GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap);
909
910
911 /**
912  * Get the current size of the heap
913  *
914  * @param heap the heap to get the size of
915  * @return number of elements stored
916  */
917 unsigned int
918 GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap);
919
920
921 /**
922  * Get the current cost of the node
923  *
924  * @param node the node to get the cost of
925  * @return cost of the node
926  */
927 GNUNET_CONTAINER_HeapCostType
928 GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode *node);
929
930 /**
931  * Iterator for heap
932  *
933  * @param cls closure
934  * @param node internal node of the heap
935  * @param element value stored at the node
936  * @param cost cost associated with the node
937  * @return GNUNET_YES if we should continue to iterate,
938  *         GNUNET_NO if not.
939  */
940 typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls,
941                                               struct GNUNET_CONTAINER_HeapNode *node,
942                                               void *element,
943                                               GNUNET_CONTAINER_HeapCostType cost);
944
945
946 /**
947  * Iterate over all entries in the heap.
948  *
949  * @param heap the heap
950  * @param iterator function to call on each entry
951  * @param iterator_cls closure for iterator
952  */
953 void
954 GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
955                                GNUNET_CONTAINER_HeapIterator iterator,
956                                void *iterator_cls);
957
958
959 /**
960  * Return a *uniform* random element from the heap.  Choose a random
961  * number between 0 and heap size and then walk directly to it.
962  * This cost can be between 0 and n, amortized cost of logN.
963  *
964  * @param heap heap to choose random element from
965  * @param max how many nodes from the heap to choose from
966  *
967  * @return data stored at the chosen random node,
968  *         NULL if the heap is empty.
969  *
970  */
971 void *
972 GNUNET_CONTAINER_heap_get_random (struct GNUNET_CONTAINER_Heap *heap, 
973                                   uint32_t max);
974
975
976 /**
977  * Perform a random walk of the tree.  The walk is biased
978  * towards elements closer to the root of the tree (since
979  * each walk starts at the root and ends at a random leaf).
980  * The heap internally tracks the current position of the
981  * walk.
982  *
983  * @param heap heap to walk
984  * @return data stored at the next random node in the walk;
985  *         NULL if the tree is empty.
986  */
987 void *
988 GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap);
989
990
991 /**
992  * Inserts a new element into the heap.
993  *
994  * @param heap heap to modify
995  * @param element element to insert
996  * @param cost cost for the element
997  * @return node for the new element
998  */
999 struct GNUNET_CONTAINER_HeapNode *
1000 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
1001                               void *element,
1002                               GNUNET_CONTAINER_HeapCostType cost);
1003
1004
1005 /**
1006  * Remove root of the heap.
1007  *
1008  * @param heap heap to modify
1009  * @return element data stored at the root node
1010  */
1011 void *
1012 GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
1013
1014
1015 /**
1016  * Removes a node from the heap.
1017  * 
1018  * @param node node to remove
1019  * @return element data stored at the node, NULL if heap is empty
1020  */
1021 void *
1022 GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node);
1023
1024
1025 /**
1026  * Updates the cost of any node in the tree
1027  *
1028  * @param heap heap to modify
1029  * @param node node for which the cost is to be changed
1030  * @param new_cost new cost for the node
1031  */
1032 void
1033 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
1034                                    struct GNUNET_CONTAINER_HeapNode *node, 
1035                                    GNUNET_CONTAINER_HeapCostType new_cost);
1036
1037
1038 /* ******************** Singly linked list *************** */
1039
1040 /**
1041  * Possible ways for how data stored in the linked list
1042  * might be allocated.
1043  */ 
1044 enum GNUNET_CONTAINER_SListDisposition
1045   {
1046     /**
1047      * Single-linked list must copy the buffer.
1048      */
1049     GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT = 0,
1050
1051     /**
1052      * Data is static, no need to copy or free.
1053      */
1054     GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC = 2,
1055
1056     /**
1057      * Data is dynamic, do not copy but free when done.
1058      */
1059     GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC = 4
1060   };
1061
1062
1063
1064 /**
1065  * Handle to a singly linked list  
1066  */
1067 struct GNUNET_CONTAINER_SList;
1068
1069 /**
1070  * Handle to a singly linked list iterator 
1071  */
1072 struct GNUNET_CONTAINER_SList_Iterator;
1073
1074
1075 /**
1076  * Add a new element to the list
1077  * @param l list
1078  * @param disp memory disposition
1079  * @param buf payload buffer
1080  * @param len length of the buffer
1081  */
1082 void GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, 
1083                                  enum GNUNET_CONTAINER_SListDisposition disp,
1084                                  const void *buf, size_t len);
1085
1086
1087 /**
1088  * Add a new element to the end of the list
1089  * @param l list
1090  * @param disp memory disposition
1091  * @param buf payload buffer
1092  * @param len length of the buffer
1093  */
1094 void GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
1095                                  enum GNUNET_CONTAINER_SListDisposition disp,
1096                                  const void *buf, size_t len);
1097
1098
1099 /**
1100  * Append a singly linked list to another
1101  * @param dst list to append to
1102  * @param src source
1103  */
1104 void
1105 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, struct GNUNET_CONTAINER_SList *src);
1106
1107
1108 /**
1109  * Create a new singly linked list
1110  * @return the new list
1111  */
1112 struct GNUNET_CONTAINER_SList *GNUNET_CONTAINER_slist_create (void);
1113
1114
1115 /**
1116  * Destroy a singly linked list
1117  * @param l the list to be destroyed
1118  */
1119 void GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l);
1120
1121
1122 /**
1123  * Return the beginning of a list
1124  *
1125  * @param l list
1126  * @return iterator pointing to the beginning, free using "GNUNET_free"
1127  */
1128 struct GNUNET_CONTAINER_SList_Iterator *
1129 GNUNET_CONTAINER_slist_begin(struct GNUNET_CONTAINER_SList *l);
1130
1131
1132 /**
1133  * Clear a list
1134  *
1135  * @param l list
1136  */
1137 void GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l);
1138
1139
1140 /**
1141  * Check if a list contains a certain element
1142  * @param l list
1143  * @param buf payload buffer to find
1144  * @param len length of the payload (number of bytes in buf)
1145  */
1146 int GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, const void *buf, size_t len);
1147
1148
1149 /**
1150  * Count the elements of a list
1151  * @param l list
1152  * @return number of elements in the list
1153  */
1154 int GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l);
1155
1156
1157 /**
1158  * Remove an element from the list
1159  * @param i iterator that points to the element to be removed
1160  */
1161 void GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i);
1162
1163
1164 /**
1165  * Insert an element into a list at a specific position
1166  * @param before where to insert the new element
1167  * @param disp memory disposition
1168  * @param buf payload buffer
1169  * @param len length of the payload
1170  */
1171 void GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
1172                                     enum GNUNET_CONTAINER_SListDisposition disp,
1173                                     const void *buf, 
1174                                     size_t len);
1175
1176
1177 /**
1178  * Advance an iterator to the next element
1179  * @param i iterator
1180  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
1181  */
1182 int GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i);
1183
1184
1185 /**
1186  * Check if an iterator points beyond the end of a list
1187  * @param i iterator
1188  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
1189  *         points to a valid element
1190  */
1191 int GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i);
1192
1193
1194 /**
1195  * Retrieve the element at a specific position in a list
1196  *
1197  * @param i iterator
1198  * @param len set to the payload length
1199  * @return payload
1200  */
1201 const void *
1202 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, 
1203                             size_t *len);
1204
1205
1206 /**
1207  * Release an iterator
1208  * @param i iterator
1209  */
1210 void GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i);
1211
1212
1213 #if 0                           /* keep Emacsens' auto-indent happy */
1214 {
1215 #endif
1216 #ifdef __cplusplus
1217 }
1218 #endif
1219
1220
1221 /* ifndef GNUNET_CONTAINER_LIB_H */
1222 #endif
1223 /* end of gnunet_container_lib.h */