2 * collectd - src/utils_heap.h
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
28 #define UTILS_HEAP_H 1
31 typedef struct c_heap_s c_heap_t;
38 * Allocates a new heap.
41 * `compare' The function-pointer `compare' is used to compare two keys. It
42 * has to return less than zero if its first argument is smaller
43 * then the second argument, more than zero if the first argument
44 * is bigger than the second argument and zero if they are equal.
45 * If your keys are char-pointers, you can use the `strcmp'
46 * function from the libc here.
49 * A c_heap_t-pointer upon success or NULL upon failure.
51 c_heap_t *c_heap_create(int (*compare)(const void *, const void *));
58 * Deallocates a heap. Stored value- and key-pointer are lost, but of course
61 void c_heap_destroy(c_heap_t *h);
68 * Stores the key-value-pair in the heap pointed to by `h'.
71 * `h' Heap to store the data in.
72 * `ptr' Value to be stored. This is typically a pointer to a data
73 * structure. The data structure is of course *not* copied and may
74 * not be free'd before the pointer has been removed from the heap
78 * Zero upon success, non-zero otherwise. It's less than zero if an error
79 * occurred or greater than zero if the key is already stored in the tree.
81 int c_heap_insert(c_heap_t *h, void *ptr);
88 * Removes the value at the root of the heap and returns both, key and value.
91 * `h' Heap to remove key-value-pair from.
94 * The pointer passed to `c_heap_insert' or NULL if there are no more
95 * elements in the heap (or an error occurred).
97 void *c_heap_get_root(c_heap_t *h);
99 #endif /* UTILS_HEAP_H */