Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_contract_matrix_core_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
57#include <cstdio>
58#include <fstream>
59#include <functional>
60#include <iostream>
61#include <set>
62#include <sstream>
63#include <string>
64#include <type_traits>
65#include <utility>
66#include <vector>
67
68#if defined(_WIN32)
69# include <process.h>
70#else
71# include <unistd.h>
72#endif
73
74#include <gtest/gtest.h>
75
76#include <tpl_graph.H>
77#include <tpl_graph_utils.H>
78#include <graph-traverse.H>
80#include <io_graph.H>
81#include <generate_graph.H>
83#include <generate_df_tree.H>
84#include <graph_to_tree.H>
85#include <single_graph.H>
86#include <warshall.H>
87#include <eulerian.H>
88#include <hamiltonian.H>
89
90#include "graph_harness.H"
93
94using namespace Aleph;
95using namespace graph_harness;
96using namespace graph_contract_matrix;
97
98namespace
99{
102 long long process_id() noexcept
103 {
104#if defined(_WIN32)
105 return static_cast<long long>(_getpid());
106#else
107 return static_cast<long long>(getpid());
108#endif
109 }
110
111
113 template <class List>
114 std::set<int> arc_numbers(const List & l)
115 {
116 std::set<int> r;
117 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
118 r.insert(it.get_curr()->get_info());
119 return r;
120 }
121} // namespace
122
123namespace
124{
127 Spec star_path()
128 {
129 Spec s;
130 s.n = 4;
131 s.arcs = {{0, 1}, {1, 2}, {0, 3}};
132 return s;
133 }
134
136 Spec triangle()
137 {
138 Spec s;
139 s.n = 3;
140 s.arcs = {{0, 1}, {1, 2}, {0, 2}};
141 return s;
142 }
143
145 Spec in_and_out()
146 {
147 Spec s;
148 s.n = 4;
149 s.arcs = {{0, 1}, {1, 2}, {3, 0}};
150 return s;
151 }
152
154 Spec parallel_pair()
155 {
156 Spec s;
157 s.n = 3;
158 s.arcs = {{0, 1}, {1, 2}, {0, 1}};
159 return s;
160 }
161
163 std::set<int> visible(std::set<int> all, bool hide)
164 {
165 if (hide)
166 all.erase(Hidden);
167 return all;
168 }
169
171 template <class It>
172 std::set<int> iterated(It & it)
173 {
174 std::set<int> r;
175 for (; it.has_curr(); it.next_ne())
176 r.insert(it.get_curr_ne()->get_info());
177 return r;
178 }
179
181 template <class List>
182 std::set<int> node_numbers(const List & l)
183 {
184 std::set<int> r;
185 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
186 r.insert(it.get_curr()->get_info());
187 return r;
188 }
189
191 template <class List>
192 std::set<int> pair_numbers(const List & l)
193 {
194 std::set<int> r;
195 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
196 r.insert(std::get<0>(it.get_curr())->get_info());
197 return r;
198 }
199
201 template <class GT>
202 std::set<int> graph_arcs(const GT & g)
203 {
204 std::set<int> r;
205 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
206 r.insert(it.get_curr_ne()->get_info());
207 return r;
208 }
209
211 template <class Key>
212 std::set<Key> tree_keys(Tree_Node<Key> * root)
213 {
214 std::set<Key> r;
215 root->traverse([&r] (Tree_Node<Key> * p) { r.insert(p->get_key()); return true; });
216 return r;
217 }
218
221 template <class GT>
222 constexpr bool singly_linked = std::is_same_v<typename GT::Node, Graph_Snode<int>>;
223
226 template <class SA>
227 constexpr bool default_built = std::is_default_constructible_v<SA>;
228
230 struct Text
231 {
232 template <class T>
233 std::string operator () (T *) const { return ""; }
234 };
235
238 size_t drawn_arcs(const std::string & text)
239 {
240 size_t n = 0;
241 std::istringstream in(text);
242 for (std::string line; std::getline(in, line); )
243 if (line.rfind("ARC ", 0) == 0 or line.rfind("CURVE-ARC ", 0) == 0
244 or (line.rfind("//", 0) != 0
245 and (line.find("->") != std::string::npos or line.find("--") != std::string::npos)))
246 ++n;
247 return n;
248 }
249
252 struct Collect_Arcs
253 {
254 std::set<int> * seen = nullptr;
255
256 template <class G, class A>
257 void operator () (G &, A * a) const { seen->insert(a->get_info()); }
258 };
259
261 struct To_Key
262 {
263 template <class N>
264 void operator () (N * gnode, Tree_Node<int> * tnode) const
265 {
266 tnode->get_key() = gnode->get_info();
267 }
268 };
269} // namespace
270
271
272template <class GT>
273class ContractCoreIterators : public ::testing::Test {};
274
276
278{
279 using GT = TypeParam;
280 Built<GT> b(star_path());
281 for_each_filter<GT>([&] (auto sa, const char * name)
282 {
283 using SA = decltype(sa);
284 constexpr bool hide = hides<GT, SA>;
285 SCOPED_TRACE(name);
286 const auto want = visible({0, 2}, hide);
287
288 Node_Arc_Iterator<GT, SA> it(b.nodes[0], sa);
290 EXPECT_EQ(iterated(copy), want);
291 ASSERT_TRUE(it.has_curr());
292 EXPECT_EQ(it.get_curr(), it.get_curr_ne());
293 EXPECT_EQ(it.count(), want.size());
294 EXPECT_FALSE(it.empty());
295 it.reset_first();
296 it.next();
297 std::set<int> each;
298 it.for_each([&each] (typename GT::Arc * a) { each.insert(a->get_info()); });
299 EXPECT_EQ(each, want);
300 EXPECT_EQ(it.find_if([] (typename GT::Arc * a) { return a->get_info() == Hidden; }),
301 not hide);
302 EXPECT_TRUE(it.has_container());
303 it.set_cookie(&each);
304 EXPECT_EQ(it.get_cookie(), &each);
305 (void) it.get_filter();
306 (void) std::as_const(it).get_filter();
307 (void) it.get_iterator();
308 (void) std::as_const(it).get_iterator();
309 // get_container() is not called: it gives the address of the
310 // parameter of the constructor, which is gone
311
312 // A default iterator builds its filter with SA()
313 if constexpr (default_built<SA>)
314 {
316 EXPECT_FALSE(none.has_container());
317 }
318
319 // set_filter() is only for a filter that can be assigned
320 if constexpr (std::is_move_assignable_v<SA>)
321 {
322 it.set_filter(sa);
323 it.reset_first();
324 EXPECT_EQ(iterated(it), want);
325 }
326
327 // The iterators of List_SGraph and List_SDigraph go only forward:
328 // see sgraph_iterator_prev and sgraph_iterator_reset_last
329 if constexpr (not singly_linked<GT>)
330 {
331 std::set<int> back;
332 it.reset_last();
333 for (int i = 0; i < 8 and it.has_curr(); ++i, it.prev_ne())
334 back.insert(it.get_curr_ne()->get_info());
335 EXPECT_EQ(back, want);
336 it.reset_last();
337 if (it.has_curr())
338 it.prev();
339 }
340 });
341}
342
344{
345 using GT = TypeParam;
346 Built<GT> b(star_path());
347 for_each_filter<GT>([&] (auto sa, const char * name)
348 {
349 using SA = decltype(sa);
350 constexpr bool hide = hides<GT, SA>;
351 SCOPED_TRACE(name);
352 const auto want = visible({0, 1, 2}, hide);
353 for_each_constness(b.g, [&] (auto & g)
354 {
355 Arc_Iterator<GT, SA> it(g, sa);
356 Arc_Iterator<GT, SA> copy(it);
357 EXPECT_EQ(iterated(copy), want);
358 ASSERT_TRUE(it.has_curr());
359 EXPECT_EQ(it.get_curr(), it.get_curr_ne());
360 EXPECT_EQ(it.count(), want.size());
361 EXPECT_FALSE(it.empty());
362 it.reset_first();
363 it.next();
364 std::set<int> each;
365 it.for_each([&each] (typename GT::Arc * a) { each.insert(a->get_info()); });
366 EXPECT_EQ(each, want);
367 EXPECT_EQ(it.find_if([] (typename GT::Arc * a) { return a->get_info() == Hidden; }),
368 not hide);
369 EXPECT_EQ(&it.get_container(), &b.g);
370 it.set_cookie(&each);
371 EXPECT_EQ(it.get_cookie(), &each);
372 (void) it.get_filter();
373 (void) std::as_const(it).get_filter();
374
375 // known failure sgraph_arc_iterator_default_constructor
376 if constexpr (default_built<SA> and not singly_linked<GT>)
377 {
379 EXPECT_FALSE(none.has_container());
380 }
381
382 // set_filter() is only for a filter that can be assigned
383 if constexpr (std::is_move_assignable_v<SA>)
384 {
385 it.set_filter(sa);
386 it.reset_first();
387 EXPECT_EQ(iterated(it), want);
388 }
389
390 // The iterators of List_SGraph and List_SDigraph go only
391 // forward: see sgraph_iterator_prev and sgraph_iterator_reset_last
392 if constexpr (not singly_linked<GT>)
393 {
394 std::set<int> back;
395 it.reset_last();
396 for (int i = 0; i < 8 and it.has_curr(); ++i, it.prev_ne())
397 back.insert(it.get_curr_ne()->get_info());
398 EXPECT_EQ(back, want);
399 it.reset_last();
400 if (it.has_curr())
401 it.prev();
402 }
403 });
404 });
405}
406
408{
409 using GT = TypeParam;
410 const Spec s = in_and_out();
411 Built<GT> b(s);
412 // In a digraph the list of a node holds its arcs out: In_Iterator(g, p)
413 // walks the arcs of the graph there, and In_Iterator(p), which walks
414 // the list of p, is only for the graphs that are not directed
415 Check in_arcs("In_Iterator(g, p)");
416 Check in_list("In_Iterator(p), if not directed");
417 for_each_filter<GT>([&] (auto sa, const char * name)
418 {
419 using SA = decltype(sa);
420 constexpr bool hide = hides<GT, SA>;
421 SCOPED_TRACE(name);
422 const auto want = visible({2}, hide);
423
424 // Their default constructors are not called: known failure
425 // digraph_iterator_default_constructors
428 EXPECT_EQ(iterated(copy), want);
429 out.reset_first();
430 if (out.has_curr())
431 {
432 EXPECT_EQ(out.get_curr(), out.get_curr_ne());
433 EXPECT_EQ(out.get_curr(), out.get_current_arc());
434 EXPECT_EQ(out.get_node(), b.nodes[0]);
435 EXPECT_EQ(out.get_node(out.get_curr()), b.nodes[0]);
436 EXPECT_EQ(out.get_tgt_node(), b.nodes[0]);
437 EXPECT_EQ(out.get_node_ne(), b.nodes[0]);
438 EXPECT_EQ(out.get_tgt_node_ne(), b.nodes[0]);
439 }
440 EXPECT_EQ(iterated(out), want);
441 Out_Iterator<GT, SA> first(b.nodes[0], sa);
442 EXPECT_EQ(iterated(first), std::set<int>({0}));
443 first.reset_first();
444 first.next();
445 EXPECT_FALSE(first.has_curr());
446
447 // The iterators of List_SGraph and List_SDigraph go only forward:
448 // see sgraph_iterator_prev and sgraph_iterator_reset_last
449 if constexpr (requires { out.reset_last(); out.prev(); out.end(); })
450 {
451 out.reset_last();
452 if (out.has_curr())
453 out.prev();
454 out.end();
455 EXPECT_FALSE(out.has_curr());
456 }
457
458 // The arcs into node 0 through an iterator in, built on it
459 auto check_in = [&] (Check & check, auto & in)
460 {
461 auto in_copy = in;
462 check.run();
463 if (iterated(in_copy) != want)
464 check.fail(s, std::string(name) + ": the arcs into node 0 are not seen");
465 in.reset_first();
466 if (in.has_curr())
467 {
468 EXPECT_EQ(in.get_curr(), in.get_curr_ne());
469 EXPECT_EQ(in.get_curr(), in.get_current_arc());
470 EXPECT_EQ(in.get_node(), b.nodes[3]);
471 EXPECT_EQ(in.get_node(in.get_curr()), b.nodes[3]);
472 EXPECT_EQ(in.get_node_ne(), b.nodes[3]);
473 EXPECT_EQ(in.get_tgt_node(), b.nodes[3]);
474 EXPECT_EQ(in.get_tgt_node_ne(), b.nodes[3]);
475 in.next();
476 }
477 EXPECT_FALSE(in.has_curr());
478 in.reset_first();
479 if (in.has_curr())
480 {
481 in.next_ne();
482 EXPECT_FALSE(in.has_curr());
483 }
484 if constexpr (requires { in.reset_last(); in.prev(); })
485 {
486 in.reset_last();
487 EXPECT_EQ(iterated(in), want);
488 in.reset_last();
489 if (in.has_curr())
490 in.prev();
491 EXPECT_FALSE(in.has_curr());
492 }
493 if constexpr (requires { in.end(); })
494 {
495 in.reset_first();
496 in.end();
497 EXPECT_FALSE(in.has_curr());
498 }
499 };
500
501 for_each_constness(b.g, [&] (auto & g)
502 {
503 In_Iterator<GT, SA> in(g, b.nodes[0], sa);
504 check_in(in_arcs, in);
505 In_Iterator<GT, SA> rvalue(g, b.nodes[0], SA(sa));
506 check_in(in_arcs, rvalue);
507 if constexpr (default_built<SA>)
508 {
509 In_Iterator<GT, SA> by_default(g, b.nodes[0]);
510 EXPECT_EQ(iterated(by_default), std::set<int>({2}));
511 }
512 });
513
514 if constexpr (not DigraphType<GT>)
515 {
516 In_Iterator<GT, SA> in(b.nodes[0], sa);
517 check_in(in_list, in);
518 }
519 });
520
521 // The iterator of the graph type, which takes no filter
522 for_each_constness(b.g, [&] (auto & g)
523 {
524 typename GT::In_Iterator in(g, b.nodes[0]);
525 EXPECT_EQ(iterated(in), std::set<int>({2}));
526 auto it = g.get_in_it(b.nodes[0]);
527 ASSERT_TRUE(it.has_curr());
528 EXPECT_EQ(it.get_node(), b.nodes[3]);
529 it.next();
530 EXPECT_FALSE(it.has_curr());
531 });
532 if constexpr (not DigraphType<GT>)
533 {
534 typename GT::In_Iterator in(b.nodes[0]);
535 EXPECT_EQ(iterated(in), std::set<int>({2}));
536 }
537
538 in_arcs.expect();
539 in_list.expect();
540}
541
547TYPED_TEST(ContractCoreIterators, BackwardMovesWhereTheListsGoBack)
548{
549 using GT = TypeParam;
551 constexpr bool back = not singly_linked<GT>;
552 auto goes_back = [] (auto & it) { return requires { it.prev(); it.reset_last(); }; };
553 Built<GT> b(star_path());
554 Node_Arc_Iterator<GT, SA> node_arc(b.nodes[0], SA{Hidden_Mask});
555 Arc_Iterator<GT, SA> arc(b.g, SA{Hidden_Mask});
556 Out_Iterator<GT, SA> out(b.nodes[0], SA{Hidden_Mask});
557 In_Iterator<GT, SA> in(b.g, b.nodes[0], SA{Hidden_Mask});
558 typename GT::Out_Iterator member_out(b.nodes[0]);
559 typename GT::In_Iterator member_in(b.g, b.nodes[0]);
560 const In_Arcs_Index<GT, SA> index(b.g, SA{Hidden_Mask});
561 auto index_it = index.get_it(b.nodes[1]);
562 EXPECT_EQ(goes_back(node_arc), back);
563 EXPECT_EQ(goes_back(arc), back);
564 EXPECT_EQ(goes_back(out), back);
565 EXPECT_EQ(goes_back(in), back);
566 EXPECT_EQ(goes_back(member_out), back);
567 EXPECT_EQ(goes_back(member_in), back);
568 EXPECT_TRUE(goes_back(index_it));
569 EXPECT_EQ((requires { node_arc.prev_ne(); }), back);
570 EXPECT_EQ((requires { out.end(); }), back);
571}
572
573template <class GT>
574class ContractCoreFunctions : public ::testing::Test {};
575
577
578TYPED_TEST(ContractCoreFunctions, ForEachArcAndForallArc)
579{
580 using GT = TypeParam;
581 Built<GT> b(star_path());
582 for_each_filter<GT>([&] (auto sa, const char * name)
583 {
584 using SA = decltype(sa);
585 constexpr bool hide = hides<GT, SA>;
586 SCOPED_TRACE(name);
587 for_each_constness(b.g, [&] (auto & g)
588 {
589 std::set<int> all, at_0;
590 for_each_arc(g, [&all] (typename GT::Arc * a) { all.insert(a->get_info()); }, sa);
591 for_each_arc(g, b.nodes[0],
592 [&at_0] (typename GT::Arc * a) { at_0.insert(a->get_info()); }, sa);
593 EXPECT_EQ(all, visible({0, 1, 2}, hide));
594 EXPECT_EQ(at_0, visible({0, 2}, hide));
595
596 auto not_hidden = [] (typename GT::Arc * a) { return a->get_info() != Hidden; };
597 EXPECT_EQ(forall_arc(g, not_hidden, sa), hide);
598 EXPECT_EQ(forall_arc<GT>(b.nodes[0], not_hidden, sa), hide);
599 });
600 });
601}
602
604{
605 using GT = TypeParam;
606 Built<GT> b(star_path());
607 for_each_filter<GT>([&] (auto sa, const char * name)
608 {
609 using SA = decltype(sa);
610 constexpr bool hide = hides<GT, SA>;
611 SCOPED_TRACE(name);
612 auto info = [] (typename GT::Arc * a) { return a->get_info(); };
613 auto as_set = [] (const DynList<int> & l)
614 {
615 std::set<int> r;
616 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
617 r.insert(it.get_curr());
618 return r;
619 };
620 const auto all = visible({0, 1, 2}, hide);
621 const auto at_0 = visible({0, 2}, hide);
622
623 // They take GT &: a const graph is outside their contract
624 EXPECT_EQ(as_set(arcs_map<GT, int>(b.g, info, sa)), all);
625 EXPECT_EQ(as_set(arcs_map<GT, int>(b.g, b.nodes[0], info, sa)), at_0);
626 std::function<int (typename GT::Arc *)> f = info;
627 EXPECT_EQ(as_set(arcs_map<GT>(b.g, f, sa)), all);
628 EXPECT_EQ(as_set(arcs_map<GT>(b.g, b.nodes[0], f, sa)), at_0);
629
630 auto sum = [] (const int & n, typename GT::Arc * a) { return n + a->get_info(); };
631 EXPECT_EQ((foldl_arcs<GT, int>(b.g, 0, sum, sa)), hide ? 1 : 3);
632 EXPECT_EQ((foldl_arcs<GT, int>(b.g, b.nodes[0], 0, sum, sa)), hide ? 0 : 2);
633 });
634}
635
637{
638 using GT = TypeParam;
639 const Spec s = in_and_out();
640 Built<GT> b(s);
641 // The forms that take the graph, on every kind; those of the node, only
642 // where the list of a node holds the arcs into it
643 Check ins("in_nodes(g, p), in_arcs(g, p), in_pairs(g, p) and in_degree(g, p)");
644 Check ins_list("in_nodes(p), in_arcs(p), in_pairs(p) and in_degree(p), if not directed");
645 for_each_filter<GT>([&] (auto sa, const char * name)
646 {
647 using SA = decltype(sa);
648 constexpr bool hide = hides<GT, SA>;
649 SCOPED_TRACE(name);
650 const auto want = visible({2}, hide);
651 const auto want_nodes = hide ? std::set<int>() : std::set<int>({3});
652 auto * out_node = b.nodes[3];
653 auto * in_node = b.nodes[0];
654
655 EXPECT_EQ(node_numbers(out_nodes<GT>(out_node, sa)),
656 hide ? std::set<int>() : std::set<int>({0}));
657 EXPECT_EQ(arc_numbers(out_arcs<GT>(out_node, sa)), want);
658 EXPECT_EQ(pair_numbers(out_pairs<GT>(out_node, sa)), want);
659 EXPECT_EQ(out_degree<GT>(out_node, sa), want.size());
660 EXPECT_EQ(arc_numbers(arcs<GT>(in_node, sa)),
661 b.g.is_digraph() ? std::set<int>({0}) : visible({0, 2}, hide));
662
663 for_each_constness(b.g, [&] (auto & g)
664 {
665 ins.run();
666 if (node_numbers(in_nodes(g, in_node, sa)) != want_nodes
667 or arc_numbers(in_arcs(g, in_node, sa)) != want
668 or pair_numbers(in_pairs(g, in_node, sa)) != want
669 or in_degree(g, in_node, sa) != want.size()
670 or in_degree(g, out_node, sa) != 0)
671 ins.fail(s, std::string(name) + ": the arcs into node 0 are not seen");
672 if constexpr (default_built<SA>)
673 {
674 EXPECT_EQ(in_degree(g, in_node), 1U);
675 }
676 });
677
678 if constexpr (not DigraphType<GT>)
679 {
680 ins_list.run();
681 if (node_numbers(in_nodes<GT>(in_node, sa)) != want_nodes
682 or arc_numbers(in_arcs<GT>(in_node, sa)) != want
683 or pair_numbers(in_pairs<GT>(in_node, sa)) != want
684 or in_degree<GT>(in_node, sa) != want.size())
685 ins_list.fail(s, std::string(name) + ": the arcs into node 0 are not seen");
686 }
687 });
688
689 ins.expect();
690 ins_list.expect();
691}
692
694{
695 using GT = TypeParam;
696 const Spec s = in_and_out();
697 Built<GT> b(s);
698 Check index("In_Arcs_Index");
699 for_each_filter<GT>([&] (auto sa, const char * name)
700 {
701 using SA = decltype(sa);
702 constexpr bool hide = hides<GT, SA>;
703 SCOPED_TRACE(name);
704 const auto want = visible({2}, hide);
705 for_each_constness(b.g, [&] (auto & g)
706 {
707 // The filter as an lvalue and as an rvalue
708 const In_Arcs_Index<GT, SA> in(g, sa);
709 const In_Arcs_Index<GT, SA> rvalue(g, SA(sa));
710 index.run();
711 auto it = in.get_it(b.nodes[0]);
712 auto rt = rvalue.get_it(b.nodes[0]);
713 if (iterated(it) != want or in.in_degree(b.nodes[0]) != want.size()
714 or in.in_degree(b.nodes[3]) != 0 or in.size() != visible({0, 1, 2}, hide).size()
715 or iterated(rt) != want)
716 index.fail(s, std::string(name) + ": the arcs into node 0 are not seen");
717
718 typename In_Arcs_Index<GT, SA>::Iterator kt(in, b.nodes[0]);
719 EXPECT_EQ(iterated(kt), want);
720
721 auto jt = in.get_it(b.nodes[0]);
722 EXPECT_EQ(jt.size(), want.size());
723 if (jt.has_curr())
724 {
725 EXPECT_EQ(jt.get_curr(), jt.get_curr_ne());
726 EXPECT_EQ(jt.get_curr(), jt.get_current_arc());
727 EXPECT_EQ(jt.get_current_arc_ne(), b.arcs[2]);
728 EXPECT_EQ(jt.get_node(), b.nodes[3]);
729 EXPECT_EQ(jt.get_node_ne(), b.nodes[3]);
730 jt.next();
731 EXPECT_FALSE(jt.has_curr());
732 EXPECT_THROW(jt.next(), std::overflow_error);
733 jt.reset_last();
734 EXPECT_TRUE(jt.has_curr());
735 jt.prev();
736 EXPECT_FALSE(jt.has_curr());
737 EXPECT_THROW((void) jt.get_curr(), std::underflow_error);
738 jt.reset_first();
739 jt.end();
740 EXPECT_FALSE(jt.has_curr());
741 }
742 else
743 EXPECT_THROW((void) jt.get_curr(), std::overflow_error);
744
745 if constexpr (default_built<SA>)
746 {
747 const In_Arcs_Index<GT, SA> all(g);
748 EXPECT_EQ(all.in_degree(b.nodes[0]), 1U);
749 EXPECT_EQ(all.size(), 3U);
750 }
751
752 // A node of another graph
753 GT other;
754 auto * q = other.insert_node(0);
755 EXPECT_THROW((void) in.in_degree(q), std::domain_error);
756 EXPECT_THROW((void) in.get_it(q), std::domain_error);
757 });
758 });
759
760 // On an empty graph
761 GT empty;
762 const In_Arcs_Index<GT> nothing(empty);
763 EXPECT_EQ(nothing.size(), 0U);
764
765 index.expect();
766}
767
768TYPED_TEST(ContractCoreFunctions, FunctionalOperationsOnTheArcsIntoANode)
769{
770 using GT = TypeParam;
771 using Arc = typename GT::Arc;
772 Built<GT> b(in_and_out());
773 auto * p = b.nodes[0];
774 auto info = [] (Arc * a) { return a->get_info(); };
775 auto is_2 = [] (Arc * a) { return a->get_info() == Hidden; };
776 auto sum = [] (const int & n, Arc * a) { return n + a->get_info(); };
777 auto as_set = [] (const DynList<int> & l)
778 {
779 std::set<int> r;
780 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
781 r.insert(it.get_curr());
782 return r;
783 };
784 std::function<int (Arc *)> f = info;
785
786 for_each_constness(b.g, [&] (auto & g)
787 {
788 std::set<int> each;
789 for_each_in_arc(g, p, [&each] (Arc * a) { each.insert(a->get_info()); });
790 EXPECT_EQ(each, std::set<int>({2}));
791 EXPECT_TRUE(traverse_in_arcs(g, p, is_2));
792 EXPECT_TRUE(all_in_arc(g, p, is_2));
793 EXPECT_TRUE(exists_in_arc(g, p, is_2));
794 EXPECT_FALSE(exists_in_arc(g, b.nodes[3], is_2));
795 EXPECT_EQ(search_in_arc(g, p, is_2), b.arcs[2]);
796 EXPECT_EQ(as_set(map_in_arcs<GT, int>(g, p, info)), std::set<int>({2}));
797 EXPECT_EQ(as_set(map_in_arcs<GT>(g, p, f)), std::set<int>({2}));
798 EXPECT_EQ((foldl_in_arcs<GT, int>(g, p, 0, sum)), 2);
799 EXPECT_EQ(arc_numbers(filter_in_arcs(g, p, is_2)), std::set<int>({2}));
800 });
801
802 // The members, and the forms of the node where the list holds the arcs
803 // into it
804 EXPECT_EQ(arc_numbers(b.g.in_arcs(p)), std::set<int>({2}));
805 EXPECT_EQ(b.g.in_degree(p), 1U);
806 EXPECT_EQ(b.g.search_in_arc(p, is_2), b.arcs[2]);
807 EXPECT_EQ(arc_numbers(b.g.filter_in_arcs(p, is_2)), std::set<int>({2}));
808 EXPECT_EQ(b.g.foldl_in_arcs(p, 0, sum), 2);
809 if constexpr (not DigraphType<GT>)
810 {
811 EXPECT_TRUE(traverse_in_arcs<GT>(p, is_2));
812 EXPECT_TRUE(exists_in_arc<GT>(p, is_2));
813 EXPECT_EQ(search_in_arc<GT>(p, is_2), b.arcs[2]);
814 EXPECT_EQ(as_set(map_in_arcs<GT, int>(p, info)), std::set<int>({2}));
815 EXPECT_EQ((foldl_in_arcs<GT, int>(p, 0, sum)), 2);
816 EXPECT_EQ(arc_numbers(filter_in_arcs<GT>(p, is_2)), std::set<int>({2}));
817 }
818}
819
821{
822 using GT = TypeParam;
823 Built<GT> b(star_path());
824 for_each_filter<GT>([&] (auto sa, const char * name)
825 {
826 using SA = decltype(sa);
827 constexpr bool hide = hides<GT, SA>;
828 SCOPED_TRACE(name);
829 auto * want = hide ? nullptr : b.arcs[Hidden];
830 for_each_constness(b.g, [&] (auto & g)
831 {
832 EXPECT_EQ(search_arc(g, b.nodes[0], b.nodes[3], sa), want);
833 EXPECT_EQ(search_directed_arc(g, b.nodes[0], b.nodes[3], sa), want);
834 EXPECT_EQ(search_arc(g, b.nodes[0], b.nodes[1], sa), b.arcs[0]);
835 });
836 });
837}
838
840{
841 using GT = TypeParam;
842 Built<GT> b(star_path());
843 for_each_filter<GT>([&] (auto sa, const char * name)
844 {
845 using SA = decltype(sa);
846 constexpr bool hide = hides<GT, SA>;
847 SCOPED_TRACE(name);
848 // An lvalue is used where it is; an rvalue is moved in, and copied
849 // with the keeper
850 Kept_Filter<SA> by_lvalue(sa);
851 Kept_Filter<SA> by_rvalue{SA(sa)};
852 Kept_Filter<SA> copy(by_rvalue);
853 Kept_Filter<SA> moved(std::move(copy));
854 SA & f = by_lvalue;
855 EXPECT_EQ(&f, &sa);
856 EXPECT_NE(&by_rvalue.get(), &sa);
857 EXPECT_EQ(bool(f(b.arcs[Hidden])), not hide);
858 EXPECT_EQ(bool(moved.get()(b.arcs[Hidden])), not hide);
859 EXPECT_TRUE(bool(by_rvalue.get()(b.arcs[0])));
860 });
861}
862
864{
865 using GT = TypeParam;
866 const Spec s = star_path();
867 Built<GT> b(s);
868 for_each_filter<GT>([&] (auto sa, const char * name)
869 {
870 using SA = decltype(sa);
871 constexpr bool hide = hides<GT, SA>;
872 SCOPED_TRACE(name);
874 for_each_constness(b.g, [&] (auto & g)
875 {
876 std::set<int> all;
877 op(g, Collect_Arcs{&all});
878 EXPECT_EQ(all, visible({0, 1, 2}, hide));
879
880 std::set<int> at_0;
881 op(g, b.nodes[0], Collect_Arcs{&at_0});
882 EXPECT_EQ(at_0, visible({0, 2}, hide));
883 });
884 });
885}
886
888{
889 using GT = TypeParam;
890 Built<GT> b(star_path());
891 for_each_filter<GT>([&] (auto sa, const char * name)
892 {
893 using SA = decltype(sa);
894 constexpr bool hide = hides<GT, SA>;
895 SCOPED_TRACE(name);
896 // It takes GT & for the source: a const graph is outside its contract
898 GT tgt;
899 copy(tgt, b.g);
900 EXPECT_EQ(tgt.get_num_nodes(), 4U);
901 EXPECT_EQ(graph_arcs(tgt), visible({0, 1, 2}, hide));
902 copy(tgt, b.g, false);
903 EXPECT_EQ(graph_arcs(tgt), visible({0, 1, 2}, hide));
904 });
905}
906
907template <class GT>
908class ContractCoreTraversals : public ::testing::Test {};
909
911
913{
914 using GT = TypeParam;
915 Built<GT> b(star_path());
916 for_each_filter<GT>([&] (auto sa, const char * name)
917 {
918 using SA = decltype(sa);
919 using Itor = Node_Arc_Iterator<GT, SA>;
920 constexpr bool hide = hides<GT, SA>;
921 SCOPED_TRACE(name);
922 const size_t want = hide ? 3 : 4;
923 auto node_op = [] (typename GT::Node *) { return true; };
924 auto pair_op = [] (typename GT::Node *, typename GT::Arc *) { return true; };
925 auto arc_op = [] (typename GT::Arc *) { return true; };
926
927 // They take GT &: a const graph is outside their contract
929 EXPECT_EQ(dfs(b.nodes[0], node_op), want);
930 EXPECT_EQ(dfs(b.nodes[0], [] (typename GT::Node *) { return true; }), want);
931 EXPECT_EQ(dfs.exec(b.nodes[0], pair_op), want);
932 EXPECT_EQ(dfs.exec(b.nodes[0],
933 [] (typename GT::Node *, typename GT::Arc *) { return true; }),
934 want);
935 EXPECT_EQ(std::get<0>(dfs(b.nodes[0], node_op, arc_op)), want);
936 EXPECT_EQ(std::get<0>(dfs(b.nodes[0], [] (typename GT::Node *) { return true; },
937 [] (typename GT::Arc *) { return true; })),
938 want);
939
941 EXPECT_EQ(bfs(b.nodes[0], node_op), want);
942 EXPECT_EQ(bfs.exec(b.nodes[0], pair_op), want);
943 EXPECT_EQ(std::get<0>(bfs(b.nodes[0], node_op, arc_op)), want);
944 });
945}
946
947TYPED_TEST(ContractCoreTraversals, GraphTraverseGenerator)
948{
949 using GT = TypeParam;
950 Built<GT> b(star_path());
951 for_each_filter<GT>([&] (auto sa, const char * name)
952 {
953 using SA = decltype(sa);
954 using Itor = Node_Arc_Iterator<GT, SA>;
955 constexpr bool hide = hides<GT, SA>;
956 SCOPED_TRACE(name);
957 const std::set<int> want =
958 hide ? std::set<int>({0, 1, 2}) : std::set<int>({0, 1, 2, 3});
959
960 // They take GT &: a const graph is outside their contract
963 std::set<int> by_dfs, by_bfs;
964 for (typename GT::Node * p : dfs.traverse(b.nodes[0]))
965 by_dfs.insert(p->get_info());
966 for (typename GT::Node * p : bfs.traverse(b.nodes[0]))
967 by_bfs.insert(p->get_info());
968 EXPECT_EQ(by_dfs, want);
969 EXPECT_EQ(by_bfs, want);
970 });
971}
972
973TYPED_TEST(ContractCoreTraversals, DepthAndBreadthFirstTraversal)
974{
975 using GT = TypeParam;
976 Built<GT> b(star_path());
977 for_each_filter<GT>([&] (auto sa, const char * name)
978 {
979 using SA = decltype(sa);
980 constexpr bool hide = hides<GT, SA>;
981 SCOPED_TRACE(name);
982 const size_t want = hide ? 3 : 4;
983 for_each_constness(b.g, [&] (auto & g)
984 {
985 // The first node of a List_SGraph follows the addresses, so that
986 // the traversals from it are not checked
987
988 Depth_First_Traversal<GT, Default_Visit_Op<GT>, SA> dft(sa);
989 (void) dft(g);
990 (void) dft(g, Default_Visit_Op<GT>());
991 EXPECT_EQ(dft(g, b.nodes[0]), want);
992 EXPECT_EQ(dft(g, b.nodes[0], Default_Visit_Op<GT>()), want);
993
994 Breadth_First_Traversal<GT, Default_Visit_Op<GT>, SA> bft(sa);
995 Default_Visit_Op<GT> op;
996 (void) bft(g, Default_Visit_Op<GT>());
997 EXPECT_EQ(bft(g, b.nodes[0]), want);
998 EXPECT_EQ(bft(g, b.nodes[0], Default_Visit_Op<GT>()), want);
999 EXPECT_EQ(bft(g, b.nodes[0], op), want);
1000 });
1001 });
1002}
1003
1004TYPED_TEST(ContractCoreTraversals, SearchSpanningTreeArcAndTotalCost)
1005{
1006 using GT = TypeParam;
1007 Built<GT> b(star_path());
1008 for_each_filter<GT>([&] (auto sa, const char * name)
1009 {
1010 using SA = decltype(sa);
1011 constexpr bool hide = hides<GT, SA>;
1012 SCOPED_TRACE(name);
1013 for_each_constness(b.g, [&] (auto & g)
1014 {
1015 EXPECT_EQ(search_spanning_tree_arc(g, b.nodes[0], b.nodes[3], sa),
1016 hide ? nullptr : b.arcs[Hidden]);
1017 });
1018
1019 // It takes GT &: a const graph is outside its contract
1021 EXPECT_EQ(cost.total_cost(b.g), hide ? 1 : 3);
1022 EXPECT_EQ(cost(b.g), hide ? 1 : 3);
1023 cost.reset();
1024 EXPECT_TRUE(cost(b.arcs[Hidden]));
1025 EXPECT_EQ(cost.value(), hide ? 0 : Hidden);
1026 });
1027}
1028
1030{
1031 using GT = TypeParam;
1032 const Spec s = star_path();
1033 Built<GT> b(s);
1034 const std::string file = ::testing::TempDir() + "graph_contract_matrix_core_"
1035 + std::to_string(process_id()) + ".bin";
1036 for_each_filter<GT>([&] (auto sa, const char * name)
1037 {
1038 using SA = decltype(sa);
1039 constexpr bool hide = hides<GT, SA>;
1040 SCOPED_TRACE(name);
1043 // It takes GT &: a const graph is outside its contract
1044 IO io(b.g, sa);
1045 IO by_pointer(&b.g, sa);
1046 by_pointer.set_verbose(false);
1047 by_pointer.set_load_node(Dft_Load_Node<GT>());
1048 by_pointer.set_store_node(Dft_Store_Node<GT>());
1049 by_pointer.set_load_arc(Dft_Load_Arc<GT>());
1050 by_pointer.set_store_arc(Dft_Store_Arc<GT>());
1051 by_pointer.set_node_filter(Dft_Show_Node<GT>());
1052 EXPECT_FALSE(by_pointer.is_verbose());
1053
1054 // Whether what save_in_text_mode() and save() of saver write loads
1055 // back as the graph without the arcs that hidden says
1056 auto text_round = [&] (IO & saver, bool hidden)
1057 {
1058 std::ostringstream text;
1059 saver.save_in_text_mode(text);
1060 GT h;
1061 IO loader(h, sa);
1062 std::istringstream in(text.str());
1063 loader.load_in_text_mode(in);
1064 return h.get_num_nodes() == 4 and graph_arcs(h) == visible({0, 1, 2}, hidden);
1065 };
1066 auto binary_round = [&] (IO & saver, bool hidden)
1067 {
1068 {
1069 std::ofstream out(file, std::ios::binary);
1070 saver.save(out);
1071 }
1072 GT h;
1073 IO loader(h, sa);
1074 std::ifstream in(file, std::ios::binary);
1075 loader.load(in);
1076 return h.get_num_nodes() == 4 and graph_arcs(h) == visible({0, 1, 2}, hidden);
1077 };
1078
1079 EXPECT_TRUE(text_round(io, hide));
1080 EXPECT_TRUE(binary_round(io, hide));
1081 EXPECT_TRUE(text_round(by_pointer, hide));
1082
1083 // IO_Graph(g) builds AF(), which hides nothing, and set_arc_filter()
1084 // assigns the filter: for those that have both
1085 if constexpr (default_built<SA>)
1086 {
1087 IO later(b.g);
1088 EXPECT_TRUE(text_round(later, false));
1089 if constexpr (std::is_copy_assignable_v<SA>)
1090 {
1091 later.set_arc_filter(sa);
1092 EXPECT_TRUE(text_round(later, hide));
1093 EXPECT_TRUE(binary_round(later, hide));
1094 }
1095 }
1096 });
1097 std::remove(file.c_str());
1098}
1099
1101{
1102 using GT = TypeParam;
1103 Built<GT> b(star_path());
1104 for_each_filter<GT>([&] (auto sa, const char * name)
1105 {
1106 using SA = decltype(sa);
1107 constexpr bool hide = hides<GT, SA>;
1108 SCOPED_TRACE(name);
1109 const size_t arcs = hide ? 2 : 3;
1110 for_each_constness(b.g, [&] (auto & g)
1111 {
1112 std::ostringstream pic, dot1, dot2, dot3, dot4, dot5;
1113 generate_graphpic<GT, Text, Text, Text, Text, SA>(g, 1.0, 1.0, pic, sa);
1114 generate_graphviz<GT, Text, Text, Dummy_Attr<GT>, Dummy_Attr<GT>,
1115 Dummy_Attr<GT>, Dummy_Attr<GT>, SA, Dft_Show_Node<GT>>
1116 (g, dot1, "TB", 0.2, 0.2, sa);
1117 generate_graphviz<GT, Dft_Node_Attr<GT>, Dft_Arc_Attr<GT>, Dft_Show_Node<GT>, SA>
1118 (g, dot2, Dft_Node_Attr<GT>(), Dft_Arc_Attr<GT>(), "TB", sa);
1119 digraph_graphviz<GT, Dft_Node_Attr<GT>, Dft_Arc_Attr<GT>, Dft_Show_Node<GT>, SA>
1120 (g, dot3, Dft_Node_Attr<GT>(), Dft_Arc_Attr<GT>(), "LR", sa);
1121 To_Graphviz<GT, Dft_Node_Attr<GT>, Dft_Arc_Attr<GT>, Dft_Show_Node<GT>, SA> to_dot(sa);
1122 to_dot(g, dot4);
1123 to_dot.digraph(g, dot5);
1124 for (const auto * out : {&pic, &dot1, &dot2, &dot3, &dot4, &dot5})
1125 EXPECT_EQ(drawn_arcs(out->str()), arcs);
1126 });
1127
1128 // They take GT &: a const graph is outside their contract
1129 std::ostringstream dot, cross1, cross2, net1, net2;
1132 generate(b.g, dot);
1133 generate_cross_graph<GT, Text, Text, Text, Text, SA>(b.g, 2, 1.0, 1.0, cross1, sa);
1134 generate_cross_graph<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, cross2, sa);
1135 generate_net_graph<GT, Text, Text, Text, Text, SA>(b.g, 2, 1.0, 1.0, net1, sa);
1136 generate_net_graph<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, net2, sa);
1137 for (const auto * out : {&dot, &cross1, &cross2, &net1, &net2})
1138 EXPECT_EQ(drawn_arcs(out->str()), arcs);
1139
1140 std::ofstream nowhere;
1141 generate_cross_spanning_tree<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, nowhere, sa);
1142 generate_net_spanning_tree<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, nowhere, sa);
1143
1144 // Without the object they build SA(), which hides nothing
1145 if constexpr (default_built<SA>)
1146 {
1147 std::ostringstream pic, dot6, cross3;
1148 generate_graphpic<GT, Text, Text, Text, Text, SA>(b.g, 1.0, 1.0, pic);
1150 generate_cross_graph<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, cross3);
1151 for (const auto * out : {&pic, &dot6, &cross3})
1152 EXPECT_EQ(drawn_arcs(out->str()), 3U);
1153 generate_cross_spanning_tree<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, nowhere);
1154 generate_net_spanning_tree<GT, Text, Text, SA>(b.g, 2, 1.0, 1.0, nowhere);
1155 }
1156 });
1157
1158 // The overloads without a filter
1159 std::ostringstream cross, net;
1160 generate_cross_graph<GT, Text, Text, Text, Text>(b.g, 2, 1.0, 1.0, cross);
1161 generate_cross_graph<GT, Text, Text>(b.g, 2, 1.0, 1.0, cross);
1162 generate_net_graph<GT, Text, Text, Text, Text>(b.g, 2, 1.0, 1.0, net);
1163 generate_net_graph<GT, Text, Text>(b.g, 2, 1.0, 1.0, net);
1164 EXPECT_FALSE(cross.str().empty());
1165 EXPECT_FALSE(net.str().empty());
1166 std::ofstream nowhere;
1167 generate_cross_spanning_tree<GT, Text, Text>(b.g, 2, 1.0, 1.0, nowhere);
1168 generate_net_spanning_tree<GT, Text, Text>(b.g, 2, 1.0, 1.0, nowhere);
1169}
1170
1172{
1173 using GT = TypeParam;
1174 Built<GT> b(star_path());
1175 for_each_filter<GT>([&] (auto sa, const char * name)
1176 {
1177 using SA = decltype(sa);
1178 constexpr bool hide = hides<GT, SA>;
1179 SCOPED_TRACE(name);
1180 // It takes GT &: a const graph is outside its contract. Without the
1181 // arc Hidden, 0 - 3, no node reaches 3
1182 Bit_Mat_Graph<GT, SA> mat(b.g, sa);
1183 EXPECT_EQ(int(mat(b.nodes[0], b.nodes[3])), hide ? 0 : 1);
1185 EXPECT_EQ(int(mat(b.nodes[0], b.nodes[2])), 1);
1186 EXPECT_EQ(int(mat(b.nodes[0], b.nodes[3])), hide ? 0 : 1);
1187
1188 Bit_Mat_Graph<GT, SA> by_functor(b.g, sa);
1190 closure(b.g, by_functor);
1191 EXPECT_EQ(int(by_functor(b.nodes[0], b.nodes[2])), 1);
1192 EXPECT_EQ(int(by_functor(b.nodes[0], b.nodes[3])), hide ? 0 : 1);
1193
1194 // Without the object, SA(), which hides nothing
1195 if constexpr (default_built<SA>)
1196 {
1199 EXPECT_EQ(int(all(b.nodes[0], b.nodes[3])), 1);
1201 EXPECT_EQ(int(all(b.nodes[0], b.nodes[3])), 1);
1202 }
1203 });
1204}
1205
1207{
1208 using GT = TypeParam;
1209 const Spec s = parallel_pair();
1210 Built<GT> b(s);
1211 for_each_filter<GT>([&] (auto sa, const char * name)
1212 {
1213 using SA = decltype(sa);
1214 constexpr bool hide = hides<GT, SA>;
1215 SCOPED_TRACE(name);
1216 // It takes the filters only as rvalues, and GT &: an lvalue filter
1217 // and a const graph are outside its contract
1219 // Simple only without the parallel arc
1220 EXPECT_EQ(single(b.g), hide);
1221 EXPECT_EQ(single.test_node(b.nodes[0]), hide);
1222 });
1223}
1224
1226{
1227 using GT = TypeParam;
1228 const Spec s = triangle();
1229 Built<GT> b(s);
1230 for_each_filter<GT>([&] (auto sa, const char * name)
1231 {
1232 using SA = decltype(sa);
1233 constexpr bool hide = hides<GT, SA>;
1234 SCOPED_TRACE(name);
1235 // They take the filters only as rvalues, and GT &: an lvalue filter
1236 // and a const graph are outside their contract
1238 const Eulerian_Type type = test.compute(b.g);
1239 (void) test(b.g);
1240 (void) test.has_eulerian_path(b.g);
1241 (void) test.test_degree_only(b.g);
1242 // Without arc 2 the triangle is a path; with it, a cycle, or nothing
1243 // in a digraph, whose arcs are 0 -> 1, 1 -> 2 and 0 -> 2
1244 const Eulerian_Type want = hide ? Eulerian_Type::Path
1245 : b.g.is_digraph() ? Eulerian_Type::None : Eulerian_Type::Cycle;
1246 EXPECT_EQ(type, want);
1247
1249 const auto r = find(b.g);
1250 if (hide)
1251 {
1252 EXPECT_EQ(arc_numbers(r.path).count(Hidden), 0U);
1253 }
1254 (void) find.find_path(b.g);
1255 (void) find.find_node_sequence(b.g);
1256
1259 const bool by_ore = ore(b.g);
1260 const bool by_dirac = dirac(b.g);
1261 EXPECT_EQ(dirac.min_required_degree(b.g), 2U);
1262 EXPECT_NE(dirac.find_min_degree_vertex(b.g).second, nullptr);
1263 if (not b.g.is_digraph())
1264 {
1265 // The triangle satisfies both conditions; the path 0 - 1 - 2,
1266 // neither
1267 EXPECT_EQ(by_ore, not hide);
1268 EXPECT_EQ(by_dirac, not hide);
1269 }
1270 });
1271}
1272
1273template <class GT>
1274class ContractCoreDigraphs : public ::testing::Test {};
1275
1277
1278TYPED_TEST(ContractCoreDigraphs, InvertDigraphAndRanks)
1279{
1280 using GT = TypeParam;
1281 Built<GT> b(star_path());
1282 for_each_filter<GT>([&] (auto sa, const char * name)
1283 {
1284 using SA = decltype(sa);
1285 constexpr bool hide = hides<GT, SA>;
1286 SCOPED_TRACE(name);
1287 for_each_constness(b.g, [&] (auto & g)
1288 {
1289 const Invert_Digraph<GT, SA> invert(sa);
1290 GT inverted = invert(g);
1291 EXPECT_EQ(inverted.get_num_nodes(), 4U);
1292 EXPECT_EQ(graph_arcs(inverted), visible({0, 1, 2}, hide));
1293
1294 // The ranks are those of the arcs shown: {0, 3}, {1}, {2} without
1295 // the arc Hidden, 0 -> 3; {0}, {1, 3}, {2} with it
1296 std::ostringstream ranks, dot;
1298 (g, ranks, Dft_Node_Attr<GT>(), Dft_Arc_Attr<GT>(), "LR", sa)), 3U);
1299 EXPECT_EQ(drawn_arcs(ranks.str()), hide ? 2U : 3U);
1301 .ranks(g, dot);
1302 EXPECT_EQ(drawn_arcs(dot.str()), hide ? 2U : 3U);
1303
1304 // Without the object, SA(), which hides nothing
1305 if constexpr (default_built<SA>)
1306 {
1307 std::ostringstream all;
1309 Dft_Show_Node<GT>, SA>(g, all)), 3U);
1310 EXPECT_EQ(drawn_arcs(all.str()), 3U);
1311 }
1312 });
1313 });
1314}
1315
1316template <class GT>
1317class ContractCoreTrees : public ::testing::Test {};
1318
1320
1322{
1323 using GT = TypeParam;
1324 Built<GT> b(triangle());
1325 for_each_filter<GT>([&] (auto sa, const char * name)
1326 {
1327 using SA = decltype(sa);
1328 constexpr bool hide = hides<GT, SA>;
1329 SCOPED_TRACE(name);
1330 for_each_constness(b.g, [&] (auto & g)
1331 {
1332 DynList<Df_Low_Non_Tree_Arc> non_tree;
1333 Df_Low_Tree_Node * root =
1334 build_df_low_tree(g, b.nodes[0], non_tree, Dft_Df_Tree_Label<GT>(), sa);
1335 ASSERT_NE(root, nullptr);
1336 size_t nodes = 0;
1337 root->traverse([&nodes] (Df_Low_Tree_Node *) { ++nodes; return true; });
1339 EXPECT_EQ(nodes, 3U);
1340 // Without arc 2, no arc is out of the tree
1341 EXPECT_EQ(non_tree.size(), hide ? 0U : 1U);
1342
1343 std::ostringstream out;
1345 write_df_low_tree<Write_Df_Tree_Df>(g, b.nodes[0], out, Dft_Df_Tree_Label<GT>(), sa);
1346 EXPECT_FALSE(out.str().empty());
1347 });
1348 });
1349}
1350
1352{
1353 using GT = TypeParam;
1354 Built<GT> tree(star_path());
1355 const Spec s = triangle();
1356 Built<GT> cycle(s);
1357 for_each_filter<GT>([&] (auto sa, const char * name)
1358 {
1359 using SA = decltype(sa);
1360 constexpr bool hide = hides<GT, SA>;
1361 SCOPED_TRACE(name);
1362 // They take GT &: a const graph is outside their contract
1363
1364 Tree_Node<int> * built =
1365 graph_to_tree_node<GT, int, To_Key, SA>(tree.g, tree.nodes[0], sa);
1366 EXPECT_EQ(tree_keys(built),
1367 hide ? std::set<int>({0, 1, 2}) : std::set<int>({0, 1, 2, 3}));
1368 destroy_tree(built);
1369 if (hide) // the filter makes a tree of the triangle
1370 {
1371 built = graph_to_tree_node<GT, int, To_Key, SA>(cycle.g, cycle.nodes[0], sa);
1372 EXPECT_EQ(tree_keys(built), std::set<int>({0, 1, 2}));
1373 destroy_tree(built);
1374 }
1375 else
1376 EXPECT_THROW_OR_FILTER((void) (graph_to_tree_node<GT, int, To_Key, SA>(cycle.g,
1377 cycle.nodes[0], sa)),
1378 std::domain_error);
1379
1380 // Without the object, SA(), which hides nothing
1381 if constexpr (default_built<SA>)
1382 {
1383 built = graph_to_tree_node<GT, int, To_Key, SA>(tree.g, tree.nodes[0]);
1384 EXPECT_EQ(tree_keys(built), std::set<int>({0, 1, 2, 3}));
1385 destroy_tree(built);
1386 }
1387
1389 To_Key key;
1390 Tree_Node<int> * root = convert(tree.g, tree.nodes[0]);
1391 EXPECT_EQ(tree_keys(root),
1392 hide ? std::set<int>({0, 1, 2}) : std::set<int>({0, 1, 2, 3}));
1394 root = convert(tree.g, tree.nodes[0], key);
1396
1397 // The filter makes a tree of the triangle
1398 if (hide)
1399 {
1400 root = convert(cycle.g, cycle.nodes[0]);
1401 EXPECT_EQ(tree_keys(root), std::set<int>({0, 1, 2}));
1403 }
1404 else
1405 EXPECT_THROW_OR_FILTER((void) convert(cycle.g, cycle.nodes[0]), std::domain_error);
1406 });
1407}
WeightedDigraph::Arc Arc
long double h
Definition btreepic.C:154
size_t size_t int32_t * out
Definition ca-c-api.h:120
Bit matrix for graph connectivity.
Filtered copy of graphs.
Definition tpl_graph.H:4951
Default distance accessor for arc weights.
void reset_first() noexcept(shows_ne)
Reset the iterator to the first arc.
Definition tpl_graph.H:1882
bool has_curr() const noexcept
Return true the iterator has an current arc.
Definition tpl_graph.H:1822
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
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
void reset_last()
Resets the iterator to the last filtered element.
void set_cookie(void *__cookie) noexcept
Sets the cookie pointer.
void next()
Advances the iterator to the next filtered element.
const Container & get_container() const
Returns a const reference to the container being iterated over.
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 * get_cookie() const noexcept
Gets the cookie pointer.
void reset_first()
Resets the iterator to the first filtered element.
void set_filter(Show_Item si)
Sets a new filter functor.
void prev_ne() noexcept(shows_ne)
Moves the iterator backward, as prev() does.
bool find_if(Pred pred)
Find the first element that satisfies an additional predicate.
Show_Item & get_filter() noexcept
Returns a reference to the filter functor.
Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.
Definition eulerian.H:477
Functor class to convert a tree graph to Tree_Node structure.
Lazily traverse a graph depth-first or breadth-first.
Aleph::Generator< typename GT::Node * > traverse(typename GT::Node *start) &
Lazily traverse the graph from start.
bool has_curr() const noexcept
Definition htlist.H:969
Graph serialization and deserialization class.
Definition io_graph.H:344
Iterator on the arcs into a node, in the order of the arc iterator of the graph when the index was bu...
Definition tpl_graph.H:2279
Index of the arcs that go into each node of a graph.
Definition tpl_graph.H:2224
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
A filter that an algorithm object keeps: one given as an lvalue is used where it is,...
Definition tpl_graph.H:1249
F & get() const noexcept
The filter.
Definition tpl_graph.H:1274
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Functor that traverses the arcs of a graph and performs an operation.
Definition tpl_graph.H:3743
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Tests Dirac's sufficient condition for Hamiltonian graphs.
Tests whether a graph or digraph is Eulerian.
Definition eulerian.H:241
Tests Ore's sufficient condition for Hamiltonian graphs.
Determines whether a graph or digraph is simple.
Represents a text string positioned at a 2D point.
Definition point.H:2817
Compute the total cost (sum of arc weights) of a graph.
Distance::Distance_Type total_cost(GT &g)
Compute the total cost.
Distance::Distance_Type value() const noexcept
Return the accumulated value (after using operator()(Arc*)).
void reset() noexcept
Reset the internal accumulator used by operator()(Arc*).
Forward declaration used by CRTP helpers before the full node definition.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Computes the transitive closure of a graph using Warshall's algorithm.
Definition warshall.H:119
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
In_Iterator get_in_it(Node *p) const
Return an input iterator on the incoming arcs to p
Definition graph-dry.H:3507
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
Traverse a graph depth-first or breadth-first and execute a visit function.
size_t exec(typename GT::Node *start, Op &op)
Execute operation op(curr, arc) where curr is the visited node and arc is the incoming arc.
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 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.
Concept for node adjacency iterators.
Definition graph-dry.H:475
Eulerian graph detection and path/cycle finding.
Pictures of depth-first trees with their df and low numbers.
Graph visualization and output generation utilities.
Spanning tree visualization with highlighted tree/non-tree edges.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Lazy (coroutine-based) graph traversal (DFS, BFS).
Graph traversal algorithms (DFS, BFS).
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractCoreIterators, NodeArcIterator)
TYPED_TEST_SUITE(ContractCoreIterators, All_Kinds)
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.
#define EXPECT_THROW_OR_FILTER(statement, type)
EXPECT_THROW(statement, type), for a statement that calls a filter: in a call of graph_harness::with_...
Convert spanning tree graphs to Tree_Node structures.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
bool traverse_in_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of incoming arcs of a node.
Definition tpl_graph.H:2947
void warshall_compute_transitive_clausure(GT &g, Bit_Mat_Graph< GT, SA > &mat, SA sa=SA())
Computes the transitive closure of a graph using Warshall's algorithm.
Definition warshall.H:84
void write_df_low_tree(const GT &g, typename GT::Node *src, std::ostream &out, Node_Label node_label=Node_Label(), SA sa=SA())
Write, for ntreepic, the depth-first spanning tree of an undirected graph with the df and low numbers...
size_t rank_graphviz(const GT &g, std::ostream &out, Node_Attr node_attr=Node_Attr(), Arc_Attr arc_attr=Arc_Attr(), const std::string &rankdir="LR", SA sa=SA())
Generate Graphviz DOT output with topological ranking.
void for_each_arc(const GT &g, Op operation, SA sa=SA())
Traverse all the arcs of graph filtering some ones according to a condition and executing an operatio...
Definition tpl_graph.H:1339
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
bool forall_arc(const GT &g, Op cond, SA sa=SA())
Return true if condition cond is met on every filtered arc of the graph.
Definition tpl_graph.H:1403
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Hamiltonian sufficiency testing for graphs.
Graph serialization and deserialization utilities.
Main namespace for Aleph-w library functions.
auto search_in_arc(typename GT::Node *p, Op op=Op())
Search an incoming arc to a node satisfying a condition op.
Definition tpl_graph.H:3119
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
DynList< typename GT::Arc * > filter_in_arcs(typename GT::Node *p, Op cond)
Filter the incoming arcs meeting an condition.
Definition tpl_graph.H:3324
void generate(Itor beg, const Itor &end, Func op)
Generate values for a range.
Definition ahAlgo.H:746
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.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
Eulerian_Type
Enumeration for Eulerian graph classification.
Definition eulerian.H:114
and
Check uniqueness with explicit hash + equality functors.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Definition ahSort.H:696
bool all_in_arc(typename GT::Node *p, Op op=Op())
Test if the incoming arcs meet a condition.
Definition tpl_graph.H:3032
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.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::testing::Types< 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 > > > Directed_Kinds
The directed kinds, with int infos.
::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 > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
::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.
Single graph utilities.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default label of a graph node: its information, written with operator <<.
Default arc loading functor for binary and text modes.
Definition io_graph.H:279
Default node loading functor for binary and text modes.
Definition io_graph.H:241
Default filter for the graph nodes.
Definition tpl_graph.H:1228
Default arc storage functor for binary and text modes.
Definition io_graph.H:203
Default node storage functor for binary and text modes.
Definition io_graph.H:165
Functor for generating Graphviz specifications.
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Functor class for generating Graphviz DOT specifications.
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 .
std::vector< std::array< int, 2 > > arcs
void test()
Definition test-comb.C:40
bool check(GT &g)
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
DynList< int > l
Warshall's algorithm for transitive closure.
ofstream file
Definition writeRb.C:51