src/ucx/avl.c

Sun, 31 May 2020 16:58:23 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 31 May 2020 16:58:23 +0200
branch
webdav
changeset 247
1df803e06076
parent 135
471e28cca288
child 254
4784c14aa639
permissions
-rw-r--r--

implement webdav delete for collections

99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "avl.h"
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #define ptrcast(ptr) ((void*)(ptr))
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
32 #define alloc_tree(al) (UcxAVLTree*) almalloc((al), sizeof(UcxAVLTree))
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
33 #define alloc_node(al) (UcxAVLNode*) almalloc((al), sizeof(UcxAVLNode))
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 static void ucx_avl_connect(UcxAVLTree *tree,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 UcxAVLNode *node, UcxAVLNode *child, intptr_t nullkey) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 if (child) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 child->parent = node;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 // if child is NULL, nullkey decides if left or right pointer is cleared
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 if (tree->cmpfunc(
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 ptrcast(child ? child->key : nullkey),
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 ptrcast(node->key), tree->userdata) > 0) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 node->right = child;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 node->left = child;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 size_t lh = node->left ? node->left->height : 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 size_t rh = node->right ? node->right->height : 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 node->height = 1 + (lh > rh ? lh : rh);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 #define avlheight(node) ((node) ? (node)->height : 0)
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 static UcxAVLNode* avl_rotright(UcxAVLTree *tree, UcxAVLNode *l0) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 UcxAVLNode *p = l0->parent;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 UcxAVLNode *l1 = l0->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 if (p) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 ucx_avl_connect(tree, p, l1, 0);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 l1->parent = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 ucx_avl_connect(tree, l0, l1->right, l1->key);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 ucx_avl_connect(tree, l1, l0, 0);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 return l1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 static UcxAVLNode* avl_rotleft(UcxAVLTree *tree, UcxAVLNode *l0) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 UcxAVLNode *p = l0->parent;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 UcxAVLNode *l1 = l0->right;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if (p) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 ucx_avl_connect(tree, p, l1, 0);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 l1->parent = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 ucx_avl_connect(tree, l0, l1->left, l1->key);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 ucx_avl_connect(tree, l1, l0, 0);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 return l1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 static void ucx_avl_balance(UcxAVLTree *tree, UcxAVLNode *n) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 int lh = avlheight(n->left);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 int rh = avlheight(n->right);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 n->height = 1 + (lh > rh ? lh : rh);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if (lh - rh == 2) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 UcxAVLNode *c = n->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 if (avlheight(c->right) - avlheight(c->left) == 1) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 avl_rotleft(tree, c);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 n = avl_rotright(tree, n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 } else if (rh - lh == 2) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 UcxAVLNode *c = n->right;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 if (avlheight(c->left) - avlheight(c->right) == 1) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 avl_rotright(tree, c);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 n = avl_rotleft(tree, n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if (n->parent) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 ucx_avl_balance(tree, n->parent);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 tree->root = n;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 return ucx_avl_new_a(cmpfunc, ucx_default_allocator());
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) {
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
112 UcxAVLTree* tree = alloc_tree(allocator);
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 if (tree) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 tree->allocator = allocator;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 tree->cmpfunc = cmpfunc;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 tree->root = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 tree->userdata = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 return tree;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 static void ucx_avl_free_node(UcxAllocator *al, UcxAVLNode *node) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 if (node) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 ucx_avl_free_node(al, node->left);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 ucx_avl_free_node(al, node->right);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 alfree(al, node);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 void ucx_avl_free(UcxAVLTree *tree) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 UcxAllocator *al = tree->allocator;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 ucx_avl_free_node(al, tree->root);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 alfree(al, tree);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 UcxAVLNode *n = tree->root;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 int cmpresult;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 while (n && (cmpresult = tree->cmpfunc(
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 ptrcast(key), ptrcast(n->key), tree->userdata))) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 n = cmpresult > 0 ? n->right : n->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 return n;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 UcxAVLNode *n = ucx_avl_get_node(tree, key);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return n ? n->value : NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 return ucx_avl_put_s(tree, key, value, NULL);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 void **oldvalue) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if (tree->root) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 UcxAVLNode *n = tree->root;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 int cmpresult;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 while ((cmpresult = tree->cmpfunc(
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 ptrcast(key), ptrcast(n->key), tree->userdata))) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 UcxAVLNode *m = cmpresult > 0 ? n->right : n->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 if (m) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 n = m;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 break;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 if (cmpresult) {
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
172 UcxAVLNode* e = alloc_node(tree->allocator);
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 if (e) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 e->key = key; e->value = value; e->height = 1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 e->parent = e->left = e->right = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 ucx_avl_connect(tree, n, e, 0);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 ucx_avl_balance(tree, n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 return 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 return 1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if (oldvalue) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 *oldvalue = n->value;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 n->value = value;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 return 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 } else {
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
190 tree->root = alloc_node(tree->allocator);
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 if (tree->root) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 tree->root->key = key; tree->root->value = value;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 tree->root->height = 1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 tree->root->parent = tree->root->left = tree->root->right = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 if (oldvalue) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 *oldvalue = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 return 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 return 1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 int ucx_avl_remove(UcxAVLTree *tree, intptr_t key) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 return ucx_avl_remove_s(tree, key, NULL, NULL);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 return ucx_avl_remove_s(tree, node->key, NULL, NULL);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 intptr_t *oldkey, void **oldvalue) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 UcxAVLNode *n = tree->root;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 int cmpresult;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 while (n && (cmpresult = tree->cmpfunc(
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 ptrcast(key), ptrcast(n->key), tree->userdata))) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 n = cmpresult > 0 ? n->right : n->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 if (n) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 if (oldkey) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 *oldkey = n->key;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 if (oldvalue) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 *oldvalue = n->value;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 UcxAVLNode *p = n->parent;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 if (n->left && n->right) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 UcxAVLNode *s = n->right;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 while (s->left) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 s = s->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 ucx_avl_connect(tree, s->parent, s->right, s->key);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 n->key = s->key; n->value = s->value;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 p = s->parent;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 alfree(tree->allocator, s);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 if (p) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 ucx_avl_connect(tree, p, n->right ? n->right:n->left, n->key);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 tree->root = n->right ? n->right : n->left;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 if (tree->root) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 tree->root->parent = NULL;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 alfree(tree->allocator, n);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 if (p) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 ucx_avl_balance(tree, p);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 return 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 return 1;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 static size_t ucx_avl_countn(UcxAVLNode *node) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 if (node) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 return 1 + ucx_avl_countn(node->left) + ucx_avl_countn(node->right);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 return 0;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 size_t ucx_avl_count(UcxAVLTree *tree) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 return ucx_avl_countn(tree->root);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 }

mercurial