Skip to content

Commit 19afe67

Browse files
committed
Merge pull request #101026 from Ivorforce/localvector-erase-unordered
Add `LocalVector.erase_unordered`, mimicking `erase` but with `remove_at_unordered`, to remove duplicate logic.
2 parents e585e6a + ccdc586 commit 19afe67

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

core/templates/local_vector.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ class LocalVector {
103103
return false;
104104
}
105105

106+
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_2d/nav_map_2d.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ void NavMap2D::add_region(NavRegion2D *p_region) {
192192
}
193193

194194
void NavMap2D::remove_region(NavRegion2D *p_region) {
195-
int64_t region_index = regions.find(p_region);
196-
if (region_index >= 0) {
197-
regions.remove_at_unordered(region_index);
195+
if (regions.erase_unordered(p_region)) {
198196
iteration_dirty = true;
199197
}
200198
}
@@ -205,9 +203,7 @@ void NavMap2D::add_link(NavLink2D *p_link) {
205203
}
206204

207205
void NavMap2D::remove_link(NavLink2D *p_link) {
208-
int64_t link_index = links.find(p_link);
209-
if (link_index >= 0) {
210-
links.remove_at_unordered(link_index);
206+
if (links.erase_unordered(p_link)) {
211207
iteration_dirty = true;
212208
}
213209
}
@@ -225,9 +221,7 @@ void NavMap2D::add_agent(NavAgent2D *p_agent) {
225221

226222
void NavMap2D::remove_agent(NavAgent2D *p_agent) {
227223
remove_agent_as_controlled(p_agent);
228-
int64_t agent_index = agents.find(p_agent);
229-
if (agent_index >= 0) {
230-
agents.remove_at_unordered(agent_index);
224+
if (agents.erase_unordered(p_agent)) {
231225
agents_dirty = true;
232226
}
233227
}
@@ -249,9 +243,7 @@ void NavMap2D::add_obstacle(NavObstacle2D *p_obstacle) {
249243
}
250244

251245
void NavMap2D::remove_obstacle(NavObstacle2D *p_obstacle) {
252-
int64_t obstacle_index = obstacles.find(p_obstacle);
253-
if (obstacle_index >= 0) {
254-
obstacles.remove_at_unordered(obstacle_index);
246+
if (obstacles.erase_unordered(p_obstacle)) {
255247
obstacles_dirty = true;
256248
}
257249
}
@@ -272,9 +264,7 @@ void NavMap2D::set_agent_as_controlled(NavAgent2D *p_agent) {
272264
}
273265

274266
void NavMap2D::remove_agent_as_controlled(NavAgent2D *p_agent) {
275-
int64_t agent_index = active_avoidance_agents.find(p_agent);
276-
if (agent_index >= 0) {
277-
active_avoidance_agents.remove_at_unordered(agent_index);
267+
if (active_avoidance_agents.erase_unordered(p_agent)) {
278268
agents_dirty = true;
279269
}
280270
}

modules/navigation_3d/nav_map_3d.cpp

Lines changed: 6 additions & 18 deletions
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
}

tests/core/templates/test_local_vector.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ TEST_CASE("[LocalVector] Remove Unordered.") {
178178
CHECK(vector.size() == 0);
179179
}
180180

181+
TEST_CASE("[LocalVector] Erase Unordered.") {
182+
LocalVector<int> vector;
183+
vector.push_back(1);
184+
vector.push_back(3);
185+
vector.push_back(0);
186+
vector.push_back(2);
187+
vector.push_back(4);
188+
189+
CHECK(vector.find(1) == 0);
190+
191+
vector.erase_unordered(1);
192+
193+
CHECK(vector.find(1) == -1);
194+
CHECK(vector.size() == 4);
195+
CHECK(vector[0] == 4);
196+
}
197+
181198
TEST_CASE("[LocalVector] Erase.") {
182199
LocalVector<int> vector;
183200
vector.push_back(1);

0 commit comments

Comments
 (0)