40#include <gtest/gtest.h>
65 constexpr bool writes_the_information_through =
requires (
It it)
67 it.get_curr()->get_info() = 1;
71 constexpr bool writes_the_node_bookkeeping_through =
requires (
It it)
79 constexpr bool writes_the_arc_bookkeeping_through =
requires (
It it)
87 constexpr bool writes_the_end_points_through =
requires (
It it)
89 it.get_src_node()->get_info() = 1;
91 or
requires (
It it) { it.get_tgt_node()->get_info() = 1; };
94 constexpr bool removes_through =
requires (
It it) { it.del(); };
97 template <
class G,
class P>
98 constexpr bool for_each_node_takes =
requires (
const G & g)
100 g.for_each_node([] (
P) {});
103 template <
class G,
class P>
104 constexpr bool for_each_arc_takes =
requires (
const G & g)
106 g.for_each_arc([] (
P) {});
109 template <
class G,
class N,
class P>
110 constexpr bool for_each_arc_of_a_node_takes =
requires (
const G & g,
N p)
115 template <
class G,
class P>
116 constexpr bool traverse_nodes_takes =
requires (
const G & g)
121 template <
class G,
class P>
122 constexpr bool traverse_arcs_takes =
requires (
const G & g)
127 template <
class G,
class P>
128 constexpr bool search_node_takes =
requires (
const G & g)
133 template <
class G,
class P>
134 constexpr bool search_arc_takes =
requires (
const G & g)
139 template <
class G,
class P>
140 constexpr bool filter_arcs_takes =
requires (
const G & g)
145 template <
class G,
class P>
146 constexpr bool free_for_each_arc_takes =
requires (
const G & g)
153 constexpr bool const_view_accepts =
requires
162 bool operator () (
const A *a)
const noexcept {
return a->get_info() >= 30; }
168 bool operator () (A *a)
const noexcept {
return ++a->get_info() > 0; }
174 bool operator () (
const N *p)
const noexcept {
return p->get_info() % 2 == 0; }
182 std::vector<typename G::Node *>
nodes;
183 std::vector<typename G::Arc *>
arcs;
187 for (
int i = 0; i < 5; ++i)
199 class ConstGraphView :
public ::testing::Test {};
201 using Graph_Types = ::testing::Types<
212TYPED_TEST(ConstGraphView, the_iterators_hand_out_pointers_to_const)
219 static_assert(std::is_same_v<typename View::Node, const Node>);
220 static_assert(std::is_same_v<typename View::Arc, const Arc>);
221 static_assert(std::is_same_v<typename View::Node_Iterator, Const_Node_Iterator<G>>);
222 static_assert(std::is_same_v<typename View::Arc_Iterator, Const_Arc_Iterator<G>>);
223 static_assert(std::is_same_v<typename View::Node_Arc_Iterator, Const_Node_Arc_Iterator<G>>);
229 static_assert(std::is_same_v<typename NIt::Item_Type, const Node *>);
230 static_assert(std::is_same_v<typename AIt::Item_Type, const Arc *>);
231 static_assert(std::is_same_v<typename NAIt::Item_Type, const Arc *>);
233 static_assert(std::is_same_v<decltype(std::declval<NIt &>().get_curr()),
const Node *>);
234 static_assert(std::is_same_v<decltype(std::declval<NIt &>().get_curr_ne()),
const Node *>);
235 static_assert(std::is_same_v<decltype(std::declval<NIt &>().get_current_node()),
const Node *>);
236 static_assert(std::is_same_v<decltype(std::declval<AIt &>().get_curr()),
const Arc *>);
237 static_assert(std::is_same_v<decltype(std::declval<AIt &>().get_curr_ne()),
const Arc *>);
238 static_assert(std::is_same_v<decltype(std::declval<AIt &>().get_current_arc()),
const Arc *>);
239 static_assert(std::is_same_v<decltype(std::declval<AIt &>().get_src_node()),
const Node *>);
240 static_assert(std::is_same_v<decltype(std::declval<AIt &>().get_tgt_node()),
const Node *>);
241 static_assert(std::is_same_v<decltype(std::declval<NAIt &>().get_curr()),
const Arc *>);
242 static_assert(std::is_same_v<decltype(std::declval<NAIt &>().get_curr_ne()),
const Arc *>);
243 static_assert(std::is_same_v<decltype(std::declval<NAIt &>().get_tgt_node()),
const Node *>);
247 static_assert(std::is_constructible_v<NIt, const G &>);
248 static_assert(std::is_constructible_v<NIt, const View &>);
249 static_assert(std::is_constructible_v<AIt, const G &>);
250 static_assert(std::is_constructible_v<AIt, const View &>);
251 static_assert(std::is_constructible_v<NAIt, const Node *>);
254 static_assert(not writes_the_information_through<NIt>);
255 static_assert(not writes_the_information_through<AIt>);
256 static_assert(not writes_the_information_through<NAIt>);
257 static_assert(not writes_the_node_bookkeeping_through<NIt>);
258 static_assert(not writes_the_arc_bookkeeping_through<AIt>);
259 static_assert(not writes_the_arc_bookkeeping_through<NAIt>);
260 static_assert(not writes_the_end_points_through<AIt>);
261 static_assert(not removes_through<NIt>);
262 static_assert(not removes_through<AIt>);
263 static_assert(not removes_through<NAIt>);
267 static_assert(std::is_constructible_v<typename G::Node_Iterator, const G &>);
268 static_assert(std::is_same_v<decltype(std::declval<typename G::Node_Iterator &>().get_curr()),
Node *>);
269 static_assert(std::is_same_v<decltype(std::declval<typename G::Arc_Iterator &>().get_curr()),
Arc *>);
270 static_assert(writes_the_information_through<typename G::Node_Iterator>);
271 static_assert(writes_the_information_through<typename G::Arc_Iterator>);
272 static_assert(writes_the_information_through<typename G::Node_Arc_Iterator>);
273 static_assert(writes_the_node_bookkeeping_through<typename G::Node_Iterator>);
274 static_assert(writes_the_arc_bookkeeping_through<typename G::Arc_Iterator>);
275 static_assert(writes_the_end_points_through<typename G::Arc_Iterator>);
278 static_assert(
sizeof(View) ==
sizeof(
void *));
279 static_assert(std::is_trivially_copyable_v<View>);
285 static_assert(std::is_constructible_v<View, const G &>);
286 static_assert(std::is_constructible_v<View, G &>);
287 static_assert(not std::is_constructible_v<View, G>);
288 static_assert(not std::is_constructible_v<View, const G>);
289 static_assert(not std::is_convertible_v<const G &, View>);
290 static_assert(const_view_accepts<G &>);
291 static_assert(const_view_accepts<const G &>);
292 static_assert(not const_view_accepts<G>);
293 static_assert(not const_view_accepts<const G>);
294 static_assert(std::is_same_v<decltype(const_view(std::declval<G &>())), View>);
299TYPED_TEST(ConstGraphView, the_operations_receive_pointers_to_const)
308 static_assert(for_each_node_takes<View, const Node *>);
309 static_assert(not for_each_node_takes<View, Node *>);
310 static_assert(for_each_arc_takes<View, const Arc *>);
311 static_assert(not for_each_arc_takes<View, Arc *>);
312 static_assert(for_each_arc_of_a_node_takes<View, const Node *, const Arc *>);
313 static_assert(not for_each_arc_of_a_node_takes<View, const Node *, Arc *>);
314 static_assert(traverse_nodes_takes<View, const Node *>);
315 static_assert(not traverse_nodes_takes<View, Node *>);
316 static_assert(traverse_arcs_takes<View, const Arc *>);
317 static_assert(not traverse_arcs_takes<View, Arc *>);
318 static_assert(search_node_takes<View, const Node *>);
319 static_assert(not search_node_takes<View, Node *>);
320 static_assert(search_arc_takes<View, const Arc *>);
321 static_assert(not search_arc_takes<View, Arc *>);
322 static_assert(filter_arcs_takes<View, const Arc *>);
323 static_assert(not filter_arcs_takes<View, Arc *>);
326 static_assert(free_for_each_arc_takes<View, const Arc *>);
327 static_assert(not free_for_each_arc_takes<View, Arc *>);
330 static_assert(for_each_node_takes<G, Node *>);
331 static_assert(for_each_node_takes<G, const Node *>);
332 static_assert(for_each_arc_takes<G, Arc *>);
333 static_assert(for_each_arc_takes<G, const Arc *>);
334 static_assert(free_for_each_arc_takes<G, Arc *>);
337 static_assert(std::is_same_v<decltype(std::declval<const View &>().get_first_node()),
const Node *>);
338 static_assert(std::is_same_v<decltype(std::declval<const View &>().get_first_arc()),
const Arc *>);
339 static_assert(std::is_same_v<decltype(std::declval<const View &>().get_src_node(
nullptr)),
const Node *>);
340 static_assert(std::is_same_v<decltype(std::declval<const View &>().get_tgt_node(
nullptr)),
const Node *>);
341 static_assert(std::is_same_v<decltype(std::declval<const View &>().find_node(0)),
const Node *>);
342 static_assert(std::is_same_v<decltype(std::declval<const View &>().find_arc(0)),
const Arc *>);
343 static_assert(std::is_same_v<decltype(std::declval<const View &>().nodes()),
DynList<const Node *>>);
344 static_assert(std::is_same_v<decltype(std::declval<const View &>().arcs()),
DynList<const Arc *>>);
349TYPED_TEST(ConstGraphView, the_view_and_its_iterators_satisfy_the_graph_concepts)
354 static_assert(AlephGraph<View>);
369 static_assert(NodeFilter<Reads_Node<typename G::Node>, View>);
370 static_assert(ArcFilter<Reads_Arc<typename G::Arc>, View>);
371 static_assert(not ArcFilter<Writes_Arc<typename G::Arc>, View>);
372 static_assert(ArcFilter<Writes_Arc<typename G::Arc>,
G>);
373 static_assert(ArcFilter<Reads_Arc<typename G::Arc>,
G>);
388 EXPECT_EQ(view.get_num_nodes(), 5u);
389 EXPECT_EQ(view.vsize(), 5u);
390 EXPECT_EQ(view.get_num_arcs(), 6u);
391 EXPECT_EQ(view.esize(), 6u);
392 EXPECT_FALSE(view.is_empty());
400 auto vit = view.get_node_it();
402 for (; vit.has_curr(); vit.next_ne(), git.next_ne(), ++n)
404 ASSERT_TRUE(git.has_curr());
405 EXPECT_EQ(vit.get_curr(), git.
get_curr());
406 EXPECT_EQ(vit.get_curr_ne(), vit.get_current_node());
407 EXPECT_EQ(vit.get_current_node_ne(), vit.get_curr());
409 EXPECT_FALSE(git.has_curr());
411 EXPECT_THROW((
void) vit.get_curr(), std::overflow_error);
414 ASSERT_TRUE(vit.has_curr());
421 auto vit = view.get_arc_it();
423 for (; vit.has_curr(); vit.next_ne(), git.
next_ne(), ++n)
426 const Arc *a = vit.get_curr();
428 EXPECT_EQ(vit.get_current_arc(), a);
429 EXPECT_EQ(vit.get_current_arc_ne(), a);
432 EXPECT_EQ(vit.get_src_node_ne(), vit.get_src_node());
433 EXPECT_EQ(vit.get_tgt_node_ne(), vit.get_tgt_node());
442 for (
Node *p : s.nodes)
446 EXPECT_EQ(view.degree(cp), g.
degree(p));
449 auto vit = view.get_arc_it(cp);
451 for (; vit.has_curr(); vit.next_ne(), git.
next_ne(), ++n)
454 EXPECT_EQ(vit.get_curr(), git.
get_curr());
455 EXPECT_EQ(vit.get_current_arc(), git.
get_curr());
456 EXPECT_EQ(vit.get_current_arc_ne(), git.
get_curr());
459 EXPECT_EQ(view.get_connected_node(vit.get_curr(), cp),
463 EXPECT_EQ(view.arcs(cp).size(), n);
464 EXPECT_EQ(view.adjacent_nodes(cp).size(), n);
469 EXPECT_EQ(vit.has_curr(), n > 0);
473 EXPECT_EQ(view.search_arc(s.nodes[0], s.nodes[1]), s.arcs[0]);
474 EXPECT_EQ(view.search_arc(s.nodes[0], s.nodes[1]), g.
search_arc(s.nodes[0], s.nodes[1]));
475 EXPECT_EQ(view.search_arc(s.nodes[4], s.nodes[1]),
nullptr);
478TYPED_TEST(ConstGraphView, the_functional_members_read_the_graph)
488 view.for_each_node([&node_sum] (
const Node *p) { node_sum += p->get_info(); });
489 EXPECT_EQ(node_sum, 0 + 1 + 2 + 3 + 4);
492 view.for_each_arc([&arc_sum] (
const Arc *a) { arc_sum += a->get_info(); });
493 EXPECT_EQ(arc_sum, 10 + 20 + 30 + 40 + 50 + 60);
497 s.g.for_each_arc(s.nodes[2], [&of_node_2] (
Arc *a) { of_node_2 += a->get_info(); });
499 view.for_each_arc(s.nodes[2], [&seen] (
const Arc *a) { seen += a->get_info(); });
500 EXPECT_EQ(seen, of_node_2);
504 view.for_each_arc([] (
auto a)
506 static_assert(std::is_same_v<
decltype(a),
const Arc *>);
510 EXPECT_FALSE(view.traverse_nodes([&visited] (
const Node *) { return ++visited < 2; }));
511 EXPECT_EQ(visited, 2u);
512 EXPECT_TRUE(view.traverse_nodes([] (
const Node *) { return true; }));
513 EXPECT_TRUE(view.traverse_arcs([] (
const Arc *a) { return a->get_info() > 0; }));
514 EXPECT_FALSE(view.traverse_arcs([] (
const Arc *a) { return a->get_info() < 60; }));
515 EXPECT_TRUE(view.traverse_arcs(s.nodes[2], [] (
const Arc *a) { return a->get_info() > 0; }));
517 EXPECT_TRUE(view.all_nodes([] (
const Node *p) { return p->get_info() < 5; }));
518 EXPECT_FALSE(view.all_nodes([] (
const Node *p) { return p->get_info() < 4; }));
519 EXPECT_TRUE(view.all_arcs([] (
const Arc *a) { return a->get_info() % 10 == 0; }));
520 EXPECT_FALSE(view.all_arcs([] (
const Arc *a) { return a->get_info() > 10; }));
521 EXPECT_TRUE(view.all_arcs(s.nodes[1], [] (
const Arc *a) { return a->get_info() <= 20; }));
522 EXPECT_FALSE(view.all_arcs(s.nodes[2], [] (
const Arc *a) { return a->get_info() == 30; }));
524 EXPECT_TRUE(view.exists_node([] (
const Node *p) { return p->get_info() == 3; }));
525 EXPECT_FALSE(view.exists_node([] (
const Node *p) { return p->get_info() == 7; }));
526 EXPECT_TRUE(view.exists_arc([] (
const Arc *a) { return a->get_info() == 40; }));
527 EXPECT_FALSE(view.exists_arc([] (
const Arc *a) { return a->get_info() == 45; }));
528 EXPECT_TRUE(view.exists_arc(s.nodes[2], [] (
const Arc *a) { return a->get_info() == 40; }));
529 EXPECT_FALSE(view.exists_arc(s.nodes[2], [] (
const Arc *a) { return a->get_info() == 50; }));
531 EXPECT_EQ(view.search_node([] (
const Node *p) { return p->get_info() == 3; }), s.nodes[3]);
532 EXPECT_EQ(view.search_node([] (
const Node *p) { return p->get_info() == 7; }),
nullptr);
533 EXPECT_EQ(view.search_arc([] (
const Arc *a) { return a->get_info() == 40; }), s.arcs[3]);
534 EXPECT_EQ(view.search_arc([] (
const Arc *a) { return a->get_info() == 45; }),
nullptr);
535 EXPECT_EQ(view.search_arc(s.nodes[2], [] (
const Arc *a) { return a->get_info() == 40; }), s.arcs[3]);
536 EXPECT_EQ(view.search_arc(s.nodes[2], [] (
const Arc *a) { return a->get_info() == 50; }),
nullptr);
538 EXPECT_EQ(view.find_node(4), s.nodes[4]);
539 EXPECT_EQ(view.find_node(9),
nullptr);
540 EXPECT_EQ(view.find_arc(60), s.arcs[5]);
541 EXPECT_EQ(view.find_arc(65),
nullptr);
543 EXPECT_EQ(view.count_nodes([] (
const Node *p) { return p->get_info() % 2 == 0; }), 3u);
544 EXPECT_EQ(view.count_arcs([] (
const Arc *a) { return a->get_info() >= 30; }), 4u);
546 EXPECT_EQ(view.nodes().size(), 5u);
547 EXPECT_EQ(view.arcs().size(), 6u);
548 EXPECT_EQ(view.filter_nodes([] (
const Node *p) { return p->get_info() % 2 == 0; }).size(), 3u);
549 EXPECT_EQ(view.filter_arcs([] (
const Arc *a) { return a->get_info() >= 30; }).size(), 4u);
550 EXPECT_EQ(view.filter_arcs(s.nodes[3], [] (
const Arc *a) { return a->get_info() == 50; }).size(), 1u);
553 view.nodes_map([] (
const Node *p) {
return 2 * p->get_info(); }).
for_each([&mapped] (
int x) { mapped += x; });
554 EXPECT_EQ(mapped, 20);
555 const auto halves = view.template arcs_map<double>([] (
const Arc *a) {
return a->get_info() / 20.0; });
556 static_assert(std::is_same_v<
decltype(halves),
const DynList<double>>);
558 halves.for_each([&half_sum] (
double x) { half_sum += x; });
559 EXPECT_DOUBLE_EQ(half_sum, 10.5);
561 EXPECT_EQ(view.foldl_nodes(100, [] (
const int & acc,
const Node *p) { return acc + p->get_info(); }), 110);
562 EXPECT_EQ(view.template foldl_arcs<long>(0, [] (
const long & acc,
const Arc *a) { return acc + a->get_info(); }),
566TYPED_TEST(ConstGraphView, a_view_is_not_a_snapshot_and_copies_share_the_graph)
578 s.g.for_each_arc([] (
Arc *a) { a->get_info() += 1; });
579 auto *extra = s.g.insert_node(99);
580 s.g.insert_arc(s.nodes[4], extra, 71);
582 for (
const auto & v : {view,
copy, deduced})
584 EXPECT_EQ(v.get_num_nodes(), 6u);
585 EXPECT_EQ(v.get_num_arcs(), 7u);
586 EXPECT_EQ(v.find_node(99), extra);
587 EXPECT_EQ(v.template foldl_arcs<long>(0, [] (
const long & acc,
const Arc *a) { return acc + a->get_info(); }),
588 11 + 21 + 31 + 41 + 51 + 61 + 71);
601 EXPECT_TRUE(view.is_empty());
602 EXPECT_EQ(view.get_num_nodes(), 0u);
603 EXPECT_EQ(view.get_num_arcs(), 0u);
604 EXPECT_FALSE(view.get_node_it().has_curr());
605 EXPECT_FALSE(view.get_arc_it().has_curr());
606 EXPECT_TRUE(view.nodes().is_empty());
607 EXPECT_TRUE(view.arcs().is_empty());
608 EXPECT_EQ(view.search_node([] (
const Node *) { return true; }),
nullptr);
609 EXPECT_EQ(view.search_arc([] (
const Arc *) { return true; }),
nullptr);
610 EXPECT_TRUE(view.all_nodes([] (
const Node *) { return false; }));
611 EXPECT_FALSE(view.exists_arc([] (
const Arc *) { return true; }));
612 EXPECT_EQ(view.count_nodes([] (
const Node *) { return true; }), 0u);
613 EXPECT_EQ(view.foldl_nodes(7, [] (
const int & acc,
const Node *) { return acc + 1; }), 7);
618TYPED_TEST(ConstGraphView, the_entry_points_of_the_graph_agree_with_the_view)
624 static_assert(std::is_same_v<decltype(std::declval<const G &>().get_node()),
Node *>);
625 static_assert(std::is_same_v<decltype(std::declval<const G &>().get_arc()),
Arc *>);
626 static_assert(std::is_same_v<decltype(std::declval<const G &>().get_arc(
nullptr)),
Arc *>);
627 static_assert(std::is_same_v<decltype(std::declval<const G &>().get_first_arc(
nullptr)),
Arc *>);
635 EXPECT_EQ(view.get_first_node(), g.
get_node());
636 EXPECT_EQ(view.get_first_arc(), g.
get_arc());
639 Node *p = s.nodes[0];
641 EXPECT_EQ(view.get_first_arc(p), g.
get_arc(p));
645 Node *lonely = s.g.insert_node(77);
646 EXPECT_THROW((
void) g.
get_first_arc(lonely), std::range_error);
647 EXPECT_THROW((
void) g.
get_arc(lonely), std::range_error);
648 EXPECT_THROW((
void) view.get_first_arc(lonely), std::range_error);
651TYPED_TEST(ConstGraphView, the_filtered_iterators_and_the_generic_functions_read_a_view)
659 const View view(s.g);
663 static_assert(std::is_same_v<decltype(std::declval<Filtered_Arcs &>().get_curr()),
const Arc *>);
664 static_assert(not writes_the_information_through<Filtered_Arcs>);
665 static_assert(not removes_through<Filtered_Arcs>);
668 for (Filtered_Arcs it(view); it.has_curr(); it.next())
669 filtered += it.get_curr()->get_info();
670 EXPECT_EQ(filtered, 30 + 40 + 50 + 60);
673 static_assert(std::is_same_v<decltype(std::declval<Filtered_Nodes &>().get_curr()),
const Node *>);
674 static_assert(not writes_the_information_through<Filtered_Nodes>);
677 for (Filtered_Nodes it(view); it.has_curr(); it.next())
678 even += it.get_curr()->get_info();
679 EXPECT_EQ(even, 0 + 2 + 4);
683 static_assert(std::is_same_v<decltype(std::declval<Filtered_Node_Arcs &>().get_curr()),
const Arc *>);
684 static_assert(not writes_the_information_through<Filtered_Node_Arcs>);
688 expected += it.get_curr()->get_info();
690 const Node *n2 = s.nodes[2];
691 for (Filtered_Node_Arcs it(n2); it.has_curr(); it.next())
692 of_node += it.get_curr()->get_info();
694 EXPECT_GT(of_node, 0);
697 auto cheap = [] (
const Arc *a) {
return a->get_info() < 30; };
698 Filter_Iterator<View,
typename View::Arc_Iterator,
decltype(cheap)> fit(view, cheap);
699 static_assert(std::is_same_v<
decltype(fit.get_curr()),
const Arc *>);
701 for (; fit.has_curr(); fit.next())
702 cheap_sum += fit.get_curr()->get_info();
703 EXPECT_EQ(cheap_sum, 10 + 20);
708 EXPECT_EQ(total, 210);
712 EXPECT_EQ(nodes_total, 10);
714 long through_filter = 0;
717 EXPECT_EQ(through_filter, 180);
731 using Distance_Type = long;
733 long operator () (Market_View::Arc *a)
const noexcept {
return a->get_info(); }
740 Market::Node *b = g.insert_node(1);
741 Market::Node *c = g.insert_node(2);
742 Market::Node *d = g.insert_node(3);
744 explicit Market_Sample(
const long closing)
746 g.insert_arc(a, b, 2);
747 g.insert_arc(b, c, -5);
748 g.insert_arc(c, a, closing);
749 g.insert_arc(a, c, 10);
750 g.insert_arc(c, d, 4);
756 return view.arcs_map([] (Market_View::Arc *a) {
return a->get_info(); });
761 return view.nodes_map([] (Market_View::Node *p) {
return p->get_info(); });
769 for (; it1.has_curr()
and it2.has_curr(); it1.next_ne(), it2.next_ne())
770 if (not (it1.get_curr() == it2.get_curr()))
772 return not it1.has_curr()
and not it2.has_curr();
776TEST(ConstGraphViewAlgorithms, the_algorithms_that_keep_bookkeeping_run_on_the_graph_and_preserve_it)
778 Market_Sample negative(1);
779 const Market_View view(negative.g);
780 const auto arcs_before = arc_information(view);
781 const auto nodes_before = node_information(view);
786 static_assert(std::is_constructible_v<Bellman_Ford<Market>, Market &>);
787 static_assert(not std::is_constructible_v<Bellman_Ford<Market>,
const Market &>);
789 EXPECT_TRUE(same_items(arc_information(view), arcs_before));
790 EXPECT_TRUE(same_items(node_information(view), nodes_before));
791 EXPECT_EQ(view.get_num_nodes(), 4u);
792 EXPECT_EQ(view.get_num_arcs(), 5u);
794 Market_Sample positive(8);
795 const Market_View positive_view(positive.g);
796 const auto positive_before = arc_information(positive_view);
798 EXPECT_TRUE(same_items(arc_information(positive_view), positive_before));
801TEST(ConstGraphViewAlgorithms, dijkstra_runs_on_the_graph_and_preserves_the_information)
804 auto *a = g.insert_node(0);
805 auto *b = g.insert_node(1);
806 auto *c = g.insert_node(2);
807 auto *d = g.insert_node(3);
808 g.insert_arc(a, b, 1);
809 g.insert_arc(b, c, 2);
810 g.insert_arc(a, c, 7);
811 g.insert_arc(c, d, 3);
812 g.insert_arc(a, d, 9);
814 const Market_View view(g);
815 const auto arcs_before = arc_information(view);
816 const auto nodes_before = node_information(view);
820 EXPECT_EQ(path.
size(), 4u);
822 EXPECT_TRUE(same_items(arc_information(view), arcs_before));
823 EXPECT_TRUE(same_items(node_information(view), nodes_before));
826TEST(ConstGraphViewAlgorithms, the_algorithms_without_bookkeeping_run_on_a_view)
828 Market_Sample negative(1);
829 const Market_View view(negative.g);
830 const auto arcs_before = arc_information(view);
832 static_assert(ArcDistance<Read_Distance, Market_View>);
841 ASSERT_TRUE(on_graph.has_cycle);
842 ASSERT_TRUE(on_view.has_cycle);
843 EXPECT_EQ(on_view.total_cost, -2);
844 EXPECT_EQ(on_view.total_cost, on_graph.total_cost);
845 EXPECT_EQ(on_view.cycle_arcs.size(), 3u);
846 EXPECT_EQ(on_view.cycle_arcs.size(), on_graph.cycle_arcs.size());
849 on_view.cycle_arcs.for_each([&recomputed] (
const Market::Arc *a) { recomputed += a->get_info(); });
850 EXPECT_EQ(recomputed, -2);
854 ASSERT_EQ(cycles.size(), 1u);
855 EXPECT_EQ(cycles.get_first().total_cost, -2);
856 EXPECT_EQ(cycles.get_first().length, 3u);
858 EXPECT_TRUE(same_items(arc_information(view), arcs_before));
862 Market_Sample positive(8);
863 const auto positive_view =
const_view(positive.g);
865 ASSERT_TRUE(cheapest.has_cycle);
866 EXPECT_EQ(cheapest.total_cost, 5);
869 Read_Distance()).is_empty());
Bellman-Ford algorithm for single-source shortest paths.
Dijkstra's shortest path algorithm.
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
C++20 concepts for the protocol shared by graph algorithms.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
Bellman-Ford algorithm for shortest paths with negative weights.
bool has_negative_cycle(Node *start)
Test if a negative cycle exists starting from a specific node.
Read-only iterator on the arcs of a graph.
Strict read-only view of a graph.
Read-only iterator on the arcs of a node.
Read-only iterator on the nodes of a graph.
Generic directed graph (digraph) wrapper template.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
void next_ne() noexcept
Move the iterator one position backward guaranteeing no exception.
bool has_curr() const noexcept
Return true if the iterator has current item.
Doubly-linked list (defined in tpl_dynList.H).
Arc for graphs implemented with simple adjacency lists.
Iterator on the arcs of a graph.
Arc * get_curr() const
See get_current_arc().
Node * get_tgt_node() const
Return the connected node to source node (src passed in construction time) through the current arc.
Graph implemented with double-linked adjacency lists.
Node * get_first_node() const
Return any node in the graph.
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
_Graph_Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Number Theoretic Transform over Z / MOD Z.
Filtered iterator on the nodes of a graph.
size_t size() const noexcept
Return the path length in nodes.
Minimal std::expected-style result type for C++20.
void for_each_arc(Operation &op) const
Unconditionally traverse all the arcs of graph and on each one perform an operation.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_node() const
Return any node in the graph.
bool traverse_nodes(Operation &op) const
Conditioned traversal of all the nodes of a graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
Arc * get_arc() const
Return any arc in the graph.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
size_t degree(Node *p) const noexcept
Return the total of arcs (or degree) of a node.
Arc * search_arc(Op &op) const
Linear search of an arc.
auto filter_arcs(Op &op) const
Filter the arcs of graph satisfying a condition.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Node * search_node(Op &op) const
Linear search of a node.
bool traverse_arcs(Operation &op) const
Conditioned traversal of all the arcs of a graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Concept for basic graph iterators.
Concept for graph arc iterators.
Concept for graph node iterators.
Concept for node adjacency iterators.
Concept for resettable graph iterators.
TYPED_TEST(ConstGraphView, the_iterators_hand_out_pointers_to_const)
Generic filter iterator wrapper for Aleph containers.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Const_Graph_View< GT > const_view(const GT &g) noexcept
Strict read-only view of a graph.
DynList< Negative_Cycle_Item< GT, typename Distance::Distance_Type > > find_disjoint_negative_cycles(const GT &g, const size_t max_cycles, const Negative_Cycle_Exclusion exclusion=Negative_Cycle_Exclusion::Min_Weight_Arc, Distance distance=Distance(), SA sa=SA())
Find up to max_cycles negative cycles, excluding arcs of each cycle found before searching for the ne...
void for_each_node(const GT &g, Op operation, SN sn=SN())
Traverse all the nodes of graph filtering some ones according to a condition and executing an operati...
#define ARC_COOKIE(p)
Return the arc cookie
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
#define NODE_COUNTER(p)
Get the counter of a node.
#define ARC_COUNTER(p)
Return the counter of arc p.
bool forall_node(const GT &g, Op cond, SN sn=SN())
Return true if condition cond is met on every filtered node of the graph.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
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...
#define NODE_BITS(p)
Get the control bits of a node.
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 find_min_path(Mat &p, long src_idx, long tgt_idx, Path< typename Mat::Graph_Type > &path)
Build a minimum path from Floyd-Warshall path matrix using indices.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
Iterator on all arcs of a graph.
Node * get_tgt_node() const
Return the target node of current arc.
Node * get_src_node() const
Return the source node of the current arc.
Filtered iterator of adjacent arcs of a node.
Node * get_curr() const
Return the current node.
tuple< int, double > Sample
Array-based graph implementation.
Strict read-only access to a graph: Const_Graph_View and the read-only iterators on the nodes,...
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.