Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
SearchPath.hpp
Go to the documentation of this file.
1#pragma once
2#include "SearchResult.hpp"
3#include <optional>
4#include <algorithm>
5#include <fishnet/Edge.hpp>
6namespace fishnet::graph{
14template<Edge E,util::HashFunction<typename E::node_type> Hash = std::hash<typename E::node_type>,fishnet::util::BiPredicate<typename E::node_type> Equal = std::equal_to<typename E::node_type>>
15class SearchPath : public SearchResult<SearchPath<E,Hash,Equal>,typename E::node_type,Hash,Equal>
16{
17private:
18 using N = E::node_type;
19 N start;
20 N goal;
21 bool found;
22 Equal eq = Equal();
23 std::unordered_map<N,N> predecessor;
24public:
25 using node_type = N;
26
27 SearchPath(const N & start, const N & goal): SearchResult<SearchPath<E,Hash,Equal>,N,Hash,Equal>(),start(start),goal(goal),found(false){};
28
29 void onOpen(const N & node) {
30 if(eq(node,goal)) {
31 found = true;
32 }
33 }
34
35 void onClose(const N & node) {
36 if(eq(node,goal)) {
37 found = true;
38 }
39 }
40
41 void onEdge(const N & from, const N & to){
42 predecessor.try_emplace(to,from);
43 }
44
45 bool stop() const {
46 return this->found;
47 }
48
49 std::optional<std::vector<N>> get() const{
50 if(not found)
51 return std::nullopt;
52 std::vector<N> nodes;
53 nodes.push_back(goal);
54 while(this->predecessor.contains(nodes.back()) && !eq(nodes.back(),start)) {
55 nodes.push_back(this->predecessor.at(nodes.back()));
56 }
57 std::reverse(nodes.begin(),nodes.end());
58 return std::optional<std::vector<N>>{nodes};
59 }
60 ~SearchPath() = default;
61};
62}
Search Path implementation.
Definition SearchPath.hpp:16
void onOpen(const N &node)
Definition SearchPath.hpp:29
SearchPath(const N &start, const N &goal)
Definition SearchPath.hpp:27
void onEdge(const N &from, const N &to)
Definition SearchPath.hpp:41
N node_type
Definition SearchPath.hpp:25
std::optional< std::vector< N > > get() const
Definition SearchPath.hpp:49
bool stop() const
Definition SearchPath.hpp:45
void onClose(const N &node)
Definition SearchPath.hpp:35
CRTP Abstraction of Search Results.
Definition SearchResult.hpp:16
Definition AdjacencyContainer.hpp:4