mirror of
https://github.com/mfocko/blog.git
synced 2024-11-21 20:43:48 +01:00
algorithms(bf-to-astar): factor out the heap and dirs
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
5164a31ce2
commit
21aeeeb042
3 changed files with 24 additions and 26 deletions
|
@ -8,10 +8,6 @@
|
||||||
|
|
||||||
#include "graph.hpp"
|
#include "graph.hpp"
|
||||||
|
|
||||||
const static std::vector<vertex_t> 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,
|
static auto _check_vertex(const graph& g,
|
||||||
std::vector<std::vector<int>>& distances, int v,
|
std::vector<std::vector<int>>& distances, int v,
|
||||||
bool check_only = false) -> bool {
|
bool check_only = false) -> bool {
|
||||||
|
|
|
@ -10,28 +10,6 @@
|
||||||
|
|
||||||
#include "graph.hpp"
|
#include "graph.hpp"
|
||||||
|
|
||||||
namespace {
|
|
||||||
using pqueue_item_t = std::pair<int, vertex_t>;
|
|
||||||
using pqueue_t = std::vector<pqueue_item_t>;
|
|
||||||
|
|
||||||
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<pqueue_item_t> {
|
|
||||||
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)
|
auto dijkstra(const graph& g, const vertex_t& source)
|
||||||
-> std::vector<std::vector<int>> {
|
-> std::vector<std::vector<int>> {
|
||||||
// make sure that ‹source› exists
|
// make sure that ‹source› exists
|
||||||
|
|
|
@ -81,4 +81,28 @@ std::ostream& operator<<(std::ostream& os, const graph& g) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const static std::vector<vertex_t> 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<int, vertex_t>;
|
||||||
|
using pqueue_t = std::vector<pqueue_item_t>;
|
||||||
|
|
||||||
|
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<pqueue_item_t> {
|
||||||
|
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 */
|
#endif /* _GRAPH_HPP */
|
||||||
|
|
Loading…
Reference in a new issue