12 static void trap(void)
18 static void validate_subtree(const struct quadtree *node)
24 for (i = 0; i < 4; i++) {
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);
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);
44 children += node->child[i]->children + 1;
46 validate_subtree(node->child[i]);
49 if (node->depth < 0) {
50 printf("%s:%d Tree statistics inconsistency detected! "
51 "Negative depth: %ld\n",
52 __func__, __LINE__, node->depth);
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);
62 for (i = 0; i < 4 && node->children; i++) {
66 if (node->depth == node->child[i]->depth + 1)
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,
78 printf("%s:%d Tree statistics inconsistency detected! "
79 "child depth mismatch.",
85 static void validate_tree(const struct quadtree *node)
88 validate_subtree(quadtree_find_parent(node));
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
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:
108 * The case of nodes sharing identical coordinates is not taken into
112 struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new,
113 struct quadtree_ops *ops)
119 validate_tree(parent);
121 ret = ops->compare(parent, new);
123 if (ret < 0 || ret >= 4) {
124 printf("Invalid comparison result of %d\n", ret);
128 if (parent->child[ret])
129 return quadtree_add(parent->child[ret], new, ops);
131 parent->child[ret] = new;
132 new->parent = parent;
134 quadtree_recalculate_parent_stats(new, ops);
142 * _quadtree_reposition_reqursively - Move everything under and
143 * including a given node under the new root node
147 _quadtree_reposition_reqursively(struct quadtree *root,
148 struct quadtree *node,
149 struct quadtree_ops *ops)
155 /* First remove all children, if any */
157 for (i = 0; i < 4; i++) {
161 if (node->child[i] == node ||
162 node->child[i] == node->parent)
165 _quadtree_reposition_reqursively(root, node->child[i], ops);
169 /* There are no children left, reset stats */
173 /* Then remove this node under the new root. */
174 quadtree_add(root, node, ops);
178 * quadtree_del - Detach a node from the tree
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.
184 struct quadtree *quadtree_del(struct quadtree *node,
185 struct quadtree_ops *ops)
187 struct quadtree *parent = 0;
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
196 for (i = 0; i < 4; i++) {
201 parent = node->child[i];
204 _quadtree_reposition_reqursively(parent,
214 * We are not deleting the parent. Detach the node from the
215 * parent abd relocate the children. The node will be then
216 * detached from the tree.
219 for (i = 0; i < 4; i++) {
220 if (node->parent->child[i] == node) {
221 node->parent->child[i] = 0;
226 printf("%s:%d Fatal! Tree inconsistency detected\n",
231 /* Fix parent stats */
232 quadtree_recalculate_parent_stats(node->parent, ops);
235 * The sub branch is now detached from the main tree. Continue
236 * relocating the detached branch.
239 for (i = 0; i < 4; i++) {
243 _quadtree_reposition_reqursively(node->parent, node->child[i],
248 parent = quadtree_find_parent(node);
254 validate_tree(parent);
259 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
261 int direction, count = 0;
263 direction = it->direction(head, (struct quadtree_iterator *)it);
265 if ((direction & QUADTREE_UPLEFT) && head->child[0])
266 count += _walk_tree(head->child[0], it);
268 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
269 count += _walk_tree(head->child[1], it);
271 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
272 count += _walk_tree(head->child[2], it);
274 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
275 count += _walk_tree(head->child[3], it);
277 if ((direction & QUADTREE_SELF) && it->callback) {
278 it->callback(head, (struct quadtree_iterator *)it);
285 int walk_quadtree(const struct quadtree_iterator *it)
287 return _walk_tree(it->head, it);