74#include <gtest/gtest.h>
102 long long process_id() noexcept
105 return static_cast<long long>(_getpid());
107 return static_cast<long long>(getpid());
113 template <
class List>
114 std::set<int> arc_numbers(
const List &
l)
117 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
118 r.insert(it.get_curr()->get_info());
131 s.
arcs = {{0, 1}, {1, 2}, {0, 3}};
140 s.
arcs = {{0, 1}, {1, 2}, {0, 2}};
149 s.
arcs = {{0, 1}, {1, 2}, {3, 0}};
158 s.
arcs = {{0, 1}, {1, 2}, {0, 1}};
163 std::set<int> visible(std::set<int>
all,
bool hide)
172 std::set<int> iterated(
It & it)
181 template <
class List>
182 std::set<int> node_numbers(
const List &
l)
191 template <
class List>
192 std::set<int> pair_numbers(
const List &
l)
196 r.insert(std::get<0>(it.
get_curr())->get_info());
202 std::set<int> graph_arcs(
const GT & g)
222 constexpr bool singly_linked = std::is_same_v<typename GT::Node, Graph_Snode<int>>;
227 constexpr bool default_built = std::is_default_constructible_v<SA>;
233 std::string operator () (
T *)
const {
return ""; }
238 size_t drawn_arcs(
const std::string & text)
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)))
254 std::set<int> * seen =
nullptr;
256 template <
class G,
class A>
257 void operator () (
G &, A * a)
const { seen->insert(a->get_info()); }
266 tnode->
get_key() = gnode->get_info();
279 using GT = TypeParam;
281 for_each_filter<GT>([&] (
auto sa,
const char * name)
283 using SA =
decltype(sa);
284 constexpr bool hide = hides<GT, SA>;
286 const auto want = visible({0, 2}, hide);
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());
299 EXPECT_EQ(
each, want);
300 EXPECT_EQ(it.find_if([] (
typename GT::Arc * a) { return a->get_info() == Hidden; }),
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();
313 if constexpr (default_built<SA>)
316 EXPECT_FALSE(
none.has_container());
320 if constexpr (std::is_move_assignable_v<SA>)
324 EXPECT_EQ(iterated(it), want);
329 if constexpr (not singly_linked<GT>)
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);
345 using GT = TypeParam;
347 for_each_filter<GT>([&] (
auto sa,
const char * name)
349 using SA =
decltype(sa);
350 constexpr bool hide = hides<GT, SA>;
352 const auto want = visible({0, 1, 2}, hide);
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());
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; }),
373 (void) std::as_const(it).get_filter();
376 if constexpr (default_built<SA>
and not singly_linked<GT>)
379 EXPECT_FALSE(
none.has_container());
383 if constexpr (std::is_move_assignable_v<SA>)
387 EXPECT_EQ(iterated(it), want);
392 if constexpr (not singly_linked<GT>)
398 EXPECT_EQ(back, want);
409 using GT = TypeParam;
410 const Spec s = in_and_out();
416 Check in_list(
"In_Iterator(p), if not directed");
417 for_each_filter<GT>([&] (
auto sa,
const char * name)
419 using SA =
decltype(sa);
420 constexpr bool hide = hides<GT, SA>;
422 const auto want = visible({2}, hide);
428 EXPECT_EQ(iterated(
copy), want);
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]);
440 EXPECT_EQ(iterated(
out), want);
442 EXPECT_EQ(iterated(first), std::set<int>({0}));
449 if constexpr (
requires {
out.reset_last();
out.prev();
out.end(); })
455 EXPECT_FALSE(
out.has_curr());
459 auto check_in = [&] (
Check &
check,
auto & in)
463 if (iterated(in_copy) != want)
464 check.fail(s, std::string(name) +
": the arcs into node 0 are not seen");
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]);
477 EXPECT_FALSE(in.has_curr());
482 EXPECT_FALSE(in.has_curr());
484 if constexpr (
requires { in.reset_last(); in.prev(); })
487 EXPECT_EQ(iterated(in), want);
491 EXPECT_FALSE(in.has_curr());
493 if constexpr (
requires { in.end(); })
497 EXPECT_FALSE(in.has_curr());
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>)
509 In_Iterator<GT, SA> by_default(g, b.nodes[0]);
510 EXPECT_EQ(iterated(by_default), std::set<int>({2}));
514 if constexpr (not DigraphType<GT>)
517 check_in(in_list, in);
524 typename GT::In_Iterator in(g, b.nodes[0]);
525 EXPECT_EQ(iterated(in), std::set<int>({2}));
528 EXPECT_EQ(it.get_node(), b.nodes[3]);
532 if constexpr (not DigraphType<GT>)
535 EXPECT_EQ(iterated(in), std::set<int>({2}));
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(); }; };
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);
580 using GT = TypeParam;
582 for_each_filter<GT>([&] (
auto sa,
const char * name)
584 using SA =
decltype(sa);
585 constexpr bool hide = hides<GT, SA>;
589 std::set<int> all, at_0;
590 for_each_arc(g, [&all] (typename GT::Arc * a) { all.insert(a->get_info()); }, sa);
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));
597 EXPECT_EQ(
forall_arc(g, not_hidden, sa), hide);
598 EXPECT_EQ(forall_arc<GT>(b.
nodes[0], not_hidden, sa), hide);
605 using GT = TypeParam;
607 for_each_filter<GT>([&] (
auto sa,
const char * name)
609 using SA =
decltype(sa);
610 constexpr bool hide = hides<GT, SA>;
612 auto info = [] (
typename GT::Arc * a) {
return a->get_info(); };
620 const auto all = visible({0, 1, 2}, hide);
621 const auto at_0 = visible({0, 2}, hide);
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);
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);
638 using GT = TypeParam;
639 const Spec s = in_and_out();
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)
647 using SA =
decltype(sa);
648 constexpr bool hide = hides<GT, SA>;
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];
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));
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>)
674 EXPECT_EQ(in_degree(g, in_node), 1U);
678 if constexpr (not DigraphType<GT>)
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");
695 using GT = TypeParam;
696 const Spec s = in_and_out();
698 Check index(
"In_Arcs_Index");
699 for_each_filter<GT>([&] (
auto sa,
const char * name)
701 using SA =
decltype(sa);
702 constexpr bool hide = hides<GT, SA>;
704 const auto want = visible({2}, hide);
708 const In_Arcs_Index<GT, SA> in(g, sa);
709 const In_Arcs_Index<GT, SA> rvalue(g, SA(sa));
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");
719 EXPECT_EQ(iterated(kt), want);
721 auto jt = in.get_it(b.
nodes[0]);
722 EXPECT_EQ(jt.size(), want.size());
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]);
731 EXPECT_FALSE(jt.has_curr());
732 EXPECT_THROW(jt.next(), std::overflow_error);
734 EXPECT_TRUE(jt.has_curr());
736 EXPECT_FALSE(jt.has_curr());
737 EXPECT_THROW((
void) jt.get_curr(), std::underflow_error);
740 EXPECT_FALSE(jt.has_curr());
743 EXPECT_THROW((
void) jt.get_curr(), std::overflow_error);
745 if constexpr (default_built<SA>)
748 EXPECT_EQ(
all.in_degree(b.
nodes[0]), 1U);
749 EXPECT_EQ(
all.size(), 3U);
755 EXPECT_THROW((
void) in.in_degree(q), std::domain_error);
756 EXPECT_THROW((
void) in.get_it(q), std::domain_error);
763 EXPECT_EQ(nothing.size(), 0U);
770 using GT = TypeParam;
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(); };
784 std::function<int (
Arc *)> f = info;
789 for_each_in_arc(g, p, [&each] (Arc * a) { each.insert(a->get_info()); });
790 EXPECT_EQ(
each, std::set<int>({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}));
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>)
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}));
822 using GT = TypeParam;
824 for_each_filter<GT>([&] (
auto sa,
const char * name)
826 using SA =
decltype(sa);
827 constexpr bool hide = hides<GT, SA>;
829 auto * want = hide ? nullptr : b.
arcs[
Hidden];
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]);
841 using GT = TypeParam;
843 for_each_filter<GT>([&] (
auto sa,
const char * name)
845 using SA =
decltype(sa);
846 constexpr bool hide = hides<GT, SA>;
856 EXPECT_NE(&by_rvalue.get(), &sa);
859 EXPECT_TRUE(
bool(by_rvalue.get()(b.
arcs[0])));
865 using GT = TypeParam;
866 const Spec s = star_path();
868 for_each_filter<GT>([&] (
auto sa,
const char * name)
870 using SA =
decltype(sa);
871 constexpr bool hide = hides<GT, SA>;
877 op(g, Collect_Arcs{&all});
878 EXPECT_EQ(
all, visible({0, 1, 2}, hide));
881 op(g, b.
nodes[0], Collect_Arcs{&at_0});
882 EXPECT_EQ(at_0, visible({0, 2}, hide));
889 using GT = TypeParam;
891 for_each_filter<GT>([&] (
auto sa,
const char * name)
893 using SA =
decltype(sa);
894 constexpr bool hide = hides<GT, SA>;
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));
914 using GT = TypeParam;
916 for_each_filter<GT>([&] (
auto sa,
const char * name)
918 using SA =
decltype(sa);
920 constexpr bool hide = hides<GT, SA>;
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; };
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],
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; })),
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);
949 using GT = TypeParam;
951 for_each_filter<GT>([&] (
auto sa,
const char * name)
953 using SA =
decltype(sa);
955 constexpr bool hide = hides<GT, SA>;
957 const std::set<int> want =
958 hide ? std::set<int>({0, 1, 2}) : std::set<int>({0, 1, 2, 3});
963 std::set<int> by_dfs, by_bfs;
965 by_dfs.insert(p->get_info());
967 by_bfs.insert(p->get_info());
968 EXPECT_EQ(by_dfs, want);
969 EXPECT_EQ(by_bfs, want);
975 using GT = TypeParam;
977 for_each_filter<GT>([&] (
auto sa,
const char * name)
979 using SA =
decltype(sa);
980 constexpr bool hide = hides<GT, SA>;
982 const size_t want = hide ? 3 : 4;
988 Depth_First_Traversal<GT, Default_Visit_Op<GT>, SA> dft(sa);
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);
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);
1006 using GT = TypeParam;
1008 for_each_filter<GT>([&] (
auto sa,
const char * name)
1010 using SA =
decltype(sa);
1011 constexpr bool hide = hides<GT, SA>;
1015 EXPECT_EQ(search_spanning_tree_arc(g, b.nodes[0], b.nodes[3], sa),
1016 hide ? nullptr : b.arcs[Hidden]);
1022 EXPECT_EQ(cost(b.
g), hide ? 1 : 3);
1031 using GT = TypeParam;
1032 const Spec s = star_path();
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)
1038 using SA =
decltype(sa);
1039 constexpr bool hide = hides<GT, SA>;
1045 IO by_pointer(&b.
g, sa);
1046 by_pointer.set_verbose(
false);
1052 EXPECT_FALSE(by_pointer.is_verbose());
1056 auto text_round = [&] (IO & saver,
bool hidden)
1058 std::ostringstream text;
1059 saver.save_in_text_mode(text);
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);
1066 auto binary_round = [&] (IO & saver,
bool hidden)
1069 std::ofstream
out(
file, std::ios::binary);
1074 std::ifstream in(
file, std::ios::binary);
1076 return h.get_num_nodes() == 4
and graph_arcs(
h) == visible({0, 1, 2}, hidden);
1079 EXPECT_TRUE(text_round(io, hide));
1080 EXPECT_TRUE(binary_round(io, hide));
1081 EXPECT_TRUE(text_round(by_pointer, hide));
1085 if constexpr (default_built<SA>)
1088 EXPECT_TRUE(text_round(later,
false));
1089 if constexpr (std::is_copy_assignable_v<SA>)
1091 later.set_arc_filter(sa);
1092 EXPECT_TRUE(text_round(later, hide));
1093 EXPECT_TRUE(binary_round(later, hide));
1097 std::remove(
file.c_str());
1102 using GT = TypeParam;
1104 for_each_filter<GT>([&] (
auto sa,
const char * name)
1106 using SA =
decltype(sa);
1107 constexpr bool hide = hides<GT, SA>;
1109 const size_t arcs = hide ? 2 : 3;
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);
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);
1129 std::ostringstream dot, cross1, cross2, net1, net2;
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);
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);
1145 if constexpr (default_built<SA>)
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);
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);
1173 using GT = TypeParam;
1175 for_each_filter<GT>([&] (
auto sa,
const char * name)
1177 using SA =
decltype(sa);
1178 constexpr bool hide = hides<GT, 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);
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);
1195 if constexpr (default_built<SA>)
1208 using GT = TypeParam;
1209 const Spec s = parallel_pair();
1211 for_each_filter<GT>([&] (
auto sa,
const char * name)
1213 using SA =
decltype(sa);
1214 constexpr bool hide = hides<GT, SA>;
1220 EXPECT_EQ(single(b.
g), hide);
1221 EXPECT_EQ(single.test_node(b.
nodes[0]), hide);
1227 using GT = TypeParam;
1228 const Spec s = triangle();
1230 for_each_filter<GT>([&] (
auto sa,
const char * name)
1232 using SA =
decltype(sa);
1233 constexpr bool hide = hides<GT, SA>;
1240 (void)
test.has_eulerian_path(b.
g);
1241 (void)
test.test_degree_only(b.
g);
1245 : b.
g.
is_digraph() ? Eulerian_Type::None : Eulerian_Type::Cycle;
1246 EXPECT_EQ(type, want);
1249 const auto r =
find(b.
g);
1252 EXPECT_EQ(arc_numbers(
r.path).count(
Hidden), 0U);
1254 (void)
find.find_path(b.
g);
1255 (void)
find.find_node_sequence(b.
g);
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);
1267 EXPECT_EQ(by_ore, not hide);
1268 EXPECT_EQ(by_dirac, not hide);
1280 using GT = TypeParam;
1282 for_each_filter<GT>([&] (
auto sa,
const char * name)
1284 using SA =
decltype(sa);
1285 constexpr bool hide = hides<GT, SA>;
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));
1296 std::ostringstream
ranks, dot;
1299 EXPECT_EQ(drawn_arcs(
ranks.str()), hide ? 2U : 3U);
1302 EXPECT_EQ(drawn_arcs(dot.str()), hide ? 2U : 3U);
1305 if constexpr (default_built<SA>)
1307 std::ostringstream
all;
1310 EXPECT_EQ(drawn_arcs(
all.str()), 3U);
1323 using GT = TypeParam;
1325 for_each_filter<GT>([&] (
auto sa,
const char * name)
1327 using SA =
decltype(sa);
1328 constexpr bool hide = hides<GT, SA>;
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);
1337 root->traverse([&nodes] (Df_Low_Tree_Node *) { ++nodes; return true; });
1339 EXPECT_EQ(
nodes, 3U);
1341 EXPECT_EQ(non_tree.size(), hide ? 0U : 1U);
1343 std::ostringstream
out;
1346 EXPECT_FALSE(
out.str().empty());
1353 using GT = TypeParam;
1355 const Spec s = triangle();
1357 for_each_filter<GT>([&] (
auto sa,
const char * name)
1359 using SA =
decltype(sa);
1360 constexpr bool hide = hides<GT, SA>;
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}));
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}));
1377 cycle.
nodes[0], sa)),
1381 if constexpr (default_built<SA>)
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}));
1391 EXPECT_EQ(tree_keys(
root),
1392 hide ? std::set<int>({0, 1, 2}) : std::set<int>({0, 1, 2, 3}));
1401 EXPECT_EQ(tree_keys(
root), std::set<int>({0, 1, 2}));
size_t size_t int32_t * out
Bit matrix for graph connectivity.
Default distance accessor for arc weights.
void reset_first() noexcept(shows_ne)
Reset the iterator to the first arc.
bool has_curr() const noexcept
Return true the iterator has an current arc.
Node belonging to a double circular linked list with header node.
T & get_curr() const
Return the current item.
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Doubly-linked list (defined in tpl_dynList.H).
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.
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
Graph serialization and deserialization class.
Iterator on the arcs into a node, in the order of the arc iterator of the graph when the index was bu...
Index of the arcs that go into each node of a graph.
Iterator get_it(const Node *p) const
Return an iterator on the arcs into p that the filter showed.
Filtered iterator for incoming arcs of a node.
A filter that an algorithm object keeps: one given as an lvalue is used where it is,...
F & get() const noexcept
The filter.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Arc< Arc_Info > Arc
The node class type.
Number Theoretic Transform over Z / MOD Z.
Functor that traverses the arcs of a graph and performs an operation.
Filtered iterator for outcoming arcs of a node.
Tests Dirac's sufficient condition for Hamiltonian graphs.
Tests whether a graph or digraph is Eulerian.
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.
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.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
In_Iterator get_in_it(Node *p) const
Return an input iterator on the incoming arcs to p
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
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.
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.
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)
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
DynArray< Graph::Arc * > arcs
bool traverse_in_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of incoming arcs of a node.
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.
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...
DynList< typename GT::Arc * > in_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
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.
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.
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.
void generate(Itor beg, const Itor &end, Func op)
Generate values for a range.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Eulerian_Type
Enumeration for Eulerian graph classification.
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.
bool all_in_arc(typename GT::Node *p, Op op=Op())
Test if the incoming arcs meet a condition.
bool exists_in_arc(typename GT::Node *p, Op op=Op())
Test if it exists an incoming arc satisfying an operation.
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.
Filtered iterator on all the arcs of a graph.
Default label of a graph node: its information, written with operator <<.
Default arc loading functor for binary and text modes.
Default node loading functor for binary and text modes.
Default filter for the graph nodes.
Default arc storage functor for binary and text modes.
Default node storage functor for binary and text modes.
Functor for generating Graphviz specifications.
Filtered iterator of adjacent arcs of a node.
Functor class for generating Graphviz DOT specifications.
Alias for Digraph_Iterator
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
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Warshall's algorithm for transitive closure.