Sequoia
Loading...
Searching...
No Matches
GraphTraversalTestingUtilities.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2019. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
14
15namespace sequoia::testing
16{
17 using bfs_type = maths::breadth_first_search_type;
18 using dfs_type = maths::depth_first_search_type;
19 using pdfs_type = maths::pseudo_depth_first_search_type;
20 using prs_type = maths::priority_first_search_type;
21
22 [[nodiscard]]
23 std::string to_string(maths::traversal_flavour f);
24
25
26 template<maths::traversal_flavour F>
27 struct Traverser;
28
29 template<> struct Traverser<maths::traversal_flavour::breadth_first>
30 {
31 constexpr static auto flavour{maths::traversal_flavour::breadth_first};
32
33 template<class G, maths::disconnected_discovery_mode Mode, class... Fn>
34 static void traverse(const G& g, const maths::traversal_conditions<Mode> conditions, Fn&&... fn)
35 {
36 maths::traverse(maths::breadth_first, g, conditions, std::forward<Fn>(fn)...);
37 }
38
39 constexpr static bool uses_forward_iterator() noexcept { return true; }
40
41 static std::string iterator_description() noexcept { return "forward"; }
42 };
43
44 template<> struct Traverser<maths::traversal_flavour::depth_first>
45 {
46 constexpr static auto flavour{maths::traversal_flavour::depth_first};
47
48 template<class G, maths::disconnected_discovery_mode Mode, class... Fn>
49 static void traverse(const G& g, const maths::traversal_conditions<Mode> conditions, Fn&&... fn)
50 {
51 maths::traverse(maths::depth_first, g, conditions, std::forward<Fn>(fn)...);
52 }
53
54 constexpr static bool uses_forward_iterator() noexcept { return true; }
55
56 static std::string iterator_description() noexcept { return "forward"; }
57 };
58
59 template<> struct Traverser<maths::traversal_flavour::pseudo_depth_first>
60 {
61 constexpr static auto flavour{maths::traversal_flavour::pseudo_depth_first};
62
63 template<class G, maths::disconnected_discovery_mode Mode, class... Fn>
64 static void traverse(const G& g, const maths::traversal_conditions<Mode> conditions, Fn&&... fn)
65 {
66 maths::traverse(maths::pseudo_depth_first, g, conditions, std::forward<Fn>(fn)...);
67 }
68
69 constexpr static bool uses_forward_iterator() noexcept { return false; }
70
71 static std::string iterator_description() noexcept { return "reverse"; }
72 };
73
74 template<> struct Traverser<maths::traversal_flavour::priority>
75 {
76 constexpr static auto flavour{maths::traversal_flavour::priority};
77
78 template<class G, maths::disconnected_discovery_mode Mode, class... Fn>
79 static void traverse(const G& g, const maths::traversal_conditions<Mode> conditions, Fn&&... fn)
80 {
81 maths::traverse(maths::priority_first, g, conditions, std::forward<Fn>(fn)...);
82 }
83
84 constexpr static bool uses_forward_iterator() noexcept { return true; }
85
86 static std::string iterator_description() noexcept { return "forward"; }
87 };
88
90 {
91 public:
92 void clear() noexcept { m_Order.clear(); }
93
94 void operator()(const std::size_t index) { m_Order.push_back(index); }
95
96 [[nodiscard]]
97 auto begin() const noexcept
98 {
99 return m_Order.begin();
100 }
101
102 [[nodiscard]]
103 auto end() const noexcept
104 {
105 return m_Order.end();
106 }
107 private:
108 std::vector<std::size_t> m_Order;
109 };
110
111 template<maths::network G, maths::traversal_flavour Flavour>
113 {
114 public:
115 using result_type = std::vector<std::pair<std::size_t, std::size_t>>;
116
117 edge_tracker(const G& graph) : m_Graph{graph} {}
118
119 void clear() noexcept { m_Order.clear(); }
120
121 template<std::input_or_output_iterator I> void operator()(I iter)
122 {
123 const auto pos{dist(maths::traversal_constant<Flavour>{}, iter)};
124 m_Order.emplace_back(iter.partition_index(), static_cast<std::size_t>(pos));
125 }
126
127 [[nodiscard]]
128 auto begin() const noexcept
129 {
130 return m_Order.begin();
131 }
132
133 [[nodiscard]]
134 auto end() const noexcept
135 {
136 return m_Order.end();
137 }
138 private:
139 result_type m_Order;
140 const G& m_Graph;
141
142 template<std::input_or_output_iterator I> [[nodiscard]] auto dist(pdfs_type, I iter)
143 {
144 return distance(m_Graph.crbegin_edges(iter.partition_index()), iter);
145 }
146
147 template<std::input_or_output_iterator I> [[nodiscard]] auto dist(bfs_type, I iter)
148 {
149 return distance(m_Graph.cbegin_edges(iter.partition_index()), iter);
150 }
151
152 template<std::input_or_output_iterator I> [[nodiscard]] auto dist(dfs_type, I iter)
153 {
154 return distance(m_Graph.cbegin_edges(iter.partition_index()), iter);
155 }
156 };
157
158 template<>
160 {
161 template<test_mode Mode>
162 static void test(equivalence_check_t, test_logger<Mode>& logger, const node_tracker& tracker, const std::vector<std::size_t>& prediction)
163 {
164 check(equality, "Visitation Order", logger, tracker.begin(), tracker.end(), prediction.begin(), prediction.end());
165 }
166 };
167
168 template<maths::network G, maths::traversal_flavour Flavour>
169 struct value_tester<edge_tracker<G, Flavour>>
170 {
172 using prediction_type = typename type::result_type;
173
174 template<test_mode Mode>
175 static void test(equivalence_check_t, test_logger<Mode>& logger, const type& tracker, const prediction_type& prediction)
176 {
177 check(equality, "Visitation Order", logger, tracker.begin(), tracker.end(), prediction.begin(), prediction.end());
178 }
179 };
180}
Headers for traversals of dynamic graphs.
bool check(CheckType flavour, std::string description, test_logger< Mode > &logger, Iter first, Sentinel last, PredictionIter predictionFirst, PredictionSentinel predictionLast, tutor< Advisor > advisor={})
The workhorse for comparing the contents of ranges.
Definition: FreeCheckers.hpp:377
Definition: GraphTraversalDetails.hpp:72
Definition: GraphTraversalTestingUtilities.hpp:113
Definition: GraphTraversalTestingUtilities.hpp:90
Definition: TestLogger.hpp:183
Definition: GraphTraversalDetails.hpp:34
Definition: GraphTraversalTestingUtilities.hpp:27
Definition: FreeCheckers.hpp:87
class template, specializations of which implement various comparisons for the specified type.
Definition: FreeCheckers.hpp:78