Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Neighborhood.hpp
Go to the documentation of this file.
1#pragma once
3#include "SearchResult.hpp"
4
5namespace fishnet::graph {
6
7template<Graph G>
8class Neighborhood: public SearchResult<Neighborhood<G>,typename G::node_type, typename G::adj_container_type::hash_function, typename G::adj_container_type::equality_predicate>{
9private:
10 using N = typename G::node_type;
11 using Hash = typename G::adj_container_type::hash_function;
12 using Equal = typename G::adj_container_type::equality_predicate;
13 using Base = SearchResult<Neighborhood<G>,N,Hash,Equal>;
14 std::unordered_map<N, size_t,Hash,Equal> nodeDistances;
15 mutable G subgraph;
16 bool stopFlag = false;
17 const size_t order;
18
19 void init(const N & start) {
20 subgraph.addNode(start);
21 nodeDistances[start] = 0;
22 }
23
24public:
25 using node_type = N;
26
27 Neighborhood(const N & start,size_t order): Base(),subgraph(),order(order){
28 init(start);
29 }
30
31 Neighborhood(G && subgraph,const N & start,size_t order): Base(),subgraph(std::move(subgraph)),order(order){
32 init(start);
33 }
34
35 void onOpen(const N & node) noexcept {
36 return;
37 }
38
39 void onClose(const N & node) noexcept {
40 return;
41 }
42
43 void onEdge(const N & from, const N & to) noexcept {
44 nodeDistances.try_emplace(to,nodeDistances.at(from)+1);
45 /*
46 this ensures that edges between nodes of k order are not included in k-order neighborhood but only in k+1 order neighborhood
47 since k + k >= 2k so edge is not added if both nodes are of order k, but if one node is of order k and the other is of order less than k,
48 then edge is added, which is correct since both nodes should be in k-order neighborhood in this case
49 */
50 if(nodeDistances.at(to) + nodeDistances.at(from) < 2*order) {
51 subgraph.addEdge(from,to);
52 }
53 if(nodeDistances.at(from) > order){
54 stopFlag = true;
55 }
56 }
57
58 bool stop() const noexcept{
59 return stopFlag;
60 }
61
62 auto get() && noexcept {
63 return std::move(subgraph);
64 }
65
66 auto get() & noexcept {
67 return subgraph;
68 }
69};
70} // namespace fishnet::graph
Definition Neighborhood.hpp:8
bool stop() const noexcept
Definition Neighborhood.hpp:58
void onEdge(const N &from, const N &to) noexcept
Definition Neighborhood.hpp:43
Neighborhood(const N &start, size_t order)
Definition Neighborhood.hpp:27
void onClose(const N &node) noexcept
Definition Neighborhood.hpp:39
N node_type
Definition Neighborhood.hpp:25
auto get() &noexcept
Definition Neighborhood.hpp:66
Neighborhood(G &&subgraph, const N &start, size_t order)
Definition Neighborhood.hpp:31
void onOpen(const N &node) noexcept
Definition Neighborhood.hpp:35
auto get() &&noexcept
Definition Neighborhood.hpp:62
CRTP Abstraction of Search Results.
Definition SearchResult.hpp:16
Definition AdjacencyContainer.hpp:4
STL namespace.