Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
test_datastructures.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <graphle.hpp>
4
5#include <vector>
6#include <utility>
7#include <memory>
8#include <unordered_set>
9#include <unordered_map>
10
11
22template <typename T> requires requires (T v) { v.vertex_id; }
23struct std::hash<T> {
24 constexpr std::size_t operator()(const T& v) const {
25 return std::hash<std::size_t>{}(v.vertex_id);
26 }
27};
28
29
30namespace graphle::test {
35 template <typename T> requires requires (T v) { v.vertex_id; }
36 constexpr bool operator==(const T& a, const T& b) {
37 return a.vertex_id == b.vertex_id;
38 }
39
40
45 GRAPHLE_MAKE_NIEBLOID(to_vertex, [] (const auto& v) { return V { v.vertex_id }; }, typename V);
46
51 GRAPHLE_MAKE_NIEBLOID(to_edge, ([] (const auto& e) { return std::pair { to_vertex<V>(e.first), to_vertex<V>(e.second) }; }), typename V);
52
53
58 template <typename M, typename K> constexpr auto range_at_map_key(M&& map, const K& key) {
59 using view_type = decltype(std::views::all(map.at(key)));
60
61 return map.contains(key)
62 ? views::maybe_exists(std::views::all(map.at(key)))
63 : views::maybe_exists(meta::deduce_as<view_type>);
64 }
65
66
72 struct vertex { std::size_t vertex_id; };
73
74 std::vector<vertex> vertices;
75 std::vector<std::pair<vertex, vertex>> edges;
76
77
78 constexpr static ve_list_graph from_ve_list(const ve_list_graph& g) {
79 return g;
80 }
81
82 constexpr auto view_as_graph(void) {
83 return graph {
84 .deduce_vertex_type = graphle::meta::deduce_as<vertex>,
85 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
86 .get_vertices = [this] { return views::all(vertices) | views::transform(util::addressof); },
87 .get_edges = [this] { return views::all(edges) | views::transform(util::transform_edge(util::addressof)); }
88 };
89 }
90 };
91
92
97 struct ve_map_graph {
98 struct vertex {
99 std::size_t vertex_id;
100 };
101
102 std::unordered_set<vertex> vertices;
103 std::unordered_map<vertex, std::vector<vertex>> edges;
104
105
107 auto vs = g.vertices | std::views::transform(to_vertex<vertex>);
108 auto es = g.edges | std::views::transform(to_edge<vertex>);
109
110
111 auto result = ve_map_graph {
112 .vertices { vs.begin(), vs.end() }
113 };
114
115 for (const auto& e : es) result.edges[e.first].push_back(e.second);
116
117
118 return result;
119 }
120
121
122 constexpr auto view_as_graph(void) {
123 return graph {
124 .deduce_vertex_type = meta::deduce_as<const vertex>,
125 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
126 .get_vertices = [this] { return views::all(vertices) | views::transform(util::addressof); },
127 .get_out_edges = [this] (const vertex* v) { return range_at_map_key(edges, *v) | views::transform(util::as_const) | views::transform(util::addressof) | views::edge_from(v); }
128 };
129 }
130 };
131
132
138 // This is bad performance-wise but this type is only used for testing.
139 struct vertex { std::size_t vertex_id; std::vector<std::shared_ptr<vertex>> out; };
140 std::shared_ptr<vertex> root;
141
142
143 static out_edge_graph from_ve_list(const ve_list_graph& g, std::size_t root = 0) {
144 std::unordered_map<std::size_t, std::shared_ptr<vertex>> vertices;
145
146 for (const auto& v : g.vertices) {
147 vertices.emplace(v.vertex_id, std::make_shared<vertex>(v.vertex_id));
148 }
149
150 for (const auto& e : g.edges) {
151 auto& from = vertices.at(e.first.vertex_id);
152 auto& to = vertices.at(e.second.vertex_id);
153
154 from->out.push_back(to);
155 }
156
157 return out_edge_graph { .root = vertices.at(root) };
158 }
159
160
161 constexpr auto view_as_graph(void) {
162 return graph {
163 .deduce_vertex_type = graphle::meta::deduce_as<vertex>,
164 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
165 .get_out_edges = [] (vertex* v) {
166 return views::all(v->out)
167 | views::transform(&std::shared_ptr<vertex>::get)
168 | views::edge_from(v);
169 }
170 };
171 }
172 };
173
174
180 struct vertex { std::size_t vertex_id; std::vector<vertex*> out; };
181 std::vector<vertex> vertices;
182
183
185 auto vs = g.vertices | std::views::transform(to_vertex<vertex>);
186 v_list_out_edge_graph result { .vertices { vs.begin(), vs.end() } };
187 std::unordered_map<std::size_t, vertex*> vertex_map;
188
189 for (const auto [from, to] : g.edges) {
190 for (auto v : { from, to }) {
191 if (!vertex_map.contains(v.vertex_id)) {
192 vertex_map.emplace(v.vertex_id, &*std::ranges::find(result.vertices, vertex { v.vertex_id }));
193 }
194 }
195
196 vertex_map.at(from.vertex_id)->out.push_back(vertex_map.at(to.vertex_id));
197 }
198
199 return result;
200 }
201
202
203 constexpr auto view_as_graph(void) {
204 return graph {
205 .deduce_vertex_type = graphle::meta::deduce_as<vertex>,
206 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
207 .get_vertices = [this] { return views::all(vertices) | views::transform(util::addressof); },
208 .get_out_edges = [] (vertex* v) { return views::all(v->out) | views::edge_from(v); }
209 };
210 }
211 };
212
213
219 // This is bad performance-wise but this type is only used for testing.
220 struct vertex {
221 std::size_t vertex_id;
222 std::vector<std::shared_ptr<vertex>> in;
223 std::vector<std::shared_ptr<vertex>> out;
224 };
225
226 std::shared_ptr<vertex> root;
227
228
229 static in_out_edge_graph from_ve_list(const ve_list_graph& g, std::size_t root = 0) {
230 std::unordered_map<std::size_t, std::shared_ptr<vertex>> vertices;
231
232 for (const auto& v : g.vertices) {
233 vertices.emplace(v.vertex_id, std::make_shared<vertex>(v.vertex_id));
234 }
235
236 for (const auto& e : g.edges) {
237 auto& from = vertices.at(e.first.vertex_id);
238 auto& to = vertices.at(e.second.vertex_id);
239
240 from->out.push_back(to);
241 to->in.push_back(from);
242 }
243
244 return in_out_edge_graph { .root = vertices.at(root) };
245 }
246
247
248 constexpr auto view_as_graph(void) {
249 return graph {
250 .deduce_vertex_type = graphle::meta::deduce_as<vertex>,
251 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
252 .get_out_edges = [] (vertex* v) {
253 return views::all(v->out)
254 | views::transform(&std::shared_ptr<vertex>::get)
255 | views::edge_from(v);
256 },
257 .get_in_edges = [] (vertex* v) {
258 return views::all(v->in)
259 | views::transform(&std::shared_ptr<vertex>::get)
260 | views::edge_to(v);
261 }
262 };
263 }
264 };
265
266
272 struct vertex {
273 std::size_t vertex_id;
274 std::vector<vertex*> in;
275 std::vector<vertex*> out;
276 };
277
278 std::vector<vertex> vertices;
279
280
282 auto vs = g.vertices | std::views::transform(to_vertex<vertex>);
283 v_list_in_out_edge_graph result { .vertices { vs.begin(), vs.end() } };
284 std::unordered_map<std::size_t, vertex*> vertex_map;
285
286 for (const auto [from, to] : g.edges) {
287 for (auto v : { from, to }) {
288 if (!vertex_map.contains(v.vertex_id)) {
289 vertex_map.emplace(v.vertex_id, &*std::ranges::find(result.vertices, vertex { v.vertex_id }));
290 }
291 }
292
293 vertex_map.at(from.vertex_id)->out.push_back(vertex_map.at(to.vertex_id));
294 vertex_map.at(to.vertex_id)->in.push_back(vertex_map.at(from.vertex_id));
295 }
296
297 return result;
298 }
299
300
301 constexpr auto view_as_graph(void) {
302 return graph {
303 .deduce_vertex_type = graphle::meta::deduce_as<vertex>,
304 .deduce_compare_as = graphle::meta::deduce_as<graphle::compare_by_value<vertex>>,
305 .get_vertices = [this] { return views::all(vertices) | views::transform(util::addressof); },
306 .get_out_edges = [] (vertex* v) { return views::all(v->out) | views::edge_from(v); },
307 .get_in_edges = [] (vertex* v) { return views::all(v->out) | views::edge_to(v); }
308 };
309 }
310 };
311
312
324 >;
325}
#define GRAPHLE_MAKE_NIEBLOID(Name, Function,...)
Definition: functional.hpp:24
constexpr bool operator==(const T &a, const T &b)
constexpr auto transform_edge(F &&fn)
Definition: functional.hpp:95
constexpr auto edge_from
constexpr auto maybe_exists
constexpr auto edge_to
constexpr auto range_at_map_key(M &&map, const K &key)
constexpr addressof_t addressof
Definition: functional.hpp:61
constexpr as_const_t as_const
Definition: functional.hpp:73
meta::deduce_as_t< Vertex > deduce_vertex_type
Definition: graph.hpp:73
std::vector< std::shared_ptr< vertex > > in
std::vector< std::shared_ptr< vertex > > out
static in_out_edge_graph from_ve_list(const ve_list_graph &g, std::size_t root=0)
std::vector< std::shared_ptr< vertex > > out
std::shared_ptr< vertex > root
static out_edge_graph from_ve_list(const ve_list_graph &g, std::size_t root=0)
static v_list_in_out_edge_graph from_ve_list(const ve_list_graph &g)
static v_list_out_edge_graph from_ve_list(const ve_list_graph &g)
constexpr auto view_as_graph(void)
static constexpr ve_list_graph from_ve_list(const ve_list_graph &g)
std::vector< std::pair< vertex, vertex > > edges
constexpr auto view_as_graph(void)
std::unordered_map< vertex, std::vector< vertex > > edges
std::unordered_set< vertex > vertices
static ve_map_graph from_ve_list(const ve_list_graph &g)
constexpr std::size_t operator()(const T &v) const