Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_graph.H
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
31
44# ifndef TPL_GRAPH_H
45# define TPL_GRAPH_H
46
47# include <memory>
48# include <cassert>
49# include <cstdint>
50# include <optional>
51# include <tuple>
52# include <utility>
53# include <functional>
54# include <bitArray.H>
55# include <tpl_dynArray.H>
56# include <tpl_sort_utils.H>
57# include <tpl_dynMapTree.H>
58# include <tpl_dynDlist.H>
59# include <tpl_treapRk.H>
60# include <filter_iterator.H>
61# include <aleph-graph.H>
62# include <graph-dry.H>
63# include <ah-graph-concepts.H>
64# include <ah-errors.H>
65# include <ah-iterator.H>
66# include <tpl_dfs_stack.H>
67
68using namespace Aleph;
69
70namespace Aleph
71{
72 template <typename Node_Info>
73 struct Graph_Node;
74
75 template <typename Arc_Info>
76 struct Graph_Arc;
77
78 class Arc_Node;
79
80 template <class GT>
81 class Path;
82
83 template <typename __Graph_Node, typename __Graph_Arc>
84 class List_Graph;
85
86 // List_Digraph is defined as a type alias later in this file
87 // using the generic Digraph<> template wrapper
88
89 template <class GT>
90 class Mat_Graph;
91
92 template <typename MT, typename Entry_Info, typename Copy>
93 class Ady_MaT;
94
122 template <typename __Node_Info = Empty_Class>
124 : public Dlink,
125 public GTNodeCommon<__Node_Info>
126 {
127 friend class GTNodeCommon<__Node_Info>;
128 friend class Arc_Node;
129
131 using Node_Info = __Node_Info;
132
142 Graph_Node(const Node_Info & info)
143 noexcept(std::is_nothrow_copy_constructible_v<Node_Info>)
144 : Base(info)
145 { /* empty */
146 }
147
158 noexcept(std::is_nothrow_move_constructible_v<Node_Info>)
159 : Base(std::move(info))
160 {
161 /* empty */
162 }
163
164 Graph_Node(const Graph_Node & node)
165 noexcept(std::is_nothrow_copy_constructible_v<Node_Info>)
166 : Graph_Node(node.node_info)
167 {
168 // empty
169 }
170
172 {
173 if (&node == this)
174 return *this;
175 this->node_info = node.node_info;
176 return *this;
177 }
178
194 : Base(node->get_info())
195 {
196 /* empty */
197 }
198
200 };
201
227 template <typename _Arc_Info = Empty_Class>
229 : public Dlink,
230 public GTArcCommon<_Arc_Info>
231 {
232 friend class GTArcCommon<_Arc_Info>;
233
235
236 using Arc_Info = _Arc_Info;
237
238 Arc_Node *src_arc_node = nullptr; // pointer to source node
239 Arc_Node *tgt_arc_node = nullptr; // pointer to target node
240
250 Graph_Arc(const Arc_Info & info)
251 noexcept(std::is_nothrow_copy_constructible_v<Arc_Info>)
252 : Base(info)
253 {
254 /* empty */
255 }
256
267 noexcept(std::is_nothrow_move_constructible_v<Arc_Info>)
268 : Base(std::move(info))
269 {
270 /* empty */
271 }
272
273 Graph_Arc(const Graph_Arc & arc)
274 : Graph_Arc(arc.arc_info)
275 {
276 /* empty */
277 }
278
280 {
281 if (&arc == this)
282 return *this;
283 this->arc_info = arc.arc_info;
284 return *this;
285 }
286 };
287
288 class Arc_Node : public Dlink
289 {
290 public:
291 void *arc = nullptr;
292
293 Arc_Node() noexcept : arc(nullptr) {}
294
295 Arc_Node(void *__arc) noexcept : arc(__arc) {}
296 };
297
434 template <typename _Graph_Node = Graph_Node<unsigned long>,
435 typename _Graph_Arc = Graph_Arc<unsigned long>>
437 : public GraphCommon<List_Graph<_Graph_Node, _Graph_Arc>,
438 _Graph_Node, _Graph_Arc>
439 {
440 public:
441 using GT = List_Graph;
442
443 using Node = _Graph_Node;
444 using Arc = _Graph_Arc;
445
447 using Node_Type = typename Node::Node_Type;
448
450 using Arc_Type = typename Arc::Arc_Type;
451
452 friend class GraphCommon<List_Graph<_Graph_Node, _Graph_Arc>,
453 _Graph_Node, _Graph_Arc>;
454
456 _Graph_Node, _Graph_Arc>;
457
460
461 private:
464
465 static Node * dlink_to_node(Dlink *p) noexcept
466 {
467 return static_cast<Node *>(p);
468 }
469
470 static Arc * dlink_to_arc(Dlink *p) noexcept
471 {
472 return static_cast<Arc *>(p);
473 }
474
475 static Arc_Node * dlink_to_arc_node(Dlink *p) noexcept
476 {
477 return static_cast<Arc_Node *>(p);
478 }
479
480 static Arc * void_to_arc(Arc_Node *arc_node) noexcept
481 {
482 return static_cast<Arc *>(arc_node->arc);
483 }
484
485 public:
498 Dlink &get_node_dlink() noexcept { return node_list; }
499
512 Dlink &get_arc_dlink() noexcept { return arc_list; }
535 virtual Node * insert_node(Node *node) noexcept
536 {
537 ++this->num_nodes;
538 node_list.append(node);
539 return node;
540 }
541
557 virtual void remove_node(Node *node)
558 {
559 assert(node != nullptr);
560 if (not this->digraph)
561 while (not node->arc_list.is_empty()) // remove each arc related to node
562 {
563 Arc_Node *arc_node = dlink_to_arc_node(node->arc_list.get_next());
564 Arc *arc = void_to_arc(arc_node);
565 remove_arc(arc);
566 }
567 else // Scan all the arcs and remove those related to node
568 this->remove_arcs_if([node, this](auto a)
569 {
570 return this->get_src_node(a) == node or this->get_tgt_node(a) == node;
571 });
572
573 // At this point the node has not more arcs
574 node->del(); // unlink it from arc_list
575 --this->num_nodes;
576 delete node;
577 }
578
591 {
592 ah_range_error_if(this->num_nodes == 0) << "Graph has not nodes";
593
594 return dlink_to_node(const_cast<Dlink &>(node_list).get_next());
595 }
596
609 Arc * get_first_arc(Node *node) const
610 {
611 ah_range_error_if(this->get_num_arcs(node) == 0) << "node has not arcs";
612
613 void *arc = dlink_to_arc_node(node->arc_list.get_next())->arc;
614 return reinterpret_cast<Arc *>(arc);
615 }
616
617 private:
618 Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
619 {
620 assert(src_node != nullptr and tgt_node != nullptr and a != nullptr);
621 Arc *arc = static_cast<Arc *>(a);
622 arc->src_node = src_node;
623 arc->tgt_node = tgt_node;
624
625 // step 1: allocate the Arc_Node of src_node, which a guard frees if
626 // the allocation of the one of tgt_node throws
627 auto src_guard = std::make_unique<Arc_Node>(arc);
628 Arc_Node *src_arc_node;
629
630 // step 2: if graph ==> allocate the Arc_Node of tgt_node, unless the
631 // arc is a loop, and insert it in the adjacency list of tgt_node.
632 // That allocation is the last thing that can throw: after it, the
633 // graph takes the Arc_Node of src_node out of its guard, and then
634 // links it, so that no guard is left owning a linked Arc_Node
635 if (not this->digraph) // if digraph ==> do not insert in another node
636 { // insertion in the target node
637 if (src_node == tgt_node) // is it a loop?
638 {
639 src_arc_node = src_guard.release();
640 arc->tgt_arc_node = src_arc_node;
641 }
642 else
643 {
644 Arc_Node *tgt_arc_node = new Arc_Node(arc);
645 src_arc_node = src_guard.release();
646
647 // insertion in an adjacency list of tgt_node
648 arc->tgt_arc_node = tgt_arc_node;
649 tgt_node->arc_list.append(tgt_arc_node);
650 ++tgt_node->num_arcs;
651 }
652 }
653 else
654 src_arc_node = src_guard.release();
655
656 // step 3: insertion in the adjacency list of src_node
657 arc->src_arc_node = src_arc_node;
658 src_node->arc_list.append(src_arc_node);
659 ++src_node->num_arcs;
660
661 arc_list.append(arc); // step 4: insert in graph arc list
662 ++this->num_arcs;
663
664 return arc;
665 }
666
667 public:
673 virtual void remove_arc(Arc *arc) noexcept
674 {
675 assert(arc != nullptr);
676 // step 1: remove Arc_node from src_node
677 Node *src_node = this->get_src_node(arc);
678 Arc_Node *src_arc_node = arc->src_arc_node;
679
680 src_arc_node->del(); // unlink src_node from node list
681 --src_node->num_arcs; // decrement arc counter of src_node
682 delete src_arc_node; // free memory
683
684 if (not this->digraph)
685 { // arc removal in target node
686 Node *tgt_node = this->get_tgt_node(arc);
687 if (src_node != tgt_node) // check for loop removal
688 { // step 2: remove Arc_node from tgt_node
689 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
690 tgt_arc_node->del();
691 --tgt_node->num_arcs;
692 delete tgt_arc_node;
693 }
694 }
695
696 // arc removal from graph
697 arc->del(); // unlink arc from the graph arc list
698 --this->num_arcs;
699 delete arc;
700 }
701
723 virtual void disconnect_arc(Arc *arc) noexcept
724 {
725 assert(arc != nullptr);
726 Node *src_node = this->get_src_node(arc);
727 Arc_Node *src_arc_node = arc->src_arc_node;
728 src_arc_node->del(); // unlink src_node from node list
729 --src_node->num_arcs; // decrement arc counter of src_node
730
731 if (not this->digraph)
732 { // arc removal in target node
733 Node *tgt_node = this->get_tgt_node(arc);
734 if (src_node != tgt_node) // check for loop removal
735 { // step 2: remove Arc_node from target node tgt_node
736 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
737 tgt_arc_node->del();
738 --tgt_node->num_arcs;
739 }
740 }
741
742 // arc removal from graph
743 arc->del(); // unlink arc from graph arc list
744 --this->num_arcs;
745 }
746
757 virtual Arc * connect_arc(Arc *arc) noexcept
758 {
759 assert(arc != nullptr);
760 Node *src_node = this->get_src_node(arc);
761 Node *tgt_node = this->get_tgt_node(arc);
762 Arc_Node *src_arc_node = arc->src_arc_node;
763 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
764
765 if (not this->digraph) // if digraph ==> no need to insert in other node
766 { // insertion in target node
767 if (src_node != tgt_node) // check if it is a loop
768 { // insertion in adjacency list of tgt_node
769 tgt_node->arc_list.append(tgt_arc_node);
770 ++tgt_node->num_arcs;
771 }
772 }
773
774 src_node->arc_list.append(src_arc_node);
775 ++src_node->num_arcs;
776 arc_list.append(arc);
777 ++this->num_arcs;
778
779 return arc;
780 }
781
794 {
795 ah_range_error_if(this->get_num_arcs() == 0) << "Graph has not arcs";
796
797 return dlink_to_arc(const_cast<Dlink &>(arc_list).get_next());
798 }
799
800 // sort_nodes() and sort_arcs() are inherited from GraphCommon via CRTP
801 // They use the get_node_dlink() and get_arc_dlink() accessors defined above
802
805
806
807 virtual ~List_Graph()
808 {
809 clear_graph(*this);
810 }
811
818 struct Node_Iterator : public GTNodeIterator<List_Graph>
819 {
821
824 : Base(const_cast<Dlink &>(g.node_list))
825 {
826 // empty
827 }
828 };
829
854 {
856
857 public:
858 using Item_Type = Arc *;
859
860 using Set_Type = Node *;
861
862 Node_Arc_Iterator() = default;
863
868 Node_Arc_Iterator(Node *src) noexcept
869 : Dlink::Iterator(&(src->arc_list)), src_node(src)
870 {
871 // empty
872 }
873
878
883
885 Arc * get_curr() const
886 {
887 return static_cast<Arc *>(get_current_arc_node()->arc);
888 }
889
891 Arc * get_curr_ne() const noexcept
892 {
893 return static_cast<Arc *>(get_current_arc_node_ne()->arc);
894 }
895
899 {
900 return get_curr();
901 }
902
903 Arc * get_current_arc_ne() const noexcept
904 {
905 return get_curr_ne();
906 }
907
912 {
913 return static_cast<Node *>(get_current_arc()->get_connected_node(src_node));
914 }
915
916 Node * get_tgt_node_ne() const noexcept
917 {
918 return static_cast<Node *>(get_current_arc_ne()->get_connected_node(src_node));
919 }
920
921 Node * get_node() const
922 {
923 return get_tgt_node();
924 }
925
926 Node * get_node_ne() const noexcept
927 {
928 return get_tgt_node_ne();
929 }
930 };
931
941 {
942 using Item_Type = Arc *;
943
945
946 Arc_Iterator() = default;
947
949 Arc_Iterator(const List_Graph & g) noexcept
950 : Dlink::Iterator(&const_cast<Dlink &>(g.arc_list))
951 {
952 // empty
953 }
954
956 Arc * get_current_arc_ne() const noexcept
957 {
958 return dlink_to_arc(const_cast<Dlink *>(Dlink::Iterator::get_curr_ne()));
959 }
960
962 {
963 return dlink_to_arc(const_cast<Dlink *>(Dlink::Iterator::get_curr()));
964 }
965
966 Arc * get_curr() const
967 {
968 return get_current_arc();
969 }
970
971 Arc * get_curr_ne() const noexcept
972 {
973 return get_current_arc_ne();
974 }
975
980 {
981 return static_cast<Node *>(get_current_arc()->src_node);
982 }
983
985 {
986 return static_cast<Node *>(get_current_arc_ne()->src_node);
987 }
988
993 {
994 return static_cast<Node *>(get_current_arc()->tgt_node);
995 }
996
998 {
999 return static_cast<Node *>(get_current_arc_ne()->tgt_node);
1000 }
1001 };
1002
1004 List_Graph() = default;
1005
1007 void swap(List_Graph & g) noexcept
1008 {
1009 this->common_swap(g);
1010 node_list.swap(&g.node_list);
1011 arc_list.swap(&g.arc_list);
1012 }
1013 };
1014
1022 template <class GT>
1024 {
1025 bool operator()(typename GT::Arc * /* arc */) const noexcept
1026 {
1027 return true;
1028 }
1029
1030 void set_cookie(void *) noexcept
1031 { /* empty */
1032 }
1033 };
1034
1042 template <class GT, class SA>
1043 inline constexpr bool nothrow_arc_filter_v =
1044 std::is_nothrow_copy_constructible_v<SA> and std::is_nothrow_move_constructible_v<SA>
1046
1150 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1152 public Filter_Iterator<typename GT::Node *,
1153 typename GT::Node_Arc_Iterator,
1154 Show_Arc>
1155 {
1157 typename GT::Node_Arc_Iterator,
1158 Show_Arc>;
1159
1160 using Itor = Filter_Iterator<typename GT::Node *,
1161 typename GT::Node_Arc_Iterator,
1162 Show_Arc>;
1163
1164 using Item_Type = typename Itor::Item_Type;
1165 using Set_Type = typename Itor::Set_Type;
1166
1168
1174 Node_Arc_Iterator(typename GT::Node *p, Show_Arc sa = Show_Arc())
1175 : Itor(p, sa)
1176 {
1177 // empty
1178 }
1179 };
1180
1197 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1199 public Filter_Iterator<GT, typename GT::Arc_Iterator, Show_Arc>
1200 {
1202
1203 using Item_Type = typename Itor::Item_Type;
1204 using Set_Type = typename Itor::Set_Type;
1205
1206 Arc_Iterator() = default;
1207
1213 Arc_Iterator(const GT & g, Show_Arc sa = Show_Arc())
1214 : Itor(g, sa)
1215 {
1216 // empty
1217 }
1218 };
1219
1226 template <class GT>
1228 {
1229 bool operator()(typename GT::Node *) const noexcept
1230 {
1231 return true;
1232 }
1233 };
1234
1247 template <class F>
1249 {
1250 std::optional<F> own; // the filter, if it was given as an rvalue
1251 F * filter; // the filter that is used
1252
1253 public:
1255 Kept_Filter(F & f) noexcept : filter(&f) {}
1256
1258 Kept_Filter(F && f) noexcept(std::is_nothrow_move_constructible_v<F>)
1259 : own(std::move(f)), filter(&*own) {}
1260
1263 noexcept(std::is_nothrow_copy_constructible_v<F>)
1264 : own(k.own), filter(own ? &*own : k.filter) {}
1265
1268 noexcept(std::is_nothrow_move_constructible_v<F>)
1269 : own(std::move(k.own)), filter(own ? &*own : k.filter) {}
1270
1272
1274 F & get() const noexcept { return *filter; }
1275
1277 operator F & () const noexcept { return *filter; }
1278 };
1279
1284 template <class GT, class Show_Node = Dft_Show_Node<GT>>
1286 public Filter_Iterator<GT, typename GT::Node_Iterator, Show_Node>
1287 {
1288 public:
1290
1291 using Item_Type = typename Itor::Item_Type;
1292
1293 using Set_Type = typename Itor::Set_Type;
1294
1295 Node_Iterator() = default;
1296
1302 Node_Iterator(const GT & g, Show_Node sn = Show_Node())
1303 : Itor(g, sn)
1304 {
1305 /* empty */
1306 }
1307 };
1308
1318 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1319 requires std::is_invocable_r_v<void, Op &, typename GT::Node *>
1320 void for_each_node(const GT & g,
1321 Op operation,
1322 SN sn = SN())
1323 {
1324 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1325 Aleph::concepts_detail::invoke_as<void (typename GT::Node *)>::call(operation, it.get_curr());
1326 }
1327
1337 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1338 requires std::is_invocable_r_v<void, Op &, typename GT::Arc *>
1339 void for_each_arc(const GT & g,
1340 Op operation,
1341 SA sa = SA())
1342 {
1343 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1344 Aleph::concepts_detail::invoke_as<void (typename GT::Arc *)>::call(operation, it.get_curr());
1345 }
1346
1357 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1358 requires std::is_invocable_r_v<void, Op &, typename GT::Arc *>
1359 void for_each_arc(const GT & g,
1360 typename GT::Node *p,
1361 Op operation,
1362 SA sa = SA())
1363 {
1364 (void) g;
1365 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1366 Aleph::concepts_detail::invoke_as<void (typename GT::Arc *)>::call(operation, it.get_curr());
1367 }
1368
1379 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1380 requires std::is_invocable_r_v<bool, Op &, typename GT::Node *>
1381 bool forall_node(const GT & g,
1382 Op cond,
1383 SN sn = SN())
1384 {
1385 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1386 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Node *)>::call(cond, it.get_curr()))
1387 return false;
1388 return true;
1389 }
1390
1401 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1402 requires std::is_invocable_r_v<bool, Op &, typename GT::Arc *>
1403 bool forall_arc(const GT & g,
1404 Op cond,
1405 SA sa = SA())
1406 {
1407 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1408 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Arc *)>::call(cond, it.get_curr()))
1409 return false;
1410 return true;
1411 }
1412
1423 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1424 requires std::is_invocable_r_v<bool, Op &, typename GT::Arc *>
1425 bool forall_arc(typename GT::Node *p,
1426 Op cond,
1427 SA sa = SA())
1428 {
1429 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1430 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Arc *)>::call(cond, it.get_curr()))
1431 return false;
1432 return true;
1433 }
1434
1449 template <AlephGraph GT, typename T,
1450 template<typename> class Container = DynList,
1451 NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1452 requires std::is_invocable_r_v<T, Op &, typename GT::Node *>
1454 Op transformation,
1455 SN sn = SN())
1456 {
1457 Container<T> ret_val;
1458 for_each_node<GT, SN>(g, [&ret_val, &transformation](typename GT::Node *p)
1459 {
1460 ret_val.append(Aleph::concepts_detail::invoke_as<T (typename GT::Node *)>::call(transformation, p));
1461 }, sn);
1462 return ret_val;
1463 }
1464
1475 template <AlephGraph GT, typename T,
1476 template<typename> class Container = DynList,
1477 NodeFilter<GT> SN = Dft_Show_Node<GT>>
1479 std::function<T (typename GT::Node *)> transformation,
1480 SN sn = SN())
1481 {
1482 // Forward through a lambda, not `transformation` itself: passing the
1483 // `std::function` as-is would make this call ambiguous between this
1484 // overload and the generic one (both exactly match a `std::function<T
1485 // (typename GT::Node *)>` argument once `Op` is deduced as that type).
1486 return nodes_map<GT, T, Container, SN>(
1487 g, [&transformation](typename GT::Node * p) { return transformation(p); }, sn);
1488 }
1489
1503 template <AlephGraph GT, typename T,
1504 template<typename> class Container = DynList,
1505 ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1506 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
1508 Op transformation,
1509 SA sa = SA())
1510 {
1511 Container<T> ret_val;
1512 for_each_arc<GT, SA>(g, [&ret_val, &transformation](typename GT::Arc *p)
1513 {
1514 ret_val.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(transformation, p));
1515 }, sa);
1516 return ret_val;
1517 }
1518
1527 template <AlephGraph GT, typename T,
1528 template<typename> class Container = DynList,
1529 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1531 std::function<T (typename GT::Arc *)> transformation,
1532 SA sa = SA())
1533 {
1534 // See the equivalent nodes_map overload for why this must go through a
1535 // lambda rather than forwarding `transformation` as-is.
1536 return arcs_map<GT, T, Container, SA>(
1537 g, [&transformation](typename GT::Arc * a) { return transformation(a); }, sa);
1538 }
1539
1560 template <AlephGraph GT, typename T,
1561 template<typename> class Container = DynList,
1562 ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1563 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
1565 typename GT::Node *p,
1566 Op transformation,
1567 SA sa = SA())
1568 {
1569 Container<T> ret_val;
1570 for_each_arc<GT, SA>(g, p, [&ret_val, &transformation](typename GT::Arc *p)
1571 {
1572 ret_val.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(transformation, p));
1573 }, sa);
1574 return ret_val;
1575 }
1576
1585 template <AlephGraph GT, typename T,
1586 template<typename> class Container = DynList,
1587 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1589 typename GT::Node *p,
1590 std::function<T (typename GT::Arc *)> transformation,
1591 SA sa = SA())
1592 {
1593 // See the equivalent nodes_map overload for why this must go through a
1594 // lambda rather than forwarding `transformation` as-is.
1595 return arcs_map<GT, T, Container, SA>(
1596 g, p, [&transformation](typename GT::Arc * a) { return transformation(a); }, sa);
1597 }
1598
1609 template <AlephGraph GT, typename T, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1610 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Node *>
1611 T foldl_nodes(GT & g, const T & init,
1612 Op operation,
1613 SN sn = SN())
1614 {
1615 T ret_val = init;
1616 for_each_node<GT, SN>(g, [&ret_val, &operation](typename GT::Node *p)
1617 {
1618 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Node *)>::call(operation, ret_val, p);
1619 }, sn);
1620 return ret_val;
1621 }
1622
1632 template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1633 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
1634 T foldl_arcs(GT & g, const T & init,
1635 Op operation,
1636 SA sa = SA())
1637 {
1638 T ret_val = init;
1639 for_each_arc<GT, SA>(g, [&ret_val, &operation](typename GT::Arc *a)
1640 {
1641 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(operation, ret_val, a);
1642 }, sa);
1643 return ret_val;
1644 }
1645
1656 template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1657 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
1658 T foldl_arcs(GT & g, typename GT::Node *p,
1659 const T & init,
1660 Op operation,
1661 SA sa = SA())
1662 {
1663 T ret_val = init;
1664 for_each_arc<GT, SA>(g, p, [&ret_val, &operation](typename GT::Arc *a)
1665 {
1666 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(operation, ret_val, a);
1667 }, sa);
1668 return ret_val;
1669 }
1670
1691 template <typename __Graph_Node = Graph_Node<int>,
1692 typename __Graph_Arc = Graph_Arc<int>>
1694
1695
1701 template <class GT>
1702 using ArcPair = std::tuple<typename GT::Arc *, typename GT::Node *>;
1703
1711 template <class GT>
1713 {
1714 typename GT::Node *src = nullptr;
1715
1716 public:
1717 __Out_Filt(typename GT::Node *__src) noexcept : src(__src)
1718 { /* empty */
1719 }
1720
1721 bool operator()(typename GT::Arc *a) const noexcept
1722 {
1723 assert(src);
1724 return a->src_node == src;
1725 }
1726
1727 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1728 {
1729 assert(src);
1730 return static_cast<typename GT::Node *>(a->tgt_node);
1731 }
1732 };
1733
1741 template <class GT>
1743 {
1744 typename GT::Node *tgt = nullptr;
1745
1746 public:
1747 __In_Filt(typename GT::Node *__tgt) noexcept : tgt(__tgt)
1748 { /* empty */
1749 }
1750
1751 bool operator()(typename GT::Arc *a) const noexcept
1752 {
1753 assert(tgt);
1754 return a->tgt_node == tgt;
1755 }
1756
1757 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1758 {
1759 assert(tgt);
1760 return static_cast<typename GT::Node *>(a->src_node);
1761 }
1762 };
1763
1768 template <class GT, class Filter>
1770 {
1771 using Itor = Filter_Iterator<typename GT::Node *,
1772 typename GT::Node_Arc_Iterator, Filter>;
1773
1774 Filter filt;
1776
1777 public:
1778 using Item_Type = typename Itor::Item_Type;
1779
1781
1785 static constexpr bool goes_back = Itor::goes_back;
1786
1789 static constexpr bool goes_to_last = Itor::goes_to_last;
1790
1794 static constexpr bool shows_ne = noexcept(std::declval<Itor &>().next_ne());
1795
1798 : filt(p), it(p, filt)
1799 {
1800 // empty
1801 }
1802
1805 void next()
1806 {
1807 it.next();
1808 }
1809
1812 void next_ne() noexcept(shows_ne)
1813 {
1814 it.next_ne();
1815 }
1816
1819 void prev() requires goes_back { it.prev(); }
1820
1822 [[nodiscard]] bool has_curr() const noexcept
1823 {
1824 return it.has_curr();
1825 }
1826
1829 typename GT::Arc * get_curr() const
1830 {
1831 return it.get_curr();
1832 }
1833
1836 typename GT::Arc * get_curr_ne() const noexcept
1837 {
1838 return it.get_curr_ne();
1839 }
1840
1843 auto get_current_arc() const
1844 {
1845 return get_curr();
1846 }
1847
1850 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1851 {
1852 return filt.get_node(a);
1853 }
1854
1856 typename GT::Node * get_node() const
1857 {
1858 return this->get_node(this->get_curr());
1859 }
1860
1863 auto get_tgt_node() const
1864 {
1865 return get_node();
1866 }
1867
1869 typename GT::Node * get_node_ne() const noexcept
1870 {
1871 return this->get_node(this->get_curr_ne());
1872 }
1873
1876 auto get_tgt_node_ne() const noexcept
1877 {
1878 return get_node_ne();
1879 }
1880
1882 void reset_first() noexcept(shows_ne)
1883 {
1884 it.reset_first();
1885 }
1886
1889 void reset_last() noexcept(shows_ne) requires goes_to_last
1890 {
1891 it.reset_last();
1892 }
1893
1897 void end() noexcept(shows_ne) requires goes_to_last
1898 {
1899 it.reset_last();
1900 if (it.has_curr())
1901 it.next_ne();
1902 }
1903 };
1904
1905
1910 template <class GT>
1912
1913
1927 template <class GT>
1928 requires (not DigraphType<GT>)
1930
1940 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1941 class Out_Iterator : public Digraph_Iterator<GT, __Out_Filt<GT>>
1942 {
1944 Show_Arc show_arc;
1945
1947 {
1948 while (Base::has_curr() and not static_cast<bool>(show_arc(Base::get_curr_ne())))
1949 Base::next_ne();
1950 }
1951
1953 {
1954 while (Base::has_curr() and not static_cast<bool>(show_arc(Base::get_curr_ne())))
1955 Base::prev();
1956 }
1957
1958 public:
1959 using Item_Type = typename GT::Arc *;
1960
1961 Out_Iterator() = default;
1962
1963 Out_Iterator(typename GT::Node *p, Show_Arc sa = Show_Arc())
1964 : Base(p), show_arc(sa)
1965 {
1967 }
1968
1969 void next()
1970 {
1971 Base::next();
1973 }
1974
1977 void next_ne() noexcept(NothrowPredicateWith<Show_Arc &, typename GT::Arc *>)
1978 {
1979 Base::next_ne();
1981 }
1982
1985 void prev() requires Base::goes_back
1986 {
1987 Base::prev();
1989 }
1990
1992 void reset_first()
1993 {
1996 }
1997
2001 void reset_last() requires Base::goes_to_last
2002 {
2005 }
2006 };
2007
2043 template <class GT, class SA = Dft_Show_Arc<GT>>
2044 class In_Iterator : public Digraph_Iterator<GT, __In_Filt<GT>>
2045 {
2048
2050 {
2051 while (Base::has_curr() and not static_cast<bool>(show_arc(Base::get_curr_ne())))
2052 Base::next_ne();
2053 }
2054
2056 {
2057 while (Base::has_curr() and not static_cast<bool>(show_arc(Base::get_curr_ne())))
2058 Base::prev();
2059 }
2060
2061 // Throws the domain_error of In_Iterator(g, p) on a graph of an
2062 // undirected type that says that it is directed; out of line, so that
2063 // the iterator on a graph that is not directed costs only the test
2064 [[gnu::noinline, gnu::cold]] static void refuse_a_digraph()
2065 {
2067 << "In_Iterator(g, p): the graph says that it is directed, but its type does not, "
2068 << "so that its arcs into p are not in the list of p: use in_arcs(g, p), or "
2069 << "In_Arcs_Index";
2070 }
2071
2072 // The node of a graph that is not directed, whose list holds the arcs
2073 // into it
2074 static typename GT::Node * of_lists(const GT & g, typename GT::Node *p)
2075 {
2076 if (g.is_digraph()) [[unlikely]]
2078 return p;
2079 }
2080
2081 public:
2082 using Item_Type = typename GT::Arc *;
2083
2084 In_Iterator() = default;
2085
2093 In_Iterator(typename GT::Node *p, SA sa = SA())
2094 : Base(p), show_arc(sa)
2095 {
2097 }
2098
2111 [[gnu::always_inline]] In_Iterator(const GT & g, typename GT::Node *p, SA sa = SA())
2112 : In_Iterator(of_lists(g, p), std::move(sa))
2113 {
2114 // empty
2115 }
2116
2117 void next()
2118 {
2119 Base::next();
2121 }
2122
2125 void next_ne() noexcept(NothrowPredicateWith<SA &, typename GT::Arc *>)
2126 {
2127 Base::next_ne();
2129 }
2130
2133 void prev() requires Base::goes_back
2134 {
2135 Base::prev();
2137 }
2138
2140 void reset_first()
2141 {
2144 }
2145
2148 void reset_last() requires Base::goes_to_last
2149 {
2152 }
2153 };
2154
2169 template <class GT, class SA>
2170 requires DigraphType<GT>
2172 {
2174
2175 public:
2176 using Base::Base;
2177 };
2178
2222 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2224 {
2225 using Node = typename GT::Node;
2226 using Arc = typename GT::Arc;
2227
2228 // The nodes in an open table, by their addresses, with linear probing,
2229 // and the number of each one; the table has 2^bits places, at least
2230 // twice the number of nodes
2231 unsigned bits = 1;
2232 Array<Node *> keys; // nullptr: an empty place
2234
2235 // The arcs into the node numbered i are arcs(first(i)) to
2236 // arcs(first(i + 1) - 1)
2239
2240 static unsigned table_bits(const size_t n) noexcept
2241 {
2242 unsigned b = 1;
2243 while ((size_t(1) << b) < 2 * n)
2244 ++b;
2245 return b;
2246 }
2247
2248 [[nodiscard]] size_t place_of(const Node *p) const noexcept
2249 {
2250 const auto key = std::uint64_t(reinterpret_cast<std::uintptr_t>(p));
2251 return size_t((key * 0x9E3779B97F4A7C15ULL) >> (64 - bits));
2252 }
2253
2254 [[nodiscard]] size_t mask() const noexcept { return (size_t(1) << bits) - 1; }
2255
2256 // The number of p, or the number of nodes if p is not of the graph
2257 [[nodiscard]] size_t number_of(const Node *p) const noexcept
2258 {
2259 for (size_t i = place_of(p); keys(i) != nullptr; i = (i + 1) & mask())
2260 if (keys(i) == p)
2261 return number(i);
2262 return first.size() - 1;
2263 }
2264
2265 [[nodiscard]] size_t checked_number_of(const Node *p) const
2266 {
2267 const size_t i = number_of(p);
2268 ah_domain_error_if(i == first.size() - 1)
2269 << "In_Arcs_Index: the node is not of the indexed graph";
2270 return i;
2271 }
2272
2273 public:
2279 {
2280 Arc * const *arcs = nullptr;
2281 long num = 0;
2282 long pos = 0; // the position of the current arc
2283
2284 public:
2286 using Item_Type = Arc *;
2287
2289 Iterator() noexcept = default;
2290
2292 Iterator(Arc * const *a, const long n) noexcept : arcs(a), num(n) {}
2293
2300 Iterator(const In_Arcs_Index & index, const Node *p) : Iterator(index.get_it(p)) {}
2301
2303 [[nodiscard]] bool has_curr() const noexcept { return pos >= 0 and pos < num; }
2304
2306 [[nodiscard]] Arc * get_curr_ne() const noexcept { return arcs[pos]; }
2307
2313 [[nodiscard]] Arc * get_curr() const
2314 {
2315 ah_underflow_error_if(pos < 0) << "In_Arcs_Index::Iterator: no current arc";
2316 ah_overflow_error_if(pos >= num) << "In_Arcs_Index::Iterator: no current arc";
2317 return get_curr_ne();
2318 }
2319
2321 [[nodiscard]] Arc * get_current_arc_ne() const noexcept { return get_curr_ne(); }
2322
2324 [[nodiscard]] Arc * get_current_arc() const { return get_curr(); }
2325
2327 [[nodiscard]] Node * get_node_ne() const noexcept
2328 {
2329 return static_cast<Node *>(get_curr_ne()->src_node);
2330 }
2331
2333 [[nodiscard]] Node * get_node() const
2334 {
2335 return static_cast<Node *>(get_curr()->src_node);
2336 }
2337
2339 void next_ne() noexcept { ++pos; }
2340
2345 void next()
2346 {
2347 ah_overflow_error_if(not has_curr()) << "In_Arcs_Index::Iterator: no current arc";
2348 next_ne();
2349 }
2350
2352 void prev_ne() noexcept { --pos; }
2353
2358 void prev()
2359 {
2360 ah_underflow_error_if(not has_curr()) << "In_Arcs_Index::Iterator: no current arc";
2361 prev_ne();
2362 }
2363
2365 void reset_first() noexcept { pos = 0; }
2366
2368 void reset_last() noexcept { pos = num - 1; }
2369
2371 void end() noexcept { pos = num; }
2372
2374 [[nodiscard]] size_t size() const noexcept { return size_t(num); }
2375 };
2376
2384 In_Arcs_Index(const GT & g, SA sa = SA())
2385 : bits(table_bits(g.get_num_nodes())),
2386 keys(size_t(1) << bits, nullptr),
2387 number(Array<size_t>::create(size_t(1) << bits)),
2388 first(g.get_num_nodes() + 1, 0),
2389 arcs(Array<Arc *>::create_reserved(g.get_num_arcs()))
2390 {
2391 size_t n = 0;
2392 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++n)
2393 {
2394 Node *p = it.get_curr_ne();
2395 size_t i = place_of(p);
2396 while (keys(i) != nullptr)
2397 i = (i + 1) & mask();
2398 keys(i) = p;
2399 number(i) = n;
2400 }
2401
2402 // The arcs that sa shows, in the order of the arcs of g, and the
2403 // number of the target of each; first(i) counts the arcs into i
2404 const size_t m = g.get_num_arcs();
2405 auto order = Array<Arc *>::create(m);
2406 auto target = Array<size_t>::create(m);
2407 size_t k = 0;
2408 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
2409 if (Arc *a = it.get_curr_ne(); sa(a))
2410 {
2411 const size_t t = number_of(static_cast<Node *>(a->tgt_node));
2412 assert(t < n);
2413 order(k) = a;
2414 target(k++) = t;
2415 ++first(t);
2416 }
2417
2418 // first(i) is now past the arcs into i, and goes back to the first
2419 // of them as they are put, from the last one, in their order
2420 for (size_t i = 1; i < first.size(); ++i)
2421 first(i) += first(i - 1);
2422 arcs.putn(k);
2423 for (size_t j = k; j-- > 0; )
2424 arcs(--first(target(j))) = order(j);
2425 }
2426
2432 In_Arcs_Index(const In_Arcs_Index & other) = default;
2433
2448 In_Arcs_Index(In_Arcs_Index && other) : keys(2, nullptr), first(1, 0)
2449 {
2450 swap(other);
2451 }
2452
2459 In_Arcs_Index & operator = (const In_Arcs_Index & other) = default;
2460
2468 {
2469 swap(other);
2470 return *this;
2471 }
2472
2477 void swap(In_Arcs_Index & other) noexcept
2478 {
2479 std::swap(bits, other.bits);
2480 keys.swap(other.keys);
2481 number.swap(other.number);
2482 first.swap(other.first);
2483 arcs.swap(other.arcs);
2484 }
2485
2488 [[nodiscard]] size_t size() const noexcept { return arcs.size(); }
2489
2495 [[nodiscard]] size_t in_degree(const Node *p) const
2496 {
2497 const size_t i = checked_number_of(p);
2498 return first(i + 1) - first(i);
2499 }
2500
2506 [[nodiscard]] Iterator get_it(const Node *p) const
2507 {
2508 const size_t i = checked_number_of(p);
2509 const size_t b = first(i), e = first(i + 1);
2510 return Iterator(b == e ? nullptr : &arcs.base() + b, long(e - b));
2511 }
2512 };
2513
2514 namespace in_arcs_detail
2515 {
2526 template <AlephGraph GT, class SA, class Op>
2527 [[gnu::noinline]] bool traverse_digraph_arcs_into(const GT & g, typename GT::Node *p,
2528 SA & sa, Op op)
2529 {
2530 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
2531 if (typename GT::Arc *a = it.get_curr_ne(); a->tgt_node == p and sa(a) and not op(a))
2532 return false;
2533 return true;
2534 }
2535
2540 template <AlephGraph GT, class SA>
2541 [[gnu::noinline]] size_t digraph_in_degree(const GT & g, typename GT::Node *p, SA & sa)
2542 {
2543 size_t count = 0;
2544 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
2545 if (typename GT::Arc *a = it.get_curr_ne(); a->tgt_node == p and sa(a))
2546 ++count;
2547 return count;
2548 }
2549
2558 template <AlephGraph GT, class SA, class Op>
2559 bool traverse_arcs_into(const GT & g, typename GT::Node *p, SA & sa, Op op)
2560 {
2561 if constexpr (not DigraphType<GT>)
2562 if (not g.is_digraph())
2563 {
2564 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2565 if (not op(it.get_curr_ne()))
2566 return false;
2567 return true;
2568 }
2569 return traverse_digraph_arcs_into(g, p, sa, op);
2570 }
2571 } // namespace in_arcs_detail
2572
2582 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2584 {
2586 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2587 ret.append(it.get_node_ne());
2588 return ret;
2589 }
2590
2605 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2607 {
2608 static_assert(not DigraphType<GT>,
2609 "in_nodes(p) does not see the arcs into p on a digraph, whose list of p "
2610 "holds only the arcs that leave p: use in_nodes(g, p)");
2612 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2613 ret.append(it.get_node_ne());
2614 return ret;
2615 }
2616
2632 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2633 DynList<typename GT::Node *> in_nodes(const GT & g, typename GT::Node *p, SA sa = SA())
2634 {
2636 in_arcs_detail::traverse_arcs_into(g, p, sa, [&ret] (typename GT::Arc *a)
2637 {
2638 ret.append(static_cast<typename GT::Node *>(a->src_node));
2639 return true;
2640 });
2641 return ret;
2642 }
2643
2651 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2653 {
2655 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2656 ret.append(it.get_curr());
2657 return ret;
2658 }
2659
2670 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2672 {
2673 static_assert(not DigraphType<GT>,
2674 "in_arcs(p) does not see the arcs into p on a digraph, whose list of p "
2675 "holds only the arcs that leave p: use in_arcs(g, p)");
2677 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2678 ret.append(it.get_curr());
2679 return ret;
2680 }
2681
2694 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2695 DynList<typename GT::Arc *> in_arcs(const GT & g, typename GT::Node *p, SA sa = SA())
2696 {
2698 in_arcs_detail::traverse_arcs_into(g, p, sa, [&ret] (typename GT::Arc *a)
2699 {
2700 ret.append(a);
2701 return true;
2702 });
2703 return ret;
2704 }
2705
2712 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2713 DynList<typename GT::Arc *> arcs(typename GT::Node *p, SA sa = SA())
2714 {
2716 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2717 ret.append(it.get_curr());
2718 return ret;
2719 }
2720
2730 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2731 DynList<ArcPair<GT>> in_pairs(typename GT::Node *p, SA sa = SA())
2732 {
2733 static_assert(not DigraphType<GT>,
2734 "in_pairs(p) does not see the arcs into p on a digraph, whose list of p "
2735 "holds only the arcs that leave p: use in_pairs(g, p)");
2737 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2738 {
2739 typename GT::Arc *a = it.get_curr();
2740 ret.append(std::make_tuple(a, static_cast<typename GT::Node *>(a->get_connected_node(p))));
2741 }
2742 return ret;
2743 }
2744
2758 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2759 DynList<ArcPair<GT>> in_pairs(const GT & g, typename GT::Node *p, SA sa = SA())
2760 {
2762 in_arcs_detail::traverse_arcs_into(g, p, sa, [&ret, p] (typename GT::Arc *a)
2763 {
2764 ret.append(std::make_tuple(a, static_cast<typename GT::Node *>(a->get_connected_node(p))));
2765 return true;
2766 });
2767 return ret;
2768 }
2769
2777 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2778 DynList<ArcPair<GT>> out_pairs(typename GT::Node *p, SA sa = SA())
2779 {
2781 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2782 {
2783 typename GT::Arc *a = it.get_curr();
2784 ret.append(std::make_tuple(a, static_cast<typename GT::Node *>(a->get_connected_node(p))));
2785 }
2786 return ret;
2787 }
2788
2805 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2806 size_t in_degree(typename GT::Node *p, SA sa = SA())
2807 {
2808 static_assert(not DigraphType<GT>,
2809 "in_degree(p) does not see the arcs into p on a digraph, whose list of p "
2810 "holds only the arcs that leave p: use in_degree(g, p), or In_Arcs_Index "
2811 "for many nodes");
2812 size_t count = 0;
2813 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2814 ++count;
2815 return count;
2816 }
2817
2833 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2834 size_t in_degree(const GT & g, typename GT::Node *p, SA sa = SA())
2835 {
2836 if constexpr (not DigraphType<GT>)
2837 if (not g.is_digraph())
2838 {
2839 size_t count = 0;
2840 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2841 ++count;
2842 return count;
2843 }
2844 return in_arcs_detail::digraph_in_degree(g, p, sa);
2845 }
2846
2858 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2859 size_t out_degree(typename GT::Node *p, SA sa = SA())
2860 {
2861 size_t count = 0;
2862 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2863 ++count;
2864 return count;
2865 }
2866
2889 template <AlephGraph GT, class Itor, class Operation>
2890 inline
2891 bool traverse_arcs(typename GT::Node *p, Operation op = Operation())
2892 {
2893 for (Itor it(p); it.has_curr(); it.next_ne())
2894 if (not op(it.get_curr()))
2895 return false;
2896 return true;
2897 }
2898
2916 template <AlephGraph GT, class Itor, class Operation>
2917 inline
2918 void for_each_arc(typename GT::Node *p, Operation op = Operation())
2919 {
2920 for (Itor it(p); it.has_curr(); it.next_ne())
2921 op(it.get_curr());
2922 }
2923
2924
2925 // Functional operations on input arcs
2926
2927 // The forms that take only the node read the arcs into it from its
2928 // list, which holds them only in a graph that is not directed: they do
2929 // not compile on a digraph type, and they cannot tell a graph of an
2930 // undirected type that says that it is directed. The forms that take the
2931 // graph tell a digraph at run time, and look for its arcs into the node
2932 // among the arcs of the graph, in O(E).
2933
2945 template <AlephGraph GT, class Op>
2946 inline
2947 bool traverse_in_arcs(typename GT::Node *p, Op op = Op())
2948 {
2949 static_assert(not DigraphType<GT>,
2950 "traverse_in_arcs(p, op) does not see the arcs into p on a digraph, whose list "
2951 "of p holds only the arcs that leave p: use traverse_in_arcs(g, p, op)");
2952 return traverse_arcs<GT, _In_Iterator<GT>, Op>(p, op);
2953 }
2954
2968 template <AlephGraph GT, class Op>
2969 inline
2970 bool traverse_in_arcs(const GT & g, typename GT::Node *p, Op op = Op())
2971 {
2973 return in_arcs_detail::traverse_arcs_into(g, p, all, [&op] (typename GT::Arc *a) -> bool
2974 {
2975 return op(a);
2976 });
2977 }
2978
2987 template <AlephGraph GT, class Op>
2988 inline
2989 void for_each_in_arc(typename GT::Node *p, Op op = Op())
2990 {
2991 static_assert(not DigraphType<GT>,
2992 "for_each_in_arc(p, op) does not see the arcs into p on a digraph, whose list "
2993 "of p holds only the arcs that leave p: use for_each_in_arc(g, p, op)");
2994 for_each_arc<GT, _In_Iterator<GT>, Op>(p, op);
2995 }
2996
3006 template <AlephGraph GT, class Op>
3007 inline
3008 void for_each_in_arc(const GT & g, typename GT::Node *p, Op op = Op())
3009 {
3011 in_arcs_detail::traverse_arcs_into(g, p, all, [&op] (typename GT::Arc *a)
3012 {
3013 op(a);
3014 return true;
3015 });
3016 }
3017
3030 template <AlephGraph GT, class Op>
3031 inline
3032 bool all_in_arc(typename GT::Node *p, Op op = Op())
3033 {
3034 static_assert(not DigraphType<GT>,
3035 "all_in_arc(p, op) does not see the arcs into p on a digraph, whose list of p "
3036 "holds only the arcs that leave p: use all_in_arc(g, p, op)");
3037 return traverse_in_arcs<GT>(p, [&op](auto a)
3038 {
3039 return op(a);
3040 });
3041 }
3042
3051 template <AlephGraph GT, class Op>
3052 inline
3053 bool all_in_arc(const GT & g, typename GT::Node *p, Op op = Op())
3054 {
3055 return traverse_in_arcs(g, p, [&op](auto a)
3056 {
3057 return op(a);
3058 });
3059 }
3060
3073 template <AlephGraph GT, class Op>
3074 inline
3075 bool exists_in_arc(typename GT::Node *p, Op op = Op())
3076 {
3077 static_assert(not DigraphType<GT>,
3078 "exists_in_arc(p, op) does not see the arcs into p on a digraph, whose list of "
3079 "p holds only the arcs that leave p: use exists_in_arc(g, p, op)");
3080 return not traverse_in_arcs<GT>(p, [&op](auto a)
3081 {
3082 return not op(a);
3083 });
3084 }
3085
3094 template <AlephGraph GT, class Op>
3095 inline
3096 bool exists_in_arc(const GT & g, typename GT::Node *p, Op op = Op())
3097 {
3098 return not traverse_in_arcs(g, p, [&op](auto a)
3099 {
3100 return not op(a);
3101 });
3102 }
3103
3117 template <AlephGraph GT, class Op>
3118 inline
3119 auto search_in_arc(typename GT::Node *p, Op op = Op())
3120 {
3121 static_assert(not DigraphType<GT>,
3122 "search_in_arc(p, op) does not see the arcs into p on a digraph, whose list of "
3123 "p holds only the arcs that leave p: use search_in_arc(g, p, op)");
3124 typename GT::Arc *ret = nullptr;
3125 traverse_in_arcs<GT>(p, [&op, &ret](auto a)
3126 {
3127 if (op(a))
3128 {
3129 ret = a;
3130 return false;
3131 }
3132 return true;
3133 });
3134 return ret;
3135 }
3136
3145 template <AlephGraph GT, class Op>
3146 inline
3147 auto search_in_arc(const GT & g, typename GT::Node *p, Op op = Op())
3148 {
3149 typename GT::Arc *ret = nullptr;
3150 traverse_in_arcs(g, p, [&op, &ret](auto a)
3151 {
3152 if (op(a))
3153 {
3154 ret = a;
3155 return false;
3156 }
3157 return true;
3158 });
3159 return ret;
3160 }
3161
3176 // An actual std::function<T (Arc *)> goes to the overload below: with T
3177 // given, the two took it, and the call was ambiguous, since GT, only in
3178 // the types of the node and of the arc, does not order them
3179 template <AlephGraph GT, typename T, class Op>
3180 requires (std::is_invocable_r_v<T, Op &, typename GT::Arc *>
3181 and not std::is_same_v<Op, std::function<T (typename GT::Arc *)>>)
3182 inline
3183 auto map_in_arcs(typename GT::Node *p, Op op)
3184 {
3185 static_assert(not DigraphType<GT>,
3186 "map_in_arcs(p, op) does not see the arcs into p on a digraph, whose list of p "
3187 "holds only the arcs that leave p: use map_in_arcs(g, p, op)");
3188 DynList<T> ret;
3189 for_each_in_arc<GT>(p, [&ret, &op](auto a)
3190 {
3191 ret.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(op, a));
3192 });
3193 return ret;
3194 }
3195
3207 template <AlephGraph GT, typename T,
3208 class Op = std::function<T (typename GT::Arc *)>>
3209 requires std::is_same_v<Op, std::function<T (typename GT::Arc *)>>
3210 inline
3211 auto map_in_arcs(typename GT::Node *p, std::function<T (typename GT::Arc *)> op)
3212 {
3213 // See the equivalent nodes_map overload for why this must go through a
3214 // lambda rather than forwarding `op` as-is.
3215 return map_in_arcs<GT, T>(p, [&op](typename GT::Arc * a) { return op(a); });
3216 }
3217
3226 template <AlephGraph GT, typename T, class Op>
3227 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
3228 inline
3229 auto map_in_arcs(const GT & g, typename GT::Node *p, Op op)
3230 {
3231 using Call = Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>;
3232 DynList<T> ret;
3233 for_each_in_arc(g, p, [&ret, &op](auto a)
3234 {
3235 ret.append(Call::call(op, a));
3236 });
3237 return ret;
3238 }
3239
3247 template <AlephGraph GT, typename T>
3248 inline
3249 auto map_in_arcs(const GT & g, typename GT::Node *p, std::function<T (typename GT::Arc *)> op)
3250 {
3251 return map_in_arcs<GT, T>(g, p, [&op](typename GT::Arc * a) { return op(a); });
3252 }
3253
3272 template <AlephGraph GT, typename T, class Op>
3273 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
3274 inline
3275 T foldl_in_arcs(typename GT::Node *p, const T & init,
3276 Op op)
3277 {
3278 static_assert(not DigraphType<GT>,
3279 "foldl_in_arcs(p, init, op) does not see the arcs into p on a digraph, whose "
3280 "list of p holds only the arcs that leave p: use foldl_in_arcs(g, p, init, op)");
3281 T ret = init;
3282 for_each_in_arc<GT>(p, [&ret, &op](auto a)
3283 {
3284 ret = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(op, ret, a);
3285 });
3286 return ret;
3287 }
3288
3299 template <AlephGraph GT, typename T, class Op>
3300 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
3301 inline
3302 T foldl_in_arcs(const GT & g, typename GT::Node *p, const T & init, Op op)
3303 {
3304 using Call = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>;
3305 T ret = init;
3306 for_each_in_arc(g, p, [&ret, &op](auto a)
3307 {
3308 ret = Call::call(op, ret, a);
3309 });
3310 return ret;
3311 }
3312
3322 template <AlephGraph GT, class Op>
3323 inline
3325 {
3326 static_assert(not DigraphType<GT>,
3327 "filter_in_arcs(p, cond) does not see the arcs into p on a digraph, whose list "
3328 "of p holds only the arcs that leave p: use filter_in_arcs(g, p, cond)");
3330 for_each_in_arc<GT>(p, [&ret, &cond](auto a)
3331 {
3332 if (cond(a))
3333 ret.append(a);
3334 });
3335 return ret;
3336 }
3337
3346 template <AlephGraph GT, class Op>
3347 inline
3349 {
3351 for_each_in_arc(g, p, [&ret, &cond](auto a)
3352 {
3353 if (cond(a))
3354 ret.append(a);
3355 });
3356 return ret;
3357 }
3358
3359
3360 // Functional operation on output arcs
3361
3371 template <AlephGraph GT, class Op>
3372 inline
3373 bool traverse_out_arcs(typename GT::Node *p, Op op = Op())
3374 {
3375 return traverse_arcs<GT, _Out_Iterator<GT>, Op>(p, op);
3376 }
3377
3384 template <AlephGraph GT, class Op>
3385 inline
3386 void for_each_out_arc(typename GT::Node *p, Op op = Op())
3387 {
3388 for_each_arc<GT, _Out_Iterator<GT>, Op>(p, op);
3389 }
3390
3401 template <AlephGraph GT, class Op>
3402 inline
3403 bool all_out_arc(typename GT::Node *p, Op op = Op())
3404 {
3405 return traverse_out_arcs<GT>(p, [&op](auto a)
3406 {
3407 return op(a);
3408 });
3409 }
3410
3421 template <AlephGraph GT, class Op>
3422 inline
3423 bool exists_out_arc(typename GT::Node *p, Op op = Op())
3424 {
3425 return not traverse_out_arcs<GT>(p, [&op](auto a)
3426 {
3427 return not op(a);
3428 });
3429 }
3430
3442 template <AlephGraph GT, class Op>
3443 inline
3444 auto search_out_arc(typename GT::Node *p, Op op = Op())
3445 {
3446 typename GT::Arc *ret = nullptr;
3447 traverse_out_arcs<GT>(p, [&op, &ret](auto a)
3448 {
3449 if (op(a))
3450 {
3451 ret = a;
3452 return false;
3453 }
3454 return true;
3455 });
3456 return ret;
3457 }
3458
3472 // An actual std::function<T (Arc *)> goes to the overload below, as in
3473 // map_in_arcs(p, op)
3474 template <AlephGraph GT, typename T, class Op>
3475 requires (std::is_invocable_r_v<T, Op &, typename GT::Arc *>
3476 and not std::is_same_v<Op, std::function<T (typename GT::Arc *)>>)
3477 inline
3478 auto map_out_arcs(typename GT::Node *p, Op op)
3479 {
3480 DynList<T> ret;
3481 for_each_out_arc<GT>(p, [&ret, &op](auto a)
3482 {
3483 ret.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(op, a));
3484 });
3485 return ret;
3486 }
3487
3498 template <AlephGraph GT, typename T,
3499 class Op = std::function<T (typename GT::Arc *)>>
3500 requires std::is_same_v<Op, std::function<T (typename GT::Arc *)>>
3501 inline
3502 auto map_out_arcs(typename GT::Node *p, std::function<T (typename GT::Arc *)> op)
3503 {
3504 // See the equivalent nodes_map overload for why this must go through a
3505 // lambda rather than forwarding `op` as-is.
3506 return map_out_arcs<GT, T>(p, [&op](typename GT::Arc * a) { return op(a); });
3507 }
3508
3525 template <AlephGraph GT, typename T, class Op>
3526 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
3527 inline
3528 T foldl_out_arcs(typename GT::Node *p, const T & init,
3529 Op op)
3530 {
3531 T ret = init;
3532 for_each_out_arc<GT>(p, [&ret, &op](auto a)
3533 {
3534 ret = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(op, ret, a);
3535 });
3536 return ret;
3537 }
3538
3545 template <AlephGraph GT, class Op>
3546 inline
3548 {
3550 for_each_out_arc<GT>(p, [&ret, &cond](auto a)
3551 {
3552 if (cond(a))
3553 ret.append(a);
3554 });
3555 return ret;
3556 }
3557
3572 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
3573 typename GT::Arc *
3574 search_arc(const GT & g,
3575 typename GT::Node *src, typename GT::Node *tgt,
3576 SA sa = SA()) noexcept(nothrow_arc_filter_v<GT, SA>)
3577 {
3578 assert(src != nullptr and tgt != nullptr);
3579
3580 if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
3581 std::swap(tgt, src); // select the node with less arcs
3582
3583 for (Node_Arc_Iterator<GT, SA> itor(src, sa); itor.has_curr(); itor.next_ne())
3584 if (itor.get_tgt_node_ne() == tgt)
3585 return itor.get_current_arc_ne();
3586
3587 return nullptr;
3588 }
3589
3601 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
3602 typename GT::Arc *
3604 typename GT::Node *src, typename GT::Node *tgt,
3605 SA sa = SA()) noexcept(nothrow_arc_filter_v<GT, SA>)
3606 {
3607 (void) g;
3608 assert(src != nullptr and tgt != nullptr);
3609
3610 for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
3611 if (typename GT::Arc *a = it.get_curr(); a->src_node == src and a->tgt_node == tgt)
3612 return a;
3613
3614 return nullptr;
3615 }
3616
3621 template <AlephGraph GT>
3622 typename GT::Node * mapped_node(typename GT::Node *p) noexcept
3623 {
3624 return static_cast<typename GT::Node *>(NODE_COOKIE(p));
3625 }
3626
3628 template <AlephGraph GT>
3629 typename GT::Arc * mapped_arc(typename GT::Arc *a) noexcept
3630 {
3631 return static_cast<typename GT::Arc *>(ARC_COOKIE(a));
3632 }
3633
3635 template <AlephGraph GTSRC, AlephGraph GTTGT>
3636 typename GTTGT::Node * mapped_node(typename GTSRC::Node *p) noexcept
3637 {
3638 return static_cast<typename GTTGT::Node *>(NODE_COOKIE(p));
3639 }
3640
3642 template <AlephGraph GTSRC, AlephGraph GTTGT>
3643 typename GTTGT::Arc * mapped_arc(typename GTSRC::Arc *a) noexcept
3644 {
3645 return static_cast<typename GTTGT::Arc *>(ARC_COOKIE(a));
3646 }
3647
3667 template <AlephGraph GT>
3668 inline
3669 void copy_graph(GT & gtgt, const GT & gsrc, bool cookie_map = false);
3670
3683 template <AlephGraph GT>
3684 inline void clear_graph(GT & g);
3685
3696 template <AlephGraph GT, class Operation, NodeFilter<GT> SN = Dft_Show_Node<GT>>
3698 {
3699 SN sn;
3700
3701 public:
3703 Operate_On_Nodes(SN __sn = SN())
3704 : sn(__sn)
3705 { /* empty */
3706 }
3707
3713 void operator()(const GT & g, Operation op = Operation())
3714 {
3715 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
3716 op(g, it.get_curr());
3717 }
3718
3724 void operator()(GT & g, Operation op = Operation())
3725 {
3726 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
3727 op(g, it.get_curr());
3728 }
3729 };
3730
3741 template <AlephGraph GT, class Operation, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
3743 {
3744 SA sa;
3745
3746 public:
3748 Operate_On_Arcs(SA __sa = SA())
3749 : sa(__sa)
3750 { /* empty */
3751 }
3752
3758 void operator()(const GT & g, Operation op = Operation()) const
3759 {
3760 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
3761 op(g, it.get_curr());
3762 }
3763
3764 void operator()(GT & g, Operation op = Operation()) const
3765 {
3766 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
3767 op(g, it.get_curr());
3768 }
3769
3776 void operator()(const GT & g, typename GT::Node *p,
3777 Operation op = Operation()) const
3778 {
3779 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
3780 op(g, it.get_current_arc_ne());
3781 }
3782
3783 void operator()(GT & g, typename GT::Node *p,
3784 Operation op = Operation()) const
3785 {
3786 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
3787 op(g, it.get_current_arc_ne());
3788 }
3789 };
3790
3830 template <class GT>
3831 class Path
3832 {
3833 public:
3835 using Node_Type = typename GT::Node_Type;
3836
3838 using Arc_Type = typename GT::Arc_Type;
3839
3840 private:
3841 const GT *g = nullptr;
3842
3843 using Node = typename GT::Node;
3844 using Arc = typename GT::Arc;
3845
3847 {
3848 Node *node; // source node
3849 Arc *arc; // adjacent arc
3850
3851 Path_Desc(Node *_node = nullptr, Arc *_arc = nullptr) noexcept
3852 : node(_node), arc(_arc)
3853 {}
3854
3855 bool operator==(const Path_Desc & r) const noexcept
3856 {
3857 if (not (node->get_info() == r.node->get_info()))
3858 return false;
3859
3860 if (arc == nullptr)
3861 return r.arc == nullptr;
3862
3863 if (r.arc == nullptr)
3864 return false;
3865
3866 return arc->get_info() == r.arc->get_info();
3867 }
3868 };
3869
3871
3873 {
3874 ah_domain_error_if(g == nullptr) << "Path: Graph has not been specified";
3875 }
3876
3877 public:
3880 [[nodiscard]] bool check() const
3881 {
3882 const Path_Desc * prev = nullptr;
3883 for (typename DynDlist<Path_Desc>::Const_Iterator it(list); it.has_curr(); it.next_ne())
3884 {
3885 const Path_Desc & d = it.get_curr_ne();
3886 if (prev != nullptr)
3887 {
3888 if (prev->arc == nullptr)
3889 return false;
3890 auto src = g->get_src_node(prev->arc);
3891 auto tgt = g->get_tgt_node(prev->arc);
3892 if (not ((src == prev->node and tgt == d.node) or
3893 (tgt == prev->node and src == d.node)))
3894 return false;
3895 }
3896 prev = &d;
3897 }
3898
3899 return prev == nullptr or prev->arc == nullptr;
3900 }
3901
3904 [[nodiscard]] bool check_directed() const
3905 {
3906 const Path_Desc * prev = nullptr;
3907 for (typename DynDlist<Path_Desc>::Const_Iterator it(list); it.has_curr(); it.next_ne())
3908 {
3909 const Path_Desc & d = it.get_curr_ne();
3910 if (prev != nullptr and
3911 (prev->arc == nullptr or g->get_src_node(prev->arc) != prev->node or
3912 g->get_tgt_node(prev->arc) != d.node))
3913 return false;
3914 prev = &d;
3915 }
3916
3917 return prev == nullptr or prev->arc == nullptr;
3918 }
3919
3921 const GT &get_graph() const noexcept
3922 {
3923 return *g;
3924 }
3925
3927 bool inside_graph(const GT & gr) const noexcept
3928 {
3929 return g == &gr;
3930 }
3931
3933 Path(const GT & __g) noexcept : g(&__g)
3934 {}
3935
3936 Path() noexcept : g(nullptr)
3937 { /* empty */
3938 }
3939
3947 void init(Node *start_node)
3948 {
3949 assert(start_node != nullptr);
3950 list.append(Path_Desc(start_node));
3951 }
3952
3959 Path(const GT & _g, Node *start_node) : g(&_g)
3960 {
3961 init(start_node);
3962 }
3963
3977 void set_graph(const GT & __g, Node *start_node = nullptr)
3978 {
3979 empty();
3980 g = &__g;
3981 if (start_node == nullptr)
3982 return;
3983 init(start_node);
3984 }
3985
3987 [[nodiscard]] size_t size() const noexcept
3988 {
3989 return list.size();
3990 }
3991
3993 [[nodiscard]] bool is_empty() const noexcept
3994 {
3995 return list.is_empty();
3996 }
3997
3999 void empty()
4000 {
4001 check_graph();
4002 while (not list.is_empty())
4003 list.remove_first();
4004 }
4005
4011 void clear() { empty(); }
4012
4014 Path(const Path & path) : g(path.g), list(path.list) {}
4015
4017 Path(Path && path) noexcept : g(path.g), list(std::move(path.list)) {}
4018
4020 Path &operator=(const Path & path)
4021 {
4022 if (this == &path)
4023 return *this;
4024
4025 empty();
4026 g = path.g;
4027 list = path.list;
4028 return *this;
4029 }
4030
4032 Path &operator=(Path && path) noexcept
4033 {
4034 std::swap(g, path.g);
4035 list.swap(path.list);
4036 return *this;
4037 }
4038
4052 void append(Arc *arc)
4053 {
4054 assert(arc != nullptr);
4055 check_graph();
4056
4057 ah_domain_error_if(list.is_empty()) << "path is empty";
4058
4059 auto & last_path_desc = list.get_last();
4060 auto last_node = last_path_desc.node;
4061 ah_invalid_argument_if(arc->src_node != last_node and arc->tgt_node != last_node)
4062 << "arc has not link to last node of path";
4063
4064 last_path_desc.arc = arc;
4066 }
4067
4085 void append(Node *node)
4086 {
4087 check_graph();
4088
4089 if (list.is_empty())
4090 {
4091 init(node);
4092 return;
4093 }
4094
4096 Arc *arc = search_arc(*g, last_node, node);
4097
4098 ah_invalid_argument_if(arc == nullptr)
4099 << "There is no an arc connecting to the node";
4100
4101 append(arc);
4102 }
4103
4125 {
4126 assert(p != nullptr);
4127 check_graph();
4128 if (list.is_empty())
4129 {
4130 init(p);
4131 return;
4132 }
4133
4134 auto & last_path_desc = list.get_last();
4135 Node *last_node = last_path_desc.node;
4136 Arc *arc = search_directed_arc(*g, last_node, p);
4137
4138 ah_invalid_argument_if(arc == nullptr) << "There is no an arc connecting to the node";
4139
4140 assert(arc->src_node == last_path_desc.node);
4141
4142 last_path_desc.arc = arc;
4143 list.append(Path_Desc(static_cast<Node *>(arc->tgt_node)));
4144 }
4145
4165 {
4166 assert(arc != nullptr);
4167 check_graph();
4168
4169 ah_domain_error_if(list.is_empty()) << "path is empty";
4170
4171 auto & last_path_desc = list.get_last();
4172 Node *last_node = last_path_desc.node;
4173 ah_invalid_argument_if(arc->src_node != last_node)
4174 << "The arc does not connect the last node";
4175
4176 last_path_desc.arc = arc;
4177 list.append(Path_Desc(static_cast<Node *>(arc->tgt_node)));
4178 }
4179
4195 void insert(Arc *arc)
4196 {
4197 assert(arc != nullptr);
4198 check_graph();
4199
4200 ah_domain_error_if(list.is_empty()) << "path is empty";
4201
4202 auto & first_path_desc = list.get_first();
4203 auto first_node = first_path_desc.node;
4204 ah_invalid_argument_if(arc->src_node != first_node and arc->tgt_node != first_node)
4205 << "arc has not link to first node of path";
4206
4207 Path_Desc item(g->get_connected_node(arc, first_node), arc);
4208 list.insert(item);
4209 }
4210
4228 void insert(Node *node)
4229 {
4230 check_graph();
4231
4232 if (list.is_empty())
4233 {
4234 init(node);
4235 return;
4236 }
4237
4238 Node *first_node = get_first_node();
4239 Arc *arc = search_arc(*g, node, first_node); // search arc first_node-node
4240 ah_invalid_argument_if(arc == nullptr) << "There is no arc connecting node";
4241
4242 Path_Desc item(node, arc);
4243 list.insert(item);
4244 }
4245
4267 {
4268 assert(p != nullptr);
4269 check_graph();
4270
4271 if (list.is_empty())
4272 {
4273 init(p);
4274 return;
4275 }
4276
4277 Node *first_node = get_first_node();
4278 Arc *arc = search_directed_arc(*g, p, first_node);
4279 ah_invalid_argument_if(arc == nullptr) << "There is no an arc connecting to the node";
4280
4281 list.insert(Path_Desc(p, arc));
4282 }
4283
4300 {
4301 assert(arc != nullptr);
4302 check_graph();
4303
4304 ah_domain_error_if(list.is_empty()) << "path is empty";
4305
4306 auto & first_path_desc = list.get_first();
4307 Node *first_node = first_path_desc.node;
4308 ah_invalid_argument_if(arc->tgt_node != first_node)
4309 << "The arc does not connect the first node";
4310
4311 list.insert(Path_Desc(static_cast<Node *>(arc->src_node), arc));
4312 }
4313
4317 {
4318 return list.get_first().node;
4319 }
4320
4324 {
4325 auto & last_path_desc = list.get_last();
4326 assert(last_path_desc.arc == nullptr);
4327 return last_path_desc.node;
4328 }
4329
4333 {
4334 return list.get_first().arc;
4335 }
4336
4340 {
4341 ah_domain_error_if(list.is_unitarian())
4342 << "Path with only a node (without any arc)";
4343
4345 it.reset_last();
4346 it.prev();
4347 return it.get_curr().arc;
4348 }
4349
4351 [[nodiscard]] bool is_cycle() const
4352 {
4353 return get_first_node() == get_last_node();
4354 }
4355
4364 {
4365 auto d = list.remove_last();
4366 if (not list.is_empty()) // the arc that went to the removed node
4367 list.get_last().arc = nullptr;
4368 return d.node;
4369 }
4370
4377 {
4378 auto d = list.remove_first();
4379 return d.node;
4380 }
4381
4383 void swap(Path & path) noexcept
4384 {
4385 std::swap(g, path.g);
4386 list.swap(path.list);
4387 }
4388
4394 class Iterator : public Aleph::const_iterator_t<DynDlist<Path_Desc>>
4395 {
4396 // The path hands out its nodes and arcs, never its entries: a
4397 // read-only view of the list is all it needs.
4399
4400 public:
4402 Iterator(const Path & path) noexcept
4403 : Base(path.list)
4404 {}
4405
4406 private:
4407 const Path_Desc &get_curr_path_desc_ne() const noexcept
4408 {
4409 return this->Base::get_curr_ne();
4410 }
4411
4413 {
4414 return this->Base::get_curr();
4415 }
4416
4417 public:
4421 {
4422 return this->get_curr_path_desc().node;
4423 }
4424
4425 Node * get_current_node_ne() const noexcept
4426 {
4427 return this->get_curr_path_desc_ne().node;
4428 }
4429
4441 {
4442 ah_overflow_error_if(this->is_in_last()) << "Path iterator is in last node of path";
4443
4444 return this->get_curr_path_desc().arc;
4445 }
4446
4447 Arc * get_current_arc_ne() const noexcept
4448 {
4449 return this->get_curr_path_desc_ne().arc;
4450 }
4451
4453 Node * get_curr_ne() const noexcept
4454 {
4455 return get_current_node_ne();
4456 }
4457
4458 Node * get_curr() const
4459 {
4460 return get_current_node();
4461 }
4462
4473 std::pair<Node *, Arc *> get_pair() const
4474 {
4475 return std::make_pair(get_current_node(), get_current_arc());
4476 }
4477
4488 std::tuple<Node *, Arc *> get_tuple() const
4489 {
4490 return std::make_tuple(get_current_node(), get_current_arc());
4491 }
4492
4493 std::tuple<Node *, Arc *> get_tuple_ne() const noexcept
4494 {
4495 return std::make_tuple(get_current_node_ne(), get_current_arc_ne());
4496 }
4497
4506 [[nodiscard]] bool has_current_arc() const noexcept
4507 {
4508 return this->has_curr() and not this->is_in_last();
4509 }
4510
4512 [[nodiscard]] bool has_current_node() const noexcept
4513 {
4514 return this->has_curr();
4515 }
4516 };
4517
4520 {
4521 return Iterator(*this);
4522 }
4523
4528 template <class Operation>
4529 void for_each_node(Operation op = Operation()) const
4530 {
4531 for (Iterator it(*this); it.has_current_node(); it.next_ne())
4532 op(it.get_current_node_ne());
4533 }
4534
4539 template <class Operation>
4540 void for_each_arc(Operation op = Operation()) const
4541 {
4542 for (Iterator it(*this); it.has_current_arc(); it.next_ne())
4543 op(it.get_current_arc_ne());
4544 }
4545
4547 bool contains_node(Node *node) const noexcept
4548 {
4549 for (Iterator it(*this); it.has_current_node(); it.next_ne())
4550 if (it.get_current_node_ne() == node)
4551 return true;
4552 return false;
4553 }
4554
4556 bool contains_arc(Arc *arc) const noexcept
4557 {
4558 for (Iterator it(*this); it.has_current_arc(); it.next_ne())
4559 if (it.get_current_arc_ne() == arc)
4560 return true;
4561 return false;
4562 }
4563
4566 {
4567 DynList<Node *> ret_val;
4568 for_each_node([&ret_val](Node *p)
4569 {
4570 ret_val.append(p);
4571 });
4572 return ret_val;
4573 }
4574
4577 {
4578 DynList<Arc *> ret_val;
4579 for_each_arc([&ret_val](Arc *a)
4580 {
4581 ret_val.append(a);
4582 });
4583 return ret_val;
4584 }
4585
4596 bool operator==(const Path & p) const noexcept
4597 {
4598 return eq(this->list, p.list);
4599 }
4600
4602 bool operator!=(const Path & p) const noexcept
4603 {
4604 return not eq(this->list, p.list);
4605 }
4606 };
4607
4608 template <AlephGraph GT>
4609 inline
4610 Path<GT> find_path_depth_first(const GT & g, typename GT::Node *start_node,
4611 typename GT::Node *end_node);
4612
4613 // Searches from curr_node, reached through curr_arc, a path to end_node,
4614 // which it puts in path. A path can have as many nodes as the graph, too
4615 // many for recursion, so that the search keeps its own stack; the order
4616 // of the visits is that of a recursion.
4617 template <class GT>
4618 static inline
4619 bool __find_path_depth_first(const GT & g, typename GT::Node *curr_node,
4620 typename GT::Arc *curr_arc,
4621 typename GT::Node *end_node, Path<GT> & path)
4622 {
4623 if (curr_node == end_node) // this test must be first in order to find cycles
4624 {
4625 path.append(curr_arc);
4626 return true;
4627 }
4628
4629 if (IS_NODE_VISITED(curr_node, Find_Path))
4630 return false;
4631
4632 path.append(curr_arc);
4633 NODE_BITS(curr_node).set_bit(Find_Path, true);
4634
4635 // The node being visited keeps its iterator in here until it visits a
4636 // node; then it waits in frames, where it goes on when it comes back
4638 Dfs_Stack<Frame> frames;
4639 Frame here(g.get_arc_it(curr_node));
4640 while (true)
4641 {
4642 if (auto tgt = here.template next_target<Find_Path>(end_node))
4643 { // the iterator stays on the arc to tgt, as in a recursion
4644 path.append(here.it.get_current_arc_ne());
4645 if (tgt == end_node)
4646 return true;
4647
4648 NODE_BITS(tgt).set_bit(Find_Path, true);
4649 frames.push(std::move(here));
4650 here = Frame(g.get_arc_it(tgt));
4651 continue;
4652 }
4653
4654 path.remove_last_node(); // there is no path through it
4655 while (true) // back to the nodes that wait
4656 {
4657 if (frames.is_empty())
4658 return false;
4659
4660 Frame & f = frames.top();
4661 f.next_arc(); // past the arc to the node that was visited
4662 if (auto tgt = f.template next_target<Find_Path>(end_node))
4663 {
4664 path.append(f.it.get_current_arc_ne());
4665 if (tgt == end_node)
4666 return true;
4667
4668 NODE_BITS(tgt).set_bit(Find_Path, true);
4669 here = Frame(g.get_arc_it(tgt));
4670 break;
4671 }
4672 path.remove_last_node();
4673 frames.pop();
4674 }
4675 }
4676 }
4677
4693 template <AlephGraph GT>
4694 inline
4695 Path<GT> find_path_depth_first(const GT & g, typename GT::Node *start_node,
4696 typename GT::Node *end_node)
4697 {
4698 Path<GT> path(g, start_node);
4699
4702 NODE_BITS(start_node).set_bit(Find_Path, true);
4703
4704 for (auto it = g.get_arc_it(start_node); it.has_curr(); it.next_ne())
4705 {
4706 auto arc = it.get_current_arc_ne();
4707 ARC_BITS(arc).set_bit(Find_Path, true);
4708 auto next_node = it.get_tgt_node();
4709 if (IS_NODE_VISITED(next_node, Find_Path))
4710 continue;
4711
4712 if (__find_path_depth_first<GT>(g, next_node, arc, end_node, path))
4713 return path;
4714 }
4715
4716 path.empty();
4717
4718 return path;
4719 }
4720
4730 template <AlephGraph GTS, AlephGraph GTT>
4731 void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
4732 {
4733 assert(p != nullptr and q != nullptr);
4734
4735 // Use reinterpret_cast to preserve an exact pointer value without any
4736 // implicit base class pointer adjustment from multiple inheritance
4737 if (NODE_COOKIE(p) == nullptr)
4738 {
4739 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
4740 NODE_COOKIE(q) = reinterpret_cast<void *>(p);
4741 return;
4742 }
4743
4744 NODE_COOKIE(q) = NODE_COOKIE(p);
4745 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
4746 }
4747
4758 template <AlephGraph GTS, AlephGraph GTT>
4759 void map_arcs(typename GTS::Arc *p, typename GTT::Arc *q) noexcept
4760 {
4761 assert(p != nullptr and q != nullptr);
4762
4763 if (ARC_COOKIE(p) == nullptr)
4764 {
4765 ARC_COOKIE(p) = q;
4766 ARC_COOKIE(q) = p;
4767
4768 return;
4769 }
4770
4771 ARC_COOKIE(q) = ARC_COOKIE(p);
4772 ARC_COOKIE(p) = q;
4773 }
4774
4775 template <AlephGraph GT>
4776 void clear_graph(GT & g)
4777 {
4778 for (typename GT::Arc_Iterator it(g); it.has_curr();) // remove arcs
4779 {
4780 typename GT::Arc *arc = it.get_curr_ne();
4781 it.next_ne();
4782 g.remove_arc(arc);
4783 }
4784
4785 for (typename GT::Node_Iterator it(g); it.has_curr();) // remove nodes
4786 {
4787 typename GT::Node *p = it.get_curr_ne();
4788 it.next_ne(); // advance before deletion (iterator consistency)
4789 g.remove_node(p); // remove it from the graph
4790 }
4791 }
4792
4793 template <AlephGraph GT>
4794 void copy_graph(GT & gtgt, const GT & gsrc, const bool cookie_map)
4795 {
4796 try
4797 {
4798 clear_graph(gtgt); // clear this before copying
4800
4801 // phase 1: traverse nodes of src_graph and insert copy in this
4802 for (typename GT::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
4803 {
4804 typename GT::Node *src_node = it.get_current_node_ne();
4805 std::unique_ptr<typename GT::Node>
4806 tgt_node(new typename GT::Node(src_node->get_info()));
4807 map.insert(src_node, tgt_node.get());
4808
4809 typename GT::Node *tgt = tgt_node.release();
4810 assert(tgt->get_info () == src_node->get_info ());
4811 gtgt.insert_node(tgt); // insert in the target graph
4812
4813 if (cookie_map)
4814 GT::map_nodes(src_node, tgt);
4815 }
4816
4817 assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
4818
4819 // phase 2: for each arc of src_graph, create in this an
4820 // arc connecting the mapped nodes from map
4821 for (typename GT::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
4822 {
4823 typename GT::Arc *src_arc = it.get_current_arc_ne();
4824
4825 // get node images in target graph and create arc
4826 typename GT::Node *src_node = map[gsrc.get_src_node(src_arc)];
4827 typename GT::Node *tgt_node = map[gsrc.get_tgt_node(src_arc)];
4828 typename GT::Arc *tgt_arc = gtgt.insert_arc(src_node, tgt_node);
4829 // tgt_arc->get_info() = src_arc->get_info();
4830 *tgt_arc = *src_arc;
4831 if (cookie_map)
4832 GT::map_arcs(src_arc, tgt_arc);
4833 }
4834
4835 assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
4836 }
4837 catch (...)
4838 { // If an exception occurs, empty gtgt without allocating: a network
4839 // allocates when it removes an arc, and its destructor does not
4840 std::remove_cvref_t<decltype(gtgt)> empty;
4841 empty.get_cookie() = gtgt.get_cookie();
4842 gtgt.swap(empty);
4843 throw;
4844 }
4845 }
4846
4851 template <class GTT, class GTS>
4853 {
4854 void operator()(typename GTT::Node *tgt, typename GTS::Node *src) noexcept
4855 {
4856 tgt->get_info() = src->get_info();
4857 }
4858 };
4859
4864 template <class GTT, class GTS>
4866 {
4867 void operator()(typename GTT::Arc *tgt, typename GTS::Arc *src)
4868 {
4869 tgt->get_info() = src->get_info();
4870 }
4871 };
4872
4881 template <AlephGraph GTT, AlephGraph GTS,
4882 class Copy_Node = Dft_Copy_Node<GTT, GTS>,
4883 class Copy_Arc = Dft_Copy_Arc<GTT, GTS>>
4884 void inter_copy_graph(GTT & gtgt, const GTS & gsrc,
4885 const bool cookie_map = false)
4886 {
4887 try
4888 {
4889 clear_graph(gtgt); // clear this before copying
4891 // phase 1: traverse nodes of src_graph and insert a copy in this
4892 for (typename GTS::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
4893 {
4894 typename GTS::Node *src_node = it.get_current_node_ne();
4895 std::unique_ptr<typename GTT::Node> tgt_node(new typename GTT::Node);
4896 Copy_Node()(tgt_node.get(), src_node);
4897 map.insert(src_node, tgt_node.get());
4898
4899 typename GTT::Node *tgt = tgt_node.release();
4900 gtgt.insert_node(tgt); // insert in the target graph
4901
4902 if (cookie_map)
4903 map_nodes<GTS, GTT>(src_node, tgt);
4904 }
4905
4906 assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
4907
4908 // phase 2: for each arc of src_graph, create in this an
4909 // arc connecting the mapped nodes from the map
4910 for (typename GTS::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
4911 {
4912 typename GTS::Arc *src_arc = it.get_current_arc_ne();
4913
4914 // get node images in target graph and create arc
4915 typename GTT::Node *src_node = map[gsrc.get_src_node(src_arc)];
4916 typename GTT::Node *tgt_node = map[gsrc.get_tgt_node(src_arc)];
4917 typename GTT::Arc *tgt_arc = gtgt.insert_arc(src_node, tgt_node);
4918 Copy_Arc()(tgt_arc, src_arc);
4919 if (cookie_map)
4920 map_arcs<GTS, GTT>(src_arc, tgt_arc);
4921 }
4922
4923 assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
4924 }
4925 catch (...)
4926 { // If an exception occurs, empty gtgt without allocating: a network
4927 // allocates when it removes an arc, and its destructor does not
4928 std::remove_cvref_t<decltype(gtgt)> empty;
4929 empty.get_cookie() = gtgt.get_cookie();
4930 gtgt.swap(empty);
4931 throw;
4932 }
4933 }
4934
4949 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
4951 {
4952 SN sn;
4953 SA sa;
4954
4955 public:
4961 Copy_Graph(SA __sa = SA(), SN __sn = SN()) : sn(__sn), sa(__sa)
4962 {
4963 // empty
4964 }
4965
4966 private:
4967 void copy(GT & gtgt, const GT & gsrc, const bool cookie_map)
4968 {
4969 try
4970 {
4971 clear_graph(gtgt); // clear this before copying
4973
4974 // phase 1: traverse nodes of src_graph and insert a copy in this
4975 for (Node_Iterator<GT, SN> it(gsrc, sn); it.has_curr(); it.next_ne())
4976 {
4977 typename GT::Node *src_node = it.get_curr();
4978 std::unique_ptr<typename GT::Node>
4979 tgt_node(new typename GT::Node(src_node));
4980 map.insert(src_node, tgt_node.get());
4981
4982 typename GT::Node *tgt = tgt_node.release();
4983 gtgt.insert_node(tgt); // insert in target graph
4984
4985 if (cookie_map)
4986 GT::map_nodes(src_node, tgt);
4987 }
4988
4989 // phase 2: for each arc of src_graph between two copied nodes,
4990 // create in this an arc connecting their images
4991 for (Arc_Iterator<GT, SA> it(gsrc, sa); it.has_curr(); it.next_ne())
4992 {
4993 typename GT::Arc *src_arc = it.get_curr();
4994
4995 // get node images in target graph and create arc
4996 auto *src_image = map.search(gsrc.get_src_node(src_arc));
4997 auto *tgt_image = map.search(gsrc.get_tgt_node(src_arc));
4998 if (src_image == nullptr or tgt_image == nullptr)
4999 continue; // a node that the node filter hides
5000
5001 typename GT::Arc *tgt_arc =
5002 gtgt.insert_arc(src_image->second, tgt_image->second,
5003 src_arc->get_info());
5004
5005 if (cookie_map)
5006 GT::map_arcs(src_arc, tgt_arc);
5007 }
5008 }
5009 catch (...)
5010 { // If an exception occurs, gtgt is emptied, without allocating
5011 GT empty;
5012 empty.get_cookie() = gtgt.get_cookie();
5013 gtgt.swap(empty);
5014 throw;
5015 }
5016 }
5017
5018 public:
5026 void operator()(GT & gtgt, GT & gsrc, const bool cookie_map = true)
5027 {
5028 copy(gtgt, gsrc, cookie_map);
5029 }
5030 };
5031
5045 template <AlephGraph GT, class Distance>
5047 {
5050
5053
5056 noexcept(std::is_nothrow_default_constructible_v<Distance>)
5057 : dist(0), distance()
5058 { /* empty */
5059 }
5060
5066 : dist(0), distance(__distance)
5067 { /* empty */
5068 }
5069
5076 bool operator()(typename GT::Arc *a)
5077 {
5079 return false;
5080
5081 dist = dist + distance(a);
5082
5083 return true;
5084 }
5085 };
5086
5106 template <AlephGraph GT>
5107 inline
5108 bool are_equal(const GT & g1, const GT & g2);
5109
5143 template <AlephGraph GT,
5144 template <class, class> class Tree = Treap>
5146 {
5147 public:
5148 using Node = typename GT::Node;
5149 using Arc = typename GT::Arc;
5150 using Node_Type = typename GT::Node_Type;
5151 using Arc_Type = typename GT::Arc_Type;
5152
5153 private:
5156
5158 void build_copy(const GT & src)
5159 {
5160 // Phase 1: Copy all nodes and build mapping
5161 for (typename GT::Node_Iterator it(src); it.has_curr(); it.next_ne())
5162 {
5163 Node *src_node = it.get_curr();
5164 Node *tgt_node = copied_graph.insert_node(src_node->get_info());
5165 node_map.insert(src_node, tgt_node);
5166 }
5167
5168 // Phase 2: Copy all arcs using the node mapping
5169 for (typename GT::Arc_Iterator it(src); it.has_curr(); it.next_ne())
5170 {
5171 Arc *src_arc = it.get_curr();
5172 Node *src_src = src.get_src_node(src_arc);
5173 Node *src_tgt = src.get_tgt_node(src_arc);
5174
5175 Node *tgt_src = node_map.find(src_src);
5176 Node *tgt_tgt = node_map.find(src_tgt);
5177
5178 copied_graph.insert_arc(tgt_src, tgt_tgt, src_arc->get_info());
5179 }
5180 }
5181
5182 public:
5193 explicit GraphCopyWithMapping(const GT & src)
5194 {
5195 build_copy(src);
5196 }
5197
5207
5218 GraphCopyWithMapping(GraphCopyWithMapping && other) noexcept = default;
5219
5233
5234 // Disable copy (graphs can be large)
5236
5238
5246 GT &get_graph() noexcept { return copied_graph; }
5247
5255 const GT &get_graph() const noexcept { return copied_graph; }
5256
5268 Node * get_copy(Node *orig) const
5269 {
5270 auto *ptr = node_map.search(orig);
5271 ah_domain_error_if(ptr == nullptr)
5272 << "Node not found in mapping (not from original graph?)";
5273 return ptr->second;
5274 }
5275
5287 Node * search_copy(Node *orig) const noexcept
5288 {
5289 auto *ptr = node_map.search(orig);
5290 return ptr ? ptr->second : nullptr;
5291 }
5292
5301 bool has_copy(Node *orig) const noexcept
5302 {
5303 return node_map.search(orig) != nullptr;
5304 }
5305
5313 [[nodiscard]] size_t num_nodes() const noexcept { return node_map.size(); }
5314
5322 [[nodiscard]] size_t num_arcs() const noexcept { return copied_graph.get_num_arcs(); }
5323
5334 template <typename Op>
5335 void for_each_mapping(Op op) const
5336 {
5337 node_map.for_each([&op](const auto & pair)
5338 {
5339 op(pair.first, pair.second);
5340 });
5341 }
5342
5355 {
5356 return copied_graph.insert_node(info);
5357 }
5358
5371 {
5372 return copied_graph.insert_node(std::forward<Node_Type>(info));
5373 }
5374
5385 void remove_node(Node *node)
5386 {
5387 // Maintain node_map consistency when a copied node is removed:
5388 // perform a reverse lookup (value -> key) and erase the corresponding entry.
5389 // This is O(N) because DynMapTree is keyed by original node, not by copy.
5390 Node *original_node = nullptr;
5391 node_map.for_each([&](const auto & pair)
5392 {
5393 if (pair.second == node)
5394 {
5395 original_node = pair.first;
5396 }
5397 });
5398
5399 if (original_node)
5400 node_map.remove(original_node);
5401
5403 }
5404
5415 Arc * insert_arc(Node *src, Node *tgt, const Arc_Type & info = Arc_Type())
5416 {
5417 return copied_graph.insert_arc(src, tgt, info);
5418 }
5419
5427 void remove_arc(Arc *arc)
5428 {
5430 }
5431
5440 void clear()
5441 {
5442 node_map.empty();
5444 }
5445 };
5446} // end namespace Aleph
5447
5448# endif /* TPL_GRAPH_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
C++20 concepts for the protocol shared by graph algorithms.
STL-compatible iterator adapters for Aleph containers.
Simplified graph interface for common use cases.
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Space-efficient bit array implementation.
Arc_Node(void *__arc) noexcept
Definition tpl_graph.H:295
Arc_Node() noexcept
Definition tpl_graph.H:293
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & base()
Return a reference to the first element of array.
Definition tpl_array.H:408
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:392
Read-only view of an Aleph iterator: the iterator of a constant container.
auto is_in_last() const noexcept(noexcept(std::declval< I & >().is_in_last())) -> decltype(std::declval< I & >().is_in_last())
true if the current item is the last one.
auto reset_last() noexcept(noexcept(std::declval< I & >().reset_last())) -> decltype(std::declval< I & >().reset_last())
Position the view on the last item.
auto prev() noexcept(noexcept(std::declval< I & >().prev())) -> decltype(std::declval< I & >().prev())
Move to the previous item; what the underlying iterator throws.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
auto get_curr_ne() const noexcept(noexcept(std::declval< I & >().get_curr_ne())) -> const_iterator_detail::const_item_t< decltype(std::declval< I & >().get_curr_ne())>
get_curr() without the checks of the underlying iterator.
auto get_curr() const noexcept(noexcept(std::declval< I & >().get_curr())) -> const_iterator_detail::const_item_t< decltype(std::declval< I & >().get_curr())>
The current item, read-only.
Filtered copy of graphs.
Definition tpl_graph.H:4951
Copy_Graph(SA __sa=SA(), SN __sn=SN())
Constructor.
Definition tpl_graph.H:4961
void copy(GT &gtgt, const GT &gsrc, const bool cookie_map)
Definition tpl_graph.H:4967
void operator()(GT &gtgt, GT &gsrc, const bool cookie_map=true)
Perform the copy from gsrc to gtgt.
Definition tpl_graph.H:5026
Stack of the frames of a depth-first search that does not recurse.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
Filtered iterator on directed graphs.
Definition tpl_graph.H:1770
GT::Arc * get_curr_ne() const noexcept
Return the current arc.
Definition tpl_graph.H:1836
GT::Node * get_node_ne() const noexcept
Return the connected node to current arc.
Definition tpl_graph.H:1869
static constexpr bool goes_to_last
Whether the iterator goes to its last arc, which reset_last() and end() need.
Definition tpl_graph.H:1789
GT::Node * get_node() const
Return the connected node to current arc.
Definition tpl_graph.H:1856
void next_ne() noexcept(shows_ne)
next() without its check: the current arc must exist.
Definition tpl_graph.H:1812
GT::Arc * get_curr() const
Return the current arc.
Definition tpl_graph.H:1829
void reset_first() noexcept(shows_ne)
Reset the iterator to the first arc.
Definition tpl_graph.H:1882
void end() noexcept(shows_ne)
Put the iterator in end state.
Definition tpl_graph.H:1897
static constexpr bool shows_ne
Whether what the filter says of an arc cannot throw, nor its conversion to bool: the moves without ch...
Definition tpl_graph.H:1794
Digraph_Iterator(typename GT::Node *p)
Initialize an iterator on adjacent arcs (incoming or outcoming) of p
Definition tpl_graph.H:1797
Filter_Iterator< typename GT::Node *, typename GT::Node_Arc_Iterator, Filter > Itor
Definition tpl_graph.H:1772
auto get_tgt_node_ne() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_graph.H:1876
auto get_tgt_node() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_graph.H:1863
static constexpr bool goes_back
Iterator type.
Definition tpl_graph.H:1785
void prev()
Move the iterator one position backward.
Definition tpl_graph.H:1819
bool has_curr() const noexcept
Return true the iterator has an current arc.
Definition tpl_graph.H:1822
auto get_current_arc() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_graph.H:1843
GT::Node * get_node(typename GT::Arc *a) const noexcept
Return the connected node to arc.
Definition tpl_graph.H:1850
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1778
void next()
Move the iterator one position forward.
Definition tpl_graph.H:1805
void reset_last() noexcept(shows_ne)
Reset the iterator to the last arc.
Definition tpl_graph.H:1889
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Dynamic map implemented with an AVL tree.
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Generic filter iterator wrapper.
void reset_last()
Resets the iterator to the last filtered element.
void next()
Advances the iterator to the next filtered element.
static constexpr bool goes_to_last
Whether the base iterator goes to its last item and moves back, as reset_last() needs.
void prev()
Moves the iterator backward to the previous filtered element.
static constexpr bool goes_back
Whether the base iterator moves back, as prev() and prev_ne() need.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
typename It::Item_Type Item_Type
The type of element returned by get_curr()
void reset_first()
Resets the iterator to the first filtered element.
Graph copy with explicit node mapping.
Definition tpl_graph.H:5146
Node * search_copy(Node *orig) const noexcept
Search for the copy of an original node (no exception).
Definition tpl_graph.H:5287
typename GT::Arc_Type Arc_Type
Definition tpl_graph.H:5151
bool has_copy(Node *orig) const noexcept
Check if an original node is in the mapping.
Definition tpl_graph.H:5301
GraphCopyWithMapping & operator=(const GraphCopyWithMapping &)=delete
Node * insert_unmapped_node(Node_Type &&info)
Insert a new node into the copied graph (not mapped, move version).
Definition tpl_graph.H:5370
void build_copy(const GT &src)
Build the copy and mapping.
Definition tpl_graph.H:5158
GraphCopyWithMapping()=default
Default constructor.
const GT & get_graph() const noexcept
Get the copied graph (const version).
Definition tpl_graph.H:5255
DynMapTree< Node *, Node *, Tree > node_map
Definition tpl_graph.H:5155
void remove_node(Node *node)
Remove a node from the copied graph.
Definition tpl_graph.H:5385
Node * get_copy(Node *orig) const
Get the copy of an original node.
Definition tpl_graph.H:5268
void clear()
Clear the copied graph and mapping.
Definition tpl_graph.H:5440
Arc * insert_arc(Node *src, Node *tgt, const Arc_Type &info=Arc_Type())
Insert an arc into the copied graph.
Definition tpl_graph.H:5415
GraphCopyWithMapping(const GT &src)
Construct a copy of the given graph with node mapping.
Definition tpl_graph.H:5193
void remove_arc(Arc *arc)
Remove an arc from the copied graph.
Definition tpl_graph.H:5427
void for_each_mapping(Op op) const
Apply a function to each (original, copy) node pair.
Definition tpl_graph.H:5335
size_t num_arcs() const noexcept
Get the number of arcs in the copied graph.
Definition tpl_graph.H:5322
Node * insert_unmapped_node(const Node_Type &info=Node_Type())
Insert a new node into the copied graph (not mapped).
Definition tpl_graph.H:5354
typename GT::Node_Type Node_Type
Definition tpl_graph.H:5150
GraphCopyWithMapping(GraphCopyWithMapping &&other) noexcept=default
Move constructor.
size_t num_nodes() const noexcept
Get the number of mapped nodes.
Definition tpl_graph.H:5313
typename GT::Node Node
Definition tpl_graph.H:5148
GT & get_graph() noexcept
Get the copied graph.
Definition tpl_graph.H:5246
GraphCopyWithMapping(const GraphCopyWithMapping &)=delete
GraphCopyWithMapping & operator=(GraphCopyWithMapping &&other) noexcept=default
Move assignment operator.
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
Arc * get_current_arc_ne() const noexcept
See get_curr_ne().
Definition tpl_graph.H:2321
Arc * get_curr_ne() const noexcept
Return the current arc, which must exist.
Definition tpl_graph.H:2306
void prev()
Move to the previous arc.
Definition tpl_graph.H:2358
void next()
Move to the next arc.
Definition tpl_graph.H:2345
void next_ne() noexcept
Move to the next arc; the iterator must be on an arc.
Definition tpl_graph.H:2339
size_t size() const noexcept
Return the number of arcs of the iterator.
Definition tpl_graph.H:2374
Node * get_node_ne() const noexcept
Return the source of the current arc, which must exist.
Definition tpl_graph.H:2327
bool has_curr() const noexcept
Return true if the iterator is on an arc.
Definition tpl_graph.H:2303
void reset_first() noexcept
Put the iterator on the first arc.
Definition tpl_graph.H:2365
void end() noexcept
Put the iterator past the last arc.
Definition tpl_graph.H:2371
void prev_ne() noexcept
Move to the previous arc; the iterator must be on an arc.
Definition tpl_graph.H:2352
Arc * get_current_arc() const
See get_curr().
Definition tpl_graph.H:2324
Iterator() noexcept=default
An iterator without arcs.
Node * get_node() const
Return the source of the current arc. Throws as get_curr().
Definition tpl_graph.H:2333
Arc * Item_Type
The type of the items: arcs.
Definition tpl_graph.H:2286
Iterator(const In_Arcs_Index &index, const Node *p)
An iterator on the arcs into p of index.
Definition tpl_graph.H:2300
Arc * get_curr() const
Return the current arc.
Definition tpl_graph.H:2313
void reset_last() noexcept
Put the iterator on the last arc.
Definition tpl_graph.H:2368
Index of the arcs that go into each node of a graph.
Definition tpl_graph.H:2224
size_t in_degree(const Node *p) const
Return the number of arcs into p that the filter showed.
Definition tpl_graph.H:2495
In_Arcs_Index(In_Arcs_Index &&other)
Take the arcs of another index, which is left as the index of a graph without nodes.
Definition tpl_graph.H:2448
Array< size_t > number
Definition tpl_graph.H:2233
static unsigned table_bits(const size_t n) noexcept
Definition tpl_graph.H:2240
size_t place_of(const Node *p) const noexcept
Definition tpl_graph.H:2248
In_Arcs_Index(const GT &g, SA sa=SA())
Index the arcs of g into each node that sa shows.
Definition tpl_graph.H:2384
In_Arcs_Index & operator=(const In_Arcs_Index &other)=default
Copy another index.
In_Arcs_Index(const In_Arcs_Index &other)=default
Copy another index.
Array< Node * > keys
Definition tpl_graph.H:2232
typename GT::Arc Arc
Definition tpl_graph.H:2226
size_t checked_number_of(const Node *p) const
Definition tpl_graph.H:2265
Iterator get_it(const Node *p) const
Return an iterator on the arcs into p that the filter showed.
Definition tpl_graph.H:2506
void swap(In_Arcs_Index &other) noexcept
Exchange in this index and another.
Definition tpl_graph.H:2477
size_t mask() const noexcept
Definition tpl_graph.H:2254
Array< Arc * > arcs
Definition tpl_graph.H:2238
size_t size() const noexcept
Return the number of arcs of the index: those that the filter showed.
Definition tpl_graph.H:2488
size_t number_of(const Node *p) const noexcept
Definition tpl_graph.H:2257
typename GT::Node Node
Definition tpl_graph.H:2225
Array< size_t > first
Definition tpl_graph.H:2237
Filtered iterator for incoming arcs of a node.
Definition tpl_graph.H:2045
typename GT::Arc * Item_Type
Definition tpl_graph.H:2082
In_Iterator()=default
static GT::Node * of_lists(const GT &g, typename GT::Node *p)
Definition tpl_graph.H:2074
void next_ne() noexcept(NothrowPredicateWith< SA &, typename GT::Arc * >)
next() without the check of the current arc; noexcept when the filter is, else what the filter throws...
Definition tpl_graph.H:2125
In_Iterator(typename GT::Node *p, SA sa=SA())
Iterator on the arcs of the list of p whose target is p and that sa shows.
Definition tpl_graph.H:2093
Digraph_Iterator< GT, __In_Filt< GT > > Base
Definition tpl_graph.H:2046
In_Iterator(const GT &g, typename GT::Node *p, SA sa=SA())
Iterator on the arcs of g into p that sa shows; on a graph of an undirected type, those of the list o...
Definition tpl_graph.H:2111
static void refuse_a_digraph()
Definition tpl_graph.H:2064
void prev()
Move to the previous arc that the filter shows.
Definition tpl_graph.H:2133
A filter that an algorithm object keeps: one given as an lvalue is used where it is,...
Definition tpl_graph.H:1249
std::optional< F > own
Definition tpl_graph.H:1250
Kept_Filter(const Kept_Filter &k) noexcept(std::is_nothrow_copy_constructible_v< F >)
Copy a moved filter, or use the same lvalue.
Definition tpl_graph.H:1262
Kept_Filter & operator=(const Kept_Filter &)=delete
Kept_Filter(F &f) noexcept
Use f where it is.
Definition tpl_graph.H:1255
F & get() const noexcept
The filter.
Definition tpl_graph.H:1274
Kept_Filter(F &&f) noexcept(std::is_nothrow_move_constructible_v< F >)
Keep f, moved.
Definition tpl_graph.H:1258
Kept_Filter(Kept_Filter &&k) noexcept(std::is_nothrow_move_constructible_v< F >)
Move a moved filter, or use the same lvalue.
Definition tpl_graph.H:1267
Iterator on the arcs of a graph.
Definition tpl_graph.H:854
Node_Arc_Iterator(Node *src) noexcept
Constructs an iterator on the node src.
Definition tpl_graph.H:868
Node * get_node_ne() const noexcept
Definition tpl_graph.H:926
Arc * get_curr() const
See get_current_arc().
Definition tpl_graph.H:885
Node * get_tgt_node_ne() const noexcept
Definition tpl_graph.H:916
Arc_Node * get_current_arc_node() const
Definition tpl_graph.H:874
Arc * get_curr_ne() const noexcept
Return the current arc, which must exist.
Definition tpl_graph.H:891
Node_Arc_Iterator()=default
The container type (a node)
Arc_Node * get_current_arc_node_ne() const noexcept
Definition tpl_graph.H:879
Arc * get_current_arc_ne() const noexcept
Definition tpl_graph.H:903
Arc * get_current_arc() const
Return the current arc.
Definition tpl_graph.H:898
Node * Set_Type
The type of data of set.
Definition tpl_graph.H:860
Node * get_tgt_node() const
Return the connected node to source node (src passed in construction time) through the current arc.
Definition tpl_graph.H:911
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
Dlink & get_node_dlink() noexcept
Return a reference to the internal node Dlink for sorting.
Definition tpl_graph.H:498
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:590
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
Definition tpl_graph.H:609
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Dlink & get_arc_dlink() noexcept
Return a reference to the internal arc Dlink for sorting.
Definition tpl_graph.H:512
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:1007
Arc * get_first_arc() const
Return any arc in the graph.
Definition tpl_graph.H:793
static Arc * dlink_to_arc(Dlink *p) noexcept
Definition tpl_graph.H:470
static Arc_Node * dlink_to_arc_node(Dlink *p) noexcept
Definition tpl_graph.H:475
static Node * dlink_to_node(Dlink *p) noexcept
Definition tpl_graph.H:465
virtual Arc * connect_arc(Arc *arc) noexcept
Connect a previously disconnected arc to the graph.
Definition tpl_graph.H:757
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:447
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:673
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Definition tpl_graph.H:557
static Arc * void_to_arc(Arc_Node *arc_node) noexcept
Definition tpl_graph.H:480
_Graph_Node Node
The graph type.
Definition tpl_graph.H:443
virtual void disconnect_arc(Arc *arc) noexcept
Disconnect an arc from graph.
Definition tpl_graph.H:723
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:444
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
List_Graph()=default
Construct an empty graph.
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1291
typename Itor::Set_Type Set_Type
The element type: Node*.
Definition tpl_graph.H:1293
Node_Iterator()=default
The set: the arcs of a graph.
Node_Iterator(const GT &g, Show_Node sn=Show_Node())
Construct a iterator with filter sn
Definition tpl_graph.H:1302
Functor that traverses the arcs of a graph and performs an operation.
Definition tpl_graph.H:3743
void operator()(const GT &g, typename GT::Node *p, Operation op=Operation()) const
Call to `op on each arc of a node.
Definition tpl_graph.H:3776
void operator()(GT &g, Operation op=Operation()) const
Definition tpl_graph.H:3764
void operator()(const GT &g, Operation op=Operation()) const
Call to `op on each arc.
Definition tpl_graph.H:3758
Operate_On_Arcs(SA __sa=SA())
Initialize the functor with the filter sa
Definition tpl_graph.H:3748
void operator()(GT &g, typename GT::Node *p, Operation op=Operation()) const
Definition tpl_graph.H:3783
Functor that traverses the nodes of a graph and performs an operation.
Definition tpl_graph.H:3698
Operate_On_Nodes(SN __sn=SN())
Initialize the functor with the filter sa
Definition tpl_graph.H:3703
void operator()(const GT &g, Operation op=Operation())
Call to operation on each node.
Definition tpl_graph.H:3713
void operator()(GT &g, Operation op=Operation())
Call to operation on each node.
Definition tpl_graph.H:3724
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Out_Iterator()=default
void prev()
Move to the previous arc that the filter shows.
Definition tpl_graph.H:1985
Out_Iterator(typename GT::Node *p, Show_Arc sa=Show_Arc())
Definition tpl_graph.H:1963
void next_ne() noexcept(NothrowPredicateWith< Show_Arc &, typename GT::Arc * >)
next() without the check of the current arc; noexcept when the filter is, else what the filter throws...
Definition tpl_graph.H:1977
typename GT::Arc * Item_Type
Definition tpl_graph.H:1959
Digraph_Iterator< GT, __Out_Filt< GT > > Base
Definition tpl_graph.H:1943
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
Node * get_current_node_ne() const noexcept
Definition tpl_graph.H:4425
Node * get_current_node() const
Return the current node of a path.
Definition tpl_graph.H:4420
const Path_Desc & get_curr_path_desc_ne() const noexcept
Definition tpl_graph.H:4407
std::tuple< Node *, Arc * > get_tuple() const
Return a tuple with the current node and arc.
Definition tpl_graph.H:4488
std::tuple< Node *, Arc * > get_tuple_ne() const noexcept
Definition tpl_graph.H:4493
Node * get_curr_ne() const noexcept
Return the current node, which must exist.
Definition tpl_graph.H:4453
Arc * get_current_arc_ne() const noexcept
Definition tpl_graph.H:4447
const Path_Desc & get_curr_path_desc() const
Definition tpl_graph.H:4412
Arc * get_current_arc() const
Return the current arc of a path.
Definition tpl_graph.H:4440
Iterator(const Path &path) noexcept
Create an iterator on the first node of path
Definition tpl_graph.H:4402
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:4512
Node * get_curr() const
Definition tpl_graph.H:4458
Aleph::const_iterator_t< DynDlist< Path_Desc > > Base
Definition tpl_graph.H:4398
std::pair< Node *, Arc * > get_pair() const
Return a pair with the current node and arc.
Definition tpl_graph.H:4473
bool has_current_arc() const noexcept
Return true if iterator has current arc.
Definition tpl_graph.H:4506
Path on a graph.
Definition tpl_graph.H:3832
bool is_cycle() const
Return true if this is a cycle; throws if path is empty.
Definition tpl_graph.H:4351
typename GT::Node Node
Definition tpl_graph.H:3843
void insert(Arc *arc)
Insert an arc as the first of a path.
Definition tpl_graph.H:4195
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
Definition tpl_graph.H:4529
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:3999
Path & operator=(const Path &path)
Copy assignment.
Definition tpl_graph.H:4020
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Definition tpl_graph.H:4576
typename GT::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:3838
void init(Node *start_node)
Set the first node of a path.
Definition tpl_graph.H:3947
void append(Node *node)
Append a node to the path.
Definition tpl_graph.H:4085
Path & operator=(Path &&path) noexcept
Move assignment.
Definition tpl_graph.H:4032
Path() noexcept
Definition tpl_graph.H:3936
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
bool check_directed() const
Return true if the directed path is consistent: each arc goes from its node to the next one,...
Definition tpl_graph.H:3904
bool contains_node(Node *node) const noexcept
Return true if node belongs to the path.
Definition tpl_graph.H:4547
void insert_directed(Node *p)
Insert a node at the beginning of a directed path.
Definition tpl_graph.H:4266
const GT * g
Definition tpl_graph.H:3841
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
bool operator==(const Path &p) const noexcept
Return true if this is equal to p,.
Definition tpl_graph.H:4596
bool operator!=(const Path &p) const noexcept
Return true if this is not equal to p,.
Definition tpl_graph.H:4602
void check_graph()
Definition tpl_graph.H:3872
Iterator get_it() const
Returns an iterator on the path.
Definition tpl_graph.H:4519
Path(Path &&path) noexcept
Move constructor.
Definition tpl_graph.H:4017
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
typename GT::Node_Type Node_Type
The type of data stored in the nodes.
Definition tpl_graph.H:3835
bool check() const
Return true if the path is consistent: each arc joins its node with the next one, either way,...
Definition tpl_graph.H:3880
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
Definition tpl_graph.H:3927
Arc * get_last_arc() const
Return the last arc of a path; throws overflow_error if the path is empty.
Definition tpl_graph.H:4339
Path(const GT &__g) noexcept
Construct a empty path on graph __g
Definition tpl_graph.H:3933
bool contains_arc(Arc *arc) const noexcept
Return true if arc belongs to the path.
Definition tpl_graph.H:4556
void swap(Path &path) noexcept
Fast swap between two paths (constant time)
Definition tpl_graph.H:4383
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:3977
Node * remove_first_node()
Remove the first node of a path.
Definition tpl_graph.H:4376
Arc * get_first_arc() const
Return the first arc of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4332
void clear()
Empties the container.
Definition tpl_graph.H:4011
DynDlist< Path_Desc > list
Definition tpl_graph.H:3870
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
void append_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:4124
typename GT::Arc Arc
Definition tpl_graph.H:3844
void insert_directed(Arc *arc)
Append an arc to a directed path.
Definition tpl_graph.H:4299
Path(const GT &_g, Node *start_node)
Construct a path starting from a given node.
Definition tpl_graph.H:3959
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition tpl_graph.H:4565
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
Path(const Path &path)
Copy constructor.
Definition tpl_graph.H:4014
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:4363
void insert(Node *node)
Insert a node to the path.
Definition tpl_graph.H:4228
const GT & get_graph() const noexcept
Get a constant reference to the graph.
Definition tpl_graph.H:3921
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Definition tpl_graph.H:4540
void append_directed(Arc *arc)
Append an arc to a directed path.
Definition tpl_graph.H:4164
Filter the incoming arcs.
Definition tpl_graph.H:1743
GT::Node * tgt
Definition tpl_graph.H:1744
bool operator()(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1751
GT::Node * get_node(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1757
__In_Filt(typename GT::Node *__tgt) noexcept
Definition tpl_graph.H:1747
Filter the outcoming arcs.
Definition tpl_graph.H:1713
bool operator()(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1721
GT::Node * get_node(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1727
__Out_Filt(typename GT::Node *__src) noexcept
Definition tpl_graph.H:1717
The iterator on the arcs into a node of a digraph type, which In_Iterator is on a type built with the...
Definition graph-dry.H:133
Common methods for the arc of a graph.
Definition graph-dry.H:787
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
void * get_connected_node(void *node) noexcept
Definition graph-dry.H:859
ArcInfo arc_info
Definition graph-dry.H:801
void * src_node
Definition graph-dry.H:792
Common attributes and methods for nodes (vertexes) belonging to graphs.
Definition graph-dry.H:694
NodeInfo node_info
Definition graph-dry.H:702
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:713
Common methods to the Aleph-w ( ) graph classes.
Definition graph-dry.H:877
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1326
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
size_t num_nodes
Definition graph-dry.H:901
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * insert_node(const Node_Type &node_info)
Allocate a new node, set by copy its data content and insert it into the graph.
Definition graph-dry.H:1402
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
size_t num_arcs
Definition graph-dry.H:902
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Definition graph-dry.H:931
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1312
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
void common_swap(GT &g) noexcept
Definition graph-dry.H:921
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1332
Arc * insert_arc(Node *src, Node *tgt, const Arc_Type &arc_info)
Create and insert a new arc linking two nodes and copying data.
Definition graph-dry.H:1479
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1281
void remove_arcs_if(Predicate pred)
Remove all arcs matching a predicate.
Definition graph-dry.H:4177
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
A graph usable by the graph algorithms.
A graph type built with the Digraph template.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
Generic filter iterator wrapper for Aleph containers.
Graph base classes and common utilities via CRTP.
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered incoming arcs to p.
Definition tpl_graph.H:2606
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Arc filtered searching given two nodes.
Definition tpl_graph.H:3574
void for_each_out_arc(typename GT::Node *p, Op op=Op())
Traverse the outcoming arcs of a node and executes an operation.
Definition tpl_graph.H:3386
constexpr bool nothrow_arc_filter_v
Whether a filter of type SA of the arcs of GT is copied, moved and called without exceptions: then an...
Definition tpl_graph.H:1043
GT::Node * mapped_node(typename GT::Node *p) noexcept
Return the mapped node through the cookie of p
Definition tpl_graph.H:3622
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...
Definition tpl_graph.H:1320
#define ARC_COOKIE(p)
Return the arc cookie
void map_arcs(typename GTS::Arc *p, typename GTT::Arc *q) noexcept
Map two arcs of different types of graphs through their cookies.
Definition tpl_graph.H:4759
bool traverse_in_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of incoming arcs of a node.
Definition tpl_graph.H:2947
DynList< ArcPair< GT > > out_pairs(typename GT::Node *p, SA sa=SA())
Return the filtered outcoming pairs of (arc,node) related to node p
Definition tpl_graph.H:2778
Container< T > arcs_map(GT &g, Op transformation, SA sa=SA())
Map the filtered arcs of a graph to a transformed type.
Definition tpl_graph.H:1507
bool traverse_arcs(typename GT::Node *p, Operation op=Operation())
Generic arcs traverse of a node.
Definition tpl_graph.H:2891
bool traverse_out_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of outcoming arcs of a node.
Definition tpl_graph.H:3373
#define ALEPH_GRAPH_COPY_MOVE_CTORS(GraphClass)
Macro to generate copy/move constructors and assignment operators.
Definition graph-dry.H:4311
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
void inter_copy_graph(GTT &gtgt, const GTS &gsrc, const bool cookie_map=false)
Copy between different types of graphs.
Definition tpl_graph.H:4884
GT::Arc * search_directed_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Searching of directed arc linking two nodes.
Definition tpl_graph.H:3603
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.
Definition tpl_graph.H:1381
Container< T > nodes_map(GT &g, Op transformation, SN sn=SN())
Map the filtered nodes of a graph to a transformed type.
Definition tpl_graph.H:1453
#define ARC_BITS(p)
Return the control bits of arc p.
size_t out_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered out degree of node p
Definition tpl_graph.H:2859
#define NODE_COOKIE(p)
Return the node cookie
DynList< typename GT::Node * > out_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered outcoming arcs of p.
Definition tpl_graph.H:2583
size_t in_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered in degree of node p.
Definition tpl_graph.H:2806
std::tuple< typename GT::Arc *, typename GT::Node * > ArcPair
Alias used for encapsulating a pair of arc and node (related between them).
Definition tpl_graph.H:1702
T foldl_nodes(GT &g, const T &init, Op operation, SN sn=SN())
Fold the filtered nodes of a graph.
Definition tpl_graph.H:1611
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
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
DynList< typename GT::Arc * > in_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
Definition tpl_graph.H:2671
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
T foldl_arcs(GT &g, const T &init, Op operation, SA sa=SA())
Fold the filtered arcs of a graph.
Definition tpl_graph.H:1634
#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.
Definition tpl_graph.H:1403
void for_each_in_arc(typename GT::Node *p, Op op=Op())
Traverse the incoming arcs of a node and executes an operation.
Definition tpl_graph.H:2989
void copy_graph(GT &gtgt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Definition tpl_graph.H:4794
void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
Map two nodes of different types of graphs through their cookies.
Definition tpl_graph.H:4731
DynList< ArcPair< GT > > in_pairs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming pairs of (arc,node) related to node p
Definition tpl_graph.H:2731
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Depth-first search of a path between two nodes.
Definition tpl_graph.H:4695
DynList< typename GT::Arc * > out_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
Definition tpl_graph.H:2652
@ Spanning_Tree
Definition aleph-graph.H:79
@ Find_Path
Definition aleph-graph.H:76
bool traverse_arcs_into(const GT &g, typename GT::Node *p, SA &sa, Op op)
Calls op(a) on each arc a of g into p that sa shows, while it returns true, and tells whether it went...
Definition tpl_graph.H:2559
size_t digraph_in_degree(const GT &g, typename GT::Node *p, SA &sa)
The number of arcs of the digraph g into p that sa shows, among the arcs of the graph,...
Definition tpl_graph.H:2541
bool traverse_digraph_arcs_into(const GT &g, typename GT::Node *p, SA &sa, Op op)
Calls op(a) on each arc a of the digraph g into p that sa shows, while it returns true,...
Definition tpl_graph.H:2527
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
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
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
@ Call
dst <- callee(args...)
bool all_out_arc(typename GT::Node *p, Op op=Op())
Test if the outcoming arcs meet a condition.
Definition tpl_graph.H:3403
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
auto map_out_arcs(typename GT::Node *p, Op op)
Map the outcoming arcs to a transformation,.
Definition tpl_graph.H:3478
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
auto map_in_arcs(typename GT::Node *p, Op op)
Map the incoming arcs to a transformation,.
Definition tpl_graph.H:3183
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
T foldl_in_arcs(typename GT::Node *p, const T &init, Op op)
Fold the incoming arcs of a node.
Definition tpl_graph.H:3275
DynList< typename GT::Arc * > filter_out_arcs(typename GT::Node *p, Op cond)
Filter the outcoming arcs meeting an condition.
Definition tpl_graph.H:3547
auto search_out_arc(typename GT::Node *p, Op op=Op())
Search an outcoming arc to a node satisfying a condition op.
Definition tpl_graph.H:3444
static bool __find_path_depth_first(const GT &g, typename GT::Node *curr_node, typename GT::Arc *curr_arc, typename GT::Node *end_node, Path< GT > &path)
Definition tpl_graph.H:4619
T foldl_out_arcs(typename GT::Node *p, const T &init, Op op)
Fold the outcoming arcs of a node.
Definition tpl_graph.H:3528
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 exists_out_arc(typename GT::Node *p, Op op=Op())
Test if it exists an outcoming arc satisfying an operation.
Definition tpl_graph.H:3423
GT::Arc * mapped_arc(typename GT::Arc *a) noexcept
Return the mapped arc through the cookie of p
Definition tpl_graph.H:3629
bool are_equal(const GT &g1, const GT &g2)
Fast graph comparison.
static std::atomic< bool > init
Definition hash-fct.C:54
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Arc_Iterator()=default
Type of set all the arcs.
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1203
typename Itor::Set_Type Set_Type
Type of element: Arc*.
Definition tpl_graph.H:1204
Arc_Iterator(const GT &g, Show_Arc sa=Show_Arc())
Constructor,.
Definition tpl_graph.H:1213
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
Default copy arc functor.
Definition tpl_graph.H:4866
void operator()(typename GTT::Arc *tgt, typename GTS::Arc *src)
Definition tpl_graph.H:4867
Default copy node functor.
Definition tpl_graph.H:4853
void operator()(typename GTT::Node *tgt, typename GTS::Node *src) noexcept
Definition tpl_graph.H:4854
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
void set_cookie(void *) noexcept
Definition tpl_graph.H:1030
bool operator()(typename GT::Arc *) const noexcept
Definition tpl_graph.H:1025
Default filter for the graph nodes.
Definition tpl_graph.H:1228
bool operator()(typename GT::Node *) const noexcept
Definition tpl_graph.H:1229
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Arc_Node * src_arc_node
The type of data stored in the arc.
Definition tpl_graph.H:238
Graph_Arc(const Graph_Arc &arc)
Definition tpl_graph.H:273
Graph_Arc(Arc_Info &&info=Arc_Info()) noexcept(std::is_nothrow_move_constructible_v< Arc_Info >)
Move or rvalue constructor.
Definition tpl_graph.H:266
Graph_Arc(const Arc_Info &info) noexcept(std::is_nothrow_copy_constructible_v< Arc_Info >)
Copy constructor.
Definition tpl_graph.H:250
Graph_Arc & operator=(const Graph_Arc &arc)
Definition tpl_graph.H:279
_Arc_Info Arc_Info
Definition tpl_graph.H:236
Arc_Node * tgt_arc_node
Definition tpl_graph.H:239
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:126
__Node_Info Node_Info
Definition tpl_graph.H:131
Graph_Node(const Node_Info &info) noexcept(std::is_nothrow_copy_constructible_v< Node_Info >)
The type of data stored in the node.
Definition tpl_graph.H:142
Graph_Node(const Graph_Node &node) noexcept(std::is_nothrow_copy_constructible_v< Node_Info >)
Definition tpl_graph.H:164
Graph_Node(Node_Info &&info=Node_Info()) noexcept(std::is_nothrow_move_constructible_v< Node_Info >)
Move or rvalue constructor.
Definition tpl_graph.H:157
Graph_Node(Graph_Node *node)
Copy constructor from a node pointer.
Definition tpl_graph.H:193
Graph_Node & operator=(const Graph_Node &node)
Definition tpl_graph.H:171
Iterator on all arcs of a graph.
Definition tpl_graph.H:941
Arc_Iterator()=default
The type of set.
Node * get_tgt_node() const
Return the target node of current arc.
Definition tpl_graph.H:992
Arc_Iterator(const List_Graph &g) noexcept
Initialize an iterator for all the arc of g
Definition tpl_graph.H:949
Arc * get_curr_ne() const noexcept
Definition tpl_graph.H:971
Arc * get_current_arc_ne() const noexcept
Return the current arc. Throw overflow_error if there is no one.
Definition tpl_graph.H:956
Node * get_src_node() const
Return the source node of the current arc.
Definition tpl_graph.H:979
Node_Iterator(const List_Graph &g)
Construct an iterator on the nodes of g
Definition tpl_graph.H:823
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Node_Arc_Iterator(typename GT::Node *p, Show_Arc sa=Show_Arc())
Construct and filtered iterator according to condition sa.
Definition tpl_graph.H:1174
Node_Arc_Iterator()=default
Type of set: p's arcs.
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1164
typename Itor::Set_Type Set_Type
type of element: Arc*
Definition tpl_graph.H:1165
Filter of painter arcs with that are set the Spanning_Tree control bit.
Definition tpl_graph.H:5047
Painted_Min_Spanning_Tree() noexcept(std::is_nothrow_default_constructible_v< Distance >)
Build the filter with a distance accessor built by Distance().
Definition tpl_graph.H:5055
Painted_Min_Spanning_Tree(Distance __distance)
Build the filter with a distance accessor.
Definition tpl_graph.H:5065
Distance distance
Distance accessor for the arc weights.
Definition tpl_graph.H:5052
Distance::Distance_Type dist
Accumulative distance from the first seen arc until the last seen.
Definition tpl_graph.H:5049
bool operator()(typename GT::Arc *a)
Show the arc if it is painted, and then add its weight to dist.
Definition tpl_graph.H:5076
Path_Desc(Node *_node=nullptr, Arc *_arc=nullptr) noexcept
Definition tpl_graph.H:3851
bool operator==(const Path_Desc &r) const noexcept
Definition tpl_graph.H:3855
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:797
Distance accessor.
Common node iterator for graph having its node derived from Dlink class.
Definition graph-dry.H:497
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r
Stack of the frames of a depth-first search that does not recurse.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic key-value map based on balanced binary search trees.
Comprehensive sorting algorithms and search utilities for Aleph-w.
Treap with rank (order statistics).
Treap< int >::Node * last_node
Definition writeTreap.C:60