Sat, 28 Oct 2017 16:07:52 +0200
UCX release
192
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
192
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
29 | #include "ucx/avl.h" |
243
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
30 | |
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
31 | #include <limits.h> |
192
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
33 | #define ptrcast(ptr) ((void*)(ptr)) |
216
dee5a88c4db7
added casts for mallocs in AVL implementation (to satisfy c++ compiler)
Mike Becker <universe@uap-core.de>
parents:
205
diff
changeset
|
34 | #define alloc_tree(al) (UcxAVLTree*) almalloc((al), sizeof(UcxAVLTree)) |
dee5a88c4db7
added casts for mallocs in AVL implementation (to satisfy c++ compiler)
Mike Becker <universe@uap-core.de>
parents:
205
diff
changeset
|
35 | #define alloc_node(al) (UcxAVLNode*) almalloc((al), sizeof(UcxAVLNode)) |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
36 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
37 | static void ucx_avl_connect(UcxAVLTree *tree, |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
38 | UcxAVLNode *node, UcxAVLNode *child, intptr_t nullkey) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
39 | if (child) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
40 | child->parent = node; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
41 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
42 | // if child is NULL, nullkey decides if left or right pointer is cleared |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
43 | if (tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
44 | ptrcast(child ? child->key : nullkey), |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
45 | ptrcast(node->key), tree->userdata) > 0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
46 | node->right = child; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
47 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
48 | node->left = child; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
49 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
50 | size_t lh = node->left ? node->left->height : 0; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
51 | size_t rh = node->right ? node->right->height : 0; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
52 | node->height = 1 + (lh > rh ? lh : rh); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
53 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
54 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
55 | #define avlheight(node) ((node) ? (node)->height : 0) |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
56 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
57 | static UcxAVLNode* avl_rotright(UcxAVLTree *tree, UcxAVLNode *l0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
58 | UcxAVLNode *p = l0->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
59 | UcxAVLNode *l1 = l0->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
60 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
61 | ucx_avl_connect(tree, p, l1, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
62 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
63 | l1->parent = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
64 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
65 | ucx_avl_connect(tree, l0, l1->right, l1->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
66 | ucx_avl_connect(tree, l1, l0, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
67 | return l1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
68 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
69 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
70 | static UcxAVLNode* avl_rotleft(UcxAVLTree *tree, UcxAVLNode *l0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
71 | UcxAVLNode *p = l0->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
72 | UcxAVLNode *l1 = l0->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
73 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
74 | ucx_avl_connect(tree, p, l1, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
75 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
76 | l1->parent = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
77 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
78 | ucx_avl_connect(tree, l0, l1->left, l1->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
79 | ucx_avl_connect(tree, l1, l0, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
80 | return l1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
81 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
82 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
83 | static void ucx_avl_balance(UcxAVLTree *tree, UcxAVLNode *n) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
84 | int lh = avlheight(n->left); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
85 | int rh = avlheight(n->right); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
86 | n->height = 1 + (lh > rh ? lh : rh); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
87 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
88 | if (lh - rh == 2) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
89 | UcxAVLNode *c = n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
90 | if (avlheight(c->right) - avlheight(c->left) == 1) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
91 | avl_rotleft(tree, c); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
92 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
93 | n = avl_rotright(tree, n); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
94 | } else if (rh - lh == 2) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
95 | UcxAVLNode *c = n->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
96 | if (avlheight(c->left) - avlheight(c->right) == 1) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
97 | avl_rotright(tree, c); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
98 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
99 | n = avl_rotleft(tree, n); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
100 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
101 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
102 | if (n->parent) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
103 | ucx_avl_balance(tree, n->parent); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
104 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
105 | tree->root = n; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
106 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
107 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
108 | |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
109 | UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) { |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
110 | return ucx_avl_new_a(cmpfunc, ucx_default_allocator()); |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
111 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
112 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
113 | UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) { |
216
dee5a88c4db7
added casts for mallocs in AVL implementation (to satisfy c++ compiler)
Mike Becker <universe@uap-core.de>
parents:
205
diff
changeset
|
114 | UcxAVLTree* tree = alloc_tree(allocator); |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
115 | if (tree) { |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
116 | tree->allocator = allocator; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
117 | tree->cmpfunc = cmpfunc; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
118 | tree->root = NULL; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
119 | tree->userdata = NULL; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
120 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
121 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
122 | return tree; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
123 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
124 | |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
125 | static void ucx_avl_free_node(UcxAllocator *al, UcxAVLNode *node) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
126 | if (node) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
127 | ucx_avl_free_node(al, node->left); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
128 | ucx_avl_free_node(al, node->right); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
129 | alfree(al, node); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
130 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
131 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
132 | |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
133 | void ucx_avl_free(UcxAVLTree *tree) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
134 | UcxAllocator *al = tree->allocator; |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
135 | ucx_avl_free_node(al, tree->root); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
136 | alfree(al, tree); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
137 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
138 | |
205
54a7ceb9151f
improved avl function names
Mike Becker <universe@uap-core.de>
parents:
204
diff
changeset
|
139 | UcxAVLNode *ucx_avl_get_node(UcxAVLTree *tree, intptr_t key) { |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
140 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
141 | int cmpresult; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
142 | while (n && (cmpresult = tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
143 | ptrcast(key), ptrcast(n->key), tree->userdata))) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
144 | n = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
145 | } |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
146 | return n; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
147 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
148 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
149 | void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) { |
205
54a7ceb9151f
improved avl function names
Mike Becker <universe@uap-core.de>
parents:
204
diff
changeset
|
150 | UcxAVLNode *n = ucx_avl_get_node(tree, key); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
151 | return n ? n->value : NULL; |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
152 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
153 | |
243
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
154 | UcxAVLNode *ucx_avl_find_node(UcxAVLTree *tree, intptr_t key, |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
155 | distance_func dfnc, int mode) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
156 | UcxAVLNode *n = tree->root; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
157 | UcxAVLNode *closest = NULL; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
158 | |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
159 | intmax_t cmpresult; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
160 | intmax_t closest_dist; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
161 | closest_dist = mode == UCX_AVL_FIND_LOWER_BOUNDED ? INTMAX_MIN : INTMAX_MAX; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
162 | |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
163 | while (n && (cmpresult = dfnc( |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
164 | ptrcast(key), ptrcast(n->key), tree->userdata))) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
165 | if (mode == UCX_AVL_FIND_CLOSEST) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
166 | intmax_t dist = cmpresult; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
167 | if (dist < 0) dist *= -1; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
168 | if (dist < closest_dist) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
169 | closest_dist = dist; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
170 | closest = n; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
171 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
172 | } else if (mode == UCX_AVL_FIND_LOWER_BOUNDED && cmpresult <= 0) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
173 | if (cmpresult > closest_dist) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
174 | closest_dist = cmpresult; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
175 | closest = n; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
176 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
177 | } else if (mode == UCX_AVL_FIND_UPPER_BOUNDED && cmpresult >= 0) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
178 | if (cmpresult < closest_dist) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
179 | closest_dist = cmpresult; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
180 | closest = n; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
181 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
182 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
183 | n = cmpresult > 0 ? n->right : n->left; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
184 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
185 | return n ? n : closest; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
186 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
187 | |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
188 | void *ucx_avl_find(UcxAVLTree *tree, intptr_t key, |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
189 | distance_func dfnc, int mode) { |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
190 | UcxAVLNode *n = ucx_avl_find_node(tree, key, dfnc, mode); |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
191 | return n ? n->value : NULL; |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
192 | } |
2e74828c5e94
adds distance function and ucx_avl_find_node()
Mike Becker <universe@uap-core.de>
parents:
225
diff
changeset
|
193 | |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
194 | int ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
195 | return ucx_avl_put_s(tree, key, value, NULL); |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
196 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
197 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
198 | int ucx_avl_put_s(UcxAVLTree *tree, intptr_t key, void *value, |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
199 | void **oldvalue) { |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
200 | if (tree->root) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
201 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
202 | int cmpresult; |
197 | 203 | while ((cmpresult = tree->cmpfunc( |
204 | ptrcast(key), ptrcast(n->key), tree->userdata))) { | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
205 | UcxAVLNode *m = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
206 | if (m) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
207 | n = m; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
208 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
209 | break; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
210 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
211 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
212 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
213 | if (cmpresult) { |
216
dee5a88c4db7
added casts for mallocs in AVL implementation (to satisfy c++ compiler)
Mike Becker <universe@uap-core.de>
parents:
205
diff
changeset
|
214 | UcxAVLNode* e = alloc_node(tree->allocator); |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
215 | if (e) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
216 | e->key = key; e->value = value; e->height = 1; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
217 | e->parent = e->left = e->right = NULL; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
218 | ucx_avl_connect(tree, n, e, 0); |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
219 | ucx_avl_balance(tree, n); |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
220 | return 0; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
221 | } else { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
222 | return 1; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
223 | } |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
224 | } else { |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
225 | if (oldvalue) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
226 | *oldvalue = n->value; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
227 | } |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
228 | n->value = value; |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
229 | return 0; |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
230 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
231 | } else { |
216
dee5a88c4db7
added casts for mallocs in AVL implementation (to satisfy c++ compiler)
Mike Becker <universe@uap-core.de>
parents:
205
diff
changeset
|
232 | tree->root = alloc_node(tree->allocator); |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
233 | if (tree->root) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
234 | tree->root->key = key; tree->root->value = value; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
235 | tree->root->height = 1; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
236 | tree->root->parent = tree->root->left = tree->root->right = NULL; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
237 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
238 | if (oldvalue) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
239 | *oldvalue = NULL; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
240 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
241 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
242 | return 0; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
243 | } else { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
244 | return 1; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
245 | } |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
246 | } |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
247 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
248 | |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
249 | int ucx_avl_remove(UcxAVLTree *tree, intptr_t key) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
250 | return ucx_avl_remove_s(tree, key, NULL, NULL); |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
251 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
252 | |
205
54a7ceb9151f
improved avl function names
Mike Becker <universe@uap-core.de>
parents:
204
diff
changeset
|
253 | int ucx_avl_remove_node(UcxAVLTree *tree, UcxAVLNode *node) { |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
254 | return ucx_avl_remove_s(tree, node->key, NULL, NULL); |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
255 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
256 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
257 | int ucx_avl_remove_s(UcxAVLTree *tree, intptr_t key, |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
258 | intptr_t *oldkey, void **oldvalue) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
259 | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
260 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
261 | int cmpresult; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
262 | while (n && (cmpresult = tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
263 | ptrcast(key), ptrcast(n->key), tree->userdata))) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
264 | n = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
265 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
266 | if (n) { |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
267 | if (oldkey) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
268 | *oldkey = n->key; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
269 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
270 | if (oldvalue) { |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
271 | *oldvalue = n->value; |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
272 | } |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
273 | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
274 | UcxAVLNode *p = n->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
275 | if (n->left && n->right) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
276 | UcxAVLNode *s = n->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
277 | while (s->left) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
278 | s = s->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
279 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
280 | ucx_avl_connect(tree, s->parent, s->right, s->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
281 | n->key = s->key; n->value = s->value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
282 | p = s->parent; |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
283 | alfree(tree->allocator, s); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
284 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
285 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
286 | ucx_avl_connect(tree, p, n->right ? n->right:n->left, n->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
287 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
288 | tree->root = n->right ? n->right : n->left; |
203
3d999ea3f780
added 1 assert in ucx_avl_remove tests and fixed source code formatting
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
202
diff
changeset
|
289 | if (tree->root) { |
202
4c84dd2408d7
fixed bug in ucx_avl_remove
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
290 | tree->root->parent = NULL; |
4c84dd2408d7
fixed bug in ucx_avl_remove
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
291 | } |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
292 | } |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
293 | alfree(tree->allocator, n); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
294 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
295 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
296 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
297 | ucx_avl_balance(tree, p); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
298 | } |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
299 | |
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
300 | return 0; |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
301 | } else { |
204
4477987d40cd
better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents:
203
diff
changeset
|
302 | return 1; |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
303 | } |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
304 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
305 | |
199 | 306 | static size_t ucx_avl_countn(UcxAVLNode *node) { |
307 | if (node) { | |
308 | return 1 + ucx_avl_countn(node->left) + ucx_avl_countn(node->right); | |
309 | } else { | |
310 | return 0; | |
311 | } | |
312 | } | |
313 | ||
314 | size_t ucx_avl_count(UcxAVLTree *tree) { | |
315 | return ucx_avl_countn(tree->root); | |
316 | } | |
245
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
317 | |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
318 | UcxAVLNode* ucx_avl_pred(UcxAVLNode* node) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
319 | if (node->left) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
320 | UcxAVLNode* n = node->left; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
321 | while (n->right) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
322 | n = n->right; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
323 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
324 | return n; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
325 | } else { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
326 | UcxAVLNode* n = node; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
327 | while (n->parent) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
328 | if (n->parent->right == n) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
329 | return n->parent; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
330 | } else { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
331 | n = n->parent; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
332 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
333 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
334 | return NULL; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
335 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
336 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
337 | |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
338 | UcxAVLNode* ucx_avl_succ(UcxAVLNode* node) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
339 | if (node->right) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
340 | UcxAVLNode* n = node->right; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
341 | while (n->left) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
342 | n = n->left; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
343 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
344 | return n; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
345 | } else { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
346 | UcxAVLNode* n = node; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
347 | while (n->parent) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
348 | if (n->parent->left == n) { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
349 | return n->parent; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
350 | } else { |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
351 | n = n->parent; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
352 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
353 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
354 | return NULL; |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
355 | } |
db732f8c083a
adds AVL predecessor and successor functions
Mike Becker <universe@uap-core.de>
parents:
243
diff
changeset
|
356 | } |