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() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator())
174 
181 UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key);
182 
189 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key);
190 
195 #define UCX_AVL_FIND_EXACT 0
196 
200 #define UCX_AVL_FIND_LOWER_BOUNDED 1
201 
205 #define UCX_AVL_FIND_UPPER_BOUNDED 2
206 
212 #define UCX_AVL_FIND_CLOSEST 3
213 
237 UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key,
238  distance_func dfnc, int mode);
239 
250 void *ucx_avl_find(UcxAVLTree *tree, intptr_t key,
251  distance_func dfnc, int mode);
252 
264 int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value);
265 
279 int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, void **oldvalue);
280 
292 int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node);
293 
301 int ucx_avl_remove(UcxAVLTree *tree, intptr_t key);
302 
321 int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key,
322  intptr_t *oldkey, void **oldvalue);
323 
329 size_t ucx_avl_count(UcxAVLTree *tree);
330 
338 
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #endif /* UCX_AVL_H */
352 
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