{GPL, other}: Relicense to MIT license.
[collectd.git] / src / utils_avltree.h
1 /**
2  * collectd - src/utils_avltree.h
3  * Copyright (C) 2006,2007  Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #ifndef UTILS_AVLTREE_H
28 #define UTILS_AVLTREE_H 1
29
30 struct c_avl_tree_s;
31 typedef struct c_avl_tree_s c_avl_tree_t;
32
33 struct c_avl_iterator_s;
34 typedef struct c_avl_iterator_s c_avl_iterator_t;
35
36 /*
37  * NAME
38  *   c_avl_create
39  *
40  * DESCRIPTION
41  *   Allocates a new AVL-tree.
42  *
43  * PARAMETERS
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.
50  *
51  * RETURN VALUE
52  *   A c_avl_tree_t-pointer upon success or NULL upon failure.
53  */
54 c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *));
55
56
57 /*
58  * NAME
59  *   c_avl_destroy
60  *
61  * DESCRIPTION
62  *   Deallocates an AVL-tree. Stored value- and key-pointer are lost, but of
63  *   course not freed.
64  */
65 void c_avl_destroy (c_avl_tree_t *t);
66
67 /*
68  * NAME
69  *   c_avl_insert
70  *
71  * DESCRIPTION
72  *   Stores the key-value-pair in the AVL-tree pointed to by `t'.
73  *
74  * PARAMETERS
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.
82  *
83  * RETURN VALUE
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.
86  */
87 int c_avl_insert (c_avl_tree_t *t, void *key, void *value);
88
89 /*
90  * NAME
91  *   c_avl_remove
92  *
93  * DESCRIPTION
94  *   Removes a key-value-pair from the tree t. The stored key and value may be
95  *   returned in `rkey' and `rvalue'.
96  *
97  * PARAMETERS
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.
106  *
107  * RETURN VALUE
108  *   Zero upon success or non-zero if the key isn't found in the tree.
109  */
110 int c_avl_remove (c_avl_tree_t *t, const void *key, void **rkey, void **rvalue);
111
112 /*
113  * NAME
114  *   c_avl_get
115  *
116  * DESCRIPTION
117  *   Retrieve the `value' belonging to `key'.
118  *
119  * PARAMETERS
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.
123  *
124  * RETURN VALUE
125  *   Zero upon success or non-zero if the key isn't found in the tree.
126  */
127 int c_avl_get (c_avl_tree_t *t, const void *key, void **value);
128
129 /*
130  * NAME
131  *   c_avl_pick
132  *
133  * DESCRIPTION
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.
138  *
139  * PARAMETERS
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.
143  *
144  * RETURN VALUE
145  *   Zero upon success or non-zero if the tree is empty or key or value is
146  *   NULL.
147  */
148 int c_avl_pick (c_avl_tree_t *t, void **key, void **value);
149
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);
154
155 /*
156  * NAME
157  *   c_avl_size
158  *
159  * DESCRIPTION
160  *   Return the size (number of nodes) of the specified tree.
161  *
162  * PARAMETERS
163  *   `t'        AVL-tree to get the size of.
164  *
165  * RETURN VALUE
166  *   Number of nodes in the tree, 0 if the tree is empty or NULL.
167  */
168 int c_avl_size (c_avl_tree_t *t);
169
170 #endif /* UTILS_AVLTREE_H */