2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
33 #define ptrcast(ptr) ((void*)(ptr))
34 #define alloc_tree(al) (UcxAVLTree*) almalloc((al), sizeof(UcxAVLTree))
35 #define alloc_node(al) (UcxAVLNode*) almalloc((al), sizeof(UcxAVLNode))
37 static void ucx_avl_connect(UcxAVLTree *tree,
38 UcxAVLNode *node, UcxAVLNode *child, intptr_t nullkey) {
42 // if child is NULL, nullkey decides if left or right pointer is cleared
44 ptrcast(child ? child->key : nullkey),
45 ptrcast(node->key), tree->userdata) > 0) {
50 size_t lh = node->left ? node->left->height : 0;
51 size_t rh = node->right ? node->right->height : 0;
52 node->height = 1 + (lh > rh ? lh : rh);
55 #define avlheight(node) ((node) ? (node)->height : 0)
57 static UcxAVLNode* avl_rotright(UcxAVLTree *tree, UcxAVLNode *l0) {
58 UcxAVLNode *p = l0->parent;
59 UcxAVLNode *l1 = l0->left;
61 ucx_avl_connect(tree, p, l1, 0);
65 ucx_avl_connect(tree, l0, l1->right, l1->key);
66 ucx_avl_connect(tree, l1, l0, 0);
70 static UcxAVLNode* avl_rotleft(UcxAVLTree *tree, UcxAVLNode *l0) {
71 UcxAVLNode *p = l0->parent;
72 UcxAVLNode *l1 = l0->right;
74 ucx_avl_connect(tree, p, l1, 0);
78 ucx_avl_connect(tree, l0, l1->left, l1->key);
79 ucx_avl_connect(tree, l1, l0, 0);
83 static void ucx_avl_balance(UcxAVLTree *tree, UcxAVLNode *n) {
84 int lh = avlheight(n->left);
85 int rh = avlheight(n->right);
86 n->height = 1 + (lh > rh ? lh : rh);
89 UcxAVLNode *c = n->left;
90 if (avlheight(c->right) - avlheight(c->left) == 1) {
93 n = avl_rotright(tree, n);
94 } else if (rh - lh == 2) {
95 UcxAVLNode *c = n->right;
96 if (avlheight(c->left) - avlheight(c->right) == 1) {
97 avl_rotright(tree, c);
99 n = avl_rotleft(tree, n);
103 ucx_avl_balance(tree, n->parent);
109 UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) {
110 return ucx_avl_new_a(cmpfunc, ucx_default_allocator());
113 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) {
114 UcxAVLTree* tree = alloc_tree(allocator);
116 tree->allocator = allocator;
117 tree->cmpfunc = cmpfunc;
119 tree->userdata = NULL;
125 static void ucx_avl_free_node(UcxAllocator *al, UcxAVLNode *node) {
127 ucx_avl_free_node(al, node->left);
128 ucx_avl_free_node(al, node->right);
133 void ucx_avl_free(UcxAVLTree *tree) {
134 UcxAllocator *al = tree->allocator;
135 ucx_avl_free_node(al, tree->root);
139 static void ucx_avl_free_content_node(UcxAllocator *al, UcxAVLNode *node,
140 ucx_destructor destr) {
142 ucx_avl_free_content_node(al, node->left, destr);
143 ucx_avl_free_content_node(al, node->right, destr);
147 alfree(al, node->value);
152 void ucx_avl_free_content(UcxAVLTree *tree, ucx_destructor destr) {
153 ucx_avl_free_content_node(tree->allocator, tree->root, destr);
156 UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key) {
157 UcxAVLNode *n = tree->root;
159 while (n && (cmpresult = tree->cmpfunc(
160 ptrcast(key), ptrcast(n->key), tree->userdata))) {
161 n = cmpresult > 0 ? n->right : n->left;
166 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) {
167 UcxAVLNode *n = ucx_avl_get_node(tree, key);
168 return n ? n->value : NULL;
171 UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key,
172 distance_func dfnc, int mode) {
173 UcxAVLNode *n = tree->root;
174 UcxAVLNode *closest = NULL;
177 intmax_t closest_dist;
178 closest_dist = mode == UCX_AVL_FIND_LOWER_BOUNDED ? INTMAX_MIN : INTMAX_MAX;
180 while (n && (cmpresult = dfnc(
181 ptrcast(key), ptrcast(n->key), tree->userdata))) {
182 if (mode == UCX_AVL_FIND_CLOSEST) {
183 intmax_t dist = cmpresult;
184 if (dist < 0) dist *= -1;
185 if (dist < closest_dist) {
189 } else if (mode == UCX_AVL_FIND_LOWER_BOUNDED && cmpresult <= 0) {
190 if (cmpresult > closest_dist) {
191 closest_dist = cmpresult;
194 } else if (mode == UCX_AVL_FIND_UPPER_BOUNDED && cmpresult >= 0) {
195 if (cmpresult < closest_dist) {
196 closest_dist = cmpresult;
200 n = cmpresult > 0 ? n->right : n->left;
202 return n ? n : closest;
205 void *ucx_avl_find(UcxAVLTree *tree, intptr_t key,
206 distance_func dfnc, int mode) {
207 UcxAVLNode *n = ucx_avl_find_node(tree, key, dfnc, mode);
208 return n ? n->value : NULL;
211 int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) {
212 return ucx_avl_put_s(tree, key, value, NULL);
215 int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value,
218 UcxAVLNode *n = tree->root;
220 while ((cmpresult = tree->cmpfunc(
221 ptrcast(key), ptrcast(n->key), tree->userdata))) {
222 UcxAVLNode *m = cmpresult > 0 ? n->right : n->left;
231 UcxAVLNode* e = alloc_node(tree->allocator);
233 e->key = key; e->value = value; e->height = 1;
234 e->parent = e->left = e->right = NULL;
235 ucx_avl_connect(tree, n, e, 0);
236 ucx_avl_balance(tree, n);
243 *oldvalue = n->value;
249 tree->root = alloc_node(tree->allocator);
251 tree->root->key = key; tree->root->value = value;
252 tree->root->height = 1;
253 tree->root->parent = tree->root->left = tree->root->right = NULL;
266 int ucx_avl_remove(UcxAVLTree *tree, intptr_t key) {
267 return ucx_avl_remove_s(tree, key, NULL, NULL);
270 int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node) {
271 return ucx_avl_remove_s(tree, node->key, NULL, NULL);
274 int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key,
275 intptr_t *oldkey, void **oldvalue) {
277 UcxAVLNode *n = tree->root;
279 while (n && (cmpresult = tree->cmpfunc(
280 ptrcast(key), ptrcast(n->key), tree->userdata))) {
281 n = cmpresult > 0 ? n->right : n->left;
288 *oldvalue = n->value;
291 UcxAVLNode *p = n->parent;
292 if (n->left && n->right) {
293 UcxAVLNode *s = n->right;
297 ucx_avl_connect(tree, s->parent, s->right, s->key);
298 n->key = s->key; n->value = s->value;
300 alfree(tree->allocator, s);
303 ucx_avl_connect(tree, p, n->right ? n->right:n->left, n->key);
305 tree->root = n->right ? n->right : n->left;
307 tree->root->parent = NULL;
310 alfree(tree->allocator, n);
314 ucx_avl_balance(tree, p);
323 static size_t ucx_avl_countn(UcxAVLNode *node) {
325 return 1 + ucx_avl_countn(node->left) + ucx_avl_countn(node->right);
331 size_t ucx_avl_count(UcxAVLTree *tree) {
332 return ucx_avl_countn(tree->root);
335 UcxAVLNode* ucx_avl_pred(UcxAVLNode* node) {
337 UcxAVLNode* n = node->left;
343 UcxAVLNode* n = node;
345 if (n->parent->right == n) {
355 UcxAVLNode* ucx_avl_succ(UcxAVLNode* node) {
357 UcxAVLNode* n = node->right;
363 UcxAVLNode* n = node;
365 if (n->parent->left == n) {