]> git.itanic.dy.fi Git - sdl-planets/blob - quadtree.c
quadtree: quadtree_del: Fix tree integrity bug
[sdl-planets] / quadtree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "quadtree.h"
5
6 #ifdef DEBUG
7 #define debug 1
8 #else
9 #define debug 0
10 #endif
11
12 static void trap(void)
13 {
14         if (debug)
15                 exit(1);
16 }
17
18 static void validate_subtree(const struct quadtree *node)
19 {
20         int i;
21         long int children;
22         children = 0;
23
24         for (i = 0; i < 4; i++) {
25                 if (!node->child[i])
26                         continue;
27
28                 if (node->child[i]->parent != node) {
29                         printf("%s:%d Fatal! Tree inconsistency detected "
30                                "at child %d in node %p\n",
31                                __func__, __LINE__, i, node);
32                         trap();
33                 }
34
35                 if (node->parent) {
36                         if (node->child[i] == node->parent) {
37                                 printf("%s:%d Fatal! Tree loop detected "
38                                        "at child %d in node %p\n",
39                                        __func__, __LINE__, i, node);
40                                 trap();
41                         }
42                 }
43
44                 children += node->child[i]->children + 1;
45
46                 validate_subtree(node->child[i]);
47         }
48
49         if (node->depth < 0) {
50                 printf("%s:%d Tree statistics inconsistency detected! "
51                        "Negative depth: %ld\n",
52                        __func__, __LINE__, node->depth);
53         }
54
55         if (node->children != children) {
56                 printf("%s:%d Tree statistics inconsistency detected! "
57                        "child count mismatch. Expected %ld, got %ld\n",
58                        __func__, __LINE__, children, node->children);
59                 trap();
60         }
61
62         for (i = 0; i < 4 && node->children; i++) {
63                 if (!node->child[i])
64                         continue;
65
66                 if (node->depth == node->child[i]->depth + 1)
67                         break;
68
69                 if (node->child[i]->depth > node->depth) {
70                         printf("%s:%d Tree statistics inconsistency detected! "
71                                "child depth mismatch %ld > %ld\n",
72                                __func__, __LINE__, node->child[i]->depth,
73                                node->depth);
74                 }
75         }
76
77         if (i == 4) {
78                 printf("%s:%d Tree statistics inconsistency detected! "
79                        "child depth mismatch.",
80                        __func__, __LINE__);
81                 trap();
82         }
83 }
84
85 static void validate_tree(const struct quadtree *node)
86 {
87         if (debug)
88                 validate_subtree(quadtree_find_parent(node));
89 }
90
91 /**
92  * quadtree_add - add a node to a quadtree
93  * @parent: parent node
94  * @new: the new node to be added
95  * @compare: pointer to a comparison function
96  *
97  * Add a node to a quadtree. The tree is kept in order, the new node
98  * is placed in the end of appropriate branch. The compare function is
99  * used to compare the new node against a branch in the tree. The
100  * comparison function must have following return value depending on
101  * the position of node a compared to the position of node b:
102  *
103  * 0: upper left
104  * 1: upper right
105  * 2: lower left
106  * 3: lower right
107  *
108  * The case of nodes sharing identical coordinates is not taken into
109  * account at all.
110  */
111
112 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
113                               struct quadtree_ops *ops)
114 {
115         int ret;
116         if (parent == new)
117                 trap();
118
119         validate_tree(parent);
120
121         ret = ops->compare(parent, new);
122
123         if (ret < 0 || ret >= 4) {
124                 printf("Invalid comparison result of %d\n", ret);
125                 return 0;
126         }
127
128         if (parent->child[ret])
129                 return quadtree_add(parent->child[ret], new, ops);
130
131         parent->child[ret] = new;
132         new->parent = parent;
133
134         quadtree_recalculate_parent_stats(new, ops);
135
136         validate_tree(new);
137
138         return new;
139 }
140
141 /*
142  * _quadtree_reposition_reqursively - Move everything under and
143  * including a given node under the new root node
144  */
145
146 static void
147 _quadtree_reposition_reqursively(struct quadtree *root,
148                                  struct quadtree *node,
149                                  struct quadtree_ops *ops)
150 {
151         int i;
152
153         validate_tree(root);
154
155         /* First remove all children, if any */
156
157         for (i = 0; i < 4; i++) {
158                 if (!node->child[i])
159                         continue;
160
161                 if (node->child[i] == node ||
162                     node->child[i] == node->parent)
163                         trap();
164
165                 _quadtree_reposition_reqursively(root, node->child[i], ops);
166                 node->child[i] = 0;
167         }
168
169         /* There are no children left, reset stats */
170         node->depth = 0;
171         node->children = 0;
172
173         /* Then remove this node under the new root. */
174         quadtree_add(root, node, ops);
175 }
176
177 /*
178  * quadtree_del - Detach a node from the tree
179  *
180  * Return value: The new root node of the tree. If we are detaching
181  * anything but the root node of the entire tree, the returned root
182  * value will be the original root of the tree.
183  */
184 struct quadtree *quadtree_del(struct quadtree *node,
185                               struct quadtree_ops *ops)
186 {
187         struct quadtree *parent = 0;
188         int i;
189
190         /*
191          * We are deleting the root node. This means we have to select
192          * a new root node and reconstruct the entire tree under it
193          * again.
194          */
195         if (!node->parent) {
196                 for (i = 0; i < 4; i++) {
197                         if (!node->child[i])
198                                 continue;
199
200                         if (!parent) {
201                                 parent = node->child[i];
202                                 parent->parent = 0;
203                                 continue;
204                         }
205                         _quadtree_reposition_reqursively(parent,
206                                                          node->child[i],
207                                                          ops);
208                         node->child[i] = 0;
209                 }
210
211                 return parent;
212         }
213
214         /*
215          * We are not deleting the parent. Detach the node from the
216          * parent abd relocate the children. The node will be then
217          * detached from the tree.
218          */
219
220         for (i = 0; i < 4; i++) {
221                 if (node->parent->child[i] == node) {
222                         node->parent->child[i] = 0;
223                         break;
224                 }
225         }
226         if (i == 4) {
227                 printf("%s:%d Fatal! Tree inconsistency detected\n",
228                        __func__, __LINE__);
229                 trap();
230         }
231
232         /* Fix parent stats */
233         quadtree_recalculate_parent_stats(node->parent, ops);
234
235         /*
236          * The sub branch is now detached from the main tree. Continue
237          * relocating the detached branch.
238          */
239
240         for (i = 0; i < 4; i++) {
241                 if (!node->child[i])
242                         continue;
243
244                 _quadtree_reposition_reqursively(node->parent, node->child[i],
245                                                  ops);
246                 node->child[i] = 0;
247         }
248
249         parent = quadtree_find_parent(node);
250         node->parent = 0;
251
252         node->children = 0;
253         node->depth = 0;
254
255         validate_tree(parent);
256         return parent;
257 }
258
259
260 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
261 {
262         int direction, count = 0;
263
264         direction = it->direction(head, (struct quadtree_iterator *)it);
265
266         if ((direction & QUADTREE_UPLEFT) && head->child[0])
267                 count += _walk_tree(head->child[0], it);
268
269         if ((direction & QUADTREE_UPRIGHT) && head->child[1])
270                 count += _walk_tree(head->child[1], it);
271
272         if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
273                 count += _walk_tree(head->child[2], it);
274
275         if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
276                 count += _walk_tree(head->child[3], it);
277
278         if ((direction & QUADTREE_SELF) && it->callback) {
279                 it->callback(head, (struct quadtree_iterator *)it);
280                 count++;
281         }
282
283         return count;
284 }
285
286 int walk_quadtree(const struct quadtree_iterator *it)
287 {
288         return _walk_tree(it->head, it);
289 }