10 static void validate_subtree(const struct quadtree *node)
14 for (i = 0; i < 4; i++) {
18 if (node->child[i]->parent != node) {
19 printf("%s:%d Fatal! Tree inconsistency detected "
20 "at child %d in node %p\n",
21 __FUNCTION__, __LINE__, i, node);
25 if (node->child[i] == node->parent) {
26 printf("%s:%d Fatal! Tree loop detected "
27 "at child %d in node %p\n",
28 __FUNCTION__, __LINE__, i, node);
33 validate_subtree(node->child[i]);
37 static void validate_tree(const struct quadtree *node)
39 const struct quadtree *parent = quadtree_find_parent(node);
41 validate_subtree(parent);
45 * quadtree_add - add a node to a quadtree
46 * @parent: parent node
47 * @new: the new node to be added
48 * @compare: pointer to a comparison function
50 * Add a node to a quadtree. The tree is kept in order, the new node
51 * is placed in the end of appropriate branch. The compare function is
52 * used to compare the new node against a branch in the tree. The
53 * comparison function must have following return value depending on
54 * the position of node a compared to the position of node b:
61 * The case of nodes sharing identical coordinates is not taken into
65 struct quadtree *quadtree_add(struct quadtree *parent,
67 int (*compare)(struct quadtree *a,
72 ret = compare(parent, new);
74 if (ret < 0 || ret >= 4) {
75 printf("Invalid comparison result of %d\n", ret);
79 if (parent->child[ret])
80 return quadtree_add(parent->child[ret], new, compare);
82 parent->child[ret] = new;
89 * _quadtree_reposition_reqursively - Move everything under and
90 * including a given node under the new root node
94 _quadtree_reposition_reqursively(struct quadtree *root,
95 struct quadtree *node,
96 int (*compare)(struct quadtree *a,
101 /* First remove all children, if any */
102 for (i = 0; i < 4; i++) {
106 _quadtree_reposition_reqursively(root, node->child[i], compare);
110 /* Then remove this node under the new root. */
111 quadtree_add(root, node, compare);
115 * quadtree_del - Detach a node from the tree
117 * Return value: The new root node of the tree. If we are detaching
118 * anything but the root node of the entire tree, the returned root
119 * value will be the original root of the tree.
121 struct quadtree *quadtree_del(struct quadtree *node,
122 int (*compare)(struct quadtree *a,
125 struct quadtree *parent = 0;
129 * We are deleting the root node. This means we have to select
130 * a new root node and reconstruct the entire tree under it
134 for (i = 0; i < 4; i++) {
139 parent = node->child[i];
142 _quadtree_reposition_reqursively(parent,
152 * We are not deleting the parent. Just relocate the children
153 * and detach the node from the tree.
155 for (i = 0; i < 4; i++) {
159 _quadtree_reposition_reqursively(node->parent, node->child[i],
163 parent = quadtree_find_parent(node);
170 static int _walk_tree(struct quadtree *head, const struct quadtree_iterator *it)
172 int direction, count = 0;
174 direction = it->direction(head, it->ptr);
176 if ((direction & QUADTREE_UPLEFT) && head->child[0])
177 count += _walk_tree(head->child[0], it);
179 if ((direction & QUADTREE_UPRIGHT) && head->child[1])
180 count += _walk_tree(head->child[1], it);
182 if ((direction & QUADTREE_DOWNLEFT) && head->child[2])
183 count += _walk_tree(head->child[2], it);
185 if ((direction & QUADTREE_DOWNRIGHT) && head->child[3])
186 count += _walk_tree(head->child[3], it);
188 if ((direction & QUADTREE_SELF) && it->callback) {
189 it->callback(head, it->ptr);
197 int walk_tree(const struct quadtree_iterator *it)
199 return _walk_tree(it->head, it);