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