11 static void trap(void )
17 static void validate_subtree(const struct quadtree *node)
21 for (i = 0; i < 4; i++) {
25 if (node->child[i]->parent != node) {
26 printf("%s:%d Fatal! Tree inconsistency detected "
27 "at child %d in node %p\n",
28 __FUNCTION__, __LINE__, i, node);
32 if (node->child[i] == node->parent) {
33 printf("%s:%d Fatal! Tree loop detected "
34 "at child %d in node %p\n",
35 __FUNCTION__, __LINE__, i, node);
40 validate_subtree(node->child[i]);
44 static void validate_tree(const struct quadtree *node)
46 const struct quadtree *parent = quadtree_find_parent(node);
49 validate_subtree(parent);
53 * quadtree_add - add a node to a quadtree
54 * @parent: parent node
55 * @new: the new node to be added
56 * @compare: pointer to a comparison function
58 * Add a node to a quadtree. The tree is kept in order, the new node
59 * is placed in the end of appropriate branch. The compare function is
60 * used to compare the new node against a branch in the tree. The
61 * comparison function must have following return value depending on
62 * the position of node a compared to the position of node b:
69 * The case of nodes sharing identical coordinates is not taken into
73 struct quadtree *quadtree_add(struct quadtree *parent,
75 int (*compare)(struct quadtree *a,
82 validate_tree(parent);
84 ret = compare(parent, new);
86 if (ret < 0 || ret >= 4) {
87 printf("Invalid comparison result of %d\n", ret);
91 if (parent->child[ret])
92 return quadtree_add(parent->child[ret], new, compare);
94 parent->child[ret] = new;
103 * _quadtree_reposition_reqursively - Move everything under and
104 * including a given node under the new root node
108 _quadtree_reposition_reqursively(struct quadtree *root,
109 struct quadtree *node,
110 int (*compare)(struct quadtree *a,
117 /* First remove all children, if any */
119 for (i = 0; i < 4; i++) {
123 if (node->child[i] == node ||
124 node->child[i] == node->parent)
127 _quadtree_reposition_reqursively(root, node->child[i], compare);
131 /* Then remove this node under the new root. */
132 quadtree_add(root, node, compare);
136 * quadtree_del - Detach a node from the tree
138 * Return value: The new root node of the tree. If we are detaching
139 * anything but the root node of the entire tree, the returned root
140 * value will be the original root of the tree.
142 struct quadtree *quadtree_del(struct quadtree *node,
143 int (*compare)(struct quadtree *a,
146 struct quadtree *parent = 0;
150 * We are deleting the root node. This means we have to select
151 * a new root node and reconstruct the entire tree under it
155 for (i = 0; i < 4; i++) {
160 parent = node->child[i];
163 _quadtree_reposition_reqursively(parent,
173 * We are not deleting the parent. Detach the node from the
174 * parent abd relocate the children. The node will be then
175 * detached from the tree.
178 for (i = 0; i < 4; i++) {
179 if (node->parent->child[i] == node) {
180 node->parent->child[i] = 0;
185 printf("%s:%d Fatal! Tree inconsistency detected\n",
186 __FUNCTION__, __LINE__);
191 * The sub branch is now detached from the main tree. Continue
192 * relocating the detached branch.
195 for (i = 0; i < 4; i++) {
199 _quadtree_reposition_reqursively(node->parent, node->child[i],
204 parent = quadtree_find_parent(node);
207 validate_tree(parent);
212 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
214 int direction, count = 0;
216 direction = it->direction(head, it->ptr);
218 if ((direction & QUADTREE_UPLEFT) && head->child[0])
219 count += _walk_tree(head->child[0], it);
221 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
222 count += _walk_tree(head->child[1], it);
224 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
225 count += _walk_tree(head->child[2], it);
227 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
228 count += _walk_tree(head->child[3], it);
230 if ((direction & QUADTREE_SELF) && it->callback) {
231 it->callback(head, it->ptr);
238 int walk_tree(const struct quadtree_iterator *it)
240 return _walk_tree(it->head, it);