Skip to content

Commit bb63222

Browse files
Refactor A* algorithm to use shared pointers in came_from map for improved memory management
1 parent 53a0a9a commit bb63222

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

include/a_star.hpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace utils
7272
{
7373
c_node = root;
7474
open_list.push({root, root->cost()});
75-
came_from[root.get()] = nullptr;
75+
came_from[root] = nullptr;
7676
g_score[root] = 0;
7777
}
7878
virtual ~a_star() = default;
@@ -89,7 +89,7 @@ namespace utils
8989

9090
#ifdef UTILS_A_STAR_ENABLE_NAVIGATION
9191
backtrack_to(find_common_ancestor(c_node, current));
92-
if (!advance_to(*current))
92+
if (!advance_to(current))
9393
{ // Conflict detected, backtrack..
9494
#ifdef UTILS_A_STAR_ENABLE_LISTENERS
9595
inconsistent_node(*current);
@@ -118,7 +118,7 @@ namespace utils
118118

119119
if (!g_score.count(neighbor) || tentative_g_score < g_score.at(neighbor))
120120
{
121-
came_from[neighbor.get()] = current.get();
121+
came_from[neighbor] = current;
122122
g_score[neighbor] = tentative_g_score;
123123
Tp f_cost = tentative_g_score + neighbor->cost(goal);
124124
open_list.push({neighbor, f_cost});
@@ -135,13 +135,13 @@ namespace utils
135135
while (a)
136136
{
137137
ancestors.insert(a);
138-
a = came_from.at(a.get());
138+
a = came_from.at(a);
139139
}
140140
while (b)
141141
{
142142
if (ancestors.count(b))
143143
return b;
144-
b = came_from.at(b.get());
144+
b = came_from.at(b);
145145
}
146146
return nullptr;
147147
}
@@ -151,27 +151,27 @@ namespace utils
151151
while (c_node != n)
152152
{
153153
retract(*c_node);
154-
c_node = came_from.at(c_node.get());
154+
c_node = came_from.at(c_node);
155155
#ifdef UTILS_A_STAR_ENABLE_LISTENERS
156156
current_node(*c_node);
157157
#endif
158158
}
159159
}
160160

161-
bool advance_to(const node<Tp> &target) noexcept
161+
bool advance_to(std::shared_ptr<node<Tp>> target) noexcept
162162
{
163-
std::vector<node<Tp> *> path;
164-
auto temp_node = &target;
165-
while (temp_node != c_node.get())
163+
std::vector<std::shared_ptr<node<Tp>>> path;
164+
auto temp_node = target;
165+
while (temp_node != c_node)
166166
{
167167
path.push_back(temp_node);
168-
temp_node = temp_node->get_parent().get();
168+
temp_node = came_from.at(temp_node);
169169
}
170170
for (auto it = path.rbegin(); it != path.rend(); ++it)
171171
{
172172
if (!expand(**it))
173173
return false;
174-
c_node = (*it)->get_parent();
174+
c_node = *it;
175175
#ifdef UTILS_A_STAR_ENABLE_LISTENERS
176176
current_node(*c_node);
177177
#endif
@@ -193,7 +193,7 @@ namespace utils
193193
while (current)
194194
{
195195
path.push_back(current);
196-
auto it = came_from.find(current.get());
196+
auto it = came_from.find(current);
197197
if (it == came_from.end() || it->second == nullptr)
198198
break;
199199
current = it->second;
@@ -229,10 +229,10 @@ namespace utils
229229
#endif
230230

231231
private:
232-
std::shared_ptr<node<Tp>> c_node; // Current node being processed
233-
std::priority_queue<pq_elem, std::vector<pq_elem>, std::greater<>> open_list; // Priority queue of nodes to explore
234-
std::unordered_map<const node<Tp> *, const node<Tp> *> came_from; // Best path to each node
235-
std::unordered_map<std::shared_ptr<const node<Tp>>, Tp> g_score; // Cost from start to each node
236-
std::unordered_set<std::shared_ptr<const node<Tp>>> closed_list; // Set of nodes already evaluated
232+
std::shared_ptr<node<Tp>> c_node; // Current node being processed
233+
std::priority_queue<pq_elem, std::vector<pq_elem>, std::greater<>> open_list; // Priority queue of nodes to explore
234+
std::unordered_map<std::shared_ptr<node<Tp>>, std::shared_ptr<node<Tp>>> came_from; // Best path to each node
235+
std::unordered_map<std::shared_ptr<const node<Tp>>, Tp> g_score; // Cost from start to each node
236+
std::unordered_set<std::shared_ptr<const node<Tp>>> closed_list; // Set of nodes already evaluated
237237
};
238238
} // namespace utils

0 commit comments

Comments
 (0)