improving documentation
[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 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 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44
45 /* ******************* bloomfilter ***************** */
46
47 /**
48  * @brief bloomfilter representation (opaque)
49  */
50 struct GNUNET_CONTAINER_BloomFilter;
51
52 /**
53  * Iterator over HashCodes.
54  *
55  * @param cls closure
56  * @param next set to the next hash code
57  * @return GNUNET_YES if next was updated
58  *         GNUNET_NO if there are no more entries
59  */
60 typedef int (*GNUNET_HashCodeIterator) (void *cls,
61                                         GNUNET_HashCode * next);
62
63 /**
64  * Load a bloom-filter from a file.
65  *
66  * @param filename the name of the file (or the prefix)
67  * @param size the size of the bloom-filter (number of
68  *        bytes of storage space to use)
69  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
70  *        element (number of bits set per element in the set)
71  * @return the bloomfilter
72  */
73 struct GNUNET_CONTAINER_BloomFilter *
74 GNUNET_CONTAINER_bloomfilter_load (const
75                                    char
76                                    *filename,
77                                    size_t
78                                    size,
79                                    unsigned
80                                    int
81                                    k);
82
83 /**
84  * Create a bloom filter from raw bits.
85  *
86  * @param data the raw bits in memory (maybe NULL,
87  *        in which case all bits should be considered
88  *        to be zero).
89  * @param size the size of the bloom-filter (number of
90  *        bytes of storage space to use); also size of data
91  *        -- unless data is NULL.  Must be a power of 2.
92  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
93  *        element (number of bits set per element in the set)
94  * @return the bloomfilter
95  */
96 struct GNUNET_CONTAINER_BloomFilter *
97 GNUNET_CONTAINER_bloomfilter_init (const
98                                    char
99                                    *data,
100                                    size_t
101                                    size,
102                                    unsigned
103                                    int
104                                    k);
105
106 /**
107  * Copy the raw data of this bloomfilter into
108  * the given data array.
109  *
110  * @param data where to write the data
111  * @param size the size of the given data array
112  * @return GNUNET_SYSERR if the data array of the wrong size
113  */
114 int GNUNET_CONTAINER_bloomfilter_get_raw_data (struct
115                                                GNUNET_CONTAINER_BloomFilter
116                                                *bf, char *data,
117                                                size_t size);
118
119 /**
120  * Test if an element is in the filter.
121  * @param e the element
122  * @param bf the filter
123  * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
124  */
125 int GNUNET_CONTAINER_bloomfilter_test (struct GNUNET_CONTAINER_BloomFilter
126                                        *bf, const GNUNET_HashCode * e);
127
128 /**
129  * Add an element to the filter
130  * @param bf the filter
131  * @param e the element
132  */
133 void GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter
134                                        *bf, const GNUNET_HashCode * e);
135
136 /**
137  * Remove an element from the filter.
138  * @param bf the filter
139  * @param e the element to remove
140  */
141 void GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter
142                                           *bf, const GNUNET_HashCode * e);
143
144 /**
145  * Free the space associcated with a filter
146  * in memory, flush to drive if needed (do not
147  * free the space on the drive)
148  * @param bf the filter
149  */
150 void GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter
151                                         *bf);
152
153 /**
154  * Reset a bloom filter to empty.
155  * @param bf the filter
156  */
157 void GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter
158                                          *bf);
159
160 /**
161  * Or the entries of the given raw data array with the
162  * data of the given bloom filter.  Assumes that
163  * the size of the data array and the current filter
164  * match.
165  *
166  * @param bf the filter
167  * @param data data to OR-in
168  * @param size size of data
169  * @return GNUNET_OK on success
170  */
171 int GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
172                                      const char *data, size_t size);
173
174 /**
175  * Resize a bloom filter.  Note that this operation
176  * is pretty costly.  Essentially, the bloom filter
177  * needs to be completely re-build.
178  *
179  * @param bf the filter
180  * @param iterator an iterator over all elements stored in the BF
181  * @param iterator_cls closure for iterator
182  * @param size the new size for the filter
183  * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
184  */
185 void GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter
186                                           *bf,
187                                           GNUNET_HashCodeIterator iterator,
188                                           void *iterator_cls,
189                                           size_t size, unsigned int k);
190
191 /* ****************** metadata ******************* */
192
193 /**
194  * Meta data to associate with a file, directory or namespace.
195  */
196 struct GNUNET_CONTAINER_MetaData;
197
198 /**
199  * Iterator over meta data.
200  *
201  * @param cls closure
202  * @param type type of the meta data
203  * @param data value of the meta data
204  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
205  */
206 typedef int (*GNUNET_CONTAINER_MetaDataProcessor) (void *cls,
207                                                    EXTRACTOR_KeywordType type,
208                                                    const char *data);
209
210 /**
211  * Create a fresh MetaData token.
212  * 
213  * @return empty meta-data container
214  */
215 struct GNUNET_CONTAINER_MetaData *GNUNET_CONTAINER_meta_data_create (void);
216
217 /**
218  * Duplicate a MetaData token.
219  * 
220  * @param meta what to duplicate
221  * @return duplicate meta-data container
222  */
223 struct GNUNET_CONTAINER_MetaData *GNUNET_CONTAINER_meta_data_duplicate (const
224                                                                         struct
225                                                                         GNUNET_CONTAINER_MetaData
226                                                                         *meta);
227
228 /**
229  * Free meta data.
230  *
231  * @param md what to free
232  */
233 void GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData
234                                          *md);
235
236 /**
237  * Test if two MDs are equal.
238  *
239  * @param md1 first value to check
240  * @param md2 other value to check
241  * @return GNUNET_YES if they are equal
242  */
243 int GNUNET_CONTAINER_meta_data_test_equal (const struct
244                                            GNUNET_CONTAINER_MetaData *md1,
245                                            const struct
246                                            GNUNET_CONTAINER_MetaData *md2);
247
248
249 /**
250  * Extend metadata.
251  *
252  * @param md metadata to extend
253  * @param type type of the new entry
254  * @param data value for the entry
255  * @return GNUNET_OK on success, GNUNET_SYSERR if this entry already exists
256  */
257 int GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
258                                        EXTRACTOR_KeywordType type,
259                                        const char *data);
260
261 /**
262  * Remove an item.
263  *
264  * @param type type of the item to remove
265  * @param data specific value to remove, NULL to remove all
266  *        entries of the given type
267  * @return GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md
268  */
269 int GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
270                                        EXTRACTOR_KeywordType type,
271                                        const char *data);
272
273 /**
274  * Add the current time as the publication date
275  * to the meta-data.
276  *
277  * @param md metadata to modify
278  */
279 void GNUNET_CONTAINER_meta_data_add_publication_date (struct
280                                                       GNUNET_CONTAINER_MetaData
281                                                       *md);
282
283 /**
284  * Iterate over MD entries, excluding thumbnails.
285  *
286  * @param md metadata to inspect
287  * @param iter function to call on each entry
288  * @param iter_cls closure for iterator
289  * @return number of entries
290  */
291 int GNUNET_CONTAINER_meta_data_get_contents (const struct
292                                              GNUNET_CONTAINER_MetaData *md,
293                                              GNUNET_CONTAINER_MetaDataProcessor
294                                              iter, void *iter_cls);
295
296 /**
297  * Get the first MD entry of the given type.
298  *
299  * @param md metadata to inspect
300  * @param type type to look for
301  * @return NULL if we do not have any such entry,
302  *  otherwise client is responsible for freeing the value!
303  */
304 char *GNUNET_CONTAINER_meta_data_get_by_type (const struct
305                                              GNUNET_CONTAINER_MetaData *md,
306                                               EXTRACTOR_KeywordType type);
307
308 /**
309  * Get the first matching MD entry of the given types.
310  *
311  * @param md metadata to inspect
312  * @param ... -1-terminated list of types
313  * @return NULL if we do not have any such entry,
314  *  otherwise client is responsible for freeing the value!
315  */
316 char *GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
317                                                      GNUNET_CONTAINER_MetaData
318                                                      *md, ...);
319
320 /**
321  * Get a thumbnail from the meta-data (if present).
322  *
323  * @param md metadata to inspect
324  * @param thumb will be set to the thumbnail data.  Must be
325  *        freed by the caller!
326  * @return number of bytes in thumbnail, 0 if not available
327  */
328 size_t GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
329                                                  GNUNET_CONTAINER_MetaData
330                                                  *md, unsigned char **thumb);
331
332 /**
333  * Extract meta-data from a file.
334  *
335  * @param md metadata to set
336  * @param filename name of file to inspect
337  * @param extractors plugins to use
338  * @return GNUNET_SYSERR on error, otherwise the number
339  *   of meta-data items obtained
340  */
341 int GNUNET_CONTAINER_meta_data_extract_from_file (struct
342                                                   GNUNET_CONTAINER_MetaData
343                                                   *md, const char *filename,
344                                                   EXTRACTOR_ExtractorList *
345                                                   extractors);
346
347
348 /**
349  * Options for metadata serialization.
350  */
351 enum GNUNET_CONTAINER_MetaDataSerializationOptions
352 {
353   /**
354    * Serialize all of the data.
355    */
356   GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL = 0,
357
358   /**
359    * If not enough space is available, it is acceptable
360    * to only serialize some of the metadata.
361    */
362   GNUNET_CONTAINER_META_DATA_SERIALIZE_PART = 1,
363
364   /**
365    * Speed is of the essence, do not allow compression.
366    */
367   GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS = 2
368 };
369
370
371 /**
372  * Serialize meta-data to target.
373  *
374  * @param md metadata to serialize
375  * @param target where to write the serialized metadata
376  * @param size maximum number of bytes available
377  * @param opt is it ok to just write SOME of the
378  *        meta-data to match the size constraint,
379  *        possibly discarding some data?
380  * @return number of bytes written on success,
381  *         -1 on error (typically: not enough
382  *         space)
383  */
384 ssize_t GNUNET_CONTAINER_meta_data_serialize (const struct
385                                               GNUNET_CONTAINER_MetaData *md,
386                                               char *target, 
387                                               size_t size,
388                                               enum
389                                           GNUNET_CONTAINER_MetaDataSerializationOptions
390                                           opt);
391
392
393 /**
394  * Estimate (!) the size of the meta-data in
395  * serialized form.  The estimate MAY be higher
396  * than what is strictly needed.
397  *
398  * @param md metadata to inspect
399  * @param opt is it ok to just write SOME of the
400  *        meta-data to match the size constraint,
401  *        possibly discarding some data?
402  * @return number of bytes needed for serialization, -1 on error
403  */
404 ssize_t GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
405                                                         GNUNET_CONTAINER_MetaData
406                                                         *md,
407                                                         enum
408                                                         GNUNET_CONTAINER_MetaDataSerializationOptions
409                                                         opt);
410
411 /**
412  * Deserialize meta-data.  Initializes md.
413  *
414  * @param input serialized meta-data.
415  * @param size number of bytes available
416  * @return MD on success, NULL on error (i.e.
417  *         bad format)
418  */
419 struct GNUNET_CONTAINER_MetaData
420   *GNUNET_CONTAINER_meta_data_deserialize (const char *input,
421                                            size_t size);
422
423 /**
424  * Does the meta-data claim that this is a directory?
425  * Checks if the mime-type is that of a GNUnet directory.
426  *
427  * @param md metadata to inspect
428  * @return GNUNET_YES if it is, GNUNET_NO if it is not, GNUNET_SYSERR if
429  *  we have no mime-type information (treat as 'GNUNET_NO')
430  */
431 int GNUNET_CONTAINER_meta_data_test_for_directory (const struct
432                                                    GNUNET_CONTAINER_MetaData
433                                                    *md);
434
435
436 /* ******************************* HashMap **************************** */
437
438 /**
439  * Opaque handle for a HashMap.
440  */
441 struct GNUNET_CONTAINER_MultiHashMap;
442
443 /**
444  * Options for storing values in the HashMap.
445  */
446 enum GNUNET_CONTAINER_MultiHashMapOption
447 {
448   /**
449    * If a value with the given key exists, replace it.
450    * Note that the old value would NOT be freed
451    * by replace (the application has to make sure that
452    * this happens if required).
453    */
454   GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE,
455
456   /**
457    * Allow multiple values with the same key.
458    */
459   GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE,
460
461   /**
462    * There must only be one value per key; storing
463    * a value should fail if a value under the same
464    * key already exists.
465    */
466   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY,
467
468   /**
469    * There must only be one value per key, but don't
470    * bother checking if a value already exists
471    * (faster than UNIQUE_ONLY; implemented just like
472    * MULTIPLE but this option documents better what
473    * is intended if UNIQUE is what is desired).
474    */
475   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
476 };
477
478
479 /**
480  * Iterator over HashCodes.
481  *
482  * @param cls closure
483  * @param key current key code
484  * @param value value in the hash map
485  * @return GNUNET_YES if we should continue to
486  *         iterate,
487  *         GNUNET_NO if not.
488  */
489 typedef int (*GNUNET_CONTAINER_HashMapIterator) (void *cls,
490                                                  const GNUNET_HashCode * key,
491                                                  void *value);
492
493
494 /**
495  * Create a multi hash map.
496  *
497  * @param len initial size (map will grow as needed)
498  * @return NULL on error
499  */
500 struct GNUNET_CONTAINER_MultiHashMap
501   *GNUNET_CONTAINER_multihashmap_create (unsigned int len);
502
503 /**
504  * Destroy a hash map.  Will not free any values
505  * stored in the hash map!
506  *
507  * @param map the map
508  */
509 void GNUNET_CONTAINER_multihashmap_destroy (struct
510                                             GNUNET_CONTAINER_MultiHashMap
511                                             *map);
512
513 /**
514  * Given a key find a value in the
515  * map matching the key.
516  *
517  * @param map the map
518  * @param key what to look for
519  * @return NULL if no value was found; note that
520  *   this is indistinguishable from values that just
521  *   happen to be NULL; use "contains" to test for
522  *   key-value pairs with value NULL
523  */
524 void *GNUNET_CONTAINER_multihashmap_get (const struct
525                                          GNUNET_CONTAINER_MultiHashMap *map,
526                                          const GNUNET_HashCode * key);
527
528 /**
529  * Remove the given key-value pair from the map.
530  * Note that if the key-value pair is in the map
531  * multiple times, only one of the pairs will be
532  * removed.
533  *
534  * @param map the map
535  * @param key key of the key-value pair
536  * @param value value of the key-value pair
537  * @return GNUNET_YES on success, GNUNET_NO if the key-value pair
538  *  is not in the map
539  */
540 int GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
541                                           *map, const GNUNET_HashCode * key,
542                                           void *value);
543
544 /**
545  * Remove all entries for the given key from the map.
546  * Note that the values would not be "freed".
547  *
548  * @param map the map
549  * @param key identifies values to be removed
550  * @return number of values removed
551  */
552 int GNUNET_CONTAINER_multihashmap_remove_all (struct
553                                               GNUNET_CONTAINER_MultiHashMap
554                                               *map,
555                                               const GNUNET_HashCode * key);
556
557 /**
558  * Check if the map contains any value under the given
559  * key (including values that are NULL).
560  *
561  * @param map the map
562  * @param key the key to test if a value exists for it
563  * @return GNUNET_YES if such a value exists,
564  *         GNUNET_NO if not
565  */
566 int GNUNET_CONTAINER_multihashmap_contains (const struct
567                                             GNUNET_CONTAINER_MultiHashMap
568                                             *map,
569                                             const GNUNET_HashCode * key);
570
571 /**
572  * Store a key-value pair in the map.
573  *
574  * @param map the map
575  * @param key key to use
576  * @param value value to use
577  * @param opt options for put
578  * @return GNUNET_OK on success,
579  *         GNUNET_NO if a value was replaced (with REPLACE)
580  *         GNUNET_SYSERR if UNIQUE_ONLY was the option and the
581  *                       value already exists
582  */
583 int GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap
584                                        *map, const GNUNET_HashCode * key,
585                                        void *value,
586                                        enum
587                                        GNUNET_CONTAINER_MultiHashMapOption
588                                        opt);
589
590 /**
591  * Get the number of key-value pairs in the map.
592  *
593  * @param map the map
594  * @return the number of key value pairs
595  */
596 unsigned int GNUNET_CONTAINER_multihashmap_size (const struct
597                                                  GNUNET_CONTAINER_MultiHashMap
598                                                  *map);
599
600
601 /**
602  * Iterate over all entries in the map.
603  *
604  * @param map the map
605  * @param iterator function to call on each entry
606  * @param cls extra argument to it
607  * @return the number of key value pairs processed,
608  *         GNUNET_SYSERR if it aborted iteration
609  */
610 int GNUNET_CONTAINER_multihashmap_iterate (const struct
611                                            GNUNET_CONTAINER_MultiHashMap *map,
612                                            GNUNET_CONTAINER_HashMapIterator
613                                            iterator, void *cls);
614
615 /**
616  * Iterate over all entries in the map
617  * that match a particular key.
618  *
619  * @param map the map
620  * @param key key that the entries must correspond to
621  * @param iterator function to call on each entry
622  * @param cls extra argument to it
623  * @return the number of key value pairs processed,
624  *         GNUNET_SYSERR if it aborted iteration
625  */
626 int GNUNET_CONTAINER_multihashmap_get_multiple (const struct
627                                                 GNUNET_CONTAINER_MultiHashMap
628                                                 *map,
629                                                 const GNUNET_HashCode * key,
630                                                 GNUNET_CONTAINER_HashMapIterator
631                                                 iterator, void *cls);
632 /**
633  * Returns the stored value of a random non-null entry
634  * in the hash table.  Returns only the first value, does
635  * not go inside bucket linked list (yet).  Runs with a
636  * worst case time of N, so it's not efficient in any way
637  * shape or form!!!!.
638  */
639 void *GNUNET_CONTAINER_multihashmap_get_random (const struct
640                                                 GNUNET_CONTAINER_MultiHashMap
641                                                 *map);
642
643
644
645
646 /* ******************** doubly-linked list *************** */
647
648 /**
649  * Insert an element into a DLL. Assumes
650  * that head, tail and element are structs
651  * with prev and next fields.
652  *
653  * @param head pointer to the head of the DLL
654  * @param tail pointer to the tail of the DLL
655  * @param element element to insert
656  */
657 #define GNUNET_CONTAINER_DLL_insert(head,tail,element) \
658   (element)->next = (head); \
659   (element)->prev = NULL; \
660   if ((tail) == NULL) \
661     (tail) = element; \
662   else \
663     (head)->prev = element; \
664   (head) = (element);
665
666 /**
667  * Insert an element into a DLL after the given other
668  * element.  Insert at the head if the other
669  * element is NULL.
670  *
671  * @param head pointer to the head of the DLL
672  * @param tail pointer to the tail of the DLL
673  * @param other prior element, NULL for insertion at head of DLL
674  * @param element element to insert
675  */
676 #define GNUNET_CONTAINER_DLL_insert_after(head,tail,other,element) \
677   (element)->prev = (other); \
678   if (NULL == other) \
679     { \
680       (element)->next = (head); \
681       (head) = (element); \
682     } \
683   else \
684     { \
685       (element)->next = (other)->next; \
686       (other)->next = (element); \
687     } \
688   if (NULL == (element)->next) \
689     (tail) = (element); \
690   else \
691     (element)->next->prev = (element);
692
693
694
695
696 /**
697  * Remove an element from a DLL. Assumes
698  * that head, tail and element are structs
699  * with prev and next fields.
700  *
701  * @param head pointer to the head of the DLL
702  * @param tail pointer to the tail of the DLL
703  * @param element element to remove
704  */
705 #define GNUNET_CONTAINER_DLL_remove(head,tail,element) \
706   if ((element)->prev == NULL) \
707     (head) = (element)->next;  \
708   else \
709     (element)->prev->next = (element)->next; \
710   if ((element)->next == NULL) \
711     (tail) = (element)->prev;  \
712   else \
713     (element)->next->prev = (element)->prev;
714
715
716
717 /* ******************** Heap *************** */
718
719
720 /**
721  * Cost by which elements in a heap can be ordered.
722  */
723 typedef uint64_t GNUNET_CONTAINER_HeapCost;
724
725
726 /*
727  * Heap type, either max or min.  Hopefully makes the
728  * implementation more useful.
729  */
730 enum GNUNET_CONTAINER_HeapOrder
731 {
732   /**
733    * Heap with the maximum cost at the root.
734    */
735   GNUNET_CONTAINER_HEAP_ORDER_MAX,
736
737   /**
738    * Heap with the minimum cost at the root.
739    */
740   GNUNET_CONTAINER_HEAP_ORDER_MIN
741 };
742
743
744 /**
745  * Handle to a Heap.
746  */
747 struct GNUNET_CONTAINER_Heap;
748
749 /**
750  * Create a new heap.
751  *
752  * @param type should the minimum or the maximum element be the root
753  * @return NULL on error, otherwise a fresh heap
754  */
755 struct GNUNET_CONTAINER_Heap *GNUNET_CONTAINER_heap_create (enum
756                                                             GNUNET_CONTAINER_HeapOrder
757                                                             type);
758
759
760 /**
761  * Free a heap
762  *
763  * @param h heap to free.
764  */
765 void GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *h);
766
767
768 /**
769  * Function called on elements of a heap.
770  *
771  * @param cls closure
772  * @param element obj stored in heap
773  * @param cost cost of the element
774  * @return GNUNET_YES if we should continue to iterate,
775  *         GNUNET_NO if not.
776  */
777 typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls,
778                                               void *element,
779                                               GNUNET_CONTAINER_HeapCost cost);
780
781
782 /**
783  * Iterate over all entries in the map.
784  *
785  * @param heap the heap
786  * @param iterator function to call on each entry
787  * @param iterator_cls closure for iterator
788  * @return number of items handled
789  *         GNUNET_SYSERR if iteration was aborted by iterator
790  */
791 int GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
792                                    GNUNET_CONTAINER_HeapIterator iterator,
793                                    void *iterator_cls);
794
795
796
797 /**
798  * Inserts a new item into the heap, item is always neighbor now.
799  * @param heap the heap
800  * @param element element to insert
801  * @param cost cost of the element
802  * @return FIXME
803  */
804 int
805 GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap,
806                               void *element, GNUNET_CONTAINER_HeapCost cost);
807
808
809 /**
810  * Removes root of the tree, is remove max if a max heap and remove min
811  * if a min heap, returns the data stored at the node.
812  *
813  * @param heap the heap
814  * @return NULL if the heap is empty
815  */
816 void *GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap);
817
818
819 /**
820  * Returns element stored at root of tree, doesn't effect anything
821  *
822  * @param heap the heap
823  * @return NULL if the heap is empty
824  */
825 void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap);
826
827
828 /**
829  * Removes any node from the tree based on the neighbor given, does
830  * not traverse the tree (backpointers) but may take more time due to
831  * percolation of nodes.
832  *
833  * @param heap the heap
834  * @param element the element to remove
835  * @return NULL if "element" was not found in the heap, otherwise element
836  */
837 void *GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *heap,
838                                          void *element);
839
840
841 /**
842  * Updates the cost of any node in the tree
843  *
844  * @param heap the heap
845  * @param element the element for which the cost is updated
846  * @param new_cost new cost for the element
847  * @return FIXME
848  */
849 int
850 GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
851                                    void *element,
852                                    GNUNET_CONTAINER_HeapCost new_cost);
853
854
855 /**
856  * Random walk of the tree, returns the data stored at the next random node
857  * in the walk.  Calls callee with the data, or NULL if the tree is empty
858  * or some other problem crops up.
859  *
860  * @param heap the heap
861  * @return the next element from the random walk
862  */
863 void *GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap
864                                            *heap);
865
866
867 /**
868  * Returns the current size of the heap
869  *
870  * @param heap the heap to get the size of
871  * @return number of elements in the heap
872  */
873 unsigned int
874 GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap);
875
876 /* ******************** Singly linked list *************** */
877
878 /**
879  * Possible ways for how data stored in the linked list
880  * might be allocated.
881  */ 
882 enum GNUNET_CONTAINER_SListDisposition
883   {
884     /**
885      * Single-linked list must copy the buffer.
886      */
887     GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT = 0,
888
889     /**
890      * Data is static, no need to copy or free.
891      */
892     GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC = 2,
893
894     /**
895      * Data is dynamic, do not copy but free when done.
896      */
897     GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC = 4
898   };
899
900
901
902 /**
903  * Handle to a singly linked list  
904  */
905 struct GNUNET_CONTAINER_SList;
906
907 /**
908  * Handle to a singly linked list iterator 
909  */
910 struct GNUNET_CONTAINER_SList_Iterator;
911
912
913 /**
914  * Add a new element to the list
915  * @param l list
916  * @param disp memory disposition
917  * @param buf payload buffer
918  * @param len length of the buffer
919  */
920 void GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, 
921                                  enum GNUNET_CONTAINER_SListDisposition disp,
922                                  const void *buf, size_t len);
923
924
925 /**
926  * Create a new singly linked list
927  * @return the new list
928  */
929 struct GNUNET_CONTAINER_SList *GNUNET_CONTAINER_slist_create (void);
930
931
932 /**
933  * Destroy a singly linked list
934  * @param l the list to be destroyed
935  */
936 void GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l);
937
938
939 /**
940  * Return the beginning of a list
941  *
942  * @param l list
943  * @return iterator pointing to the beginning, free using "GNUNET_free"
944  */
945 struct GNUNET_CONTAINER_SList_Iterator *
946 GNUNET_CONTAINER_slist_begin(struct GNUNET_CONTAINER_SList *l);
947
948
949 /**
950  * Clear a list
951  *
952  * @param l list
953  */
954 void GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l);
955
956
957 /**
958  * Check if a list contains a certain element
959  * @param l list
960  * @param buf payload buffer to find
961  * @param lenght of the payload
962  */
963 int GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, const void *buf, size_t len);
964
965
966 /**
967  * Count the elements of a list
968  * @param l list
969  * @return number of elements in the list
970  */
971 int GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l);
972
973
974 /**
975  * Remove an element from the list
976  * @param i iterator that points to the element to be removed
977  */
978 void GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i);
979
980
981 /**
982  * Insert an element into a list at a specific position
983  * @param before where to insert the new element
984  * @param disp memory disposition
985  * @param buf payload buffer
986  * @param len length of the payload
987  */
988 void GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
989                                     enum GNUNET_CONTAINER_SListDisposition disp,
990                                     const void *buf, 
991                                     size_t len);
992
993
994 /**
995  * Advance an iterator to the next element
996  * @param i iterator
997  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
998  */
999 int GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i);
1000
1001
1002 /**
1003  * Check if an iterator points beyond the end of a list
1004  * @param i iterator
1005  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
1006  *         points to a valid element
1007  */
1008 int GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i);
1009
1010
1011 /**
1012  * Retrieve the element at a specific position in a list
1013  *
1014  * @param i iterator
1015  * @param len set to the payload length
1016  * @return payload
1017  */
1018 const void *
1019 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, 
1020                             size_t *len);
1021
1022
1023 #if 0                           /* keep Emacsens' auto-indent happy */
1024 {
1025 #endif
1026 #ifdef __cplusplus
1027 }
1028 #endif
1029
1030
1031 /* ifndef GNUNET_CONTAINER_LIB_H */
1032 #endif
1033 /* end of gnunet_container_lib.h */