Skip to content

Commit

Permalink
memcpy instead of manual
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 21, 2024
1 parent ce85a58 commit 48b9260
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/quad.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <string.h> // memcpy
#include <unistd.h>
#include <float.h> // DBL_MAX
#include <stddef.h> // size_t
Expand Down Expand Up @@ -229,11 +229,19 @@ double qtree_nearest_neighbor(quadtree_t* qtree, point_t* query, point_t* neares
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) {
if (node->points[i].id == point->id && node->count > 1) {
#if 1
// shift last point into deleted point
node->points[i] = node->points[--node->count];
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
return;
} else if (node->points[i].id == point->id && node->count == 1) {
node->count--;
return;
}

}
} else {
// search top-down
Expand Down
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,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);
return 0;
}

0 comments on commit 48b9260

Please sign in to comment.