Skip to content

Commit

Permalink
using std::vector instead of std::set (daancode#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide authored Jul 5, 2020
1 parent 1a48fb6 commit 2bbdc7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions source/AStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,28 @@ AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_)
{
Node *current = nullptr;
NodeSet openSet, closedSet;
openSet.insert(new Node(source_));
openSet.reserve(100);
closedSet.reserve(100);
openSet.push_back(new Node(source_));

while (!openSet.empty()) {
current = *openSet.begin();
for (auto node : openSet) {
auto current_it = openSet.begin();
current = *current_it;

for (auto it = openSet.begin(); it != openSet.end(); it++) {
auto node = *it;
if (node->getScore() <= current->getScore()) {
current = node;
current_it = it;
}
}

if (current->coordinates == target_) {
break;
}

closedSet.insert(current);
openSet.erase(std::find(openSet.begin(), openSet.end(), current));
closedSet.push_back(current);
openSet.erase(current_it);

for (uint i = 0; i < directions; ++i) {
Vec2i newCoordinates(current->coordinates + direction[i]);
Expand All @@ -103,7 +109,7 @@ AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_)
successor = new Node(newCoordinates, current);
successor->G = totalCost;
successor->H = heuristic(successor->coordinates, target_);
openSet.insert(successor);
openSet.push_back(successor);
}
else if (totalCost < successor->G) {
successor->parent = current;
Expand Down Expand Up @@ -173,4 +179,4 @@ AStar::uint AStar::Heuristic::octagonal(Vec2i source_, Vec2i target_)
{
auto delta = std::move(getDelta(source_, target_));
return 10 * (delta.x + delta.y) + (-6) * std::min(delta.x, delta.y);
}
}
4 changes: 2 additions & 2 deletions source/AStar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace AStar
uint getScore();
};

using NodeSet = std::set<Node*>;
using NodeSet = std::vector<Node*>;

class Generator
{
Expand Down Expand Up @@ -69,4 +69,4 @@ namespace AStar
};
}

#endif // __ASTAR_HPP_8F637DB91972F6C878D41D63F7E7214F__
#endif // __ASTAR_HPP_8F637DB91972F6C878D41D63F7E7214F__

0 comments on commit 2bbdc7e

Please sign in to comment.