Skip to content

Commit

Permalink
Add LocalVector.erase_unordered, mimicking erase but with `remove…
Browse files Browse the repository at this point in the history
…_at_unordered`, to remove duplicate logic.

`erase_unordered` should be preferred over `erase` where order is not important, for its performance benefits.
  • Loading branch information
Ivorforce committed Jan 2, 2025
1 parent 0f95e9f commit b37e1dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
9 changes: 9 additions & 0 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ class LocalVector {
return false;
}

_FORCE_INLINE_ bool erase_unordered(const T &p_val) {
int64_t idx = find(p_val);
if (idx >= 0) {
remove_at_unordered(idx);
return true;
}
return false;
}

U erase_multiple_unordered(const T &p_val) {
U from = 0;
U occurrences = 0;
Expand Down
25 changes: 5 additions & 20 deletions modules/navigation/nav_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ void NavMap::add_region(NavRegion *p_region) {
}

void NavMap::remove_region(NavRegion *p_region) {
int64_t region_index = regions.find(p_region);
if (region_index >= 0) {
regions.remove_at_unordered(region_index);
if (regions.erase_unordered(p_region)) {
iteration_dirty = true;
}
}
Expand All @@ -247,9 +245,7 @@ void NavMap::add_link(NavLink *p_link) {
}

void NavMap::remove_link(NavLink *p_link) {
int64_t link_index = links.find(p_link);
if (link_index >= 0) {
links.remove_at_unordered(link_index);
if (links.erase_unordered(p_link)) {
iteration_dirty = true;
}
}
Expand All @@ -267,9 +263,7 @@ void NavMap::add_agent(NavAgent *agent) {

void NavMap::remove_agent(NavAgent *agent) {
remove_agent_as_controlled(agent);
int64_t agent_index = agents.find(agent);
if (agent_index >= 0) {
agents.remove_at_unordered(agent_index);
if (agents.erase_unordered(agent)) {
agents_dirty = true;
}
}
Expand All @@ -291,9 +285,7 @@ void NavMap::add_obstacle(NavObstacle *obstacle) {
}

void NavMap::remove_obstacle(NavObstacle *obstacle) {
int64_t obstacle_index = obstacles.find(obstacle);
if (obstacle_index >= 0) {
obstacles.remove_at_unordered(obstacle_index);
if (obstacles.erase_unordered(obstacle)) {
obstacles_dirty = true;
}
}
Expand Down Expand Up @@ -322,14 +314,7 @@ void NavMap::set_agent_as_controlled(NavAgent *agent) {
}

void NavMap::remove_agent_as_controlled(NavAgent *agent) {
int64_t agent_3d_index = active_3d_avoidance_agents.find(agent);
if (agent_3d_index >= 0) {
active_3d_avoidance_agents.remove_at_unordered(agent_3d_index);
agents_dirty = true;
}
int64_t agent_2d_index = active_2d_avoidance_agents.find(agent);
if (agent_2d_index >= 0) {
active_2d_avoidance_agents.remove_at_unordered(agent_2d_index);
if (active_3d_avoidance_agents.erase_unordered(agent) || active_2d_avoidance_agents.erase_unordered(agent)) {
agents_dirty = true;
}
}
Expand Down

0 comments on commit b37e1dd

Please sign in to comment.