1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 |
|
30 /** |
|
31 * @file avl.h |
|
32 * |
|
33 * AVL tree implementation. |
|
34 * |
|
35 * This binary search tree implementation allows average O(1) insertion and |
|
36 * removal of elements (excluding binary search time). |
|
37 * |
|
38 * @author Mike Becker |
|
39 * @author Olaf Wintermann |
|
40 */ |
|
41 |
|
42 #ifndef UCX_AVL_H |
|
43 #define UCX_AVL_H |
|
44 |
|
45 #include "ucx.h" |
|
46 #include "allocator.h" |
|
47 #include <inttypes.h> |
|
48 |
|
49 #ifdef __cplusplus |
|
50 extern "C" { |
|
51 #endif |
|
52 |
|
53 /** |
|
54 * UCX AVL Node type. |
|
55 * |
|
56 * @see UcxAVLNode |
|
57 */ |
|
58 typedef struct UcxAVLNode UcxAVLNode; |
|
59 |
|
60 /** |
|
61 * UCX AVL Node. |
|
62 */ |
|
63 struct UcxAVLNode { |
|
64 /** |
|
65 * The key for this node. |
|
66 */ |
|
67 intptr_t key; |
|
68 /** |
|
69 * Data contained by this node. |
|
70 */ |
|
71 void *value; |
|
72 /** |
|
73 * The height of this (sub)-tree. |
|
74 */ |
|
75 size_t height; |
|
76 /** |
|
77 * Parent node. |
|
78 */ |
|
79 UcxAVLNode *parent; |
|
80 /** |
|
81 * Root node of left subtree. |
|
82 */ |
|
83 UcxAVLNode *left; |
|
84 /** |
|
85 * Root node of right subtree. |
|
86 */ |
|
87 UcxAVLNode *right; |
|
88 }; |
|
89 |
|
90 /** |
|
91 * UCX AVL Tree. |
|
92 */ |
|
93 typedef struct { |
|
94 /** |
|
95 * The UcxAllocator that shall be used to manage the memory for node data. |
|
96 */ |
|
97 UcxAllocator *allocator; |
|
98 /** |
|
99 * Root node of the tree. |
|
100 */ |
|
101 UcxAVLNode *root; |
|
102 /** |
|
103 * Compare function that shall be used to compare the UcxAVLNode keys. |
|
104 * @see UcxAVLNode.key |
|
105 */ |
|
106 cmp_func cmpfunc; |
|
107 /** |
|
108 * Custom user data. |
|
109 * This data will also be provided to the cmpfunc. |
|
110 */ |
|
111 void *userdata; |
|
112 } UcxAVLTree; |
|
113 |
|
114 /** |
|
115 * Initializes a new UcxAVLTree with a default allocator. |
|
116 * |
|
117 * @param cmpfunc the compare function that shall be used |
|
118 * @return a new UcxAVLTree object |
|
119 * @see ucx_avl_new_a() |
|
120 */ |
|
121 UcxAVLTree *ucx_avl_new(cmp_func cmpfunc); |
|
122 |
|
123 /** |
|
124 * Initializes a new UcxAVLTree with the specified allocator. |
|
125 * |
|
126 * The cmpfunc should be capable of comparing two keys within this AVL tree. |
|
127 * So if you want to use null terminated strings as keys, you could use the |
|
128 * ucx_strcmp() function here. |
|
129 * |
|
130 * @param cmpfunc the compare function that shall be used |
|
131 * @param allocator the UcxAllocator that shall be used |
|
132 * @return a new UcxAVLTree object |
|
133 */ |
|
134 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); |
|
135 |
|
136 /** |
|
137 * Destroys a UcxAVLTree. |
|
138 * @param tree the tree to destroy |
|
139 */ |
|
140 void ucx_avl_free(UcxAVLTree *tree); |
|
141 |
|
142 /** |
|
143 * Macro for initializing a new UcxAVLTree with the default allocator and a |
|
144 * ucx_ptrcmp() compare function. |
|
145 * |
|
146 * @return a new default UcxAVLTree object |
|
147 */ |
|
148 #define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator()) |
|
149 |
|
150 /** |
|
151 * Gets the node from the tree, that is associated with the specified key. |
|
152 * @param tree the UcxAVLTree |
|
153 * @param key the key |
|
154 * @return the node (or <code>NULL</code>, if the key is not present) |
|
155 */ |
|
156 UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key); |
|
157 |
|
158 /** |
|
159 * Gets the value from the tree, that is associated with the specified key. |
|
160 * @param tree the UcxAVLTree |
|
161 * @param key the key |
|
162 * @return the value (or <code>NULL</code>, if the key is not present) |
|
163 */ |
|
164 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); |
|
165 |
|
166 /** |
|
167 * A mode for #ucx_avl_find_node() with the same behavior as |
|
168 * #ucx_avl_get_node(). |
|
169 */ |
|
170 #define UCX_AVL_FIND_EXACT 0 |
|
171 /** |
|
172 * A mode for #ucx_avl_find_node() finding the node whose key is at least |
|
173 * as large as the specified key. |
|
174 */ |
|
175 #define UCX_AVL_FIND_LOWER_BOUNDED 1 |
|
176 /** |
|
177 * A mode for #ucx_avl_find_node() finding the node whose key is at most |
|
178 * as large as the specified key. |
|
179 */ |
|
180 #define UCX_AVL_FIND_UPPER_BOUNDED 2 |
|
181 /** |
|
182 * A mode for #ucx_avl_find_node() finding the node with a key that is as close |
|
183 * to the specified key as possible. If the key is present, the behavior is |
|
184 * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on |
|
185 * empty trees. |
|
186 */ |
|
187 #define UCX_AVL_FIND_CLOSEST 3 |
|
188 |
|
189 /** |
|
190 * Finds a node within the tree. The following modes are supported: |
|
191 * <ul> |
|
192 * <li>#UCX_AVL_FIND_EXACT: the same behavior as #ucx_avl_get_node()</li> |
|
193 * <li>#UCX_AVL_FIND_LOWER_BOUNDED: finds the node whose key is at least |
|
194 * as large as the specified key</li> |
|
195 * <li>#UCX_AVL_FIND_UPPER_BOUNDED: finds the node whose key is at most |
|
196 * as large as the specified key</li> |
|
197 * <li>#UCX_AVL_FIND_CLOSEST: finds the node with a key that is as close to |
|
198 * the specified key as possible. If the key is present, the behavior is |
|
199 * like #ucx_avl_get_node(). This mode only returns <code>NULL</code> on |
|
200 * empty trees.</li> |
|
201 * </ul> |
|
202 * |
|
203 * The distance function provided MUST agree with the compare function of |
|
204 * the AVL tree. |
|
205 * |
|
206 * @param tree the UcxAVLTree |
|
207 * @param key the key |
|
208 * @param dfnc the distance function |
|
209 * @param mode the find mode |
|
210 * @return the node (or <code>NULL</code>, if no node can be found) |
|
211 */ |
|
212 UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key, |
|
213 distance_func dfnc, int mode); |
|
214 |
|
215 /** |
|
216 * Finds a value within the tree. |
|
217 * See #ucx_avl_find_node() for details. |
|
218 * |
|
219 * @param tree the UcxAVLTree |
|
220 * @param key the key |
|
221 * @param dfnc the distance function |
|
222 * @param mode the find mode |
|
223 * @return the value (or <code>NULL</code>, if no value can be found) |
|
224 */ |
|
225 void *ucx_avl_find(UcxAVLTree *tree, intptr_t key, |
|
226 distance_func dfnc, int mode); |
|
227 |
|
228 /** |
|
229 * Puts a key/value pair into the tree. |
|
230 * |
|
231 * Attention: use this function only, if a possible old value does not need |
|
232 * to be preserved. |
|
233 * |
|
234 * @param tree the UcxAVLTree |
|
235 * @param key the key |
|
236 * @param value the new value |
|
237 * @return zero, if and only if the operation succeeded |
|
238 */ |
|
239 int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value); |
|
240 |
|
241 /** |
|
242 * Puts a key/value pair into the tree. |
|
243 * |
|
244 * This is a secure function which saves the old value to the variable pointed |
|
245 * at by oldvalue. |
|
246 * |
|
247 * @param tree the UcxAVLTree |
|
248 * @param key the key |
|
249 * @param value the new value |
|
250 * @param oldvalue optional: a pointer to the location where a possible old |
|
251 * value shall be stored |
|
252 * @return zero, if and only if the operation succeeded |
|
253 */ |
|
254 int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue); |
|
255 |
|
256 /** |
|
257 * Removes a node from the AVL tree. |
|
258 * |
|
259 * Note: the specified node is logically removed. The tree implementation |
|
260 * decides which memory area is freed. In most cases the here provided node |
|
261 * is freed, so its further use is generally undefined. |
|
262 * |
|
263 * @param tree the UcxAVLTree |
|
264 * @param node the node to remove |
|
265 * @return zero, if and only if an element has been removed |
|
266 */ |
|
267 int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node); |
|
268 |
|
269 /** |
|
270 * Removes an element from the AVL tree. |
|
271 * |
|
272 * @param tree the UcxAVLTree |
|
273 * @param key the key |
|
274 * @return zero, if and only if an element has been removed |
|
275 */ |
|
276 int ucx_avl_remove(UcxAVLTree *tree, intptr_t key); |
|
277 |
|
278 /** |
|
279 * Removes an element from the AVL tree. |
|
280 * |
|
281 * This is a secure function which saves the old key and value data from node |
|
282 * to the variables at the location of oldkey and oldvalue (if specified), so |
|
283 * they can be freed afterwards (if necessary). |
|
284 * |
|
285 * Note: the returned key in oldkey is possibly not the same as the provided |
|
286 * key for the lookup (in terms of memory location). |
|
287 * |
|
288 * @param tree the UcxAVLTree |
|
289 * @param key the key of the element to remove |
|
290 * @param oldkey optional: a pointer to the location where the old key shall be |
|
291 * stored |
|
292 * @param oldvalue optional: a pointer to the location where the old value |
|
293 * shall be stored |
|
294 * @return zero, if and only if an element has been removed |
|
295 */ |
|
296 int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key, |
|
297 intptr_t *oldkey, void **oldvalue); |
|
298 |
|
299 /** |
|
300 * Counts the nodes in the specified UcxAVLTree. |
|
301 * @param tree the AVL tree |
|
302 * @return the node count |
|
303 */ |
|
304 size_t ucx_avl_count(UcxAVLTree *tree); |
|
305 |
|
306 /** |
|
307 * Finds the in-order predecessor of the given node. |
|
308 * @param node an AVL node |
|
309 * @return the in-order predecessor of the given node, or <code>NULL</code> if |
|
310 * the given node is the in-order minimum |
|
311 */ |
|
312 UcxAVLNode* ucx_avl_pred(UcxAVLNode* node); |
|
313 |
|
314 /** |
|
315 * Finds the in-order successor of the given node. |
|
316 * @param node an AVL node |
|
317 * @return the in-order successor of the given node, or <code>NULL</code> if |
|
318 * the given node is the in-order maximum |
|
319 */ |
|
320 UcxAVLNode* ucx_avl_succ(UcxAVLNode* node); |
|
321 |
|
322 #ifdef __cplusplus |
|
323 } |
|
324 #endif |
|
325 |
|
326 #endif /* UCX_AVL_H */ |
|
327 |
|