ucx/avl.h

changeset 152
62921b370c60
parent 124
80609f9675f1
equal deleted inserted replaced
151:11f3bb408051 152:62921b370c60
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
42 #ifndef UCX_AVL_H 42 #ifndef UCX_AVL_H
43 #define UCX_AVL_H 43 #define UCX_AVL_H
44 44
45 #include "ucx.h" 45 #include "ucx.h"
46 #include "allocator.h" 46 #include "allocator.h"
47 #include <stdint.h> 47 #include <inttypes.h>
48 48
49 #ifdef __cplusplus 49 #ifdef __cplusplus
50 extern "C" { 50 extern "C" {
51 #endif 51 #endif
52 52
132 * @return a new UcxAVLTree object 132 * @return a new UcxAVLTree object
133 */ 133 */
134 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); 134 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator);
135 135
136 /** 136 /**
137 * Destroys an UcxAVLTree. 137 * Destroys a UcxAVLTree.
138 * @param tree the tree to destroy 138 * @param tree the tree to destroy
139 */ 139 */
140 void ucx_avl_free(UcxAVLTree *tree); 140 void ucx_avl_free(UcxAVLTree *tree);
141 141
142 /** 142 /**
160 * @param tree the UcxAVLTree 160 * @param tree the UcxAVLTree
161 * @param key the key 161 * @param key the key
162 * @return the value (or <code>NULL</code>, if the key is not present) 162 * @return the value (or <code>NULL</code>, if the key is not present)
163 */ 163 */
164 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); 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);
165 227
166 /** 228 /**
167 * Puts a key/value pair into the tree. 229 * Puts a key/value pair into the tree.
168 * 230 *
169 * Attention: use this function only, if a possible old value does not need 231 * Attention: use this function only, if a possible old value does not need
194 /** 256 /**
195 * Removes a node from the AVL tree. 257 * Removes a node from the AVL tree.
196 * 258 *
197 * Note: the specified node is logically removed. The tree implementation 259 * Note: the specified node is logically removed. The tree implementation
198 * decides which memory area is freed. In most cases the here provided node 260 * decides which memory area is freed. In most cases the here provided node
199 * is freed, so it's further use is generally undefined. 261 * is freed, so its further use is generally undefined.
200 * 262 *
201 * @param tree the UcxAVLTree 263 * @param tree the UcxAVLTree
202 * @param node the node to remove 264 * @param node the node to remove
203 * @return zero, if and only if an element has been removed 265 * @return zero, if and only if an element has been removed
204 */ 266 */
239 * @param tree the AVL tree 301 * @param tree the AVL tree
240 * @return the node count 302 * @return the node count
241 */ 303 */
242 size_t ucx_avl_count(UcxAVLTree *tree); 304 size_t ucx_avl_count(UcxAVLTree *tree);
243 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
244 #ifdef __cplusplus 322 #ifdef __cplusplus
245 } 323 }
246 #endif 324 #endif
247 325
248 #endif /* UCX_AVL_H */ 326 #endif /* UCX_AVL_H */

mercurial