12 static void _trap(int line)
15 printf("Trapped from line %d, use debugger to get backtrace\n",
22 #define trap() _trap(__LINE__)
24 static int quadtree_compare_coord(const struct vector *a,
25 const struct vector *b)
41 static int quadtree_compare(const struct quadtree *a, const struct quadtree *b)
43 return quadtree_compare_coord(&a->pos, &b->pos);
46 static void validate_subtree(const struct quadtree *node)
53 printf("Attempted to validate a null pointer\n");
58 for (i = 0; i < 4; i++) {
62 if (node->child[i]->parent != node) {
63 printf("%s:%d Fatal! Tree inconsistency detected at "
64 "child %d %p in node %p, incorrent parent %p\n",
65 __func__, __LINE__, i, node->child[i], node,
66 node->child[i]->parent);
72 if (node->child[i] == node->parent) {
73 printf("%s:%d Fatal! Tree loop detected "
74 "at child %d in node %p\n",
75 __func__, __LINE__, i, node);
81 children += node->child[i]->children + 1;
83 validate_subtree(node->child[i]);
86 if (node->depth < 0) {
87 printf("%s:%d Tree statistics inconsistency detected! "
88 "Negative depth: %ld\n",
89 __func__, __LINE__, node->depth);
92 if (node->children != children) {
93 printf("%s:%d Tree statistics inconsistency detected! "
94 "child count mismatch. Expected %ld, got %ld\n",
95 __func__, __LINE__, children, node->children);
100 for (i = 0; i < 4 && node->children; i++) {
104 if (node->depth == node->child[i]->depth + 1)
107 if (node->child[i]->depth > node->depth) {
108 printf("%s:%d Tree statistics inconsistency detected! "
109 "child depth mismatch %ld > %ld\n",
110 __func__, __LINE__, node->child[i]->depth,
116 printf("%s:%d Tree statistics inconsistency detected! "
117 "child depth mismatch.",
124 static void validate_tree(const struct quadtree *node)
127 validate_subtree(quadtree_find_parent(node));
131 * quadtree_add - add a node to a quadtree
132 * @parent: parent node
133 * @new: the new node to be added
135 * Add a node to a quadtree. The tree is kept in order, the new node
136 * is placed in the end of appropriate branch.
138 * The case of nodes sharing identical coordinates is not taken into
142 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
143 struct quadtree_ops *ops)
149 validate_tree(parent);
151 ret = quadtree_compare(parent, new);
153 if (ret < 0 || ret >= 4) {
154 printf("Invalid comparison result of %d\n", ret);
158 if (parent->child[ret])
159 return quadtree_add(parent->child[ret], new, ops);
161 parent->child[ret] = new;
162 new->parent = parent;
163 for (i = 0; i < 4; i++)
167 printf("adding node %p to parent %p\n", new, parent);
171 quadtree_recalculate_parent_stats(new, ops);
179 * _qt_optimal_del - Delete nodes from a detached subtree in optimal order
181 * All nodes are moved under the new parent node
183 static void _qt_optimal_del(struct quadtree *parent, struct quadtree *node,
184 struct quadtree_ops *ops, int parents)
186 struct quadtree *child[4] = {0};
187 int i, children = 0, max_c, max_i;
189 for (i = 0; i < 4; i++) {
190 if (!node->child[i]) {
195 child[children] = node->child[i];
196 node->child[i] = NULL;
201 * Try to remove the nodes in such order that the node which
202 * has child number one quarter of the parent's child numer
203 * gets deleted first.
206 if (node->children <= parents / 4) {
208 printf("%d: Moving node %p away from parent %p\n",
209 __LINE__, node, parent);
212 validate_tree(parent);
213 quadtree_add(parent, node, ops);
221 for (i = 0; i < 4; i++) {
225 if (max_c < child[i]->children) {
226 max_c = child[i]->children;
231 _qt_optimal_del(parent, child[max_i], ops, parents / 4);
237 validate_tree(parent);
239 printf("%d: Moving node %p away from parent %p\n",
240 __LINE__, node, parent);
243 quadtree_add(parent, node, ops);
248 * quadtree_del - Detach a node from the tree
250 * Return value: The new root node of the tree. If we are detaching
251 * anything but the root node of the entire tree, the returned root
252 * value will be the original root of the tree.
254 struct quadtree *quadtree_del(struct quadtree *node,
255 struct quadtree_ops *ops)
257 struct quadtree *parent = 0;
261 printf("Deleting node %p under parent %p\n",
266 * We are deleting the root node. This means we have to select
267 * a new root node and reconstruct the entire tree under it
271 for (i = 0; i < 4; i++) {
276 parent = node->child[i];
280 _qt_optimal_del(parent, node->child[i], ops,
289 * We are not deleting the parent. Detach the node from the
290 * parent abd relocate the children. The node will be then
291 * detached from the tree.
294 for (i = 0; i < 4; i++) {
295 if (node->parent->child[i] == node) {
296 node->parent->child[i] = 0;
303 parent = node->parent;
306 * The sub branch is now detached from the main tree. Fix the
309 quadtree_recalculate_parent_stats(node->parent, ops);
311 parent = quadtree_find_parent(parent);
313 if (node->children) {
314 for (i = 0; i < 4; i++) {
318 _qt_optimal_del(parent, node->child[i], ops,
323 validate_tree(parent);
328 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
330 int direction, count = 0;
332 direction = it->direction(head, (struct quadtree_iterator *)it);
334 if ((direction & QUADTREE_UPLEFT) && head->child[0])
335 count += _walk_tree(head->child[0], it);
337 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
338 count += _walk_tree(head->child[1], it);
340 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
341 count += _walk_tree(head->child[2], it);
343 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
344 count += _walk_tree(head->child[3], it);
346 if ((direction & QUADTREE_SELF) && it->callback) {
347 it->callback(head, (struct quadtree_iterator *)it);
354 int walk_quadtree(const struct quadtree_iterator *it)
356 return _walk_tree(it->head, it);