ucx/tree.c

Sun, 05 Jan 2025 22:00:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 05 Jan 2025 22:00:39 +0100
changeset 440
7c4b9cba09ca
parent 324
ce13a778654a
permissions
-rw-r--r--

update ucx

253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
087cc9216f28 initial newapi GTK port
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
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/tree.h"
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
31 #include "cx/array_list.h"
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
32
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <assert.h>
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #define tree_parent(node) CX_TREE_PTR(node, loc_parent)
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #define tree_children(node) CX_TREE_PTR(node, loc_children)
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
38 #define tree_last_child(node) CX_TREE_PTR(node, loc_last_child)
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #define tree_prev(node) CX_TREE_PTR(node, loc_prev)
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #define tree_next(node) CX_TREE_PTR(node, loc_next)
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
42 #define cx_tree_ptr_locations \
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
43 loc_parent, loc_children, loc_last_child, loc_prev, loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
44
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
45 #define cx_tree_node_layout(tree) \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
46 (tree)->loc_parent,\
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
47 (tree)->loc_children,\
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
48 (tree)->loc_last_child,\
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
49 (tree)->loc_prev, \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
50 (tree)->loc_next
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
51
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
52 static void cx_tree_zero_pointers(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
53 void *node,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
54 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
55 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
56 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
57 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
58 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
59 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
60 tree_parent(node) = NULL;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
61 if (loc_prev >= 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
62 tree_prev(node) = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
63 }
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
64 tree_next(node) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
65 tree_children(node) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
66 if (loc_last_child >= 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
67 tree_last_child(node) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
68 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
69 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
70
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 void cx_tree_link(
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
72 void *parent,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
73 void *node,
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 ptrdiff_t loc_parent,
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 ptrdiff_t loc_children,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
76 ptrdiff_t loc_last_child,
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 ptrdiff_t loc_prev,
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 ptrdiff_t loc_next
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 ) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
80 assert(loc_parent >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
81 assert(loc_children >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
82 assert(loc_next >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
83
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 void *current_parent = tree_parent(node);
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 if (current_parent == parent) return;
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if (current_parent != NULL) {
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
87 cx_tree_unlink(node, cx_tree_ptr_locations);
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if (tree_children(parent) == NULL) {
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 tree_children(parent) = node;
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
92 if (loc_last_child >= 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
93 tree_last_child(parent) = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
94 }
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 } else {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
96 void *child;
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
97 if (loc_last_child >= 0) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
98 child = tree_last_child(parent);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
99 tree_last_child(parent) = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
100 } else {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
101 child = tree_children(parent);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
102 void *next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
103 while ((next = tree_next(child)) != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
104 child = next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
105 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
106 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
107 if (loc_prev >= 0) {
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
108 tree_prev(node) = child;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
109 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
110 tree_next(child) = node;
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 tree_parent(node) = parent;
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 }
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
115 static void *cx_tree_node_prev(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
116 ptrdiff_t loc_parent,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
117 ptrdiff_t loc_children,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
118 ptrdiff_t loc_next,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
119 const void *node
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
120 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
121 void *parent = tree_parent(node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
122 void *begin = tree_children(parent);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
123 if (begin == node) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
124 const void *cur = begin;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
125 const void *next;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
126 while (1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
127 next = tree_next(cur);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
128 if (next == node) return (void *) cur;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
129 cur = next;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
130 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
131 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
132
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 void cx_tree_unlink(
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 void *node,
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 ptrdiff_t loc_parent,
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 ptrdiff_t loc_children,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
137 ptrdiff_t loc_last_child,
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 ptrdiff_t loc_prev,
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 ptrdiff_t loc_next
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 ) {
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 if (tree_parent(node) == NULL) return;
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
143 assert(loc_children >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
144 assert(loc_next >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
145 assert(loc_parent >= 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
146 void *left;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
147 if (loc_prev >= 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
148 left = tree_prev(node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
149 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
150 left = cx_tree_node_prev(loc_parent, loc_children, loc_next, node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
151 }
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 void *right = tree_next(node);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
153 void *parent = tree_parent(node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
154 assert(left == NULL || tree_children(parent) != node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
155 assert(right == NULL || loc_last_child < 0 ||
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
156 tree_last_child(parent) != node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
157
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if (left == NULL) {
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
159 tree_children(parent) = right;
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 } else {
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 tree_next(left) = right;
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
163 if (right == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
164 if (loc_last_child >= 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
165 tree_last_child(parent) = left;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
166 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
167 } else {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
168 if (loc_prev >= 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
169 tree_prev(right) = left;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
170 }
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
171 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
172
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 tree_parent(node) = NULL;
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 tree_next(node) = NULL;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
175 if (loc_prev >= 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
176 tree_prev(node) = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
177 }
253
087cc9216f28 initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
179
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
180 int cx_tree_search(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
181 const void *root,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
182 size_t depth,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
183 const void *node,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
184 cx_tree_search_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
185 void **result,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
186 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
187 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
188 ) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
189 // help avoiding bugs due to uninitialized memory
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
190 assert(result != NULL);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
191 *result = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
192
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
193 // remember return value for best match
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
194 int ret = sfunc(root, node);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
195 if (ret < 0) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
196 // not contained, exit
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
197 return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
198 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
199 *result = (void*) root;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
200 // if root is already exact match, exit
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
201 if (ret == 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
202 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
203 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
204
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
205 // when depth is one, we are already done
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
206 if (depth == 1) {
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
207 return ret;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
208 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
209
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
210 // special case: indefinite depth
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
211 if (depth == 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
212 depth = SIZE_MAX;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
213 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
214
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
215 // create an iterator
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
216 CxTreeIterator iter = cx_tree_iterator(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
217 (void*) root, false, loc_children, loc_next
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
218 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
219
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
220 // skip root, we already handled it
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
221 cxIteratorNext(iter);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
222
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
223 // loop through the remaining tree
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
224 cx_foreach(void *, elem, iter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
225 // investigate the current node
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
226 int ret_elem = sfunc(elem, node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
227 if (ret_elem == 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
228 // if found, exit the search
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
229 *result = (void *) elem;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
230 ret = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
231 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
232 } else if (ret_elem > 0 && ret_elem < ret) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
233 // new distance is better
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
234 *result = elem;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
235 ret = ret_elem;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
236 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
237 // not contained or distance is worse, skip entire subtree
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
238 cxTreeIteratorContinue(iter);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
239 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
240
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
241 // when we reached the max depth, skip the subtree
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
242 if (iter.depth == depth) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
243 cxTreeIteratorContinue(iter);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
244 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
245 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
246
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
247 // dispose the iterator as we might have exited the loop early
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
248 cxTreeIteratorDispose(&iter);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
249
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
250 assert(ret < 0 || *result != NULL);
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
251 return ret;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
252 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
253
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
254 int cx_tree_search_data(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
255 const void *root,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
256 size_t depth,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
257 const void *data,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
258 cx_tree_search_data_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
259 void **result,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
260 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
261 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
262 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
263 // it is basically the same implementation
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
264 return cx_tree_search(
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
265 root, depth, data,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
266 (cx_tree_search_func) sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
267 result,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
268 loc_children, loc_next);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
269 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
270
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
271 static bool cx_tree_iter_valid(const void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
272 const struct cx_tree_iterator_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
273 return iter->node != NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
274 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
275
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
276 static void *cx_tree_iter_current(const void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
277 const struct cx_tree_iterator_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
278 return iter->node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
279 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
280
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
281 static void cx_tree_iter_next(void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
282 struct cx_tree_iterator_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
283 ptrdiff_t const loc_next = iter->loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
284 ptrdiff_t const loc_children = iter->loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
285 // protect us from misuse
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
286 if (!iter->base.valid(iter)) return;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
287
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
288 void *children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
289
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
290 // check if we are currently exiting or entering nodes
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
291 if (iter->exiting) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
292 children = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
293 // skipping on exit is pointless, just clear the flag
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
294 iter->skip = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
295 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
296 if (iter->skip) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
297 // skip flag is set, pretend that there are no children
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
298 iter->skip = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
299 children = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
300 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
301 // try to enter the children (if any)
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
302 children = tree_children(iter->node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
303 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
304 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
305
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
306 if (children == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
307 // search for the next node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
308 void *next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
309 cx_tree_iter_search_next:
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
310 // check if there is a sibling
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
311 if (iter->exiting) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
312 next = iter->node_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
313 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
314 next = tree_next(iter->node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
315 iter->node_next = next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
316 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
317 if (next == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
318 // no sibling, we are done with this node and exit
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
319 if (iter->visit_on_exit && !iter->exiting) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
320 // iter is supposed to visit the node again
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
321 iter->exiting = true;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
322 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
323 iter->exiting = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
324 if (iter->depth == 1) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
325 // there is no parent - we have iterated the entire tree
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
326 // invalidate the iterator and free the node stack
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
327 iter->node = iter->node_next = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
328 iter->stack_capacity = iter->depth = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
329 free(iter->stack);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
330 iter->stack = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
331 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
332 // the parent node can be obtained from the top of stack
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
333 // this way we can avoid the loc_parent in the iterator
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
334 iter->depth--;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
335 iter->node = iter->stack[iter->depth - 1];
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
336 // retry with the parent node to find a sibling
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
337 goto cx_tree_iter_search_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
338 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
339 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
340 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
341 if (iter->visit_on_exit && !iter->exiting) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
342 // iter is supposed to visit the node again
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
343 iter->exiting = true;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
344 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
345 iter->exiting = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
346 // move to the sibling
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
347 iter->counter++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
348 iter->node = next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
349 // new top of stack is the sibling
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
350 iter->stack[iter->depth - 1] = next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
351 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
352 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
353 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
354 // node has children, push the first child onto the stack and enter it
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
355 cx_array_simple_add(iter->stack, children);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
356 iter->node = children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
357 iter->counter++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
358 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
359 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
360
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
361 CxTreeIterator cx_tree_iterator(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
362 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
363 bool visit_on_exit,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
364 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
365 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
366 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
367 CxTreeIterator iter;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
368 iter.loc_children = loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
369 iter.loc_next = loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
370 iter.visit_on_exit = visit_on_exit;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
371
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
372 // initialize members
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
373 iter.node_next = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
374 iter.exiting = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
375 iter.skip = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
376
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
377 // assign base iterator functions
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
378 iter.base.mutating = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
379 iter.base.remove = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
380 iter.base.current_impl = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
381 iter.base.valid = cx_tree_iter_valid;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
382 iter.base.next = cx_tree_iter_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
383 iter.base.current = cx_tree_iter_current;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
384
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
385 // visit the root node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
386 iter.node = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
387 if (root != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
388 iter.stack_capacity = 16;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
389 iter.stack = malloc(sizeof(void *) * 16);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
390 iter.stack[0] = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
391 iter.counter = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
392 iter.depth = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
393 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
394 iter.stack_capacity = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
395 iter.stack = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
396 iter.counter = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
397 iter.depth = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
398 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
399
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
400 return iter;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
401 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
402
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
403 static bool cx_tree_visitor_valid(const void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
404 const struct cx_tree_visitor_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
405 return iter->node != NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
406 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
407
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
408 static void *cx_tree_visitor_current(const void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
409 const struct cx_tree_visitor_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
410 return iter->node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
411 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
412
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
413 cx_attr_nonnull
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
414 static void cx_tree_visitor_enqueue_siblings(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
415 struct cx_tree_visitor_s *iter, void *node, ptrdiff_t loc_next) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
416 node = tree_next(node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
417 while (node != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
418 struct cx_tree_visitor_queue_s *q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
419 q = malloc(sizeof(struct cx_tree_visitor_queue_s));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
420 q->depth = iter->queue_last->depth;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
421 q->node = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
422 iter->queue_last->next = q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
423 iter->queue_last = q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
424 node = tree_next(node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
425 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
426 iter->queue_last->next = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
427 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
428
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
429 static void cx_tree_visitor_next(void *it) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
430 struct cx_tree_visitor_s *iter = it;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
431 // protect us from misuse
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
432 if (!iter->base.valid(iter)) return;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
433
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
434 ptrdiff_t const loc_next = iter->loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
435 ptrdiff_t const loc_children = iter->loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
436
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
437 // add the children of the current node to the queue
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
438 // unless the skip flag is set
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
439 void *children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
440 if (iter->skip) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
441 iter->skip = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
442 children = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
443 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
444 children = tree_children(iter->node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
445 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
446 if (children != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
447 struct cx_tree_visitor_queue_s *q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
448 q = malloc(sizeof(struct cx_tree_visitor_queue_s));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
449 q->depth = iter->depth + 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
450 q->node = children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
451 if (iter->queue_last == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
452 assert(iter->queue_next == NULL);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
453 iter->queue_next = q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
454 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
455 iter->queue_last->next = q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
456 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
457 iter->queue_last = q;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
458 cx_tree_visitor_enqueue_siblings(iter, children, loc_next);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
459 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
460
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
461 // check if there is a next node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
462 if (iter->queue_next == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
463 iter->node = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
464 return;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
465 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
466
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
467 // dequeue the next node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
468 iter->node = iter->queue_next->node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
469 iter->depth = iter->queue_next->depth;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
470 {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
471 struct cx_tree_visitor_queue_s *q = iter->queue_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
472 iter->queue_next = q->next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
473 if (iter->queue_next == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
474 assert(iter->queue_last == q);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
475 iter->queue_last = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
476 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
477 free(q);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
478 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
479
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
480 // increment the node counter
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
481 iter->counter++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
482 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
483
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
484 CxTreeVisitor cx_tree_visitor(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
485 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
486 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
487 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
488 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
489 CxTreeVisitor iter;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
490 iter.loc_children = loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
491 iter.loc_next = loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
492
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
493 // initialize members
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
494 iter.skip = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
495 iter.queue_next = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
496 iter.queue_last = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
497
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
498 // assign base iterator functions
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
499 iter.base.mutating = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
500 iter.base.remove = false;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
501 iter.base.current_impl = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
502 iter.base.valid = cx_tree_visitor_valid;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
503 iter.base.next = cx_tree_visitor_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
504 iter.base.current = cx_tree_visitor_current;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
505
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
506 // visit the root node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
507 iter.node = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
508 if (root != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
509 iter.counter = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
510 iter.depth = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
511 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
512 iter.counter = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
513 iter.depth = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
514 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
515
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
516 return iter;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
517 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
518
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
519 static void cx_tree_add_link_duplicate(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
520 void *original, void *duplicate,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
521 ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
522 ptrdiff_t loc_prev, ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
523 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
524 void *shared_parent = tree_parent(original);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
525 if (shared_parent == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
526 cx_tree_link(original, duplicate, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
527 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
528 cx_tree_link(shared_parent, duplicate, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
529 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
530 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
531
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
532 static void cx_tree_add_link_new(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
533 void *parent, void *node, cx_tree_search_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
534 ptrdiff_t loc_parent, ptrdiff_t loc_children, ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
535 ptrdiff_t loc_prev, ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
536 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
537 // check the current children one by one,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
538 // if they could be children of the new node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
539 void *child = tree_children(parent);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
540 while (child != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
541 void *next = tree_next(child);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
542
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
543 if (sfunc(node, child) > 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
544 // the sibling could be a child -> re-link
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
545 cx_tree_link(node, child, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
546 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
547
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
548 child = next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
549 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
550
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
551 // add new node as new child
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
552 cx_tree_link(parent, node, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
553 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
554
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
555 int cx_tree_add(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
556 const void *src,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
557 cx_tree_search_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
558 cx_tree_node_create_func cfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
559 void *cdata,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
560 void **cnode,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
561 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
562 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
563 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
564 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
565 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
566 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
567 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
568 *cnode = cfunc(src, cdata);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
569 if (*cnode == NULL) return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
570 cx_tree_zero_pointers(*cnode, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
571
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
572 void *match = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
573 int result = cx_tree_search(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
574 root,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
575 0,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
576 *cnode,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
577 sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
578 &match,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
579 loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
580 loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
581 );
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
582
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
583 if (result < 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
584 // node does not fit into the tree - return non-zero value
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
585 return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
586 } else if (result == 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
587 // data already found in the tree, link duplicate
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
588 cx_tree_add_link_duplicate(match, *cnode, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
589 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
590 // closest match found, add new node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
591 cx_tree_add_link_new(match, *cnode, sfunc, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
592 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
593
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
594 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
595 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
596
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
597 unsigned int cx_tree_add_look_around_depth = 3;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
598
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
599 size_t cx_tree_add_iter(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
600 struct cx_iterator_base_s *iter,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
601 size_t num,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
602 cx_tree_search_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
603 cx_tree_node_create_func cfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
604 void *cdata,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
605 void **failed,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
606 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
607 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
608 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
609 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
610 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
611 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
612 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
613 // erase the failed pointer
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
614 *failed = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
615
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
616 // iter not valid? cancel...
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
617 if (!iter->valid(iter)) return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
618
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
619 size_t processed = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
620 void *current_node = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
621 const void *elem;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
622
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
623 for (void **eptr; processed < num &&
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
624 iter->valid(iter) && (eptr = iter->current(iter)) != NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
625 iter->next(iter)) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
626 elem = *eptr;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
627
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
628 // create the new node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
629 void *new_node = cfunc(elem, cdata);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
630 if (new_node == NULL) return processed;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
631 cx_tree_zero_pointers(new_node, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
632
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
633 // start searching from current node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
634 void *match;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
635 int result;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
636 unsigned int look_around_retries = cx_tree_add_look_around_depth;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
637 cx_tree_add_look_around_retry:
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
638 result = cx_tree_search(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
639 current_node,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
640 0,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
641 new_node,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
642 sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
643 &match,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
644 loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
645 loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
646 );
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
647
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
648 if (result < 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
649 // traverse upwards and try to find better parents
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
650 void *parent = tree_parent(current_node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
651 if (parent != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
652 if (look_around_retries > 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
653 look_around_retries--;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
654 current_node = parent;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
655 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
656 // look around retries exhausted, start from the root
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
657 current_node = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
658 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
659 goto cx_tree_add_look_around_retry;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
660 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
661 // no parents. so we failed
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
662 *failed = new_node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
663 return processed;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
664 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
665 } else if (result == 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
666 // data already found in the tree, link duplicate
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
667 cx_tree_add_link_duplicate(match, new_node, cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
668 // but stick with the original match, in case we needed a new root
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
669 current_node = match;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
670 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
671 // closest match found, add new node as child
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
672 cx_tree_add_link_new(match, new_node, sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
673 cx_tree_ptr_locations);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
674 current_node = match;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
675 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
676
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
677 processed++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
678 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
679 return processed;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
680 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
681
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
682 size_t cx_tree_add_array(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
683 const void *src,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
684 size_t num,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
685 size_t elem_size,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
686 cx_tree_search_func sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
687 cx_tree_node_create_func cfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
688 void *cdata,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
689 void **failed,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
690 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
691 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
692 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
693 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
694 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
695 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
696 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
697 // erase failed pointer
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
698 *failed = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
699
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
700 // super special case: zero elements
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
701 if (num == 0) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
702 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
703 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
704
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
705 // special case: one element does not need an iterator
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
706 if (num == 1) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
707 void *node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
708 if (0 == cx_tree_add(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
709 src, sfunc, cfunc, cdata, &node, root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
710 loc_parent, loc_children, loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
711 loc_prev, loc_next)) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
712 return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
713 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
714 *failed = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
715 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
716 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
717 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
718
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
719 // otherwise, create iterator and hand over to other function
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
720 CxIterator iter = cxIterator(src, elem_size, num);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
721 return cx_tree_add_iter(cxIteratorRef(iter), num, sfunc,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
722 cfunc, cdata, failed, root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
723 loc_parent, loc_children, loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
724 loc_prev, loc_next);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
725 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
726
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
727 static int cx_tree_default_insert_element(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
728 CxTree *tree,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
729 const void *data
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
730 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
731 void *node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
732 if (tree->root == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
733 node = tree->node_create(data, tree);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
734 if (node == NULL) return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
735 cx_tree_zero_pointers(node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
736 tree->root = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
737 tree->size = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
738 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
739 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
740 int result = cx_tree_add(data, tree->search, tree->node_create,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
741 tree, &node, tree->root, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
742 if (0 == result) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
743 tree->size++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
744 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
745 cxFree(tree->allocator, node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
746 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
747 return result;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
748 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
749
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
750 static size_t cx_tree_default_insert_many(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
751 CxTree *tree,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
752 struct cx_iterator_base_s *iter,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
753 size_t n
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
754 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
755 size_t ins = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
756 if (!iter->valid(iter)) return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
757 if (tree->root == NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
758 // use the first element from the iter to create the root node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
759 void **eptr = iter->current(iter);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
760 void *node = tree->node_create(*eptr, tree);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
761 if (node == NULL) return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
762 cx_tree_zero_pointers(node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
763 tree->root = node;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
764 ins = 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
765 iter->next(iter);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
766 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
767 void *failed;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
768 ins += cx_tree_add_iter(iter, n, tree->search, tree->node_create,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
769 tree, &failed, tree->root, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
770 tree->size += ins;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
771 if (ins < n) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
772 cxFree(tree->allocator, failed);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
773 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
774 return ins;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
775 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
776
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
777 static void *cx_tree_default_find(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
778 CxTree *tree,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
779 const void *subtree,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
780 const void *data,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
781 size_t depth
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
782 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
783 if (tree->root == NULL) return NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
784
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
785 void *found;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
786 if (0 == cx_tree_search_data(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
787 subtree,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
788 depth,
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
789 data,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
790 tree->search_data,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
791 &found,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
792 tree->loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
793 tree->loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
794 )) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
795 return found;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
796 } else {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
797 return NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
798 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
799 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
800
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
801 static cx_tree_class cx_tree_default_class = {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
802 cx_tree_default_insert_element,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
803 cx_tree_default_insert_many,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
804 cx_tree_default_find
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
805 };
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
806
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
807 CxTree *cxTreeCreate(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
808 const CxAllocator *allocator,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
809 cx_tree_node_create_func create_func,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
810 cx_tree_search_func search_func,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
811 cx_tree_search_data_func search_data_func,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
812 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
813 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
814 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
815 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
816 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
817 ) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
818 if (allocator == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
819 allocator = cxDefaultAllocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
820 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
821 assert(create_func != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
822 assert(search_func != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
823 assert(search_data_func != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
824
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
825 CxTree *tree = cxMalloc(allocator, sizeof(CxTree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
826 if (tree == NULL) return NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
827
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
828 tree->cl = &cx_tree_default_class;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
829 tree->allocator = allocator;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
830 tree->node_create = create_func;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
831 tree->search = search_func;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
832 tree->search_data = search_data_func;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
833 tree->simple_destructor = NULL;
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
834 tree->advanced_destructor = (cx_destructor_func2) cxFree;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
835 tree->destructor_data = (void *) allocator;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
836 tree->loc_parent = loc_parent;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
837 tree->loc_children = loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
838 tree->loc_last_child = loc_last_child;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
839 tree->loc_prev = loc_prev;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
840 tree->loc_next = loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
841 tree->root = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
842 tree->size = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
843
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
844 return tree;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
845 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
846
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
847 void cxTreeFree(CxTree *tree) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
848 if (tree == NULL) return;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
849 if (tree->root != NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
850 cxTreeClear(tree);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
851 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
852 cxFree(tree->allocator, tree);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
853 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
854
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
855 CxTree *cxTreeCreateWrapped(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
856 const CxAllocator *allocator,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
857 void *root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
858 ptrdiff_t loc_parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
859 ptrdiff_t loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
860 ptrdiff_t loc_last_child,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
861 ptrdiff_t loc_prev,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
862 ptrdiff_t loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
863 ) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
864 if (allocator == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
865 allocator = cxDefaultAllocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
866 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
867 assert(root != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
868
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
869 CxTree *tree = cxMalloc(allocator, sizeof(CxTree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
870 if (tree == NULL) return NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
871
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
872 tree->cl = &cx_tree_default_class;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
873 // set the allocator anyway, just in case...
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
874 tree->allocator = allocator;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
875 tree->node_create = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
876 tree->search = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
877 tree->search_data = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
878 tree->simple_destructor = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
879 tree->advanced_destructor = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
880 tree->destructor_data = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
881 tree->loc_parent = loc_parent;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
882 tree->loc_children = loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
883 tree->loc_last_child = loc_last_child;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
884 tree->loc_prev = loc_prev;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
885 tree->loc_next = loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
886 tree->root = root;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
887 tree->size = cxTreeSubtreeSize(tree, root);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
888 return tree;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
889 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
890
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
891 void cxTreeSetParent(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
892 CxTree *tree,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
893 void *parent,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
894 void *child
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
895 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
896 size_t loc_parent = tree->loc_parent;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
897 if (tree_parent(child) == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
898 tree->size++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
899 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
900 cx_tree_link(parent, child, cx_tree_node_layout(tree));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
901 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
902
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
903 void cxTreeAddChildNode(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
904 CxTree *tree,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
905 void *parent,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
906 void *child
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
907 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
908 cx_tree_link(parent, child, cx_tree_node_layout(tree));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
909 tree->size++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
910 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
911
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
912 int cxTreeAddChild(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
913 CxTree *tree,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
914 void *parent,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
915 const void *data) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
916 void *node = tree->node_create(data, tree);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
917 if (node == NULL) return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
918 cx_tree_zero_pointers(node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
919 cx_tree_link(parent, node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
920 tree->size++;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
921 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
922 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
923
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
924 size_t cxTreeSubtreeSize(CxTree *tree, void *subtree_root) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
925 CxTreeVisitor visitor = cx_tree_visitor(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
926 subtree_root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
927 tree->loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
928 tree->loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
929 );
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
930 while (cxIteratorValid(visitor)) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
931 cxIteratorNext(visitor);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
932 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
933 return visitor.counter;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
934 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
935
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
936 size_t cxTreeSubtreeDepth(CxTree *tree, void *subtree_root) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
937 CxTreeVisitor visitor = cx_tree_visitor(
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
938 subtree_root,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
939 tree->loc_children,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
940 tree->loc_next
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
941 );
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
942 while (cxIteratorValid(visitor)) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
943 cxIteratorNext(visitor);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
944 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
945 return visitor.depth;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
946 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
947
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
948 size_t cxTreeDepth(CxTree *tree) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
949 CxTreeVisitor visitor = cx_tree_visitor(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
950 tree->root, tree->loc_children, tree->loc_next
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
951 );
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
952 while (cxIteratorValid(visitor)) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
953 cxIteratorNext(visitor);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
954 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
955 return visitor.depth;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
956 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
957
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
958 int cxTreeRemoveNode(
324
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
959 CxTree *tree,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
960 void *node,
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
961 cx_tree_relink_func relink_func
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
962 ) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
963 if (node == tree->root) return 1;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
964
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
965 // determine the new parent
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
966 ptrdiff_t loc_parent = tree->loc_parent;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
967 void *new_parent = tree_parent(node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
968
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
969 // first, unlink from the parent
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
970 cx_tree_unlink(node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
971
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
972 // then relink each child
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
973 ptrdiff_t loc_children = tree->loc_children;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
974 ptrdiff_t loc_next = tree->loc_next;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
975 void *child = tree_children(node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
976 while (child != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
977 // forcibly set the parent to NULL - we do not use the unlink function
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
978 // because that would unnecessarily modify the children linked list
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
979 tree_parent(child) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
980
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
981 // update contents, if required
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
982 if (relink_func != NULL) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
983 relink_func(child, node, new_parent);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
984 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
985
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
986 // link to new parent
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
987 cx_tree_link(new_parent, child, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
988
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
989 // proceed to next child
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
990 child = tree_next(child);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
991 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
992
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
993 // clear the linked list of the removed node
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
994 tree_children(node) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
995 ptrdiff_t loc_last_child = tree->loc_last_child;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
996 if (loc_last_child >= 0) tree_last_child(node) = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
997
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
998 // the tree now has one member less
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
999 tree->size--;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1000
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1001 return 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1002 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1003
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1004 void cxTreeRemoveSubtree(CxTree *tree, void *node) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1005 if (node == tree->root) {
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1006 tree->root = NULL;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1007 tree->size = 0;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1008 return;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1009 }
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1010 size_t subtree_size = cxTreeSubtreeSize(tree, node);
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1011 cx_tree_unlink(node, cx_tree_node_layout(tree));
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1012 tree->size -= subtree_size;
ce13a778654a update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 253
diff changeset
1013 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1014
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1015 int cxTreeDestroyNode(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1016 CxTree *tree,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1017 void *node,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1018 cx_tree_relink_func relink_func
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1019 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1020 int result = cxTreeRemoveNode(tree, node, relink_func);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1021 if (result == 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1022 if (tree->simple_destructor) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1023 tree->simple_destructor(node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1024 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1025 if (tree->advanced_destructor) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1026 tree->advanced_destructor(tree->destructor_data, node);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1027 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1028 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1029 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1030 return result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1031 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1032 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1033
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1034 void cxTreeDestroySubtree(CxTree *tree, void *node) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1035 cx_tree_unlink(node, cx_tree_node_layout(tree));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1036 CxTreeIterator iter = cx_tree_iterator(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1037 node, true,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1038 tree->loc_children, tree->loc_next
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1039 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1040 cx_foreach(void *, child, iter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1041 if (iter.exiting) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1042 if (tree->simple_destructor) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1043 tree->simple_destructor(child);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1044 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1045 if (tree->advanced_destructor) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1046 tree->advanced_destructor(tree->destructor_data, child);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1047 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1048 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1049 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1050 tree->size -= iter.counter;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1051 if (node == tree->root) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1052 tree->root = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1053 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 324
diff changeset
1054 }

mercurial