Skip to content

Commit

Permalink
Rewrite merge and update to handle edge cases and full capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 21, 2024
1 parent 48b9260 commit 7ce93b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 13 additions & 16 deletions src/quad.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,7 @@ static void node_remove_point(node_t* node, point_t* point) {
if (node_is_leaf(node)) {
for (int i = 0; i < node->count; ++i) {
if (node->points[i].id == point->id && node->count > 1) {
#if 1
// shift last point into deleted point
memcpy(&node->points[i], &node->points[--node->count], sizeof(node->points[0]));
#else
memcpy(&node->points[i], &node->points[node->count], sizeof(node->points[0]));
#endif
memcpy(&node->points[i], &node->points[--node->count], sizeof(point_t));
return;
} else if (node->points[i].id == point->id && node->count == 1) {
node->count--;
Expand Down Expand Up @@ -294,12 +289,16 @@ static void node_merge(node_t* node) {
(node->count)++;
}
}
for (int i = 0; i < 4; ++i) {
if (children[i] != NULL) {
free(children[i]->points);
free(children[i]);
}
}
free(node->nw);
free(node->nw->points);
free(node->ne);
free(node->ne->points);
free(node->se);
free(node->se->points);
free(node->sw);
free(node->sw->points);
// make the current node a leaf
node->nw = node->ne = node->se = node->sw = NULL;
}
}
}
Expand All @@ -315,10 +314,8 @@ void node_del_all(node_t* node) {
node_del_all(node->ne);
node_del_all(node->sw);
node_del_all(node->se);
if (node != NULL) {
free(node->points);
free(node);
}
free(node->points);
free(node);
}

void qtree_del(quadtree_t* qtree) {
Expand Down
4 changes: 3 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int main(int argc, char** argv) {
// Update point
//-------------------------------------------------------------------------
point_t pnew = {90, 90};
// {108, 120} -> 90, 90
qtree_update_point(&qtree, &points[6], &pnew);
NTEST_ASSERT(qtree.root->nw->sw->points[0].id == pnew.id);
NTEST_ASSERT(qtree.root->sw->ne->count == 1 && qtree.root->nw->sw->count == 1);
Expand All @@ -61,6 +62,7 @@ int main(int argc, char** argv) {
// now sw->nw should have one point and so should sw->ne; mergeable into sw
qtree_merge(&qtree);
// p7, p8 should have propagated up into sw
printf("%d\n", qtree.root->sw->count);
NTEST_ASSERT(qtree.root->sw->count == 2);
NTEST_ASSERT((are_points_equal(&qtree.root->sw->points[1], &points[7]) &&
are_points_equal(&qtree.root->sw->points[0], &points[8])) ||
Expand All @@ -72,6 +74,6 @@ int main(int argc, char** argv) {
NTEST_ASSERT(qtree.root->nw->se->sw->count == 1);
qtree_remove_point(&qtree, &points[5]);
NTEST_ASSERT(qtree.root->nw->se->sw->count == 0);
//qtree_del(&qtree);
qtree_del(&qtree);
return 0;
}

0 comments on commit 7ce93b7

Please sign in to comment.