Skip to content

Commit e12874d

Browse files
committed
Add LocalVector.erase_unordered, mimicking erase but with remove_at_unordered, to remove duplicate logic.
`erase_unordered` should be preferred over `erase` where order is not important, for its performance benefits.
1 parent ba34829 commit e12874d

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

core/templates/local_vector.h

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ class LocalVector {
103103
return false;
104104
}
105105

106+
_FORCE_INLINE_ bool erase_unordered(const T &p_val) {
107+
int64_t idx = find(p_val);
108+
if (idx >= 0) {
109+
remove_at_unordered(idx);
110+
return true;
111+
}
112+
return false;
113+
}
114+
106115
U erase_multiple_unordered(const T &p_val) {
107116
U from = 0;
108117
U occurrences = 0;

modules/navigation/nav_map_3d.cpp

+6-18
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ void NavMap3D::add_region(NavRegion3D *p_region) {
235235
}
236236

237237
void NavMap3D::remove_region(NavRegion3D *p_region) {
238-
int64_t region_index = regions.find(p_region);
239-
if (region_index >= 0) {
240-
regions.remove_at_unordered(region_index);
238+
if (regions.erase_unordered(p_region)) {
241239
iteration_dirty = true;
242240
}
243241
}
@@ -248,9 +246,7 @@ void NavMap3D::add_link(NavLink3D *p_link) {
248246
}
249247

250248
void NavMap3D::remove_link(NavLink3D *p_link) {
251-
int64_t link_index = links.find(p_link);
252-
if (link_index >= 0) {
253-
links.remove_at_unordered(link_index);
249+
if (links.erase_unordered(p_link)) {
254250
iteration_dirty = true;
255251
}
256252
}
@@ -268,9 +264,7 @@ void NavMap3D::add_agent(NavAgent3D *agent) {
268264

269265
void NavMap3D::remove_agent(NavAgent3D *agent) {
270266
remove_agent_as_controlled(agent);
271-
int64_t agent_index = agents.find(agent);
272-
if (agent_index >= 0) {
273-
agents.remove_at_unordered(agent_index);
267+
if (agents.erase_unordered(agent)) {
274268
agents_dirty = true;
275269
}
276270
}
@@ -292,9 +286,7 @@ void NavMap3D::add_obstacle(NavObstacle3D *obstacle) {
292286
}
293287

294288
void NavMap3D::remove_obstacle(NavObstacle3D *obstacle) {
295-
int64_t obstacle_index = obstacles.find(obstacle);
296-
if (obstacle_index >= 0) {
297-
obstacles.remove_at_unordered(obstacle_index);
289+
if (obstacles.erase_unordered(obstacle)) {
298290
obstacles_dirty = true;
299291
}
300292
}
@@ -323,14 +315,10 @@ void NavMap3D::set_agent_as_controlled(NavAgent3D *agent) {
323315
}
324316

325317
void NavMap3D::remove_agent_as_controlled(NavAgent3D *agent) {
326-
int64_t agent_3d_index = active_3d_avoidance_agents.find(agent);
327-
if (agent_3d_index >= 0) {
328-
active_3d_avoidance_agents.remove_at_unordered(agent_3d_index);
318+
if (active_3d_avoidance_agents.erase_unordered(agent)) {
329319
agents_dirty = true;
330320
}
331-
int64_t agent_2d_index = active_2d_avoidance_agents.find(agent);
332-
if (agent_2d_index >= 0) {
333-
active_2d_avoidance_agents.remove_at_unordered(agent_2d_index);
321+
if (active_2d_avoidance_agents.erase_unordered(agent)) {
334322
agents_dirty = true;
335323
}
336324
}

0 commit comments

Comments
 (0)