ucx
UAP Common Extensions
avl.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, 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 
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 
58 typedef struct UcxAVLNode UcxAVLNode;
59 
63 struct UcxAVLNode {
67  intptr_t key;
71  void *value;
75  size_t height;
88 };
89 
93 typedef struct {
111  void *userdata;
112 } UcxAVLTree;
113 
122 
134 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator);
135 
145 void ucx_avl_free(UcxAVLTree *tree);
146 
166 
173 #define ucx_avl_default_new() \
174  ucx_avl_new_a(ucx_cmp_ptr, ucx_default_allocator())
175 
182 UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key);
183 
190 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key);
191 
196 #define UCX_AVL_FIND_EXACT 0
197 
201 #define UCX_AVL_FIND_LOWER_BOUNDED 1
202 
206 #define UCX_AVL_FIND_UPPER_BOUNDED 2
207 
213 #define UCX_AVL_FIND_CLOSEST 3
214 
238 UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key,
239  distance_func dfnc, int mode);
240 
251 void *ucx_avl_find(UcxAVLTree *tree, intptr_t key,
252  distance_func dfnc, int mode);
253 
265 int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value);
266 
280 int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue);
281 
293 int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node);
294 
302 int ucx_avl_remove(UcxAVLTree *tree, intptr_t key);
303 
322 int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key,
323  intptr_t *oldkey, void **oldvalue);
324 
330 size_t ucx_avl_count(UcxAVLTree *tree);
331 
339 
347 
348 #ifdef __cplusplus
349 }
350 #endif
351 
352 #endif /* UCX_AVL_H */
353 
UcxAVLTree * ucx_avl_new(cmp_func cmpfunc)
Initializes a new UcxAVLTree with a default allocator.
Definition: avl.c:109
int ucx_avl_remove(UcxAVLTree *tree, intptr_t key)
Removes an element from the AVL tree.
Definition: avl.c:266
UCX AVL Node.
Definition: avl.h:63
int(* cmp_func)(const void *, const void *, void *)
Function pointer to a compare function.
Definition: ucx.h:84
UcxAVLNode * parent
Parent node.
Definition: avl.h:79
UcxAVLNode * ucx_avl_pred(UcxAVLNode *node)
Finds the in-order predecessor of the given node.
Definition: avl.c:335
int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value)
Puts a key/value pair into the tree.
Definition: avl.c:211
Main UCX Header providing most common definitions.
UCX AVL Tree.
Definition: avl.h:93
size_t ucx_avl_count(UcxAVLTree *tree)
Counts the nodes in the specified UcxAVLTree.
Definition: avl.c:331
UcxAVLTree * ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator)
Initializes a new UcxAVLTree with the specified allocator.
Definition: avl.c:113
void * userdata
Custom user data.
Definition: avl.h:111
int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node)
Removes a node from the AVL tree.
Definition: avl.c:270
cmp_func cmpfunc
Compare function that shall be used to compare the UcxAVLNode keys.
Definition: avl.h:106
UcxAVLNode * ucx_avl_succ(UcxAVLNode *node)
Finds the in-order successor of the given node.
Definition: avl.c:355
void ucx_avl_free_content(UcxAVLTree *tree, ucx_destructor destr)
Frees the contents of a UcxAVLTree.
Definition: avl.c:152
UcxAllocator * allocator
The UcxAllocator that shall be used to manage the memory for node data.
Definition: avl.h:97
int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key, intptr_t *oldkey, void **oldvalue)
Removes an element from the AVL tree.
Definition: avl.c:274
void * ucx_avl_find(UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode)
Finds a value within the tree.
Definition: avl.c:205
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue)
Puts a key/value pair into the tree.
Definition: avl.c:215
UcxAVLNode * root
Root node of the tree.
Definition: avl.h:101
intmax_t(* distance_func)(const void *, const void *, void *)
Function pointer to a distance function.
Definition: ucx.h:93
Allocator for custom memory management.
void * ucx_avl_get(UcxAVLTree *tree, intptr_t key)
Gets the value from the tree, that is associated with the specified key.
Definition: avl.c:166
UcxAVLNode * left
Root node of left subtree.
Definition: avl.h:83
void ucx_avl_free(UcxAVLTree *tree)
Destroys a UcxAVLTree.
Definition: avl.c:133
void * value
Data contained by this node.
Definition: avl.h:71
UcxAVLNode * ucx_avl_find_node(UcxAVLTree *tree, intptr_t key, distance_func dfnc, int mode)
Finds a node within the tree.
Definition: avl.c:171
intptr_t key
The key for this node.
Definition: avl.h:67
UcxAVLNode * right
Root node of right subtree.
Definition: avl.h:87
UcxAVLNode * ucx_avl_get_node(UcxAVLTree *tree, intptr_t key)
Gets the node from the tree, that is associated with the specified key.
Definition: avl.c:156
size_t height
The height of this (sub)-tree.
Definition: avl.h:75
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72