From 987f88fbeb9bf311695aae16d3f77dc8d3b4ba64 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 11 Apr 2010 18:05:13 +0300 Subject: [PATCH] Introduce struct quadree_ops Store all related function pointers (so far only the comparison) in one structure. This will make it easier to add function pointers that are needed in various quadtree operations. Signed-off-by: Timo Kokkonen --- planet.c | 17 ++++++++++------- quadtree.c | 24 ++++++++++-------------- quadtree.h | 14 +++++++------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/planet.c b/planet.c index de0e49f..6a3e594 100644 --- a/planet.c +++ b/planet.c @@ -4,6 +4,10 @@ #include "planet.h" #include "utils.h" +struct quadtree_ops planet_ops = { + .compare = planet_spatial_compare, +}; + static void putpixel(struct SDL_Surface *screen, const int x, const int y, const unsigned char r, const unsigned char g, const unsigned char b) @@ -90,8 +94,7 @@ void create_planets(struct planet *p, int num, double total_mass, double radius) total_mass, radius); - quadtree_add(&p->tree, &new_planet->tree, - planet_spatial_compare); + quadtree_add(&p->tree, &new_planet->tree, &planet_ops); sum += new_planet->mass; } @@ -201,7 +204,7 @@ struct planet *merge_planets(struct planet *a, struct planet *b) _merge_planets(a, b); list_del(&b->list); - quadtree_del(&b->tree, planet_spatial_compare); + quadtree_del(&b->tree, &planet_ops); free(b); return a; @@ -244,8 +247,8 @@ void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it) { struct quadtree *parent; - parent = quadtree_del(node, planet_spatial_compare); - quadtree_add(parent, node, planet_spatial_compare); + parent = quadtree_del(node, &planet_ops); + quadtree_add(parent, node, &planet_ops); } struct planet *move_planet(struct planet *p, const double time) @@ -278,9 +281,9 @@ struct planet *move_planet(struct planet *p, const double time) continue; } - tree_parent = quadtree_del(&p->tree, planet_spatial_compare); + tree_parent = quadtree_del(&p->tree, &planet_ops); p->pos = new_pos; - quadtree_add(tree_parent, &p->tree, planet_spatial_compare); + quadtree_add(tree_parent, &p->tree, &planet_ops); return tree_to_planet(tree_parent); } diff --git a/quadtree.c b/quadtree.c index f188d80..ec683aa 100644 --- a/quadtree.c +++ b/quadtree.c @@ -109,10 +109,8 @@ static void validate_tree(const struct quadtree *node) * account at all. */ -struct quadtree *quadtree_add(struct quadtree *parent, - struct quadtree *new, - int (*compare)(struct quadtree *a, - struct quadtree *b)) +struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new, + struct quadtree_ops *ops) { int ret; if (parent == new) @@ -120,7 +118,7 @@ struct quadtree *quadtree_add(struct quadtree *parent, validate_tree(parent); - ret = compare(parent, new); + ret = ops->compare(parent, new); if (ret < 0 || ret >= 4) { printf("Invalid comparison result of %d\n", ret); @@ -128,7 +126,7 @@ struct quadtree *quadtree_add(struct quadtree *parent, } if (parent->child[ret]) - return quadtree_add(parent->child[ret], new, compare); + return quadtree_add(parent->child[ret], new, ops); parent->child[ret] = new; new->parent = parent; @@ -148,8 +146,7 @@ struct quadtree *quadtree_add(struct quadtree *parent, static void _quadtree_reposition_reqursively(struct quadtree *root, struct quadtree *node, - int (*compare)(struct quadtree *a, - struct quadtree *b)) + struct quadtree_ops *ops) { int i; @@ -165,7 +162,7 @@ _quadtree_reposition_reqursively(struct quadtree *root, node->child[i] == node->parent) trap(); - _quadtree_reposition_reqursively(root, node->child[i], compare); + _quadtree_reposition_reqursively(root, node->child[i], ops); node->child[i] = 0; } @@ -174,7 +171,7 @@ _quadtree_reposition_reqursively(struct quadtree *root, node->children = 0; /* Then remove this node under the new root. */ - quadtree_add(root, node, compare); + quadtree_add(root, node, ops); } /* @@ -185,8 +182,7 @@ _quadtree_reposition_reqursively(struct quadtree *root, * value will be the original root of the tree. */ struct quadtree *quadtree_del(struct quadtree *node, - int (*compare)(struct quadtree *a, - struct quadtree *b)) + struct quadtree_ops *ops) { struct quadtree *parent = 0; int i; @@ -207,7 +203,7 @@ struct quadtree *quadtree_del(struct quadtree *node, } _quadtree_reposition_reqursively(parent, node->child[i], - compare); + ops); node->child[i] = 0; } @@ -245,7 +241,7 @@ struct quadtree *quadtree_del(struct quadtree *node, continue; _quadtree_reposition_reqursively(node->parent, node->child[i], - compare); + ops); node->child[i] = 0; } diff --git a/quadtree.h b/quadtree.h index 3d8f518..917abd5 100644 --- a/quadtree.h +++ b/quadtree.h @@ -14,6 +14,10 @@ struct quadtree { long int depth; /* The deepest subtree branch */ }; +struct quadtree_ops { + int (*compare)(struct quadtree *a, struct quadtree *b); +}; + static inline void init_quadtree(struct quadtree *t) { memset(t, 0, sizeof(*t)); @@ -33,14 +37,10 @@ struct quadtree_iterator { void (*callback)(struct quadtree *head, struct quadtree_iterator *it); }; -struct quadtree *quadtree_add(struct quadtree *parent, - struct quadtree *new, - int (*compare)(struct quadtree *a, - struct quadtree *b)); +struct quadtree *quadtree_add(struct quadtree *parent, struct quadtree *new, + struct quadtree_ops *ops); -struct quadtree *quadtree_del(struct quadtree *node, - int (*compare)(struct quadtree *a, - struct quadtree *b)); +struct quadtree *quadtree_del(struct quadtree *node, struct quadtree_ops *ops); int walk_quadtree(const struct quadtree_iterator *iterator); -- 2.44.0