2 * collectd - src/utils_avltree.h
3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
23 #ifndef UTILS_AVLTREE_H
24 #define UTILS_AVLTREE_H 1
27 typedef struct avl_tree_s avl_tree_t;
29 struct avl_iterator_s;
30 typedef struct avl_iterator_s avl_iterator_t;
37 * Allocates a new AVL-tree.
40 * `compare' The function-pointer `compare' is used to compare two keys. It
41 * has to return less than zero if it's first argument is smaller
42 * then the second argument, more than zero if the first argument
43 * is bigger than the second argument and zero if they are equal.
44 * If your keys are char-pointers, you can use the `strcmp'
45 * function from the libc here.
48 * A avl_tree_t-pointer upon success or NULL upon failure.
50 avl_tree_t *avl_create (int (*compare) (const void *, const void *));
58 * Deallocates an AVL-tree. Stored value- and key-pointer are lost, but of
61 void avl_destroy (avl_tree_t *t);
68 * Stores the key-value-pair in the AVL-tree pointed to by `t'.
71 * `t' AVL-tree to store the data in.
72 * `key' Key used to store the value under. This is used to get back to
74 * `value' Value to be stored.
77 * Zero upon success and non-zero upon failure and if the key is already
80 int avl_insert (avl_tree_t *t, void *key, void *value);
87 * Removes a key-value-pair from the tree t. The stored key and value may be
88 * returned in `rkey' and `rvalue'.
91 * `t' AVL-tree to remove key-value-pair from.
92 * `key' Key to identify the entry.
93 * `rkey' Pointer to a pointer in which to store the key. May be NULL.
94 * `rvalue' Pointer to a pointer in which to store the value. May be NULL.
97 * Zero upon success or non-zero if the key isn't found in the tree.
99 int avl_remove (avl_tree_t *t, const void *key, void **rkey, void **rvalue);
106 * Retrieve the `value' belonging to `key'.
109 * `t' AVL-tree to get the value from.
110 * `key' Key to identify the entry.
111 * `value' Pointer to a pointer in which to store the value. May be NULL.
114 * Zero upon success or non-zero if the key isn't found in the tree.
116 int avl_get (avl_tree_t *t, const void *key, void **value);
123 * Remove a (pseudo-)random element from the tree and return it's `key' and
124 * `value'. Entries are not returned in any particular order. This function
125 * is intended for cache-flushes that don't care about the order but simply
126 * want to remove all elements, one at a time.
129 * `t' AVL-tree to get the value from.
130 * `key' Pointer to a pointer in which to store the key.
131 * `value' Pointer to a pointer in which to store the value.
134 * Zero upon success or non-zero if the tree is empty or key or value is
137 int avl_pick (avl_tree_t *t, void **key, void **value);
139 avl_iterator_t *avl_get_iterator (avl_tree_t *t);
140 int avl_iterator_next (avl_iterator_t *iter, void **key, void **value);
141 int avl_iterator_prev (avl_iterator_t *iter, void **key, void **value);
142 void avl_iterator_destroy (avl_iterator_t *iter);
144 #endif /* UTILS_AVLTREE_H */