2 * collectd - src/utils_avltree.h
3 * Copyright (C) 2006,2007 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>
27 #ifndef UTILS_AVLTREE_H
28 #define UTILS_AVLTREE_H 1
31 typedef struct c_avl_tree_s c_avl_tree_t;
33 struct c_avl_iterator_s;
34 typedef struct c_avl_iterator_s c_avl_iterator_t;
41 * Allocates a new AVL-tree.
44 * `compare' The function-pointer `compare' is used to compare two keys. It
45 * has to return less than zero if it's first argument is smaller
46 * then the second argument, more than zero if the first argument
47 * is bigger than the second argument and zero if they are equal.
48 * If your keys are char-pointers, you can use the `strcmp'
49 * function from the libc here.
52 * A c_avl_tree_t-pointer upon success or NULL upon failure.
54 c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *));
62 * Deallocates an AVL-tree. Stored value- and key-pointer are lost, but of
65 void c_avl_destroy (c_avl_tree_t *t);
72 * Stores the key-value-pair in the AVL-tree pointed to by `t'.
75 * `t' AVL-tree to store the data in.
76 * `key' Key used to store the value under. This is used to get back to
77 * the value again. The pointer is stored in an internal structure
78 * and _not_ copied. So the memory pointed to may _not_ be freed
79 * before this entry is removed. You can use the `rkey' argument
80 * to `avl_remove' to get the original pointer back and free it.
81 * `value' Value to be stored.
84 * Zero upon success, non-zero otherwise. It's less than zero if an error
85 * occurred or greater than zero if the key is already stored in the tree.
87 int c_avl_insert (c_avl_tree_t *t, void *key, void *value);
94 * Removes a key-value-pair from the tree t. The stored key and value may be
95 * returned in `rkey' and `rvalue'.
98 * `t' AVL-tree to remove key-value-pair from.
99 * `key' Key to identify the entry.
100 * `rkey' Pointer to a pointer in which to store the key. May be NULL.
101 * Since the `key' pointer is not copied when creating an entry,
102 * the pointer may not be available anymore from outside the tree.
103 * You can use this argument to get the actual pointer back and
104 * free the memory pointed to by it.
105 * `rvalue' Pointer to a pointer in which to store the value. May be NULL.
108 * Zero upon success or non-zero if the key isn't found in the tree.
110 int c_avl_remove (c_avl_tree_t *t, const void *key, void **rkey, void **rvalue);
117 * Retrieve the `value' belonging to `key'.
120 * `t' AVL-tree to get the value from.
121 * `key' Key to identify the entry.
122 * `value' Pointer to a pointer in which to store the value. May be NULL.
125 * Zero upon success or non-zero if the key isn't found in the tree.
127 int c_avl_get (c_avl_tree_t *t, const void *key, void **value);
134 * Remove a (pseudo-)random element from the tree and return it's `key' and
135 * `value'. Entries are not returned in any particular order. This function
136 * is intended for cache-flushes that don't care about the order but simply
137 * want to remove all elements, one at a time.
140 * `t' AVL-tree to get the value from.
141 * `key' Pointer to a pointer in which to store the key.
142 * `value' Pointer to a pointer in which to store the value.
145 * Zero upon success or non-zero if the tree is empty or key or value is
148 int c_avl_pick (c_avl_tree_t *t, void **key, void **value);
150 c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t);
151 int c_avl_iterator_next (c_avl_iterator_t *iter, void **key, void **value);
152 int c_avl_iterator_prev (c_avl_iterator_t *iter, void **key, void **value);
153 void c_avl_iterator_destroy (c_avl_iterator_t *iter);
160 * Return the size (number of nodes) of the specified tree.
163 * `t' AVL-tree to get the size of.
166 * Number of nodes in the tree, 0 if the tree is empty or NULL.
168 int c_avl_size (c_avl_tree_t *t);
170 #endif /* UTILS_AVLTREE_H */