Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_structure_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
49#include <algorithm>
50#include <map>
51#include <set>
52#include <stdexcept>
53#include <string>
54#include <type_traits>
55#include <typeinfo>
56#include <utility>
57#include <vector>
58
59#include <gtest/gtest.h>
60
61#include <tpl_graph.H>
62#include <single_graph.H>
63
65#include "graph_harness.H"
66#include "graph_oracles.H"
67
68using namespace Aleph;
69using namespace graph_harness;
71
72namespace
73{
74 std::string text(const std::multiset<int> & s)
75 {
76 std::string r = "{";
77 for (int x : s)
78 r += (r.size() > 1 ? " " : "") + std::to_string(x);
79 return r + "}";
80 }
81
82 std::string text(const std::vector<int> & v)
83 {
84 std::string r = "(";
85 for (size_t i = 0; i < v.size(); ++i)
86 r += (i == 0 ? "" : " ") + std::to_string(v[i]);
87 return r + ")";
88 }
89
91 struct Alive
92 {
93 std::vector<bool> nodes;
94 std::vector<bool> arcs;
95
96 explicit Alive(const Spec & s)
97 : nodes(size_t(s.n), true), arcs(s.arcs.size(), true) {}
98
100 void remove_node(const Spec & s, int x)
101 {
102 nodes[size_t(x)] = false;
103 for (size_t i = 0; i < s.arcs.size(); ++i)
104 if (s.arcs[i][0] == x or s.arcs[i][1] == x)
105 arcs[i] = false;
106 }
107 };
108
110 std::multiset<int> model_list(const Spec & s, const Alive & alive, int x, bool digraph)
111 {
112 std::multiset<int> r;
113 for (size_t i = 0; i < s.arcs.size(); ++i)
114 if (alive.arcs[i] and (s.arcs[i][0] == x or (not digraph and s.arcs[i][1] == x)))
115 r.insert(int(i));
116 return r;
117 }
118
120 std::multiset<int> model_end(const Spec & s, const Alive & alive, int x, int e)
121 {
122 std::multiset<int> r;
123 for (size_t i = 0; i < s.arcs.size(); ++i)
124 if (alive.arcs[i] and s.arcs[i][size_t(e)] == x)
125 r.insert(int(i));
126 return r;
127 }
128
130 int other_end(const Spec & s, int i, int x)
131 {
132 return s.arcs[size_t(i)][0] == x ? s.arcs[size_t(i)][1] : s.arcs[size_t(i)][0];
133 }
134
136 template <class It>
137 std::vector<int> forward(It it)
138 {
139 std::vector<int> r;
140 for (; it.has_curr(); it.next())
141 r.push_back(num(it.get_curr()));
142 return r;
143 }
144
151 template <class GT>
152 std::string incidence_error(const GT & g, const Spec & s, const Alive & alive)
153 {
154 const bool digraph = g.is_digraph();
155 const size_t n = size_t(std::count(alive.nodes.begin(), alive.nodes.end(), true));
156 const size_t m = size_t(std::count(alive.arcs.begin(), alive.arcs.end(), true));
157 if (g.get_num_nodes() != n or g.get_num_arcs() != m)
158 return std::to_string(g.get_num_nodes()) + " nodes and " + std::to_string(g.get_num_arcs())
159 + " arcs, not " + std::to_string(n) + " and " + std::to_string(m);
160
161 std::vector<typename GT::Node *> node(size_t(s.n), nullptr);
162 size_t seen = 0;
163 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne(), ++seen)
164 {
165 auto * p = it.get_curr();
166 const int x = num(p);
167 if (x < 0 or x >= s.n or not alive.nodes[size_t(x)] or node[size_t(x)] != nullptr)
168 return "the node " + std::to_string(x) + " is not of the graph, or is twice";
169 node[size_t(x)] = p;
170 }
171 if (seen != n)
172 return "the node iterator gives " + std::to_string(seen) + " nodes";
173
174 std::vector<bool> arc_seen(s.arcs.size(), false);
175 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
176 {
177 auto * a = it.get_curr();
178 const int i = num(a);
179 if (i < 0 or size_t(i) >= s.arcs.size() or not alive.arcs[size_t(i)] or arc_seen[size_t(i)])
180 return "the arc " + std::to_string(i) + " is not of the graph, or is twice";
181 arc_seen[size_t(i)] = true;
182 if (g.get_src_node(a) != node[size_t(s.arcs[size_t(i)][0])]
183 or g.get_tgt_node(a) != node[size_t(s.arcs[size_t(i)][1])])
184 return "the ends of the arc " + std::to_string(i) + " are wrong";
185 }
186 if (std::count(arc_seen.begin(), arc_seen.end(), true) != std::ptrdiff_t(m))
187 return "the arc iterator misses arcs";
188
189 for (int x = 0; x < s.n; ++x)
190 {
191 if (not alive.nodes[size_t(x)])
192 continue;
193 auto * p = node[size_t(x)];
194 const auto want = model_list(s, alive, x, digraph);
195 if (g.get_num_arcs(p) != want.size())
196 return "get_num_arcs(" + std::to_string(x) + ") is " + std::to_string(g.get_num_arcs(p))
197 + ", not " + std::to_string(want.size());
198 std::multiset<int> got;
199 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
200 {
201 auto * a = it.get_curr();
202 got.insert(num(a));
203 const int i = num(a);
204 if (num(g.get_connected_node(a, p)) != other_end(s, i, x)
205 or num(it.get_tgt_node_ne()) != other_end(s, i, x))
206 return "the arc " + std::to_string(i) + " from " + std::to_string(x)
207 + " goes to the wrong node";
208 }
209 if (got != want)
210 return "the list of " + std::to_string(x) + " is " + text(got) + ", not " + text(want);
211 }
212 return "";
213 }
214}
215
216template <class GT>
217class ExhaustiveStructure : public ::testing::Test {};
218
220
221TYPED_TEST(ExhaustiveStructure, IncidenceMatchesTheModel)
222{
223 using GT = TypeParam;
224 using Node = typename GT::Node;
225 using Arc = typename GT::Arc;
226 const bool digraph = GT().is_digraph();
227 Check incidence("the counts, the iterators and the lists of the nodes");
228 Check out("Out_Iterator and the out_ methods");
229 Check in("In_Iterator(g, p) and the in_ methods");
230 Check in_free("GT::In_Iterator(g, p) and the in_ functions that take the graph");
231 Check in_list("In_Iterator(p) and the in_ functions of the node, if not directed");
232 Check index("In_Arcs_Index");
233 Check search("search_arc(u, v)");
234 Check directed("search_directed_arc(u, v)");
235
236 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
237 {
238 Built<GT> b(s);
239 const Alive alive(s);
240 const In_Arcs_Index<GT> in_arcs_index(b.g);
241
242 incidence.run();
243 if (auto e = incidence_error(b.g, s, alive); not e.empty())
244 incidence.fail(s, e);
245
246 for (int x = 0; x < s.n; ++x)
247 {
248 Node * p = b.nodes[size_t(x)];
249 const std::string at = "at " + std::to_string(x) + ": ";
250
251 // The arcs out and in of a node, and the nodes at their other
252 // ends, through the iterator and through every method
253 auto check_end = [&] (Check & check, int e, auto && it_arcs, const DynList<Arc *> & arcs,
254 const DynList<Node *> & nodes, const auto & pairs, size_t degree,
255 size_t traversed, bool all_ok)
256 {
257 check.run();
258 const auto want = model_end(s, alive, x, e);
259 std::multiset<int> want_nodes;
260 for (int i : want)
261 want_nodes.insert(s.arcs[size_t(i)][size_t(1 - e)]);
262 std::multiset<int> got_it(it_arcs.begin(), it_arcs.end()), got_arcs, got_nodes,
263 pair_arcs, pair_nodes;
264 for (auto it = arcs.get_it(); it.has_curr(); it.next_ne())
265 got_arcs.insert(num(it.get_curr()));
266 for (auto it = nodes.get_it(); it.has_curr(); it.next_ne())
267 got_nodes.insert(num(it.get_curr()));
268 for (auto it = pairs.get_it(); it.has_curr(); it.next_ne())
269 {
270 pair_arcs.insert(num(std::get<0>(it.get_curr())));
271 pair_nodes.insert(num(std::get<1>(it.get_curr())));
272 }
273 if (got_it != want)
274 check.fail(s, at + "the iterator gives " + text(got_it) + ", not " + text(want));
275 else if (got_arcs != want or pair_arcs != want)
276 check.fail(s, at + "the arcs are " + text(got_arcs) + ", not " + text(want));
277 else if (got_nodes != want_nodes or pair_nodes != want_nodes)
278 check.fail(s, at + "the nodes are " + text(got_nodes) + ", not " + text(want_nodes));
279 else if (degree != want.size() or traversed != want.size() or not all_ok)
280 check.fail(s, at + "the degree is " + std::to_string(degree) + ", not "
281 + std::to_string(want.size()));
282 };
283
284 {
285 size_t traversed = 0;
286 b.g.for_each_out_arc(p, [&] (Arc *) { ++traversed; });
287 const bool all_ok = b.g.all_out_arcs(p, [&] (Arc * a) { return b.g.get_src_node(a) == p; });
288 check_end(out, 0, forward(Out_Iterator<GT>(p)), b.g.out_arcs(p), b.g.out_nodes(p),
289 b.g.out_pairs(p), b.g.out_degree(p), traversed, all_ok);
290 }
291 auto into_p = [&] (Arc * a) { return b.g.get_tgt_node(a) == p; };
292 {
293 size_t traversed = 0;
294 b.g.for_each_in_arc(p, [&] (Arc *) { ++traversed; });
295 const bool all_ok = b.g.all_in_arcs(p, into_p);
296 check_end(in, 1, forward(In_Iterator<GT>(b.g, p)), b.g.in_arcs(p), b.g.in_nodes(p),
297 b.g.in_pairs(p), b.g.in_degree(p), traversed, all_ok);
298 }
299 {
300 size_t traversed = 0;
301 for_each_in_arc(b.g, p, [&] (Arc *) { ++traversed; });
302 const bool all_ok = all_in_arc(b.g, p, into_p) and traverse_in_arcs(b.g, p, into_p)
303 and not exists_in_arc(b.g, p, [&] (Arc * a) { return not into_p(a); });
304 check_end(in_free, 1, forward(typename GT::In_Iterator(b.g, p)), in_arcs(b.g, p),
305 in_nodes(b.g, p), in_pairs(b.g, p), in_degree(b.g, p), traversed, all_ok);
306 }
307 if constexpr (not DigraphType<GT>)
308 {
309 size_t traversed = 0;
310 for_each_in_arc<GT>(p, [&] (Arc *) { ++traversed; });
311 const bool all_ok = all_in_arc<GT>(p, into_p) and traverse_in_arcs<GT>(p, into_p);
312 check_end(in_list, 1, forward(In_Iterator<GT>(p)), in_arcs<GT>(p), in_nodes<GT>(p),
313 in_pairs<GT>(p), in_degree<GT>(p), traversed, all_ok);
314 }
315
316 index.run();
317 {
318 const auto want = model_end(s, alive, x, 1);
319 const auto got = forward(in_arcs_index.get_it(p));
320 if (std::multiset<int>(got.begin(), got.end()) != want
321 or in_arcs_index.in_degree(p) != want.size())
322 index.fail(s, at + "gives " + text(got) + ", not " + text(want));
323 else
324 for (auto it = in_arcs_index.get_it(p); it.has_curr(); it.next_ne())
325 if (it.get_node_ne() != b.g.get_src_node(it.get_curr_ne()))
326 {
327 index.fail(s, at + "the node of an arc is not its source");
328 break;
329 }
330 }
331
332 for (int y = 0; y < s.n; ++y)
333 {
334 Node * q = b.nodes[size_t(y)];
335 bool joined = false, from_to = false;
336 for (const auto & a : s.arcs)
337 {
338 from_to = from_to or (a[0] == x and a[1] == y);
339 joined = joined or (a[0] == x and a[1] == y) or (a[0] == y and a[1] == x);
340 }
341
342 search.run();
343 {
344 Arc * a = b.g.search_arc(p, q);
345 const bool want = digraph ? from_to : joined;
346 if ((a != nullptr) != want)
347 search.fail(s, at + "to " + std::to_string(y) + (want ? " finds no arc" : " finds an arc"));
348 else if (a != nullptr and not ((b.g.get_src_node(a) == p and b.g.get_tgt_node(a) == q)
349 or (not digraph and b.g.get_src_node(a) == q
350 and b.g.get_tgt_node(a) == p)))
351 search.fail(s, at + "to " + std::to_string(y) + " finds an arc between other nodes");
352 }
353
354 directed.run();
355 {
356 Arc * a = b.g.search_directed_arc(p, q);
357 if ((a != nullptr) != from_to)
358 directed.fail(s, at + "to " + std::to_string(y)
359 + (from_to ? " finds no arc" : " finds an arc"));
360 else if (a != nullptr and (b.g.get_src_node(a) != p or b.g.get_tgt_node(a) != q))
361 directed.fail(s, at + "to " + std::to_string(y) + " finds an arc of other ends");
362 }
363 }
364 }
365 });
366
367 incidence.expect();
368 out.expect();
369 in.expect();
370 in_free.expect();
371 in_list.expect();
372 index.expect();
373 search.expect();
374 directed.expect();
375}
376
377namespace
378{
386 template <class GT, class It>
387 std::string walk_error(const Spec & s, Built<GT> & b, int x, int e, unsigned long hidden, It it)
388 {
389 const bool digraph = b.g.is_digraph();
390 const Alive alive(s);
391 std::multiset<int> want = e < 0 ? model_list(s, alive, x, digraph) : model_end(s, alive, x, e);
392 for (auto i = want.begin(); i != want.end(); )
393 i = graph_filters::shown(hidden, *i) ? std::next(i) : want.erase(i);
394
395 const auto first = forward(it); // on a copy
396 if (std::multiset<int>(first.begin(), first.end()) != want)
397 return "forward it gives " + text(first) + ", not " + text(want);
398
399 while (it.has_curr()) // off its end, as a loop leaves it
400 it.next();
401 it.reset_first();
402 if (const auto again = forward(it); again != first)
403 return "after reset_first() it gives " + text(again) + ", not " + text(first);
404
405 // The iterators of List_SGraph and List_SDigraph go only forward: they
406 // have no reset_last() nor prev()
407 if constexpr (requires { it.reset_last(); it.prev(); })
408 {
409 std::vector<int> back;
410 for (it.reset_last(); it.has_curr(); it.prev())
411 back.push_back(num(it.get_curr()));
412 std::reverse(back.begin(), back.end());
413 if (back != first)
414 return "backward it gives " + text(back) + " reversed, not " + text(first);
415 }
416 return "";
417 }
418
421 template <class GT, template <class, class> class It>
422 std::string iterator_error(const Spec & s, Built<GT> & b, int x, int e, unsigned long hidden)
423 {
424 using Filter = Hide_Mask<GT>;
425 return walk_error(s, b, x, e, hidden, It<GT, Filter>(b.nodes[size_t(x)], Filter{hidden}));
426 }
427}
428
429TYPED_TEST(ExhaustiveStructure, IteratorsWalkBothWaysWithTheFilter)
430{
431 using GT = TypeParam;
432 using Node = typename GT::Node;
433 using Filter = Hide_Mask<GT>;
434 const bool digraph = GT().is_digraph();
435 Check node_arc("Node_Arc_Iterator");
436 Check out("Out_Iterator");
437 Check in("In_Iterator(g, p)");
438 Check in_list("In_Iterator(p), if not directed");
439 Check index("In_Arcs_Index::Iterator");
440
441 for_each_spec(domain(3, 3), digraph, [&] (const Spec & s)
442 {
443 Built<GT> b(s);
444 for (unsigned long hidden = 0; hidden < (1UL << s.num_arcs()); ++hidden)
445 {
446 const In_Arcs_Index<GT, Filter> in_arcs_index(b.g, Filter{hidden});
447 for (int x = 0; x < s.n; ++x)
448 {
449 Node * p = b.nodes[size_t(x)];
450 const std::string at = "at " + std::to_string(x) + " hiding "
451 + std::to_string(hidden) + ": ";
452 node_arc.run();
453 if (auto e = iterator_error<GT, Node_Arc_Iterator>(s, b, x, -1, hidden);
454 not e.empty())
455 node_arc.fail(s, at + e);
456 out.run();
457 if (auto e = iterator_error<GT, Out_Iterator>(s, b, x, 0, hidden); not e.empty())
458 out.fail(s, at + e);
459 in.run();
460 In_Iterator<GT, Filter> in_it(b.g, p, Filter{hidden});
461 if (auto e = walk_error(s, b, x, 1, hidden, in_it); not e.empty())
462 in.fail(s, at + e);
463 if constexpr (not DigraphType<GT>)
464 {
465 in_list.run();
466 if (auto e = iterator_error<GT, In_Iterator>(s, b, x, 1, hidden); not e.empty())
467 in_list.fail(s, at + e);
468 }
469 index.run();
470 if (auto e = walk_error(s, b, x, 1, hidden, in_arcs_index.get_it(p)); not e.empty())
471 index.fail(s, at + e);
472 }
473 }
474 });
475
476 node_arc.expect();
477 out.expect();
478 in.expect();
479 in_list.expect();
480 index.expect();
481}
482
483TYPED_TEST(ExhaustiveStructure, InArcsOfAGraphThatSaysThatItIsDirected)
484{
485 using GT = TypeParam;
486 using Arc = typename GT::Arc;
487 // A graph of an undirected kind, flipped with set_digraph(true) before
488 // its arcs, keeps each arc only in the list of its source: the forms
489 // that take the graph tell it at run time, and In_Iterator(g, p), which
490 // would walk the list of p, refuses it
491 Check lists("the counts, the iterators and the lists of the nodes");
492 Check in("the in_ methods and the in_ functions that take the graph");
493 Check index("In_Arcs_Index");
494 Check refused("In_Iterator(g, p) and GT::In_Iterator(g, p) throw domain_error");
495
496 if constexpr (not DigraphType<GT>)
497 for_each_spec(domain(4, 3), true, [&] (const Spec & s)
498 {
499 GT g;
500 g.set_digraph(true);
501 std::vector<typename GT::Node *> node;
502 for (int i = 0; i < s.n; ++i)
503 node.push_back(g.insert_node(i));
504 for (size_t i = 0; i < s.arcs.size(); ++i)
505 g.insert_arc(node[size_t(s.arcs[i][0])], node[size_t(s.arcs[i][1])], int(i));
506 const Alive alive(s);
507
508 lists.run();
509 if (auto e = incidence_error(g, s, alive); not e.empty())
510 lists.fail(s, e);
511
512 const In_Arcs_Index<GT> in_arcs_index(g);
513 for (int x = 0; x < s.n; ++x)
514 {
515 auto * p = node[size_t(x)];
516 const std::string at = "at " + std::to_string(x) + ": ";
517 const auto want = model_end(s, alive, x, 1);
518 auto numbers = [] (const DynList<Arc *> & l)
519 {
520 std::multiset<int> r;
521 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
522 r.insert(num(it.get_curr()));
523 return r;
524 };
525
526 in.run();
527 size_t traversed = 0;
528 for_each_in_arc(g, p, [&] (Arc *) { ++traversed; });
529 if (numbers(g.in_arcs(p)) != want or numbers(in_arcs(g, p)) != want
530 or g.in_degree(p) != want.size() or in_degree(g, p) != want.size()
531 or in_nodes(g, p).size() != want.size() or g.in_nodes(p).size() != want.size()
532 or traversed != want.size())
533 in.fail(s, at + "the arcs into it are not " + text(want));
534
535 index.run();
536 const auto got = forward(in_arcs_index.get_it(p));
537 if (std::multiset<int>(got.begin(), got.end()) != want
538 or in_arcs_index.in_degree(p) != want.size())
539 index.fail(s, at + "gives " + text(got) + ", not " + text(want));
540
541 refused.run();
542 bool by_free = false, by_member = false;
543 try { (void) In_Iterator<GT>(g, p); }
544 catch (const std::domain_error &) { by_free = true; }
545 try { (void) typename GT::In_Iterator(g, p); }
546 catch (const std::domain_error &) { by_member = true; }
547 if (not by_free or not by_member)
548 refused.fail(s, at + "an In_Iterator(g, p) walks the list");
549 }
550 });
551
552 lists.expect();
553 in.expect();
554 index.expect();
555 refused.expect();
556}
557
558TYPED_TEST(ExhaustiveStructure, RemovalsMatchTheModel)
559{
560 using GT = TypeParam;
561 const bool digraph = GT().is_digraph();
562 Check arcs("remove_arc()");
563 Check nodes("remove_node()");
564 Check all("removing every node");
565
566 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
567 {
568 for (size_t i = 0; i < s.arcs.size(); ++i)
569 {
570 arcs.run();
571 Built<GT> b(s);
572 b.g.remove_arc(b.arcs[i]);
573 Alive alive(s);
574 alive.arcs[i] = false;
575 if (auto e = incidence_error(b.g, s, alive); not e.empty())
576 arcs.fail(s, "without the arc " + std::to_string(i) + ": " + e);
577 }
578
579 for (int x = 0; x < s.n; ++x)
580 {
581 nodes.run();
582 Built<GT> b(s);
583 b.g.remove_node(b.nodes[size_t(x)]);
584 Alive alive(s);
585 alive.remove_node(s, x);
586 if (auto e = incidence_error(b.g, s, alive); not e.empty())
587 nodes.fail(s, "without the node " + std::to_string(x) + ": " + e);
588 }
589
590 // Every node, in the order of the spec, checked after each removal
591 all.run();
592 Built<GT> b(s);
593 Alive alive(s);
594 for (int x = 0; x < s.n; ++x)
595 {
596 b.g.remove_node(b.nodes[size_t(x)]);
597 alive.remove_node(s, x);
598 if (auto e = incidence_error(b.g, s, alive); not e.empty())
599 {
600 all.fail(s, "after the node " + std::to_string(x) + ": " + e);
601 break;
602 }
603 }
604 });
605
606 arcs.expect();
607 nodes.expect();
608 all.expect();
609}
610
611TYPED_TEST(ExhaustiveStructure, CopiesMatchTheModel)
612{
613 using GT = TypeParam;
614 const bool digraph = GT().is_digraph();
615 Check copy("the copy constructor");
616 Check assign("the copy assignment");
617 Check move("the move constructor and assignment");
618 Check swap("swap()");
619 Check mapped("copy_graph() with the cookies");
620
621 // A graph to be overwritten, with a node and a loop
622 auto other = []
623 {
624 GT g;
625 auto * p = g.insert_node(-1);
626 g.insert_arc(p, p, -1);
627 return g;
628 };
629
630 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
631 {
632 Built<GT> b(s);
633 const Alive alive(s);
634 const Spec none{};
635 const Alive nothing(none);
636
637 copy.run();
638 {
639 const GT c(b.g);
640 if (auto e = incidence_error(c, s, alive); not e.empty())
641 copy.fail(s, "the copy: " + e);
642 else if (auto e2 = incidence_error(b.g, s, alive); not e2.empty())
643 copy.fail(s, "the original: " + e2);
644 }
645
646 assign.run();
647 {
648 GT c = other();
649 c = b.g;
650 if (auto e = incidence_error(c, s, alive); not e.empty())
651 assign.fail(s, "the copy: " + e);
652 else if (auto e2 = incidence_error(b.g, s, alive); not e2.empty())
653 assign.fail(s, "the original: " + e2);
654 }
655
656 move.run();
657 {
658 GT c(b.g);
659 GT moved(std::move(c));
660 GT assigned = other();
661 assigned = std::move(moved);
662 if (auto e = incidence_error(assigned, s, alive); not e.empty())
663 move.fail(s, "the result: " + e);
664 else
665 {
666 // What was moved from is still a graph
667 auto * p = moved.insert_node(0);
668 moved.insert_arc(p, p, 0);
669 if (moved.get_num_nodes() < 1 or moved.get_num_arcs() < 1)
670 move.fail(s, "a graph moved from does not take nodes");
671 }
672 }
673
674 swap.run();
675 {
676 GT c(b.g), d;
677 c.swap(d);
678 if (auto e = incidence_error(d, s, alive); not e.empty())
679 swap.fail(s, "the swapped graph: " + e);
680 else if (auto e2 = incidence_error(c, none, nothing); not e2.empty())
681 swap.fail(s, "the empty one: " + e2);
682 }
683
684 mapped.run();
685 {
686 GT c = other();
687 copy_graph(c, b.g, true);
688 bool ok = incidence_error(c, s, alive).empty();
689 for (auto it = b.g.get_node_it(); ok and it.has_curr(); it.next_ne())
690 {
691 auto * p = it.get_curr();
692 auto * q = mapped_node<GT>(p);
693 ok = q != nullptr and num(q) == num(p) and mapped_node<GT>(q) == p;
694 }
695 for (auto it = b.g.get_arc_it(); ok and it.has_curr(); it.next_ne())
696 {
697 auto * a = it.get_curr();
698 auto * ca = mapped_arc<GT>(a);
699 ok = ca != nullptr and num(ca) == num(a) and mapped_arc<GT>(ca) == a;
700 }
701 if (not ok)
702 mapped.fail(s, "a wrong copy or map");
703 }
704 });
705
706 copy.expect();
707 assign.expect();
708 move.expect();
709 swap.expect();
710 mapped.expect();
711}
712
713namespace
714{
717 struct Spec_Walk
718 {
719 int start;
720 std::vector<int> arcs;
721 std::vector<int> nodes; // start, then the node after each arc
722 };
723
725 template <class F>
726 void for_each_walk(const Spec & s, size_t len, bool directed, F && f)
727 {
728 std::vector<Spec_Walk> todo;
729 for (int x = 0; x < s.n; ++x)
730 todo.push_back({x, {}, {x}});
731 while (not todo.empty())
732 {
733 Spec_Walk w = todo.back();
734 todo.pop_back();
735 f(static_cast<const Spec_Walk &>(w));
736 if (w.arcs.size() == len)
737 continue;
738 const int at = w.nodes.back();
739 for (size_t i = 0; i < s.arcs.size(); ++i)
740 for (int e = 0; e < (directed ? 1 : 2); ++e)
741 {
742 const auto & a = s.arcs[i];
743 if (a[size_t(e)] != at or (e == 1 and a[0] == a[1]))
744 continue;
745 Spec_Walk next = w;
746 next.arcs.push_back(int(i));
747 next.nodes.push_back(a[size_t(1 - e)]);
748 todo.push_back(std::move(next));
749 }
750 }
751 }
752
754 template <class GT>
755 typename GT::Arc * arc_of(const GT & g, int i)
756 {
757 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
758 if (num(it.get_curr()) == i)
759 return it.get_curr();
760 return nullptr;
761 }
762
763 template <class GT>
764 std::vector<int> path_nodes(const Path<GT> & p)
765 {
766 std::vector<int> r;
767 for (typename Path<GT>::Iterator it(p); it.has_current_node(); it.next_ne())
768 r.push_back(num(it.get_current_node_ne()));
769 return r;
770 }
771
772 template <class GT>
773 std::vector<int> path_arcs(const Path<GT> & p)
774 {
775 std::vector<int> r;
776 for (typename Path<GT>::Iterator it(p); it.has_current_arc(); it.next_ne())
777 r.push_back(num(it.get_current_arc_ne()));
778 return r;
779 }
780
783 template <class GT>
784 std::string path_error(const Spec & s, const Path<GT> & p, const Spec_Walk & w,
785 bool any_arc, bool directed)
786 {
787 const auto nodes = path_nodes(p);
788 const auto arcs = path_arcs(p);
789 if (nodes != w.nodes)
790 return "the nodes are " + text(nodes) + ", not " + text(w.nodes);
791 if (arcs.size() != w.arcs.size() or p.size() != w.nodes.size())
792 return "the path has " + std::to_string(arcs.size()) + " arcs";
793 for (size_t i = 0; i < arcs.size(); ++i)
794 {
795 if (not any_arc and arcs[i] != w.arcs[i])
796 return "the arcs are " + text(arcs) + ", not " + text(w.arcs);
797 const auto & a = s.arcs[size_t(arcs[i])];
798 const bool along = a[0] == w.nodes[i] and a[1] == w.nodes[i + 1];
799 const bool against = a[1] == w.nodes[i] and a[0] == w.nodes[i + 1];
800 if (not along and (directed or not against))
801 return "the arc " + std::to_string(arcs[i]) + " does not join "
802 + std::to_string(w.nodes[i]) + " and " + std::to_string(w.nodes[i + 1]);
803 }
804 if (num(p.get_first_node()) != w.nodes.front() or num(p.get_last_node()) != w.nodes.back())
805 return "wrong first or last node";
806 if (not arcs.empty() and (num(p.get_first_arc()) != arcs.front()
807 or num(p.get_last_arc()) != arcs.back()))
808 return "wrong first or last arc";
809 if (p.is_cycle() != (w.nodes.front() == w.nodes.back()))
810 return "is_cycle() is wrong";
811
812 // The other ways to read the path
813 const GT & g = p.get_graph();
814 std::vector<int> listed_nodes, listed_arcs;
815 const auto node_list = p.nodes();
816 const auto arc_list = p.arcs();
817 for (auto it = node_list.get_it(); it.has_curr(); it.next_ne())
818 listed_nodes.push_back(num(it.get_curr()));
819 for (auto it = arc_list.get_it(); it.has_curr(); it.next_ne())
820 listed_arcs.push_back(num(it.get_curr()));
821 if (listed_nodes != nodes or listed_arcs != arcs)
822 return "nodes() or arcs() differ from the iterator";
823 for (int x : nodes)
824 if (not p.contains_node(node_of(g, x)))
825 return "contains_node() misses " + std::to_string(x);
826 for (int i : arcs)
827 if (not p.contains_arc(arc_of(g, i)))
828 return "contains_arc() misses " + std::to_string(i);
829 return "";
830 }
831}
832
833TYPED_TEST(ExhaustiveStructure, PathsFollowEveryWalk)
834{
835 using GT = TypeParam;
836 using Node = typename GT::Node;
837 const bool digraph = GT().is_digraph();
838 Check by_arcs("Path::append(Arc *)");
839 Check by_nodes("Path::append(Node *)");
840 Check ins_arcs("Path::insert(Arc *)");
841 Check ins_nodes("Path::insert(Node *)");
842 Check dir_arcs("Path::append_directed(Arc *) and insert_directed(Arc *)");
843 Check dir_nodes("Path::append_directed(Node *) and insert_directed(Node *)");
844 Check removals("Path::remove_last_node() and remove_first_node()");
845 Check copies("the copies, moves and swaps of a path");
846 Check check("Path::check()");
847 Check check_directed("Path::check_directed()");
848
849 for_each_spec(domain(3, 3), digraph, [&] (const Spec & s)
850 {
851 Built<GT> b(s);
852 auto node = [&] (int x) -> Node * { return b.nodes[size_t(x)]; };
853
854 // The walks along the arcs and, if not directed, against them
855 for_each_walk(s, 3, digraph, [&] (const Spec_Walk & w)
856 {
857 by_arcs.run();
858 Path<GT> p(b.g, node(w.start));
859 for (int i : w.arcs)
860 p.append(b.arcs[size_t(i)]);
861 if (auto e = path_error(s, p, w, false, digraph); not e.empty())
862 by_arcs.fail(s, e);
863
864 by_nodes.run();
865 Path<GT> q(b.g);
866 for (int x : w.nodes)
867 q.append(node(x));
868 if (auto e = path_error(s, q, w, true, digraph); not e.empty())
869 by_nodes.fail(s, e);
870
871 ins_arcs.run();
872 Path<GT> r(b.g, node(w.nodes.back()));
873 for (size_t i = w.arcs.size(); i-- > 0; )
874 r.insert(b.arcs[size_t(w.arcs[i])]);
875 if (auto e = path_error(s, r, w, false, digraph); not e.empty())
876 ins_arcs.fail(s, e);
877
878 ins_nodes.run();
879 Path<GT> t(b.g);
880 for (size_t i = w.nodes.size(); i-- > 0; )
881 t.insert(node(w.nodes[i]));
882 if (auto e = path_error(s, t, w, true, digraph); not e.empty())
883 ins_nodes.fail(s, e);
884
885 check.run();
886 if (not p.check())
887 check.fail(s, "says that the path " + text(w.nodes) + " is not consistent");
888
889 removals.run();
890 {
891 Path<GT> c = p;
892 Spec_Walk rest = w;
893 bool ok = true;
894 while (ok and not rest.nodes.empty())
895 {
896 const bool last = rest.nodes.size() % 2 == 0;
897 const int x = num(last ? c.remove_last_node() : c.remove_first_node());
898 ok = x == (last ? rest.nodes.back() : rest.nodes.front());
899 if (last)
900 {
901 rest.nodes.pop_back();
902 if (not rest.arcs.empty())
903 rest.arcs.pop_back();
904 }
905 else
906 {
907 rest.nodes.erase(rest.nodes.begin());
908 if (not rest.arcs.empty())
909 rest.arcs.erase(rest.arcs.begin());
910 }
911 ok = ok and (rest.nodes.empty() ? c.is_empty()
912 : path_error(s, c, rest, false, digraph).empty());
913 }
914 if (not ok)
915 removals.fail(s, "taking out the nodes of " + text(w.nodes));
916 }
917
918 copies.run();
919 {
920 Path<GT> c(p), d(b.g);
921 Path<GT> moved(std::move(c));
922 d = moved;
923 Path<GT> e(b.g);
924 e.swap(d);
925 if (not path_error(s, moved, w, false, digraph).empty()
926 or not path_error(s, e, w, false, digraph).empty() or not d.is_empty())
927 copies.fail(s, "of " + text(w.nodes));
928 }
929
930 // A walk against an arc is not a directed path
931 check_directed.run();
932 bool along = true;
933 for (size_t i = 0; i < w.arcs.size(); ++i)
934 along = along and s.arcs[size_t(w.arcs[i])][0] == w.nodes[i]
935 and s.arcs[size_t(w.arcs[i])][1] == w.nodes[i + 1];
936 if (p.check_directed() != along)
937 check_directed.fail(s, "says that the path " + text(w.nodes)
938 + (along ? " is not" : " is") + " directed");
939 });
940
941 // The walks along the arcs, as directed, on every kind
942 for_each_walk(s, 3, true, [&] (const Spec_Walk & w)
943 {
944 dir_arcs.run();
945 Path<GT> p(b.g, node(w.start));
946 for (int i : w.arcs)
947 p.append_directed(b.arcs[size_t(i)]);
948 Path<GT> r(b.g, node(w.nodes.back()));
949 for (size_t i = w.arcs.size(); i-- > 0; )
950 r.insert_directed(b.arcs[size_t(w.arcs[i])]);
951 if (auto e = path_error(s, p, w, false, true); not e.empty())
952 dir_arcs.fail(s, "appending: " + e);
953 else if (auto e2 = path_error(s, r, w, false, true); not e2.empty())
954 dir_arcs.fail(s, "inserting: " + e2);
955
956 dir_nodes.run();
957 Path<GT> q(b.g), t(b.g);
958 for (int x : w.nodes)
959 q.append_directed(node(x));
960 for (size_t i = w.nodes.size(); i-- > 0; )
961 t.insert_directed(node(w.nodes[i]));
962 if (auto e = path_error(s, q, w, true, true); not e.empty())
963 dir_nodes.fail(s, "appending: " + e);
964 else if (auto e2 = path_error(s, t, w, true, true); not e2.empty())
965 dir_nodes.fail(s, "inserting: " + e2);
966 });
967 });
968
969 by_arcs.expect();
970 by_nodes.expect();
971 ins_arcs.expect();
972 ins_nodes.expect();
973 dir_arcs.expect();
974 dir_nodes.expect();
975 removals.expect();
976 copies.expect();
977 check.expect();
978 check_directed.expect();
979}
980
981TYPED_TEST(ExhaustiveStructure, PathsRejectWhatDoesNotFollow)
982{
983 using GT = TypeParam;
984 const bool digraph = GT().is_digraph();
985 Check arc("Path::append(Arc *) with an arc away from the last node");
986 Check node("Path::append(Node *) toward a node without an arc");
987 Check inserted("Path::insert(Node *) from a node without an arc");
988 Check inserted_directed("Path::insert_directed(Node *) from a node without an arc to the first");
989 Check directed("Path::append_directed(Arc *) with an arc that does not leave the last node");
990
991 for_each_spec(domain(3, 2), digraph, [&] (const Spec & s)
992 {
993 Built<GT> b(s);
994 for (int x = 0; x < s.n; ++x)
995 {
996 const std::string at = "from " + std::to_string(x) + ": ";
997 for (size_t i = 0; i < s.arcs.size(); ++i)
998 {
999 const auto & a = s.arcs[i];
1000 Path<GT> p(b.g, b.nodes[size_t(x)]);
1001 if (a[0] != x and a[1] != x)
1002 {
1003 arc.run();
1004 try
1005 {
1006 p.append(b.arcs[i]);
1007 arc.fail(s, at + "takes the arc " + std::to_string(i));
1008 }
1009 catch (const std::invalid_argument &) {}
1010 }
1011 if (a[0] != x)
1012 {
1013 directed.run();
1014 Path<GT> q(b.g, b.nodes[size_t(x)]);
1015 try
1016 {
1017 q.append_directed(b.arcs[i]);
1018 directed.fail(s, at + "takes the arc " + std::to_string(i));
1019 }
1020 catch (const std::invalid_argument &) {}
1021 }
1022 }
1023
1024 // append() goes along an arc from the last node, and insert()
1025 // along one to the first node; in a graph that is not directed,
1026 // along any arc between them
1027 for (int y = 0; y < s.n; ++y)
1028 {
1029 bool to_y = false, from_y = false;
1030 for (const auto & a : s.arcs)
1031 {
1032 const bool x_y = a[0] == x and a[1] == y, y_x = a[0] == y and a[1] == x;
1033 to_y = to_y or x_y or (not digraph and y_x);
1034 from_y = from_y or y_x or (not digraph and x_y);
1035 }
1036
1037 if (not to_y)
1038 {
1039 node.run();
1040 try
1041 {
1042 Path<GT> p(b.g, b.nodes[size_t(x)]);
1043 p.append(b.nodes[size_t(y)]);
1044 node.fail(s, at + "goes to " + std::to_string(y));
1045 }
1046 catch (const std::invalid_argument &) {}
1047 }
1048
1049 if (not from_y)
1050 {
1051 inserted.run();
1052 Path<GT> p(b.g, b.nodes[size_t(x)]);
1053 try
1054 {
1055 p.insert(b.nodes[size_t(y)]);
1056 inserted.fail(s, at + "comes from " + std::to_string(y));
1057 }
1058 catch (const std::invalid_argument &)
1059 {
1060 if (p.size() != 1 or p.get_first_node() != b.nodes[size_t(x)])
1061 inserted.fail(s, at + "changes the path");
1062 }
1063 catch (const std::exception & e)
1064 {
1065 inserted.fail(s, at + "throws " + std::string(typeid(e).name()));
1066 }
1067 }
1068 }
1069
1070 // insert_directed() needs an arc from the inserted node to the
1071 // first one, on every kind
1072 for (int y = 0; y < s.n; ++y)
1073 {
1074 bool from_y = false;
1075 for (const auto & a : s.arcs)
1076 from_y = from_y or (a[0] == y and a[1] == x);
1077 if (from_y)
1078 continue;
1079
1080 inserted_directed.run();
1081 Path<GT> p(b.g, b.nodes[size_t(x)]);
1082 try
1083 {
1084 p.insert_directed(b.nodes[size_t(y)]);
1085 inserted_directed.fail(s, at + "comes from " + std::to_string(y));
1086 }
1087 catch (const std::invalid_argument &)
1088 {
1089 if (p.size() != 1 or p.get_first_node() != b.nodes[size_t(x)])
1090 inserted_directed.fail(s, at + "changes the path");
1091 }
1092 catch (const std::exception & e)
1093 {
1094 inserted_directed.fail(s, at + "throws " + std::string(typeid(e).name()));
1095 }
1096 }
1097 }
1098 });
1099
1100 arc.expect();
1101 node.expect();
1102 inserted.expect();
1103 inserted_directed.expect();
1104 directed.expect();
1105}
1106
1107TYPED_TEST(ExhaustiveStructure, DisconnectedArcsComeBack)
1108{
1109 using GT = TypeParam;
1110 const bool digraph = GT().is_digraph();
1111 Check disconnect("disconnect_arc()");
1112 Check connect("connect_arc() after disconnect_arc()");
1113
1114 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
1115 {
1116 // List_SGraph keeps them private
1117 if constexpr (requires (GT & g, typename GT::Arc * a) { g.disconnect_arc(a);
1118 g.connect_arc(a); })
1119 for (size_t i = 0; i < s.arcs.size(); ++i)
1120 {
1121 Built<GT> b(s);
1122 auto * a = b.arcs[i];
1123 disconnect.run();
1124 b.g.disconnect_arc(a);
1125 Alive alive(s);
1126 alive.arcs[i] = false;
1127 if (auto e = incidence_error(b.g, s, alive); not e.empty())
1128 disconnect.fail(s, "without the arc " + std::to_string(i) + ": " + e);
1129
1130 connect.run();
1131 b.g.connect_arc(a);
1132 if (auto e = incidence_error(b.g, s, Alive(s)); not e.empty())
1133 connect.fail(s, "with the arc " + std::to_string(i) + " back: " + e);
1134 }
1135 });
1136
1137 disconnect.expect();
1138 connect.expect();
1139}
1140
1141TYPED_TEST(ExhaustiveStructure, ClearedGraphsAreEmptyAndTakeNodesAgain)
1142{
1143 using GT = TypeParam;
1144 const bool digraph = GT().is_digraph();
1145 Check clear("clear_graph()");
1146
1147 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
1148 {
1149 clear.run();
1150 Built<GT> b(s);
1151 clear_graph(b.g);
1152 const Spec none{};
1153 if (auto e = incidence_error(b.g, none, Alive(none)); not e.empty())
1154 clear.fail(s, "after clear_graph(): " + e);
1155 else
1156 {
1157 Built<GT> again(s);
1158 again.g.swap(b.g);
1159 if (auto e = incidence_error(b.g, s, Alive(s)); not e.empty())
1160 clear.fail(s, "a cleared graph does not take a graph: " + e);
1161 }
1162 });
1163
1164 clear.expect();
1165}
1166
1167TYPED_TEST(ExhaustiveStructure, SortsOrderTheIteratorsAndKeepTheGraph)
1168{
1169 using GT = TypeParam;
1170 const bool digraph = GT().is_digraph();
1171 Check arcs("sort_arcs()");
1172 Check nodes("sort_nodes()");
1173
1174 // A key with ties, against the order of insertion
1175 auto arc_key = [] (int i) { return (7 - i) % 3; };
1176 auto node_key = [] (int x) { return (5 - x) % 2; };
1177
1178 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
1179 {
1180 Built<GT> b(s);
1181 if constexpr (requires { b.g.sort_arcs([] (auto *, auto *) { return false; }); })
1182 {
1183 arcs.run();
1184 b.g.sort_arcs([&] (typename GT::Arc * x, typename GT::Arc * y)
1185 { return arc_key(num(x)) < arc_key(num(y)); });
1186 std::vector<int> order;
1187 for (auto it = b.g.get_arc_it(); it.has_curr(); it.next_ne())
1188 order.push_back(arc_key(num(it.get_curr())));
1189 if (not std::is_sorted(order.begin(), order.end()))
1190 arcs.fail(s, "the arcs are not sorted");
1191 else if (auto e = incidence_error(b.g, s, Alive(s)); not e.empty())
1192 arcs.fail(s, e);
1193
1194 nodes.run();
1195 b.g.sort_nodes([&] (typename GT::Node * x, typename GT::Node * y)
1196 { return node_key(num(x)) < node_key(num(y)); });
1197 std::vector<int> node_order;
1198 for (auto it = b.g.get_node_it(); it.has_curr(); it.next_ne())
1199 node_order.push_back(node_key(num(it.get_curr())));
1200 if (not std::is_sorted(node_order.begin(), node_order.end()))
1201 nodes.fail(s, "the nodes are not sorted");
1202 else if (auto e = incidence_error(b.g, s, Alive(s)); not e.empty())
1203 nodes.fail(s, e);
1204 }
1205 });
1206
1207 arcs.expect();
1208 nodes.expect();
1209}
1210
1211TYPED_TEST(ExhaustiveStructure, SingleGraphsHaveNoLoopsNorParallelArcs)
1212{
1213 using GT = TypeParam;
1214 const bool digraph = GT().is_digraph();
1215 Check single("Test_Single_Graph");
1216
1217 for_each_spec(domain(4, 3), digraph, [&] (const Spec & s)
1218 {
1219 single.run();
1220 Built<GT> b(s);
1221 // Two arcs between the same nodes are parallel, in a graph that is
1222 // not directed whatever their directions
1223 std::set<std::pair<int, int>> seen;
1224 bool simple = true;
1225 for (const auto & a : s.arcs)
1226 {
1227 const auto key = digraph ? std::pair(a[0], a[1])
1228 : std::pair(std::min(a[0], a[1]), std::max(a[0], a[1]));
1229 simple = simple and a[0] != a[1] and seen.insert(key).second;
1230 }
1231 if (Test_Single_Graph<GT>()(b.g) != simple)
1232 single.fail(s, simple ? "says that a simple graph is not" : "says that it is simple");
1233 });
1234
1235 single.expect();
1236}
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
long double w
Definition btreepic.C:153
size_t size_t int32_t * out
Definition ca-c-api.h:120
T & get_curr() const
Return the current item.
Definition htlist.H:1679
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1669
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
void reset_last()
Resets the iterator to the last filtered element.
void next()
Advances the iterator to the next filtered element.
void prev()
Moves the iterator backward to the previous filtered element.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
void reset_first()
Resets the iterator to the first filtered element.
bool has_curr() const noexcept
Definition htlist.H:969
Index of the arcs that go into each node of a graph.
Definition tpl_graph.H:2224
size_t in_degree(const Node *p) const
Return the number of arcs into p that the filter showed.
Definition tpl_graph.H:2495
Iterator get_it(const Node *p) const
Return an iterator on the arcs into p that the filter showed.
Definition tpl_graph.H:2506
Filtered iterator for incoming arcs of a node.
Definition tpl_graph.H:2045
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:1007
virtual Arc * connect_arc(Arc *arc) noexcept
Connect a previously disconnected arc to the graph.
Definition tpl_graph.H:757
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:673
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Definition tpl_graph.H:557
virtual void disconnect_arc(Arc *arc) noexcept
Disconnect an arc from graph.
Definition tpl_graph.H:723
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
Path on a graph.
Definition tpl_graph.H:3832
bool is_cycle() const
Return true if this is a cycle; throws if path is empty.
Definition tpl_graph.H:4351
void insert(Arc *arc)
Insert an arc as the first of a path.
Definition tpl_graph.H:4195
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Definition tpl_graph.H:4576
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
bool check_directed() const
Return true if the directed path is consistent: each arc goes from its node to the next one,...
Definition tpl_graph.H:3904
void insert_directed(Node *p)
Insert a node at the beginning of a directed path.
Definition tpl_graph.H:4266
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
bool check() const
Return true if the path is consistent: each arc joins its node with the next one, either way,...
Definition tpl_graph.H:3880
Arc * get_last_arc() const
Return the last arc of a path; throws overflow_error if the path is empty.
Definition tpl_graph.H:4339
void swap(Path &path) noexcept
Fast swap between two paths (constant time)
Definition tpl_graph.H:4383
Node * remove_first_node()
Remove the first node of a path.
Definition tpl_graph.H:4376
Arc * get_first_arc() const
Return the first arc of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4332
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
void append_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:4124
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition tpl_graph.H:4565
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:4363
const GT & get_graph() const noexcept
Get a constant reference to the graph.
Definition tpl_graph.H:3921
Determines whether a graph or digraph is simple.
void sort_arcs(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Arc >)
Sort all the arcs of the graph according to a specific criteria.
Definition graph-dry.H:4261
void sort_nodes(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Node >)
Sort all the nodes of the graph according to a specific criteria.
Definition graph-dry.H:4225
void for_each_out_arc(Node *p, Op &op) const
Perform op on each outcoming arc of node p
Definition graph-dry.H:3995
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
void for_each_in_arc(Node *p, Op &op) const
Perform op on each incoming arc of node p; on a digraph, among the arcs of the graph,...
Definition graph-dry.H:3832
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
DynList< Node * > in_nodes(Node *p) const
Return a list with the incoming nodes to p
Definition graph-dry.H:3587
DynList< Arc * > out_arcs(Node *p) const
Return a list with the outcoming arcs to p`.
Definition graph-dry.H:3631
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
Definition graph-dry.H:972
bool all_in_arcs(Node *p, Op &op) const
Return true if op is true for all the incoming arcs to node p
Definition graph-dry.H:3855
auto in_pairs(Node *p) const
Return a list of pair incoming arcs and nodes.
Definition graph-dry.H:3681
DynList< Arc * > in_arcs(Node *p) const
Return a list with the incoming arcs to p
Definition graph-dry.H:3648
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
bool all_out_arcs(Node *p, Op &op) const
Return true if op is true for all the outcoming arcs to node p
Definition graph-dry.H:4009
size_t out_degree(Node *p) const noexcept
Compute the output degree of a node.
Definition graph-dry.H:3765
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Arc * search_arc(Op &op) const
Linear search of an arc.
Definition graph-dry.H:2945
Arc * search_directed_arc(Node *src, Node *tgt) const noexcept
Search a directed arc linking two nodes.
Definition graph-dry.H:3539
size_t in_degree(Node *p) const noexcept
Compute the input degree of a node.
Definition graph-dry.H:3740
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
DynList< Node * > out_nodes(Node *p) const
Return a list with the outcoming nodes to p
Definition graph-dry.H:3614
auto out_pairs(Node *p) const
Return a list of pair outcoming arcs and nodes.
Definition graph-dry.H:3714
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
TYPED_TEST_SUITE(ExhaustiveStructure, All_Kinds)
TYPED_TEST(ExhaustiveStructure, IncidenceMatchesTheModel)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered incoming arcs to p.
Definition tpl_graph.H:2606
bool traverse_in_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of incoming arcs of a node.
Definition tpl_graph.H:2947
size_t in_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered in degree of node p.
Definition tpl_graph.H:2806
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
DynList< typename GT::Arc * > in_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
Definition tpl_graph.H:2671
void for_each_in_arc(typename GT::Node *p, Op op=Op())
Traverse the incoming arcs of a node and executes an operation.
Definition tpl_graph.H:2989
void copy_graph(GT &gtgt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Definition tpl_graph.H:4794
DynList< ArcPair< GT > > in_pairs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming pairs of (arc,node) related to node p
Definition tpl_graph.H:2731
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
bool all_in_arc(typename GT::Node *p, Op op=Op())
Test if the incoming arcs meet a condition.
Definition tpl_graph.H:3032
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
bool exists_in_arc(typename GT::Node *p, Op op=Op())
Test if it exists an incoming arc satisfying an operation.
Definition tpl_graph.H:3075
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
constexpr bool shown(unsigned long hidden, int i) noexcept
Whether the arc number i is shown by the mask hidden.
GT::Node * node_of(const GT &g, int i)
The node of a graph whose number is i.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > >, Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > All_Kinds
The six kinds, with int infos.
STL namespace.
Single graph utilities.
Alias for Digraph_Iterator
Definition graph-dry.H:3430
Hides the arcs of a mask; copyable, assignable, noexcept.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
std::vector< typename GT::Arc * > arcs
A small multigraph: its nodes are 0 .
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
bool check(GT &g)
void assign(BinTree< int >::Node *node, int, int)
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
DynList< int > l