12 static void trap(void)
18 static void validate_subtree(const struct quadtree *node)
25 printf("Attempted to validate a null pointer\n");
30 for (i = 0; i < 4; i++) {
34 if (node->child[i]->parent != node) {
35 printf("%s:%d Fatal! Tree inconsistency detected at "
36 "child %d %p in node %p, incorrent parent %p\n",
37 __func__, __LINE__, i, node->child[i], node,
38 node->child[i]->parent);
44 if (node->child[i] == node->parent) {
45 printf("%s:%d Fatal! Tree loop detected "
46 "at child %d in node %p\n",
47 __func__, __LINE__, i, node);
53 children += node->child[i]->children + 1;
55 validate_subtree(node->child[i]);
58 if (node->depth < 0) {
59 printf("%s:%d Tree statistics inconsistency detected! "
60 "Negative depth: %ld\n",
61 __func__, __LINE__, node->depth);
64 if (node->children != children) {
65 printf("%s:%d Tree statistics inconsistency detected! "
66 "child count mismatch. Expected %ld, got %ld\n",
67 __func__, __LINE__, children, node->children);
72 for (i = 0; i < 4 && node->children; i++) {
76 if (node->depth == node->child[i]->depth + 1)
79 if (node->child[i]->depth > node->depth) {
80 printf("%s:%d Tree statistics inconsistency detected! "
81 "child depth mismatch %ld > %ld\n",
82 __func__, __LINE__, node->child[i]->depth,
88 printf("%s:%d Tree statistics inconsistency detected! "
89 "child depth mismatch.",
96 static void validate_tree(const struct quadtree *node)
99 validate_subtree(quadtree_find_parent(node));
103 * quadtree_add - add a node to a quadtree
104 * @parent: parent node
105 * @new: the new node to be added
106 * @compare: pointer to a comparison function
108 * Add a node to a quadtree. The tree is kept in order, the new node
109 * is placed in the end of appropriate branch. The compare function is
110 * used to compare the new node against a branch in the tree. The
111 * comparison function must have following return value depending on
112 * the position of node a compared to the position of node b:
119 * The case of nodes sharing identical coordinates is not taken into
123 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
124 struct quadtree_ops *ops)
130 validate_tree(parent);
132 ret = ops->compare(parent, new);
134 if (ret < 0 || ret >= 4) {
135 printf("Invalid comparison result of %d\n", ret);
139 if (parent->child[ret])
140 return quadtree_add(parent->child[ret], new, ops);
142 parent->child[ret] = new;
143 new->parent = parent;
146 printf("adding node %p to parent %p\n", new, parent);
150 quadtree_recalculate_parent_stats(new, ops);
158 * _quadtree_reposition_reqursively - Move everything under and
159 * including a given node under the new root node
163 _quadtree_reposition_reqursively(struct quadtree *root,
164 struct quadtree *node,
165 struct quadtree_ops *ops)
171 /* First remove all children, if any */
173 for (i = 0; i < 4; i++) {
177 if (node->child[i] == node ||
178 node->child[i] == node->parent)
181 _quadtree_reposition_reqursively(root, node->child[i], ops);
185 /* There are no children left, reset stats */
189 /* Then remove this node under the new root. */
190 quadtree_add(root, node, ops);
194 * quadtree_del - Detach a node from the tree
196 * Return value: The new root node of the tree. If we are detaching
197 * anything but the root node of the entire tree, the returned root
198 * value will be the original root of the tree.
200 struct quadtree *quadtree_del(struct quadtree *node,
201 struct quadtree_ops *ops)
203 struct quadtree *parent = 0;
207 printf("Deleting node %p under parent %p\n",
212 * We are deleting the root node. This means we have to select
213 * a new root node and reconstruct the entire tree under it
217 for (i = 0; i < 4; i++) {
222 parent = node->child[i];
226 _quadtree_reposition_reqursively(parent,
236 * We are not deleting the parent. Detach the node from the
237 * parent abd relocate the children. The node will be then
238 * detached from the tree.
241 for (i = 0; i < 4; i++) {
242 if (node->parent->child[i] == node) {
243 node->parent->child[i] = 0;
248 printf("%s:%d Fatal! Tree inconsistency detected\n",
253 /* Fix parent stats */
254 quadtree_recalculate_parent_stats(node->parent, ops);
257 * The sub branch is now detached from the main tree. Continue
258 * relocating the detached branch.
261 for (i = 0; i < 4; i++) {
265 _quadtree_reposition_reqursively(node->parent, node->child[i],
270 parent = quadtree_find_parent(node);
276 validate_tree(parent);
281 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
283 int direction, count = 0;
285 direction = it->direction(head, (struct quadtree_iterator *)it);
287 if ((direction & QUADTREE_UPLEFT) && head->child[0])
288 count += _walk_tree(head->child[0], it);
290 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
291 count += _walk_tree(head->child[1], it);
293 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
294 count += _walk_tree(head->child[2], it);
296 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
297 count += _walk_tree(head->child[3], it);
299 if ((direction & QUADTREE_SELF) && it->callback) {
300 it->callback(head, (struct quadtree_iterator *)it);
307 int walk_quadtree(const struct quadtree_iterator *it)
309 return _walk_tree(it->head, it);