]> git.itanic.dy.fi Git - sdl-planets/blobdiff - quadtree.c
quadtree: Move quadtree_recalculate_parent_stats() out from the header
[sdl-planets] / quadtree.c
index 60ecd6db737a7640d17811f74799d97ed6f1a7af..1417c54ca132583eb20e392809147a9089e7cf15 100644 (file)
@@ -189,6 +189,35 @@ static struct quadtree *_quadtree_add(struct quadtree *parent,
        return new;
 }
 
+/*
+ * Recursively walk through the tree and propagate changes made to the
+ * given node up until the highest parent.
+ */
+static void quadtree_recalculate_parent_stats(struct quadtree *node,
+                                                    struct quadtree_ops *ops)
+{
+       int i;
+
+       while (node) {
+               node->depth = 0;
+               node->children = 0;
+
+               for (i = 0; i < 4; i++) {
+                       if (!node->child[i])
+                               continue;
+
+                       node->depth = MAX(node->depth,
+                                         node->child[i]->depth + 1);
+                       node->children += node->child[i]->children + 1;
+               }
+
+               if (ops->recalculate_stats)
+                       ops->recalculate_stats(node);
+
+               node = node->parent;
+       }
+}
+
 /**
  * quadtree_add - add a node to a quadtree
  * @parent: parent node