From b37e1ddbed7c7932e6d8bcf7f080318c8eeaba8d Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Thu, 2 Jan 2025 16:32:54 +0100 Subject: [PATCH] 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. --- core/templates/local_vector.h | 9 +++++++++ modules/navigation/nav_map.cpp | 25 +++++-------------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index d20fe49eba6f..923fa782d15b 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -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; diff --git a/modules/navigation/nav_map.cpp b/modules/navigation/nav_map.cpp index b7dbd62d5ff6..01235dc8bf24 100644 --- a/modules/navigation/nav_map.cpp +++ b/modules/navigation/nav_map.cpp @@ -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; } } @@ -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; } } @@ -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; } } @@ -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; } } @@ -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; } }