From 21aeeeb042af2a466e4b4fc3761639a31170071d Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 3 Jan 2024 15:03:33 +0100 Subject: [PATCH] algorithms(bf-to-astar): factor out the heap and dirs Signed-off-by: Matej Focko --- .../files/algorithms/paths/bf-to-astar/bf.hpp | 4 ---- .../algorithms/paths/bf-to-astar/dijkstra.hpp | 22 ----------------- .../algorithms/paths/bf-to-astar/graph.hpp | 24 +++++++++++++++++++ 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/static/files/algorithms/paths/bf-to-astar/bf.hpp b/static/files/algorithms/paths/bf-to-astar/bf.hpp index dbad150..48bfb12 100644 --- a/static/files/algorithms/paths/bf-to-astar/bf.hpp +++ b/static/files/algorithms/paths/bf-to-astar/bf.hpp @@ -8,10 +8,6 @@ #include "graph.hpp" -const static std::vector DIRECTIONS = - std::vector{std::make_pair(0, 1), std::make_pair(0, -1), - std::make_pair(1, 0), std::make_pair(-1, 0)}; - static auto _check_vertex(const graph& g, std::vector>& distances, int v, bool check_only = false) -> bool { diff --git a/static/files/algorithms/paths/bf-to-astar/dijkstra.hpp b/static/files/algorithms/paths/bf-to-astar/dijkstra.hpp index 2b23120..3a1fb69 100644 --- a/static/files/algorithms/paths/bf-to-astar/dijkstra.hpp +++ b/static/files/algorithms/paths/bf-to-astar/dijkstra.hpp @@ -10,28 +10,6 @@ #include "graph.hpp" -namespace { -using pqueue_item_t = std::pair; -using pqueue_t = std::vector; - -auto pushq(pqueue_t& q, pqueue_item_t v) -> void { - q.push_back(v); - std::push_heap(q.begin(), q.end(), std::greater<>{}); -} - -auto popq(pqueue_t& q) -> std::optional { - if (q.empty()) { - return {}; - } - - std::pop_heap(q.begin(), q.end(), std::greater<>{}); - pqueue_item_t top = q.back(); - q.pop_back(); - - return std::make_optional(top); -} -} // namespace - auto dijkstra(const graph& g, const vertex_t& source) -> std::vector> { // make sure that ‹source› exists diff --git a/static/files/algorithms/paths/bf-to-astar/graph.hpp b/static/files/algorithms/paths/bf-to-astar/graph.hpp index 1bdebcd..2d59b87 100644 --- a/static/files/algorithms/paths/bf-to-astar/graph.hpp +++ b/static/files/algorithms/paths/bf-to-astar/graph.hpp @@ -81,4 +81,28 @@ std::ostream& operator<<(std::ostream& os, const graph& g) { return os; } +const static std::vector DIRECTIONS = + std::vector{std::make_pair(0, 1), std::make_pair(0, -1), + std::make_pair(1, 0), std::make_pair(-1, 0)}; + +using pqueue_item_t = std::pair; +using pqueue_t = std::vector; + +auto pushq(pqueue_t& q, pqueue_item_t v) -> void { + q.push_back(v); + std::push_heap(q.begin(), q.end(), std::greater<>{}); +} + +auto popq(pqueue_t& q) -> std::optional { + if (q.empty()) { + return {}; + } + + std::pop_heap(q.begin(), q.end(), std::greater<>{}); + pqueue_item_t top = q.back(); + q.pop_back(); + + return std::make_optional(top); +} + #endif /* _GRAPH_HPP */