Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
search_impl.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common.hpp>
4#include <graph/graph.hpp>
10#include <meta/if_constexpr.hpp>
12#include <search/visitor.hpp>
13
14#include <utility>
15
16
17namespace graphle::detail {
39 template <
41 graph_ref G,
42 search::search_visitor_ref<G> V,
43 typename EmplacePP,
44 typename TakePP,
45 store::storage_provider_ref<ST, vertex_of<G>> PP
46 = store::default_provided_t<ST, vertex_of<G>>,
47 store::storage_provider_ref<store::storage_type::UNORDERED_SET, vertex_of<G>, vertex_hash_of<G>, vertex_compare_of<G>> PS
48 = store::default_provided_t<store::storage_type::UNORDERED_SET, vertex_of<G>, vertex_hash_of<G>, vertex_compare_of<G>>
49 > requires (
50 edge_list_graph<G> ||
51 out_edges_graph<G> ||
52 (non_directed_graph<G> && in_edges_graph<G>)
53 ) constexpr inline bool search(
54 G&& graph,
55 vertex_of<G> root,
56 V&& visitor,
57 EmplacePP&& emplace,
58 TakePP&& take,
59 PP&& pending_provider = store::get_default_storage_provider<ST, vertex_of<G>>(),
61 ) {
62 using VR = search::visitor_result;
64
65
66 if (auto result = visitor.begin_search_base(graph); result == NVR::STOP_SEARCH) return false;
67
68
69 decltype(auto) pending = pending_provider();
70 emplace(pending, root);
71
72 decltype(auto) seen = set_provider();
73 seen.emplace(root);
74
75
76 while (!rng::empty(pending)) {
77 vertex_of<G> next = take(pending);
78
79
80 switch (visitor.discover_vertex_base(next, graph)) {
81 case VR::STOP_SEARCH: return false;
82 case VR::STOP_TREE: continue;
83 case VR::CONTINUE: break;
84 }
85
86 // Concepts 'branch' and 'leaf' don't make sense for a non-directed graph so ignore these visitor callbacks.
87 if constexpr (graph_is_directed<G>) {
88 if (util::is_branch(graph, next)) {
89 switch (visitor.discover_branch_base(next, graph)) {
90 case VR::STOP_SEARCH: return false;
91 case VR::STOP_TREE: continue;
92 case VR::CONTINUE: break;
93 }
94 }
95
96 if (util::is_leaf(graph, next)) {
97 switch (visitor.discover_leaf_base(next, graph)) {
98 case VR::STOP_SEARCH: return false;
99 case VR::STOP_TREE: continue;
100 case VR::CONTINUE: break;
101 }
102 }
103 }
104
105
106 for (auto edge : util::out_edges(graph, next)) {
107 if (seen.contains(edge.second)) {
108 switch (visitor.discover_edge_to_known_vertex_base(edge, graph)) {
109 case VR::STOP_SEARCH: return false;
110 case VR::STOP_TREE: continue;
111 case VR::CONTINUE: continue;
112 }
113 } else {
114 switch (visitor.discover_edge_to_new_vertex_base(edge, graph)) {
115 case VR::STOP_SEARCH: return false;
116 case VR::STOP_TREE: continue;
117 case VR::CONTINUE:
118 seen.emplace(edge.second);
119 emplace(pending, edge.second);
120 break;
121 }
122 }
123 }
124 }
125
126
127 if (auto result = visitor.finish_search_base(graph); result == NVR::STOP_SEARCH) return false;
128 return true;
129 }
130}
typename std::remove_cvref_t< G >::vertex_hash_t vertex_hash_of
Definition: graph.hpp:99
typename std::remove_cvref_t< G >::vertex_compare_t vertex_compare_of
Definition: graph.hpp:95
typename std::remove_cvref_t< G >::vertex_type vertex_of
Definition: graph.hpp:91
constexpr bool search(G &&graph, vertex_of< G > root, V &&visitor, EmplacePP &&emplace, TakePP &&take, PP &&pending_provider=store::get_default_storage_provider< ST, vertex_of< G > >(), PS &&set_provider=store::get_default_storage_provider< store::storage_type::UNORDERED_SET, vertex_of< G >, vertex_hash_of< G >, vertex_compare_of< G > >())
Definition: search_impl.hpp:53
constexpr bool is_leaf(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:205
constexpr auto out_edges(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:75
constexpr bool is_branch(G &&graph, vertex_of< G > vertex)
Definition: edge_utils.hpp:188
constexpr auto get_default_storage_provider(void)