Aleph-w 3.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 <tuple>
50# include <utility>
51# include <functional>
52# include <bitArray.H>
53# include <tpl_dynArray.H>
54# include <tpl_sort_utils.H>
55# include <tpl_dynMapTree.H>
56# include <tpl_dynDlist.H>
57# include <tpl_treapRk.H>
58# include <filter_iterator.H>
59# include <aleph-graph.H>
60# include <graph-dry.H>
61# include <ah-graph-concepts.H>
62# include <ah-errors.H>
63
64using namespace Aleph;
65
66namespace Aleph
67{
68 template <typename Node_Info>
69 struct Graph_Node;
70
71 template <typename Arc_Info>
72 struct Graph_Arc;
73
74 class Arc_Node;
75
76 template <class GT>
77 class Path;
78
79 template <typename __Graph_Node, typename __Graph_Arc>
80 class List_Graph;
81
82 // List_Digraph is defined as a type alias later in this file
83 // using the generic Digraph<> template wrapper
84
85 template <class GT>
86 class Mat_Graph;
87
88 template <typename MT, typename Entry_Info, typename Copy>
89 class Ady_MaT;
90
118 template <typename __Node_Info = Empty_Class>
120 : public Dlink,
121 public GTNodeCommon<__Node_Info>
122 {
123 friend class GTNodeCommon<__Node_Info>;
124 friend class Arc_Node;
125
128
139 { /* empty */
140 }
141
152 : Base(std::move(info))
153 {
154 /* empty */
155 }
156
158 : Graph_Node(node.node_info)
159 {
160 // empty
161 }
162
164 {
165 if (&node == this)
166 return *this;
167 this->node_info = node.node_info;
168 return *this;
169 }
170
186 : Base(node->get_info())
187 {
188 /* empty */
189 }
190
192 };
193
219 template <typename _Arc_Info = Empty_Class>
221 : public Dlink,
222 public GTArcCommon<_Arc_Info>
223 {
224 friend class GTArcCommon<_Arc_Info>;
225
227
229
230 Arc_Node *src_arc_node = nullptr; // pointer to source node
231 Arc_Node *tgt_arc_node = nullptr; // pointer to target node
232
243 : Base(info)
244 {
245 /* empty */
246 }
247
258 : Base(std::move(info))
259 {
260 /* empty */
261 }
262
263 Graph_Arc(const Graph_Arc & arc)
264 : Graph_Arc(arc.arc_info)
265 {
266 /* empty */
267 }
268
270 {
271 if (&arc == this)
272 return *this;
273 this->arc_info = arc.arc_info;
274 return *this;
275 }
276 };
277
278 class Arc_Node : public Dlink
279 {
280 public:
281 void *arc = nullptr;
282
283 Arc_Node() noexcept : arc(nullptr) {}
284
286 };
287
424 template <typename _Graph_Node = Graph_Node<unsigned long>,
425 typename _Graph_Arc = Graph_Arc<unsigned long>>
427 : public GraphCommon<List_Graph<_Graph_Node, _Graph_Arc>,
428 _Graph_Node, _Graph_Arc>
429 {
430 public:
431 using GT = List_Graph;
432
434 using Arc = _Graph_Arc;
435
437 using Node_Type = typename Node::Node_Type;
438
440 using Arc_Type = typename Arc::Arc_Type;
441
444
447
450
451 private:
454
455 static Node * dlink_to_node(Dlink *p) noexcept
456 {
457 return static_cast<Node *>(p);
458 }
459
460 static Arc * dlink_to_arc(Dlink *p) noexcept
461 {
462 return static_cast<Arc *>(p);
463 }
464
465 static Arc_Node * dlink_to_arc_node(Dlink *p) noexcept
466 {
467 return static_cast<Arc_Node *>(p);
468 }
469
470 static Arc * void_to_arc(Arc_Node *arc_node) noexcept
471 {
472 return static_cast<Arc *>(arc_node->arc);
473 }
474
475 public:
489
525 virtual Node * insert_node(Node *node) noexcept
526 {
527 ++this->num_nodes;
528 node_list.append(node);
529 return node;
530 }
531
544 virtual void remove_node(Node *node) noexcept
545 {
546 assert(node != nullptr);
547 if (not this->digraph)
548 while (not node->arc_list.is_empty()) // remove each arc related to node
549 {
550 Arc_Node *arc_node = dlink_to_arc_node(node->arc_list.get_next());
551 Arc *arc = void_to_arc(arc_node);
552 remove_arc(arc);
553 }
554 else // Scan all the arcs and remove those related to node
555 this->remove_arcs_if([node, this](auto a)
556 {
557 return this->get_src_node(a) == node or this->get_tgt_node(a) == node;
558 });
559
560 // At this point the node has not more arcs
561 node->del(); // unlink it from arc_list
562 --this->num_nodes;
563 delete node;
564 }
565
578 {
579 ah_range_error_if(this->num_nodes == 0) << "Graph has not nodes";
580
581 return dlink_to_node(const_cast<Dlink &>(node_list).get_next());
582 }
583
596 Arc * get_first_arc(Node *node) const
597 {
598 ah_range_error_if(get_num_arcs (node) == 0) << "node has not arcs";
599
600 void *arc = dlink_to_arc_node(node->arc_list.get_next())->arc;
601 return reinterpret_cast<Arc *>(arc);
602 }
603
604 private:
605 Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
606 {
607 assert(src_node != nullptr and tgt_node != nullptr and a != nullptr);
608 Arc *arc = static_cast<Arc *>(a);
609 arc->src_node = src_node;
610 arc->tgt_node = tgt_node;
611
612 // step 3 (partial): allocate Arc_Node for src_node
613 std::unique_ptr<Arc_Node> src_arc_node = std::make_unique<Arc_Node>(arc);
614
615 // step 2: if graph ==> allocate Arc_Node for tgt_node
616 if (not this->digraph) // if digraph ==> do not insert in another node
617 { // insertion in the target node
618 if (src_node == tgt_node) // is it a loop?
619 arc->tgt_arc_node = src_arc_node.get();
620 else
621 { // allocate arc node for tgt_node
622 std::unique_ptr<Arc_Node> tgt_arc_node = std::make_unique<Arc_Node>(arc);
623
624 // insertion in an adjacency list of tgt_node
625 arc->tgt_arc_node = tgt_arc_node.get();
626 tgt_node->arc_list.append(tgt_arc_node.get());
627 ++tgt_node->num_arcs;
628 tgt_arc_node.release();
629 }
630 }
631
632 // step 3 (remainder): insertion in the adjacency list of src_node
633 arc->src_arc_node = src_arc_node.get();
634 src_node->arc_list.append(src_arc_node.get());
635 ++src_node->num_arcs;
636
637 arc_list.append(arc); // step 4: insert in graph arc list
638 ++this->num_arcs;
639 src_arc_node.release();
640
641 return arc;
642 }
643
644 public:
650 virtual void remove_arc(Arc *arc) noexcept
651 {
652 assert(arc != nullptr);
653 // step 1: remove Arc_node from src_node
654 Node *src_node = this->get_src_node(arc);
655 Arc_Node *src_arc_node = arc->src_arc_node;
656
657 src_arc_node->del(); // unlink src_node from node list
658 --src_node->num_arcs; // decrement arc counter of src_node
659 delete src_arc_node; // free memory
660
661 if (not this->digraph)
662 { // arc removal in target node
663 Node *tgt_node = this->get_tgt_node(arc);
664 if (src_node != tgt_node) // check for loop removal
665 { // step 2: remove Arc_node from tgt_node
666 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
667 tgt_arc_node->del();
668 --tgt_node->num_arcs;
669 delete tgt_arc_node;
670 }
671 }
672
673 // arc removal from graph
674 arc->del(); // unlink arc from the graph arc list
675 --this->num_arcs;
676 delete arc;
677 }
678
700 virtual void disconnect_arc(Arc *arc) noexcept
701 {
702 assert(arc != nullptr);
703 Node *src_node = this->get_src_node(arc);
704 Arc_Node *src_arc_node = arc->src_arc_node;
705 src_arc_node->del(); // unlink src_node from node list
706 --src_node->num_arcs; // decrement arc counter of src_node
707
708 if (not this->digraph)
709 { // arc removal in target node
710 Node *tgt_node = this->get_tgt_node(arc);
711 if (src_node != tgt_node) // check for loop removal
712 { // step 2: remove Arc_node from target node tgt_node
713 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
714 tgt_arc_node->del();
715 --tgt_node->num_arcs;
716 }
717 }
718
719 // arc removal from graph
720 arc->del(); // unlink arc from graph arc list
721 --this->num_arcs;
722 }
723
734 virtual Arc * connect_arc(Arc *arc) noexcept
735 {
736 assert(arc != nullptr);
737 Node *src_node = this->get_src_node(arc);
738 Node *tgt_node = this->get_tgt_node(arc);
739 Arc_Node *src_arc_node = arc->src_arc_node;
740 Arc_Node *tgt_arc_node = arc->tgt_arc_node;
741
742 if (not this->digraph) // if digraph ==> no need to insert in other node
743 { // insertion in target node
744 if (src_node != tgt_node) // check if it is a loop
745 { // insertion in adjacency list of tgt_node
746 tgt_node->arc_list.append(tgt_arc_node);
747 ++tgt_node->num_arcs;
748 }
749 }
750
751 src_node->arc_list.append(src_arc_node);
752 ++src_node->num_arcs;
753 arc_list.append(arc);
754 ++this->num_arcs;
755
756 return arc;
757 }
758
771 {
772 ah_range_error_if(this->get_num_arcs() == 0) << "Graph has not arcs";
773
774 return dlink_to_arc(const_cast<Dlink &>(arc_list).get_next());
775 }
776
777 // sort_nodes() and sort_arcs() are inherited from GraphCommon via CRTP
778 // They use the get_node_dlink() and get_arc_dlink() accessors defined above
779
782
783
785 {
786 clear_graph(*this);
787 }
788
795 struct Node_Iterator : public GTNodeIterator<List_Graph>
796 {
798
802 {
803 // empty
804 }
805 };
806
831 {
833
834 public:
835 using Item_Type = Arc *;
836
837 using Set_Type = Node *;
838
839 Node_Arc_Iterator() = default;
840
846 : Dlink::Iterator(&(src->arc_list)), src_node(src)
847 {
848 // empty
849 }
850
855
860
862 Arc * get_curr() const
863 {
864 return static_cast<Arc *>(get_current_arc_node()->arc);
865 }
866
869 {
870 return static_cast<Arc *>(get_current_arc_node_ne()->arc);
871 }
872
876 {
877 return get_curr();
878 }
879
881 {
882 return get_curr_ne();
883 }
884
889 {
890 return static_cast<Node *>(get_current_arc()->get_connected_node(src_node));
891 }
892
894 {
895 return static_cast<Node *>(get_current_arc_ne()->get_connected_node(src_node));
896 }
897
898 Node * get_node() const
899 {
900 return get_tgt_node();
901 }
902
904 {
905 return get_tgt_node_ne();
906 }
907 };
908
918 {
919 using Item_Type = Arc *;
920
922
923 Arc_Iterator() = default;
924
927 : Dlink::Iterator(&const_cast<Dlink &>(g.arc_list))
928 {
929 // empty
930 }
931
937
939 {
940 return dlink_to_arc(const_cast<Dlink *>(Dlink::Iterator::get_curr()));
941 }
942
943 Arc * get_curr() const
944 {
945 return get_current_arc();
946 }
947
949 {
950 return get_current_arc_ne();
951 }
952
957 {
958 return static_cast<Node *>(get_current_arc()->src_node);
959 }
960
962 {
963 return static_cast<Node *>(get_current_arc_ne()->src_node);
964 }
965
970 {
971 return static_cast<Node *>(get_current_arc()->tgt_node);
972 }
973
975 {
976 return static_cast<Node *>(get_current_arc_ne()->tgt_node);
977 }
978 };
979
981 List_Graph() = default;
982
984 void swap(List_Graph & g) noexcept
985 {
986 this->common_swap(g);
987 node_list.swap(&g.node_list);
988 arc_list.swap(&g.arc_list);
989 }
990 };
991
999 template <class GT>
1001 {
1002 bool operator()(typename GT::Arc * /* arc */) const noexcept
1003 {
1004 return true;
1005 }
1006
1007 void set_cookie(void *) noexcept
1008 { /* empty */
1009 }
1010 };
1011
1115 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1117 public Filter_Iterator<typename GT::Node *,
1118 typename GT::Node_Arc_Iterator,
1119 Show_Arc>
1120 {
1122 typename GT::Node_Arc_Iterator,
1123 Show_Arc>;
1124
1125 using Itor = Filter_Iterator<typename GT::Node *,
1126 typename GT::Node_Arc_Iterator,
1127 Show_Arc>;
1128
1129 using Item_Type = typename Itor::Item_Type;
1130 using Set_Type = typename Itor::Set_Type;
1131
1133
1140 : Itor(p, sa)
1141 {
1142 // empty
1143 }
1144 };
1145
1162 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1164 public Filter_Iterator<GT, typename GT::Arc_Iterator, Show_Arc>
1165 {
1167
1168 using Item_Type = typename Itor::Item_Type;
1169 using Set_Type = typename Itor::Set_Type;
1170
1171 Arc_Iterator() = default;
1172
1179 : Itor(g, sa)
1180 {
1181 // empty
1182 }
1183 };
1184
1191 template <class GT>
1193 {
1194 bool operator()(typename GT::Node *) const noexcept
1195 {
1196 return true;
1197 }
1198 };
1199
1204 template <class GT, class Show_Node = Dft_Show_Node<GT>>
1206 public Filter_Iterator<GT, typename GT::Node_Iterator, Show_Node>
1207 {
1208 public:
1210
1211 using Item_Type = typename Itor::Item_Type;
1212
1213 using Set_Type = typename Itor::Set_Type;
1214
1215 Node_Iterator() = default;
1216
1223 : Itor(g, sn)
1224 {
1225 /* empty */
1226 }
1227 };
1228
1238 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1239 requires std::is_invocable_r_v<void, Op &, typename GT::Node *>
1240 void for_each_node(const GT & g,
1241 Op operation,
1242 SN sn = SN())
1243 {
1244 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1245 Aleph::concepts_detail::invoke_as<void (typename GT::Node *)>::call(operation, it.get_curr());
1246 }
1247
1257 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1258 requires std::is_invocable_r_v<void, Op &, typename GT::Arc *>
1259 void for_each_arc(const GT & g,
1260 Op operation,
1261 SA sa = SA())
1262 {
1263 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1264 Aleph::concepts_detail::invoke_as<void (typename GT::Arc *)>::call(operation, it.get_curr());
1265 }
1266
1277 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1278 requires std::is_invocable_r_v<void, Op &, typename GT::Arc *>
1279 void for_each_arc(const GT & g,
1280 typename GT::Node *p,
1281 Op operation,
1282 SA sa = SA())
1283 {
1284 (void) g;
1285 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1286 Aleph::concepts_detail::invoke_as<void (typename GT::Arc *)>::call(operation, it.get_curr());
1287 }
1288
1299 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1300 requires std::is_invocable_r_v<bool, Op &, typename GT::Node *>
1301 bool forall_node(const GT & g,
1302 Op cond,
1303 SN sn = SN())
1304 {
1305 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1306 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Node *)>::call(cond, it.get_curr()))
1307 return false;
1308 return true;
1309 }
1310
1321 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1322 requires std::is_invocable_r_v<bool, Op &, typename GT::Arc *>
1323 bool forall_arc(const GT & g,
1324 Op cond,
1325 SA sa = SA())
1326 {
1327 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1328 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Arc *)>::call(cond, it.get_curr()))
1329 return false;
1330 return true;
1331 }
1332
1343 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1344 requires std::is_invocable_r_v<bool, Op &, typename GT::Arc *>
1345 bool forall_arc(typename GT::Node *p,
1346 Op cond,
1347 SA sa = SA())
1348 {
1349 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1350 if (not Aleph::concepts_detail::invoke_as<bool (typename GT::Arc *)>::call(cond, it.get_curr()))
1351 return false;
1352 return true;
1353 }
1354
1369 template <AlephGraph GT, typename T,
1370 template<typename> class Container = DynList,
1372 requires std::is_invocable_r_v<T, Op &, typename GT::Node *>
1374 Op transformation,
1375 SN sn = SN())
1376 {
1379 {
1380 ret_val.append(Aleph::concepts_detail::invoke_as<T (typename GT::Node *)>::call(transformation, p));
1381 }, sn);
1382 return ret_val;
1383 }
1384
1395 template <AlephGraph GT, typename T,
1396 template<typename> class Container = DynList,
1399 std::function<T (typename GT::Node *)> transformation,
1400 SN sn = SN())
1401 {
1402 // Forward through a lambda, not `transformation` itself: passing the
1403 // `std::function` as-is would make this call ambiguous between this
1404 // overload and the generic one (both exactly match a `std::function<T
1405 // (typename GT::Node *)>` argument once `Op` is deduced as that type).
1407 g, [&transformation](typename GT::Node * p) { return transformation(p); }, sn);
1408 }
1409
1423 template <AlephGraph GT, typename T,
1424 template<typename> class Container = DynList,
1425 ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1426 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
1428 Op transformation,
1429 SA sa = SA())
1430 {
1433 {
1434 ret_val.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(transformation, p));
1435 }, sa);
1436 return ret_val;
1437 }
1438
1447 template <AlephGraph GT, typename T,
1448 template<typename> class Container = DynList,
1451 std::function<T (typename GT::Arc *)> transformation,
1452 SA sa = SA())
1453 {
1454 // See the equivalent nodes_map overload for why this must go through a
1455 // lambda rather than forwarding `transformation` as-is.
1457 g, [&transformation](typename GT::Arc * a) { return transformation(a); }, sa);
1458 }
1459
1480 template <AlephGraph GT, typename T,
1481 template<typename> class Container = DynList,
1482 ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1483 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
1485 typename GT::Node *p,
1486 Op transformation,
1487 SA sa = SA())
1488 {
1490 for_each_arc<GT, SA>(g, p, [&ret_val, &transformation](typename GT::Arc *p)
1491 {
1493 }, sa);
1494 return ret_val;
1495 }
1496
1505 template <AlephGraph GT, typename T,
1506 template<typename> class Container = DynList,
1509 typename GT::Node *p,
1510 std::function<T (typename GT::Arc *)> transformation,
1511 SA sa = SA())
1512 {
1513 // See the equivalent nodes_map overload for why this must go through a
1514 // lambda rather than forwarding `transformation` as-is.
1516 g, p, [&transformation](typename GT::Arc * a) { return transformation(a); }, sa);
1517 }
1518
1529 template <AlephGraph GT, typename T, NodeFilter<GT> SN = Dft_Show_Node<GT>, class Op>
1530 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Node *>
1531 T foldl_nodes(GT & g, const T & init,
1532 Op operation,
1533 SN sn = SN())
1534 {
1535 T ret_val = init;
1536 for_each_node<GT, SN>(g, [&ret_val, &operation](typename GT::Node *p)
1537 {
1538 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Node *)>::call(operation, ret_val, p);
1539 }, sn);
1540 return ret_val;
1541 }
1542
1552 template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1553 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
1554 T foldl_arcs(GT & g, const T & init,
1555 Op operation,
1556 SA sa = SA())
1557 {
1558 T ret_val = init;
1559 for_each_arc<GT, SA>(g, [&ret_val, &operation](typename GT::Arc *a)
1560 {
1561 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(operation, ret_val, a);
1562 }, sa);
1563 return ret_val;
1564 }
1565
1576 template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>, class Op>
1577 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
1578 T foldl_arcs(GT & g, typename GT::Node *p,
1579 const T & init,
1580 Op operation,
1581 SA sa = SA())
1582 {
1583 T ret_val = init;
1584 for_each_arc<GT, SA>(g, p, [&ret_val, &operation](typename GT::Arc *a)
1585 {
1586 ret_val = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(operation, ret_val, a);
1587 }, sa);
1588 return ret_val;
1589 }
1590
1611 template <typename __Graph_Node = Graph_Node<int>,
1612 typename __Graph_Arc = Graph_Arc<int>>
1614
1615
1621 template <class GT>
1622 using ArcPair = std::tuple<typename GT::Arc *, typename GT::Node *>;
1623
1631 template <class GT>
1633 {
1634 typename GT::Node *src = nullptr;
1635
1636 public:
1638 { /* empty */
1639 }
1640
1641 bool operator()(typename GT::Arc *a) const noexcept
1642 {
1643 assert(src);
1644 return a->src_node == src;
1645 }
1646
1647 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1648 {
1649 assert(src);
1650 return static_cast<typename GT::Node *>(a->tgt_node);
1651 }
1652 };
1653
1661 template <class GT>
1663 {
1664 typename GT::Node *tgt = nullptr;
1665
1666 public:
1668 { /* empty */
1669 }
1670
1671 bool operator()(typename GT::Arc *a) const noexcept
1672 {
1673 assert(tgt);
1674 return a->tgt_node == tgt;
1675 }
1676
1677 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1678 {
1679 assert(tgt);
1680 return static_cast<typename GT::Node *>(a->src_node);
1681 }
1682 };
1683
1688 template <class GT, class Filter>
1690 {
1691 using Itor = Filter_Iterator<typename GT::Node *,
1692 typename GT::Node_Arc_Iterator, Filter>;
1693
1696
1697 public:
1698 using Item_Type = typename Itor::Item_Type;
1699
1701
1704 : filt(p), it(p, filt)
1705 {
1706 // empty
1707 }
1708
1711 void next()
1712 {
1713 it.next();
1714 }
1715
1717 {
1718 it.next_ne();
1719 }
1720
1723 void prev() { it.prev(); }
1724
1727 {
1728 return it.has_curr();
1729 }
1730
1733 typename GT::Arc * get_curr() const
1734 {
1735 return it.get_curr();
1736 }
1737
1741 {
1742 return it.get_curr_ne();
1743 }
1744
1747 auto get_current_arc() const
1748 {
1749 return get_curr();
1750 }
1751
1754 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
1755 {
1756 return filt.get_node(a);
1757 }
1758
1760 typename GT::Node * get_node() const
1761 {
1762 return this->get_node(this->get_curr());
1763 }
1764
1767 auto get_tgt_node() const
1768 {
1769 return get_node();
1770 }
1771
1773 typename GT::Node * get_node_ne() const noexcept
1774 {
1775 return this->get_node(this->get_curr_ne());
1776 }
1777
1781 {
1782 return get_node_ne();
1783 }
1784
1787 {
1788 it.reset_first();
1789 }
1790
1793 {
1794 it.reset_last();
1795 }
1796
1799 {
1800 put_itor_at_the_end(*this);
1801 }
1802 };
1803
1804
1809 template <class GT>
1811
1812
1817 template <class GT>
1819
1829 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1830 class Out_Iterator : public Digraph_Iterator<GT, __Out_Filt<GT>>
1831 {
1834
1836 {
1838 Base::next_ne();
1839 }
1840
1841 public:
1842 using Item_Type = typename GT::Arc *;
1843
1844 Out_Iterator() = default;
1845
1847 : Base(p), show_arc(sa)
1848 {
1850 }
1851
1852 void next()
1853 {
1854 Base::next();
1856 }
1857
1859 {
1860 Base::next_ne();
1862 }
1863 };
1864
1874 template <class GT, class SA = Dft_Show_Arc<GT>>
1875 class In_Iterator : public Digraph_Iterator<GT, __In_Filt<GT>>
1876 {
1879
1881 {
1883 Base::next_ne();
1884 }
1885
1886 public:
1887 using Item_Type = typename GT::Arc *;
1888
1889 In_Iterator() = default;
1890
1891 In_Iterator(typename GT::Node *p, SA sa = SA())
1892 : Base(p), show_arc(sa)
1893 {
1895 }
1896
1897 void next()
1898 {
1899 Base::next();
1901 }
1902
1904 {
1905 Base::next_ne();
1907 }
1908 };
1909
1919 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1921 {
1923 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1924 ret.append(it.get_node_ne());
1925 return ret;
1926 }
1927
1936 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1938 {
1940 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1941 ret.append(it.get_node_ne());
1942 return ret;
1943 }
1944
1952 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1954 {
1956 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1957 ret.append(it.get_curr());
1958 return ret;
1959 }
1960
1968 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1970 {
1972 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1973 ret.append(it.get_curr());
1974 return ret;
1975 }
1976
1983 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1984 DynList<typename GT::Arc *> arcs(typename GT::Node *p, SA sa = SA())
1985 {
1987 for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1988 ret.append(it.get_curr());
1989 return ret;
1990 }
1991
1998 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1999 DynList<ArcPair<GT>> in_pairs(typename GT::Node *p, SA sa = SA())
2000 {
2002 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2003 {
2004 typename GT::Arc *a = it.get_curr();
2005 ret.append(std::make_tuple(a, static_cast<typename GT::Node *>(a->get_connected_node(p))));
2006 }
2007 return ret;
2008 }
2009
2017 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2018 DynList<ArcPair<GT>> out_pairs(typename GT::Node *p, SA sa = SA())
2019 {
2021 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2022 {
2023 typename GT::Arc *a = it.get_curr();
2024 ret.append(std::make_tuple(a, static_cast<typename GT::Node *>(a->get_connected_node(p))));
2025 }
2026 return ret;
2027 }
2028
2039 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2040 size_t in_degree(typename GT::Node *p, SA sa = SA())
2041 {
2042 size_t count = 0;
2043 for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2044 ++count;
2045 return count;
2046 }
2047
2059 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2060 size_t out_degree(typename GT::Node *p, SA sa = SA())
2061 {
2062 size_t count = 0;
2063 for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
2064 ++count;
2065 return count;
2066 }
2067
2090 template <AlephGraph GT, class Itor, class Operation>
2091 inline
2092 bool traverse_arcs(typename GT::Node *p, Operation op = Operation())
2093 {
2094 for (Itor it(p); it.has_curr(); it.next_ne())
2095 if (not op(it.get_curr()))
2096 return false;
2097 return true;
2098 }
2099
2117 template <AlephGraph GT, class Itor, class Operation>
2118 inline
2119 void for_each_arc(typename GT::Node *p, Operation op = Operation())
2120 {
2121 for (Itor it(p); it.has_curr(); it.next_ne())
2122 op(it.get_curr());
2123 }
2124
2125
2126 // Functional operations on input arcs
2127
2137 template <AlephGraph GT, class Op>
2138 inline
2139 bool traverse_in_arcs(typename GT::Node *p, Op op = Op())
2140 {
2141 return traverse_arcs<GT, _In_Iterator<GT>, Op>(p, op);
2142 }
2143
2150 template <AlephGraph GT, class Op>
2151 inline
2152 void for_each_in_arc(typename GT::Node *p, Op op = Op())
2153 {
2155 }
2156
2167 template <AlephGraph GT, class Op>
2168 inline
2169 bool all_in_arc(typename GT::Node *p, Op op = Op())
2170 {
2171 return traverse_in_arcs<GT>(p, [&op](auto a)
2172 {
2173 return op(a);
2174 });
2175 }
2176
2187 template <AlephGraph GT, class Op>
2188 inline
2189 bool exists_in_arc(typename GT::Node *p, Op op = Op())
2190 {
2191 return not traverse_in_arcs<GT>(p, [&op](auto a)
2192 {
2193 return not op(a);
2194 });
2195 }
2196
2208 template <AlephGraph GT, class Op>
2209 inline
2210 auto search_in_arc(typename GT::Node *p, Op op = Op())
2211 {
2212 typename GT::Arc *ret = nullptr;
2213 traverse_in_arcs<GT>(p, [&op, &ret](auto a)
2214 {
2215 if (op(a))
2216 {
2217 ret = a;
2218 return false;
2219 }
2220 return true;
2221 });
2222 return ret;
2223 }
2224
2238 template <AlephGraph GT, typename T, class Op>
2239 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
2240 inline
2241 auto map_in_arcs(typename GT::Node *p, Op op)
2242 {
2244 for_each_in_arc<GT>(p, [&ret, &op](auto a)
2245 {
2246 ret.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(op, a));
2247 });
2248 return ret;
2249 }
2250
2259 template <AlephGraph GT, typename T>
2260 inline
2261 auto map_in_arcs(typename GT::Node *p, std::function<T (typename GT::Arc *)> op)
2262 {
2263 // See the equivalent nodes_map overload for why this must go through a
2264 // lambda rather than forwarding `op` as-is.
2265 return map_in_arcs<GT, T>(p, [&op](typename GT::Arc * a) { return op(a); });
2266 }
2267
2284 template <AlephGraph GT, typename T, class Op>
2285 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
2286 inline
2287 T foldl_in_arcs(typename GT::Node *p, const T & init,
2288 Op op)
2289 {
2290 T ret = init;
2291 for_each_in_arc<GT>(p, [&ret, &op](auto a)
2292 {
2293 ret = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(op, ret, a);
2294 });
2295 return ret;
2296 }
2297
2304 template <AlephGraph GT, class Op>
2305 inline
2307 {
2309 for_each_in_arc<GT>(p, [&ret, &cond](auto a)
2310 {
2311 if (cond(a))
2312 ret.append(a);
2313 });
2314 return ret;
2315 }
2316
2317
2318 // Functional operation on output arcs
2319
2329 template <AlephGraph GT, class Op>
2330 inline
2331 bool traverse_out_arcs(typename GT::Node *p, Op op = Op())
2332 {
2333 return traverse_arcs<GT, _Out_Iterator<GT>, Op>(p, op);
2334 }
2335
2342 template <AlephGraph GT, class Op>
2343 inline
2344 void for_each_out_arc(typename GT::Node *p, Op op = Op())
2345 {
2347 }
2348
2359 template <AlephGraph GT, class Op>
2360 inline
2361 bool all_out_arc(typename GT::Node *p, Op op = Op())
2362 {
2363 return traverse_out_arcs<GT>(p, [&op](auto a)
2364 {
2365 return op(a);
2366 });
2367 }
2368
2379 template <AlephGraph GT, class Op>
2380 inline
2381 bool exists_out_arc(typename GT::Node *p, Op op = Op())
2382 {
2383 return not traverse_out_arcs<GT>(p, [&op](auto a)
2384 {
2385 return not op(a);
2386 });
2387 }
2388
2400 template <AlephGraph GT, class Op>
2401 inline
2402 auto search_out_arc(typename GT::Node *p, Op op = Op())
2403 {
2404 typename GT::Arc *ret = nullptr;
2405 traverse_out_arcs<GT>(p, [&op, &ret](auto a)
2406 {
2407 if (op(a))
2408 {
2409 ret = a;
2410 return false;
2411 }
2412 return true;
2413 });
2414 return ret;
2415 }
2416
2430 template <AlephGraph GT, typename T, class Op>
2431 requires std::is_invocable_r_v<T, Op &, typename GT::Arc *>
2432 inline
2433 auto map_out_arcs(typename GT::Node *p, Op op)
2434 {
2436 for_each_out_arc<GT>(p, [&ret, &op](auto a)
2437 {
2438 ret.append(Aleph::concepts_detail::invoke_as<T (typename GT::Arc *)>::call(op, a));
2439 });
2440 return ret;
2441 }
2442
2451 template <AlephGraph GT, typename T>
2452 inline
2453 auto map_out_arcs(typename GT::Node *p, std::function<T (typename GT::Arc *)> op)
2454 {
2455 // See the equivalent nodes_map overload for why this must go through a
2456 // lambda rather than forwarding `op` as-is.
2457 return map_out_arcs<GT, T>(p, [&op](typename GT::Arc * a) { return op(a); });
2458 }
2459
2476 template <AlephGraph GT, typename T, class Op>
2477 requires std::is_invocable_r_v<T, Op &, const T &, typename GT::Arc *>
2478 inline
2479 T foldl_out_arcs(typename GT::Node *p, const T & init,
2480 Op op)
2481 {
2482 T ret = init;
2483 for_each_out_arc<GT>(p, [&ret, &op](auto a)
2484 {
2485 ret = Aleph::concepts_detail::invoke_as<T (const T &, typename GT::Arc *)>::call(op, ret, a);
2486 });
2487 return ret;
2488 }
2489
2496 template <AlephGraph GT, class Op>
2497 inline
2499 {
2501 for_each_out_arc<GT>(p, [&ret, &cond](auto a)
2502 {
2503 if (cond(a))
2504 ret.append(a);
2505 });
2506 return ret;
2507 }
2508
2522 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2523 typename GT::Arc *
2524 search_arc(const GT & g,
2525 typename GT::Node *src, typename GT::Node *tgt,
2526 SA sa = SA()) noexcept
2527 {
2528 assert(src != nullptr and tgt != nullptr);
2529
2530 if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
2531 std::swap(tgt, src); // select the node with less arcs
2532
2533 for (Node_Arc_Iterator<GT, SA> itor(src, sa); itor.has_curr(); itor.next_ne())
2534 if (itor.get_tgt_node_ne() == tgt)
2535 return itor.get_current_arc_ne();
2536
2537 return nullptr;
2538 }
2539
2550 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2551 typename GT::Arc *
2553 typename GT::Node *src, typename GT::Node *tgt,
2554 SA sa = SA()) noexcept
2555 {
2556 (void) g;
2557 assert(src != nullptr and tgt != nullptr);
2558
2559 for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
2560 if (typename GT::Arc *a = it.get_curr(); a->src_node == src and a->tgt_node == tgt)
2561 return a;
2562
2563 return nullptr;
2564 }
2565
2570 template <AlephGraph GT>
2571 typename GT::Node * mapped_node(typename GT::Node *p) noexcept
2572 {
2573 return static_cast<typename GT::Node *>(NODE_COOKIE(p));
2574 }
2575
2577 template <AlephGraph GT>
2578 typename GT::Arc * mapped_arc(typename GT::Arc *a) noexcept
2579 {
2580 return static_cast<typename GT::Arc *>(ARC_COOKIE(a));
2581 }
2582
2584 template <AlephGraph GTSRC, AlephGraph GTTGT>
2585 typename GTTGT::Node * mapped_node(typename GTSRC::Node *p) noexcept
2586 {
2587 return static_cast<typename GTTGT::Node *>(NODE_COOKIE(p));
2588 }
2589
2591 template <AlephGraph GTSRC, AlephGraph GTTGT>
2592 typename GTTGT::Arc * mapped_arc(typename GTSRC::Arc *a) noexcept
2593 {
2594 return static_cast<typename GTTGT::Arc *>(ARC_COOKIE(a));
2595 }
2596
2614 template <AlephGraph GT>
2615 inline
2616 void copy_graph(GT & gtgt, const GT & gsrc, bool cookie_map = false);
2617
2623 template <AlephGraph GT>
2624 inline void clear_graph(GT & g) noexcept;
2625
2636 template <AlephGraph GT, class Operation, NodeFilter<GT> SN = Dft_Show_Node<GT>>
2638 {
2640
2641 public:
2644 : sn(__sn)
2645 { /* empty */
2646 }
2647
2653 void operator()(const GT & g, Operation op = Operation())
2654 {
2655 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
2656 op(g, it.get_curr());
2657 }
2658
2665 {
2666 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
2667 op(g, it.get_curr());
2668 }
2669 };
2670
2681 template <AlephGraph GT, class Operation, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
2683 {
2684 SA sa;
2685
2686 public:
2689 : sa(__sa)
2690 { /* empty */
2691 }
2692
2698 void operator()(const GT & g, Operation op = Operation()) const
2699 {
2700 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2701 op(g, it.get_curr());
2702 }
2703
2704 void operator()(GT & g, Operation op = Operation()) const
2705 {
2706 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2707 op(g, it.get_curr());
2708 }
2709
2716 void operator()(const GT & g, typename GT::Node *p,
2717 Operation op = Operation()) const
2718 {
2719 for (Node_Arc_Iterator<GT, SA> it(p); it.has_curr(); it.next_ne())
2720 op(g, it.get_current_arc_ne());
2721 }
2722
2723 void operator()(GT & g, typename GT::Node *p,
2724 Operation op = Operation()) const
2725 {
2726 for (Node_Arc_Iterator<GT, SA> it(p); it.has_curr(); it.next_ne())
2727 op(g, it.get_current_arc_ne());
2728 }
2729 };
2730
2770 template <class GT>
2771 class Path
2772 {
2773 public:
2775 using Node_Type = typename GT::Node_Type;
2776
2778 using Arc_Type = typename GT::Arc_Type;
2779
2780 private:
2781 const GT *g = nullptr;
2782
2783 using Node = typename GT::Node;
2784 using Arc = typename GT::Arc;
2785
2787 {
2788 Node *node; // source node
2789 Arc *arc; // adjacent arc
2790
2791 Path_Desc(Node *_node = nullptr, Arc *_arc = nullptr) noexcept
2792 : node(_node), arc(_arc)
2793 {}
2794
2795 bool operator==(const Path_Desc & r) const noexcept
2796 {
2797 if (not (node->get_info() == r.node->get_info()))
2798 return false;
2799
2800 if (arc == nullptr)
2801 return r.arc == nullptr;
2802
2803 if (r.arc == nullptr)
2804 return false;
2805
2806 return arc->get_info() == r.arc->get_info();
2807 }
2808 };
2809
2811
2813 {
2814 ah_domain_error_if(g == nullptr) << "Path: Graph has not been specified";
2815 }
2816
2817 public:
2819 [[nodiscard]] bool check() const
2820 {
2821 auto l = list;
2822 while (not l.is_unitarian_or_empty())
2823 {
2824 auto d = l.remove_first();
2825 auto nxt = l.get_first().node;
2826 if (nxt == g->get_connected_node(d.arc, d.node))
2827 return false;
2828 }
2829
2830 return true;
2831 }
2832
2834 [[nodiscard]] bool check_directed() const
2835 {
2836 return list.all([this](auto d)
2837 {
2838 return g->get_src_node(d.arc) == d.node;
2839 })
2840 and list.get_last().arc == nullptr;
2841 }
2842
2845 {
2846 return *g;
2847 }
2848
2850 bool inside_graph(const GT & gr) const noexcept
2851 {
2852 return g == &gr;
2853 }
2854
2856 Path(const GT & __g) noexcept : g(&__g)
2857 {}
2858
2859 Path() noexcept : g(nullptr)
2860 { /* empty */
2861 }
2862
2871 {
2872 assert(start_node != nullptr);
2873 list.append(Path_Desc(start_node));
2874 }
2875
2882 Path(const GT & _g, Node *start_node) : g(&_g)
2883 {
2885 }
2886
2900 void set_graph(const GT & __g, Node *start_node = nullptr)
2901 {
2902 empty();
2903 g = &__g;
2904 if (start_node == nullptr)
2905 return;
2907 }
2908
2911 {
2912 return list.size();
2913 }
2914
2917 {
2918 return list.is_empty();
2919 }
2920
2922 void empty()
2923 {
2924 check_graph();
2925 while (not list.is_empty())
2926 list.remove_first();
2927 }
2928
2934 void clear() { empty(); }
2935
2937 Path(const Path & path) : g(path.g), list(path.list) {}
2938
2940 Path(Path && path) noexcept : g(path.g), list(std::move(path.list)) {}
2941
2943 Path &operator=(const Path & path)
2944 {
2945 if (this == &path)
2946 return *this;
2947
2948 empty();
2949 g = path.g;
2950 list = path.list;
2951 return *this;
2952 }
2953
2955 Path &operator=(Path && path) noexcept
2956 {
2957 std::swap(g, path.g);
2958 list.swap(path.list);
2959 return *this;
2960 }
2961
2975 void append(Arc *arc)
2976 {
2977 assert(arc != nullptr);
2978 check_graph();
2979
2980 ah_domain_error_if(list.is_empty()) << "path is empty";
2981
2982 auto & last_path_desc = list.get_last();
2983 auto last_node = last_path_desc.node;
2984 ah_invalid_argument_if(arc->src_node != last_node and arc->tgt_node != last_node)
2985 << "arc has not link to last node of path";
2986
2987 last_path_desc.arc = arc;
2989 }
2990
3008 void append(Node *node)
3009 {
3010 check_graph();
3011
3012 if (list.is_empty())
3013 {
3014 init(node);
3015 return;
3016 }
3017
3019 Arc *arc = search_arc(*g, last_node, node);
3020
3021 ah_invalid_argument_if(arc == nullptr)
3022 << "There is no an arc connecting to the node";
3023
3024 append(arc);
3025 }
3026
3048 {
3049 assert(p != nullptr);
3050 check_graph();
3051 if (list.is_empty())
3052 {
3053 init(p);
3054 return;
3055 }
3056
3057 auto & last_path_desc = list.get_last();
3059 Arc *arc = search_directed_arc(*g, last_node, p);
3060
3061 ah_invalid_argument_if(arc == nullptr) << "There is no an arc connecting to the node";
3062
3063 assert(arc->src_node == last_path_desc.node);
3064
3065 last_path_desc.arc = arc;
3066 list.append(Path_Desc(static_cast<Node *>(arc->tgt_node)));
3067 }
3068
3088 {
3089 assert(arc != nullptr);
3090 check_graph();
3091
3092 ah_domain_error_if(list.is_empty()) << "path is empty";
3093
3094 auto & last_path_desc = list.get_last();
3096 ah_invalid_argument_if(arc->src_node != last_node)
3097 << "The arc does not connect the last node";
3098
3099 last_path_desc.arc = arc;
3100 list.append(Path_Desc(static_cast<Node *>(arc->tgt_node)));
3101 }
3102
3118 void insert(Arc *arc)
3119 {
3120 assert(arc != nullptr);
3121 check_graph();
3122
3123 ah_domain_error_if(list.is_empty()) << "path is empty";
3124
3125 auto & first_path_desc = list.get_first();
3126 auto first_node = first_path_desc.node;
3127 ah_invalid_argument_if(arc->src_node != first_node and arc->tgt_node != first_node)
3128 << "arc has not link to first node of path";
3129
3130 Path_Desc item(g->get_connected_node(arc, first_node), arc);
3131 list.insert(item);
3132 }
3133
3149 void insert(Node *node)
3150 {
3151 check_graph();
3152
3153 if (list.is_empty())
3154 {
3155 init(node);
3156 return;
3157 }
3158
3160 Arc *arc = search_arc(*g, node, first_node); // search arc first_node-node
3161 ah_domain_error_if(arc == nullptr) << "There is no arc connecting node";
3162
3163 Path_Desc item(node, arc);
3164 list.insert(item);
3165 }
3166
3186 {
3187 assert(p != nullptr);
3188 check_graph();
3189
3190 if (list.is_empty())
3191 {
3192 init(p);
3193 return;
3194 }
3195
3197 Arc *arc = search_directed_arc(*g, p, first_node);
3198 ah_domain_error_if(arc == nullptr) << "There is no an arc connecting to the node";
3199
3200 list.insert(Path_Desc(p, arc));
3201 }
3202
3219 {
3220 assert(arc != nullptr);
3221 check_graph();
3222
3223 ah_domain_error_if(list.is_empty()) << "path is empty";
3224
3225 auto & first_path_desc = list.get_first();
3227 ah_invalid_argument_if(arc->tgt_node != first_node)
3228 << "The arc does not connect the first node";
3229
3230 list.insert(Path_Desc(static_cast<Node *>(arc->src_node), arc));
3231 }
3232
3236 {
3237 return list.get_first().node;
3238 }
3239
3243 {
3244 auto & last_path_desc = list.get_last();
3245 assert(last_path_desc.arc == nullptr);
3246 return last_path_desc.node;
3247 }
3248
3252 {
3253 return list.get_first().arc;
3254 }
3255
3259 {
3260 ah_domain_error_if(list.is_unitarian())
3261 << "Path with only a node (without any arc)";
3262
3264 it.reset_last();
3265 it.prev();
3266 return it.get_curr().arc;
3267 }
3268
3270 [[nodiscard]] bool is_cycle() const
3271 {
3272 return get_first_node() == get_last_node();
3273 }
3274
3281 {
3282 auto d = list.remove_last();
3283 list.get_last().arc = nullptr;
3284 return d.node;
3285 }
3286
3293 {
3294 auto d = list.remove_first();
3295 return d.node;
3296 }
3297
3299 void swap(Path & path) noexcept
3300 {
3301 std::swap(g, path.g);
3302 list.swap(path.list);
3303 }
3304
3310 class Iterator : public DynDlist<Path_Desc>::Iterator
3311 {
3312 public:
3314 Iterator(const Path & path) noexcept
3316 {}
3317
3318 private:
3323
3325 {
3327 }
3328
3329 public:
3333 {
3334 return this->get_curr_path_desc().node;
3335 }
3336
3338 {
3339 return this->get_curr_path_desc_ne().node;
3340 }
3341
3353 {
3354 ah_overflow_error_if(this->is_in_last()) << "Path iterator is in last node of path";
3355
3356 return this->get_curr_path_desc().arc;
3357 }
3358
3360 {
3361 return this->get_curr_path_desc_ne().arc;
3362 }
3363
3366 {
3367 return get_current_node_ne();
3368 }
3369
3370 Node * get_curr() const
3371 {
3372 return get_current_node();
3373 }
3374
3385 std::pair<Node *, Arc *> get_pair() const
3386 {
3387 return std::make_pair(get_current_node(), get_current_arc());
3388 }
3389
3400 std::tuple<Node *, Arc *> get_tuple() const
3401 {
3402 return std::make_tuple(get_current_node(), get_current_arc());
3403 }
3404
3405 std::tuple<Node *, Arc *> get_tuple_ne() const noexcept
3406 {
3407 return std::make_tuple(get_current_node_ne(), get_current_arc_ne());
3408 }
3409
3419 {
3420 return this->has_curr() and not this->is_in_last();
3421 }
3422
3425 {
3426 return this->has_curr();
3427 }
3428 };
3429
3432 {
3433 return Iterator(*this);
3434 }
3435
3440 template <class Operation>
3442 {
3443 for (Iterator it(*this); it.has_current_node(); it.next_ne())
3444 op(it.get_current_node_ne());
3445 }
3446
3451 template <class Operation>
3453 {
3454 for (Iterator it(*this); it.has_current_arc(); it.next_ne())
3455 op(it.get_current_arc_ne());
3456 }
3457
3459 bool contains_node(Node *node) const noexcept
3460 {
3461 for (Iterator it(*this); it.has_current_node(); it.next_ne())
3462 if (it.get_current_node_ne() == node)
3463 return true;
3464 return false;
3465 }
3466
3468 bool contains_arc(Arc *arc) const noexcept
3469 {
3470 for (Iterator it(*this); it.has_current_arc(); it.next_ne())
3471 if (it.get_current_arc_ne() == arc)
3472 return true;
3473 return false;
3474 }
3475
3478 {
3481 {
3482 ret_val.append(p);
3483 });
3484 return ret_val;
3485 }
3486
3489 {
3491 for_each_arc([&ret_val](Arc *a)
3492 {
3493 ret_val.append(a);
3494 });
3495 return ret_val;
3496 }
3497
3508 bool operator==(const Path & p) const noexcept
3509 {
3510 return eq(this->list, p.list);
3511 }
3512
3514 bool operator!=(const Path & p) const noexcept
3515 {
3516 return not eq(this->list, p.list);
3517 }
3518 };
3519
3520 template <AlephGraph GT>
3521 inline
3523 typename GT::Node *end_node);
3524
3525 template <class GT>
3526 static inline
3528 typename GT::Arc *curr_arc,
3529 typename GT::Node *end_node, Path<GT> & path)
3530 {
3531 if (curr_node == end_node) // this test must be first in order to find cycles
3532 {
3533 path.append(curr_arc);
3534 return true;
3535 }
3536
3538 return false;
3539
3540 path.append(curr_arc);
3541 NODE_BITS(curr_node).set_bit(Find_Path, true);
3542
3543 for (auto it = g.get_arc_it(curr_node); it.has_curr(); it.next_ne())
3544 {
3545 auto next_arc = it.get_curr();
3547 continue;
3548
3549 ARC_BITS(next_arc).set_bit(Find_Path, true);
3550 if (auto next_node = it.get_tgt_node(); __find_path_depth_first<GT>(g, next_node, next_arc, end_node, path))
3551 {
3552 assert(path.get_last_node () == end_node);
3553 return true;
3554 }
3555 }
3556
3557 path.remove_last_node();
3558
3559 return false;
3560 }
3561
3576 template <AlephGraph GT>
3577 inline
3579 typename GT::Node *end_node)
3580 {
3581 Path<GT> path(g, start_node);
3582
3585 NODE_BITS(start_node).set_bit(Find_Path, true);
3586
3587 for (auto it = g.get_arc_it(start_node); it.has_curr(); it.next_ne())
3588 {
3589 auto arc = it.get_current_arc_ne();
3590 ARC_BITS(arc).set_bit(Find_Path, true);
3591 auto next_node = it.get_tgt_node();
3593 continue;
3594
3596 return path;
3597 }
3598
3599 path.empty();
3600
3601 return path;
3602 }
3603
3613 template <AlephGraph GTS, AlephGraph GTT>
3614 void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
3615 {
3616 assert(p != nullptr and q != nullptr);
3617
3618 // Use reinterpret_cast to preserve an exact pointer value without any
3619 // implicit base class pointer adjustment from multiple inheritance
3620 if (NODE_COOKIE(p) == nullptr)
3621 {
3622 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
3623 NODE_COOKIE(q) = reinterpret_cast<void *>(p);
3624 return;
3625 }
3626
3627 NODE_COOKIE(q) = NODE_COOKIE(p);
3628 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
3629 }
3630
3641 template <AlephGraph GTS, AlephGraph GTT>
3642 void map_arcs(typename GTS::Arc *p, typename GTT::Arc *q) noexcept
3643 {
3644 assert(p != nullptr and q != nullptr);
3645
3646 if (ARC_COOKIE(p) == nullptr)
3647 {
3648 ARC_COOKIE(p) = q;
3649 ARC_COOKIE(q) = p;
3650
3651 return;
3652 }
3653
3654 ARC_COOKIE(q) = ARC_COOKIE(p);
3655 ARC_COOKIE(p) = q;
3656 }
3657
3658 template <AlephGraph GT>
3659 void clear_graph(GT & g) noexcept
3660 {
3661 for (typename GT::Arc_Iterator it(g); it.has_curr();) // remove arcs
3662 {
3663 typename GT::Arc *arc = it.get_curr_ne();
3664 it.next_ne();
3665 g.remove_arc(arc);
3666 }
3667
3668 for (typename GT::Node_Iterator it(g); it.has_curr();) // remove nodes
3669 {
3670 typename GT::Node *p = it.get_curr_ne();
3671 it.next_ne(); // advance before deletion (iterator consistency)
3672 g.remove_node(p); // remove it from the graph
3673 }
3674 }
3675
3676 template <AlephGraph GT>
3677 void copy_graph(GT & gtgt, const GT & gsrc, const bool cookie_map)
3678 {
3679 try
3680 {
3681 clear_graph(gtgt); // clear this before copying
3683
3684 // phase 1: traverse nodes of src_graph and insert copy in this
3685 for (typename GT::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
3686 {
3687 typename GT::Node *src_node = it.get_current_node_ne();
3688 std::unique_ptr<typename GT::Node>
3689 tgt_node(new typename GT::Node(src_node->get_info()));
3690 map.insert(src_node, tgt_node.get());
3691
3692 typename GT::Node *tgt = tgt_node.release();
3693 assert(tgt->get_info () == src_node->get_info ());
3694 gtgt.insert_node(tgt); // insert in the target graph
3695
3696 if (cookie_map)
3697 GT::map_nodes(src_node, tgt);
3698 }
3699
3700 assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
3701
3702 // phase 2: for each arc of src_graph, create in this an
3703 // arc connecting the mapped nodes from map
3704 for (typename GT::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
3705 {
3706 typename GT::Arc *src_arc = it.get_current_arc_ne();
3707
3708 // get node images in target graph and create arc
3709 typename GT::Node *src_node = map[gsrc.get_src_node(src_arc)];
3710 typename GT::Node *tgt_node = map[gsrc.get_tgt_node(src_arc)];
3711 typename GT::Arc *tgt_arc = gtgt.insert_arc(src_node, tgt_node);
3712 // tgt_arc->get_info() = src_arc->get_info();
3713 *tgt_arc = *src_arc;
3714 if (cookie_map)
3716 }
3717
3718 assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
3719 }
3720 catch (...)
3721 { // If an exception occurs, clean this
3723 throw;
3724 }
3725 }
3726
3731 template <class GTT, class GTS>
3733 {
3734 void operator()(typename GTT::Node *tgt, typename GTS::Node *src) noexcept
3735 {
3736 tgt->get_info() = src->get_info();
3737 }
3738 };
3739
3744 template <class GTT, class GTS>
3746 {
3747 void operator()(typename GTT::Arc *tgt, typename GTS::Arc *src)
3748 {
3749 tgt->get_info() = src->get_info();
3750 }
3751 };
3752
3761 template <AlephGraph GTT, AlephGraph GTS,
3765 const bool cookie_map = false)
3766 {
3767 try
3768 {
3769 clear_graph(gtgt); // clear this before copying
3771 // phase 1: traverse nodes of src_graph and insert a copy in this
3772 for (typename GTS::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
3773 {
3774 typename GTS::Node *src_node = it.get_current_node_ne();
3775 std::unique_ptr<typename GTT::Node> tgt_node(new typename GTT::Node);
3776 Copy_Node()(tgt_node.get(), src_node);
3777 map.insert(src_node, tgt_node.get());
3778
3779 typename GTT::Node *tgt = tgt_node.release();
3780 gtgt.insert_node(tgt); // insert in the target graph
3781
3782 if (cookie_map)
3783 map_nodes<GTS, GTT>(src_node, tgt);
3784 }
3785
3786 assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
3787
3788 // phase 2: for each arc of src_graph, create in this an
3789 // arc connecting the mapped nodes from the map
3790 for (typename GTS::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
3791 {
3792 typename GTS::Arc *src_arc = it.get_current_arc_ne();
3793
3794 // get node images in target graph and create arc
3795 typename GTT::Node *src_node = map[gsrc.get_src_node(src_arc)];
3796 typename GTT::Node *tgt_node = map[gsrc.get_tgt_node(src_arc)];
3797 typename GTT::Arc *tgt_arc = gtgt.insert_arc(src_node, tgt_node);
3799 if (cookie_map)
3801 }
3802
3803 assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
3804 }
3805 catch (...)
3806 { // If an exception occurs, clean this
3808 throw;
3809 }
3810 }
3811
3824 template <AlephGraph GT, NodeFilter<GT> SN = Dft_Show_Node<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
3826 {
3828 SA sa;
3829
3830 public:
3836 Copy_Graph(SA __sa = SA(), SN __sn = SN()) : sn(__sn), sa(__sa)
3837 {
3838 // empty
3839 }
3840
3841 private:
3842 void copy(GT & gtgt, const GT & gsrc, const bool cookie_map)
3843 {
3844 try
3845 {
3846 clear_graph(gtgt); // clear this before copying
3848
3849 // phase 1: traverse nodes of src_graph and insert a copy in this
3850 for (Node_Iterator<GT, SN> it(gsrc, sn); it.has_curr(); it.next_ne())
3851 {
3852 typename GT::Node *src_node = it.get_curr();
3853 std::unique_ptr<typename GT::Node>
3854 tgt_node(new typename GT::Node(src_node));
3855 map.insert(src_node, tgt_node.get());
3856
3857 typename GT::Node *tgt = tgt_node.release();
3858 gtgt.insert_node(tgt); // insert in target graph
3859
3860 if (cookie_map)
3861 GT::map_nodes(src_node, tgt);
3862 }
3863
3864 // phase 2: for each arc of src_graph, create in this an
3865 // arc connecting the mapped nodes from a map
3866 for (Arc_Iterator<GT, SA> it(gsrc, sa); it.has_curr(); it.next_ne())
3867 {
3868 typename GT::Arc *src_arc = it.get_curr();
3869
3870 // get node images in target graph and create arc
3871 typename GT::Node *src_node = map[gsrc.get_src_node(src_arc)];
3872 typename GT::Node *tgt_node = map[gsrc.get_tgt_node(src_arc)];
3873 typename GT::Arc *tgt_arc =
3874 gtgt.insert_arc(src_node, tgt_node, src_arc->get_info());
3875
3876 if (cookie_map)
3878 }
3879 }
3880 catch (...)
3881 { // If an exception occurs, it is cleaned this
3883 throw;
3884 }
3885 }
3886
3887 public:
3895 void operator()(GT & gtgt, GT & gsrc, const bool cookie_map = true)
3896 {
3898 }
3899 };
3900
3911 template <AlephGraph GT, class Distance>
3913 {
3916
3918 { /* empty */
3919 }
3920
3921 bool operator()(typename GT::Arc *a) noexcept
3922 {
3924 return false;
3925
3926 dist = dist + Distance()(a);
3927
3928 return true;
3929 }
3930 };
3931
3951 template <AlephGraph GT>
3952 inline
3953 bool are_equal(const GT & g1, const GT & g2);
3954
3988 template <AlephGraph GT,
3989 template <class, class> class Tree = Treap>
3991 {
3992 public:
3993 using Node = typename GT::Node;
3994 using Arc = typename GT::Arc;
3995 using Node_Type = typename GT::Node_Type;
3996 using Arc_Type = typename GT::Arc_Type;
3997
3998 private:
4001
4003 void build_copy(const GT & src)
4004 {
4005 // Phase 1: Copy all nodes and build mapping
4006 for (typename GT::Node_Iterator it(src); it.has_curr(); it.next_ne())
4007 {
4008 Node *src_node = it.get_curr();
4009 Node *tgt_node = copied_graph.insert_node(src_node->get_info());
4010 node_map.insert(src_node, tgt_node);
4011 }
4012
4013 // Phase 2: Copy all arcs using the node mapping
4014 for (typename GT::Arc_Iterator it(src); it.has_curr(); it.next_ne())
4015 {
4016 Arc *src_arc = it.get_curr();
4019
4020 Node *tgt_src = node_map.find(src_src);
4021 Node *tgt_tgt = node_map.find(src_tgt);
4022
4024 }
4025 }
4026
4027 public:
4038 explicit GraphCopyWithMapping(const GT & src)
4039 {
4040 build_copy(src);
4041 }
4042
4052
4064
4078
4079 // Disable copy (graphs can be large)
4081
4083
4092
4101
4114 {
4115 auto *ptr = node_map.search(orig);
4116 ah_domain_error_if(ptr == nullptr)
4117 << "Node not found in mapping (not from original graph?)";
4118 return ptr->second;
4119 }
4120
4132 Node * search_copy(Node *orig) const noexcept
4133 {
4134 auto *ptr = node_map.search(orig);
4135 return ptr ? ptr->second : nullptr;
4136 }
4137
4146 bool has_copy(Node *orig) const noexcept
4147 {
4148 return node_map.search(orig) != nullptr;
4149 }
4150
4158 [[nodiscard]] size_t num_nodes() const noexcept { return node_map.size(); }
4159
4168
4179 template <typename Op>
4180 void for_each_mapping(Op op) const
4181 {
4182 node_map.for_each([&op](const auto & pair)
4183 {
4184 op(pair.first, pair.second);
4185 });
4186 }
4187
4200 {
4202 }
4203
4216 {
4217 return copied_graph.insert_node(std::forward<Node_Type>(info));
4218 }
4219
4230 void remove_node(Node *node)
4231 {
4232 // Maintain node_map consistency when a copied node is removed:
4233 // perform a reverse lookup (value -> key) and erase the corresponding entry.
4234 // This is O(N) because DynMapTree is keyed by original node, not by copy.
4235 Node *original_node = nullptr;
4236 node_map.for_each([&](const auto & pair)
4237 {
4238 if (pair.second == node)
4239 {
4240 original_node = pair.first;
4241 }
4242 });
4243
4244 if (original_node)
4245 node_map.remove(original_node);
4246
4248 }
4249
4260 Arc * insert_arc(Node *src, Node *tgt, const Arc_Type & info = Arc_Type())
4261 {
4262 return copied_graph.insert_arc(src, tgt, info);
4263 }
4264
4272 void remove_arc(Arc *arc)
4273 {
4275 }
4276
4285 void clear()
4286 {
4287 node_map.empty();
4289 }
4290 };
4291} // end namespace Aleph
4292
4293# endif /* TPL_GRAPH_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:468
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:212
C++20 concepts for the protocol shared by graph algorithms.
Simplified graph interface for common use cases.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
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:285
Arc_Node() noexcept
Definition tpl_graph.H:283
Filtered copy of graphs.
Definition tpl_graph.H:3826
Copy_Graph(SA __sa=SA(), SN __sn=SN())
Constructor.
Definition tpl_graph.H:3836
void copy(GT &gtgt, const GT &gsrc, const bool cookie_map)
Definition tpl_graph.H:3842
void operator()(GT &gtgt, GT &gsrc, const bool cookie_map=true)
Perform the copy from gsrc to gtgt.
Definition tpl_graph.H:3895
Filtered iterator on directed graphs.
Definition tpl_graph.H:1690
GT::Arc * get_curr_ne() const noexcept
Return the current arc.
Definition tpl_graph.H:1740
void reset_last() noexcept
Reset the iterator to the last arc.
Definition tpl_graph.H:1792
void reset_first() noexcept
Reset the iterator to the first arc.
Definition tpl_graph.H:1786
void next_ne() noexcept
Definition tpl_graph.H:1716
GT::Node * get_node_ne() const noexcept
Return the connected node to current arc.
Definition tpl_graph.H:1773
GT::Node * get_node() const
Return the connected node to current arc.
Definition tpl_graph.H:1760
GT::Arc * get_curr() const
Return the current arc.
Definition tpl_graph.H:1733
Digraph_Iterator(typename GT::Node *p)
Iterator type.
Definition tpl_graph.H:1703
Filter_Iterator< typename GT::Node *, typename GT::Node_Arc_Iterator, Filter > Itor
Definition tpl_graph.H:1692
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:1780
void end() noexcept
Put the iterator in end state.
Definition tpl_graph.H:1798
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:1767
bool has_curr() const noexcept
Return true the iterator has an current arc.
Definition tpl_graph.H:1726
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:1747
GT::Node * get_node(typename GT::Arc *a) const noexcept
Return the connected node to arc.
Definition tpl_graph.H:1754
void prev()
Move the iterator one position backward.
Definition tpl_graph.H:1723
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1698
void next()
Move the iterator one position forward.
Definition tpl_graph.H:1711
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3960
Iterator dynamic list.
T & get_curr() const
Return the current item; throw overflow_error if there is no current item.
void reset_last() noexcept
Reset the iterator to the last item.
void prev()
Move the iterator one item backward.
T & get_curr_ne() const noexcept
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
T & get_first() const
Return the first item of the list.
Definition htlist.H:1375
Dynamic map implemented with an AVL tree.
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Generic filter iterator wrapper.
void next()
Advances the iterator to the next filtered element.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
void reset_last()
Resets the iterator to the last filtered element.
void prev()
Moves the iterator backward to the previous filtered element.
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:3991
Node * search_copy(Node *orig) const noexcept
Search for the copy of an original node (no exception).
Definition tpl_graph.H:4132
typename GT::Arc_Type Arc_Type
Definition tpl_graph.H:3996
bool has_copy(Node *orig) const noexcept
Check if an original node is in the mapping.
Definition tpl_graph.H:4146
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:4215
void build_copy(const GT &src)
Build the copy and mapping.
Definition tpl_graph.H:4003
GraphCopyWithMapping()=default
Default constructor.
const GT & get_graph() const noexcept
Get the copied graph (const version).
Definition tpl_graph.H:4100
DynMapTree< Node *, Node *, Tree > node_map
Definition tpl_graph.H:4000
void remove_node(Node *node)
Remove a node from the copied graph.
Definition tpl_graph.H:4230
Node * get_copy(Node *orig) const
Get the copy of an original node.
Definition tpl_graph.H:4113
void clear()
Clear the copied graph and mapping.
Definition tpl_graph.H:4285
Arc * insert_arc(Node *src, Node *tgt, const Arc_Type &info=Arc_Type())
Insert an arc into the copied graph.
Definition tpl_graph.H:4260
GraphCopyWithMapping(const GT &src)
Construct a copy of the given graph with node mapping.
Definition tpl_graph.H:4038
void remove_arc(Arc *arc)
Remove an arc from the copied graph.
Definition tpl_graph.H:4272
void for_each_mapping(Op op) const
Apply a function to each (original, copy) node pair.
Definition tpl_graph.H:4180
size_t num_arcs() const noexcept
Get the number of arcs in the copied graph.
Definition tpl_graph.H:4167
Node * insert_unmapped_node(const Node_Type &info=Node_Type())
Insert a new node into the copied graph (not mapped).
Definition tpl_graph.H:4199
typename GT::Node_Type Node_Type
Definition tpl_graph.H:3995
GraphCopyWithMapping(GraphCopyWithMapping &&other) noexcept=default
Move constructor.
size_t num_nodes() const noexcept
Get the number of mapped nodes.
Definition tpl_graph.H:4158
typename GT::Node Node
Definition tpl_graph.H:3993
GT & get_graph() noexcept
Get the copied graph.
Definition tpl_graph.H:4091
GraphCopyWithMapping(const GraphCopyWithMapping &)=delete
GraphCopyWithMapping & operator=(GraphCopyWithMapping &&other) noexcept=default
Move assignment operator.
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Definition htlist.H:431
Filtered iterator for incoming arcs of a node.
Definition tpl_graph.H:1876
typename GT::Arc * Item_Type
Definition tpl_graph.H:1887
In_Iterator()=default
In_Iterator(typename GT::Node *p, SA sa=SA())
Definition tpl_graph.H:1891
void next_ne() noexcept
Definition tpl_graph.H:1903
Iterator on the arcs of a graph.
Definition tpl_graph.H:831
Node_Arc_Iterator(Node *src) noexcept
Constructs an iterator on the node src.
Definition tpl_graph.H:845
Node * get_node_ne() const noexcept
Definition tpl_graph.H:903
Node * get_tgt_node_ne() const noexcept
Definition tpl_graph.H:893
Arc_Node * get_current_arc_node() const
Definition tpl_graph.H:851
Arc * get_current_arc() const
Return the current arc.
Definition tpl_graph.H:875
Arc * get_curr_ne() const noexcept
Definition tpl_graph.H:868
Node_Arc_Iterator()=default
The container type (a node)
Arc_Node * get_current_arc_node_ne() const noexcept
Definition tpl_graph.H:856
Arc * get_current_arc_ne() const noexcept
Definition tpl_graph.H:880
Node * Set_Type
The type of data of set.
Definition tpl_graph.H:837
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:888
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:429
Dlink & get_node_dlink() noexcept
Return a reference to the internal node Dlink for sorting.
Definition tpl_graph.H:488
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:577
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
Definition tpl_graph.H:596
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
Dlink & get_arc_dlink() noexcept
Return a reference to the internal arc Dlink for sorting.
Definition tpl_graph.H:502
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:984
Arc * get_first_arc() const
Return any arc in the graph.
Definition tpl_graph.H:770
static Arc * dlink_to_arc(Dlink *p) noexcept
Definition tpl_graph.H:460
static Arc_Node * dlink_to_arc_node(Dlink *p) noexcept
Definition tpl_graph.H:465
virtual void remove_node(Node *node) noexcept
Remove a node from the graph and free its memory.
Definition tpl_graph.H:544
static Node * dlink_to_node(Dlink *p) noexcept
Definition tpl_graph.H:455
virtual Arc * connect_arc(Arc *arc) noexcept
Connect a previously disconnected arc to the graph.
Definition tpl_graph.H:734
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:437
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:650
static Arc * void_to_arc(Arc_Node *arc_node) noexcept
Definition tpl_graph.H:470
_Graph_Node Node
The graph type.
Definition tpl_graph.H:433
virtual void disconnect_arc(Arc *arc) noexcept
Disconnect an arc from graph.
Definition tpl_graph.H:700
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:434
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
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:440
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1207
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1211
typename Itor::Set_Type Set_Type
The element type: Node*.
Definition tpl_graph.H:1213
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:1222
Functor that traverses the arcs of a graph and performs an operation.
Definition tpl_graph.H:2683
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:2716
void operator()(GT &g, Operation op=Operation()) const
Definition tpl_graph.H:2704
void operator()(const GT &g, Operation op=Operation()) const
Call to `op on each arc.
Definition tpl_graph.H:2698
Operate_On_Arcs(SA __sa=SA())
Initialize the functor with the filter sa
Definition tpl_graph.H:2688
void operator()(GT &g, typename GT::Node *p, Operation op=Operation()) const
Definition tpl_graph.H:2723
Functor that traverses the nodes of a graph and performs an operation.
Definition tpl_graph.H:2638
Operate_On_Nodes(SN __sn=SN())
Initialize the functor with the filter sa
Definition tpl_graph.H:2643
void operator()(const GT &g, Operation op=Operation())
Call to operation on each node.
Definition tpl_graph.H:2653
void operator()(GT &g, Operation op=Operation())
Call to operation on each node.
Definition tpl_graph.H:2664
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1831
Out_Iterator()=default
Out_Iterator(typename GT::Node *p, Show_Arc sa=Show_Arc())
Definition tpl_graph.H:1846
void next_ne() noexcept
Definition tpl_graph.H:1858
typename GT::Arc * Item_Type
Definition tpl_graph.H:1842
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:3311
Node * get_current_node_ne() const noexcept
Definition tpl_graph.H:3337
std::tuple< Node *, Arc * > get_tuple() const
Return a tuple with the current node and arc.
Definition tpl_graph.H:3400
Node * get_current_node() const
Return the current node of a path.
Definition tpl_graph.H:3332
std::tuple< Node *, Arc * > get_tuple_ne() const noexcept
Definition tpl_graph.H:3405
Node * get_curr_ne() const noexcept
Definition tpl_graph.H:3365
Arc * get_current_arc_ne() const noexcept
Definition tpl_graph.H:3359
Arc * get_current_arc() const
Return the current arc of a path.
Definition tpl_graph.H:3352
Iterator(const Path &path) noexcept
Create an iterator on the first node of path
Definition tpl_graph.H:3314
Path_Desc & get_curr_path_desc() const
Definition tpl_graph.H:3324
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:3424
Node * get_curr() const
Definition tpl_graph.H:3370
Path_Desc & get_curr_path_desc_ne() const noexcept
Definition tpl_graph.H:3319
std::pair< Node *, Arc * > get_pair() const
Return a pair with the current node and arc.
Definition tpl_graph.H:3385
bool has_current_arc() const noexcept
Return true if iterator has current arc.
Definition tpl_graph.H:3418
Path on a graph.
Definition tpl_graph.H:2772
bool is_cycle() const
Return true if this is a cycle; throws if path is empty.
Definition tpl_graph.H:3270
typename GT::Node Node
Definition tpl_graph.H:2783
void insert(Arc *arc)
Insert an arc as the first of a path.
Definition tpl_graph.H:3118
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
Definition tpl_graph.H:3441
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:2922
Path & operator=(const Path &path)
Copy assignment.
Definition tpl_graph.H:2943
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Definition tpl_graph.H:3488
typename GT::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:2778
void init(Node *start_node)
Set the first node of a path.
Definition tpl_graph.H:2870
void append(Node *node)
Append a node to the path.
Definition tpl_graph.H:3008
Path & operator=(Path &&path) noexcept
Move assignment.
Definition tpl_graph.H:2955
Path() noexcept
Definition tpl_graph.H:2859
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:2910
bool check_directed() const
Return true if the directed path is consistent.
Definition tpl_graph.H:2834
bool contains_node(Node *node) const noexcept
Return true if node belongs to the path.
Definition tpl_graph.H:3459
void insert_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:3185
const GT * g
Definition tpl_graph.H:2781
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:3235
bool operator==(const Path &p) const noexcept
Return true if this is equal to p,.
Definition tpl_graph.H:3508
bool operator!=(const Path &p) const noexcept
Return true if this is not equal to p,.
Definition tpl_graph.H:3514
void check_graph()
Definition tpl_graph.H:2812
Iterator get_it() const
Returns an iterator on the path.
Definition tpl_graph.H:3431
Path(Path &&path) noexcept
Move constructor.
Definition tpl_graph.H:2940
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:2916
typename GT::Node_Type Node_Type
The type of data stored in the nodes.
Definition tpl_graph.H:2775
bool check() const
Return true if the path is consistent.
Definition tpl_graph.H:2819
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
Definition tpl_graph.H:2850
Arc * get_last_arc() const
Return the last arc of a path; throws overflow_error if the path is empty.
Definition tpl_graph.H:3258
Path(const GT &__g) noexcept
Construct a empty path on graph __g
Definition tpl_graph.H:2856
bool contains_arc(Arc *arc) const noexcept
Return true if arc belongs to the path.
Definition tpl_graph.H:3468
void swap(Path &path) noexcept
Fast swap between two paths (constant time)
Definition tpl_graph.H:3299
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:2900
Node * remove_first_node()
Remove the first node of a path.
Definition tpl_graph.H:3292
Arc * get_first_arc() const
Return the first arc of path; throws overflow_error if path is empty.
Definition tpl_graph.H:3251
void clear()
Empties the container.
Definition tpl_graph.H:2934
DynDlist< Path_Desc > list
Definition tpl_graph.H:2810
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:2975
void append_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:3047
typename GT::Arc Arc
Definition tpl_graph.H:2784
void insert_directed(Arc *arc)
Append an arc to a directed path.
Definition tpl_graph.H:3218
Path(const GT &_g, Node *start_node)
Construct a path starting from a given node.
Definition tpl_graph.H:2882
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition tpl_graph.H:3477
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:3242
Path(const Path &path)
Copy constructor.
Definition tpl_graph.H:2937
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:3280
void insert(Node *node)
Insert a node to the path.
Definition tpl_graph.H:3149
const GT & get_graph() const noexcept
Get a constant reference to the graph.
Definition tpl_graph.H:2844
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Definition tpl_graph.H:3452
void append_directed(Arc *arc)
Append an arc to a directed path.
Definition tpl_graph.H:3087
Filter the incoming arcs.
Definition tpl_graph.H:1663
GT::Node * tgt
Definition tpl_graph.H:1664
bool operator()(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1671
GT::Node * get_node(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1677
__In_Filt(typename GT::Node *__tgt) noexcept
Definition tpl_graph.H:1667
Filter the outcoming arcs.
Definition tpl_graph.H:1633
bool operator()(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1641
GT::Node * get_node(typename GT::Arc *a) const noexcept
Definition tpl_graph.H:1647
__Out_Filt(typename GT::Node *__src) noexcept
Definition tpl_graph.H:1637
Common methods for the arc of a graph.
Definition graph-dry.H:570
void * get_connected_node(void *node) noexcept
Definition graph-dry.H:642
ArcInfo arc_info
Definition graph-dry.H:584
Common attributes and methods for nodes (vertexes) belonging to graphs.
Definition graph-dry.H:477
NodeInfo node_info
Definition graph-dry.H:485
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:496
Common methods to the Aleph-w ( ) graph classes.
Definition graph-dry.H:660
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1088
size_t num_nodes
Definition graph-dry.H:667
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:2908
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:1164
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:699
size_t num_arcs
Definition graph-dry.H:668
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1074
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:820
void common_swap(GT &g) noexcept
Definition graph-dry.H:683
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1094
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:1241
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1043
void remove_arcs_if(Predicate pred)
Remove all arcs matching a predicate.
Definition graph-dry.H:3777
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
A graph usable by the graph algorithms.
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:1937
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:2344
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:1240
#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:3642
bool traverse_in_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of incoming arcs of a node.
Definition tpl_graph.H:2139
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:2018
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:1427
bool traverse_arcs(typename GT::Node *p, Operation op=Operation())
Generic arcs traverse of a node.
Definition tpl_graph.H:2092
bool traverse_out_arcs(typename GT::Node *p, Op op=Op())
Conditioned traversal of outcoming arcs of a node.
Definition tpl_graph.H:2331
#define ALEPH_GRAPH_COPY_MOVE_CTORS(GraphClass)
Macro to generate copy/move constructors and assignment operators.
Definition graph-dry.H:3898
#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:3764
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:1301
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:1373
#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:2060
#define NODE_COOKIE(p)
Return the node cookie
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3659
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:1920
size_t in_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered in degree of node p.
Definition tpl_graph.H:2040
GT::Node * mapped_node(typename GT::Node *p) noexcept
Return the mapped node through the cookie of p
Definition tpl_graph.H:2571
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:1622
T foldl_nodes(GT &g, const T &init, Op operation, SN sn=SN())
Fold the filtered nodes of a graph.
Definition tpl_graph.H:1531
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:1259
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
DynList< typename GT::Arc * > in_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
Definition tpl_graph.H:1969
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Arc filtered searching given two nodes.
Definition tpl_graph.H:2524
T foldl_arcs(GT &g, const T &init, Op operation, SA sa=SA())
Fold the filtered arcs of a graph.
Definition tpl_graph.H:1554
GT::Arc * search_directed_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Searching of directed arc linking two nodes.
Definition tpl_graph.H:2552
#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:1323
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:2152
void copy_graph(GT &gtgt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Definition tpl_graph.H:3677
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:3614
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:1999
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:3578
DynList< typename GT::Arc * > out_arcs(typename GT::Node *p, SA sa=SA())
Return the filtered incoming arcs of p.
Definition tpl_graph.H:1953
@ Spanning_Tree
Definition aleph-graph.H:79
@ Find_Path
Definition aleph-graph.H:76
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:2210
DynList< typename GT::Arc * > filter_in_arcs(typename GT::Node *p, Op cond)
Filter the incoming arcs meeting an condition.
Definition tpl_graph.H:2306
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
auto map_in_arcs(typename GT::Node *p, Op op)
Map the incoming arcs to a transformation,.
Definition tpl_graph.H:2241
bool all_out_arc(typename GT::Node *p, Op op=Op())
Test if the outcoming arcs meet a condition.
Definition tpl_graph.H:2361
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
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:2287
auto map_out_arcs(typename GT::Node *p, Op op)
Map the outcoming arcs to a transformation,.
Definition tpl_graph.H:2433
DynList< typename GT::Arc * > filter_out_arcs(typename GT::Node *p, Op cond)
Filter the outcoming arcs meeting an condition.
Definition tpl_graph.H:2498
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:2402
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:3527
T foldl_out_arcs(typename GT::Node *p, const T &init, Op op)
Fold the outcoming arcs of a node.
Definition tpl_graph.H:2479
bool all_in_arc(typename GT::Node *p, Op op=Op())
Test if the incoming arcs meet a condition.
Definition tpl_graph.H:2169
GT::Arc * mapped_arc(typename GT::Arc *a) noexcept
Return the mapped arc through the cookie of p
Definition tpl_graph.H:2578
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:2189
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:2381
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:1165
Arc_Iterator()=default
Type of set all the arcs.
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1168
typename Itor::Set_Type Set_Type
Type of element: Arc*.
Definition tpl_graph.H:1169
Arc_Iterator(const GT &g, Show_Arc sa=Show_Arc())
Constructor,.
Definition tpl_graph.H:1178
Default copy arc functor.
Definition tpl_graph.H:3746
void operator()(typename GTT::Arc *tgt, typename GTS::Arc *src)
Definition tpl_graph.H:3747
Default copy node functor.
Definition tpl_graph.H:3733
void operator()(typename GTT::Node *tgt, typename GTS::Node *src) noexcept
Definition tpl_graph.H:3734
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1001
void set_cookie(void *) noexcept
Definition tpl_graph.H:1007
bool operator()(typename GT::Arc *) const noexcept
Definition tpl_graph.H:1002
Default filter for the graph nodes.
Definition tpl_graph.H:1193
bool operator()(typename GT::Node *) const noexcept
Definition tpl_graph.H:1194
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:223
Arc_Node * src_arc_node
The type of data stored in the arc.
Definition tpl_graph.H:230
Graph_Arc(const Graph_Arc &arc)
Definition tpl_graph.H:263
Graph_Arc(const Arc_Info &info) noexcept
Copy constructor.
Definition tpl_graph.H:242
Graph_Arc(Arc_Info &&info=Arc_Info()) noexcept
Move or rvalue constructor.
Definition tpl_graph.H:257
GTArcCommon< _Arc_Info > Base
Definition tpl_graph.H:226
Graph_Arc & operator=(const Graph_Arc &arc)
Definition tpl_graph.H:269
_Arc_Info Arc_Info
Definition tpl_graph.H:228
Arc_Node * tgt_arc_node
Definition tpl_graph.H:231
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:122
GTNodeCommon< __Node_Info > Base
Definition tpl_graph.H:126
__Node_Info Node_Info
Definition tpl_graph.H:127
Graph_Node(const Graph_Node &node) noexcept
Definition tpl_graph.H:157
Graph_Node(Graph_Node *node)
Copy constructor from a node pointer.
Definition tpl_graph.H:185
Graph_Node(Node_Info &&info=Node_Info()) noexcept
Move or rvalue constructor.
Definition tpl_graph.H:151
Graph_Node(const Node_Info &info) noexcept
The type of data stored in the node.
Definition tpl_graph.H:138
Graph_Node & operator=(const Graph_Node &node)
Definition tpl_graph.H:163
Iterator on all arcs of a graph.
Definition tpl_graph.H:918
Arc_Iterator()=default
The type of set.
Node * get_tgt_node() const
Return the target node of current arc.
Definition tpl_graph.H:969
Arc_Iterator(const List_Graph &g) noexcept
Initialize an iterator for all the arc of g
Definition tpl_graph.H:926
Arc * get_curr_ne() const noexcept
Definition tpl_graph.H:948
Arc * get_current_arc_ne() const noexcept
Return the current arc. Throw overflow_error if there is no one.
Definition tpl_graph.H:933
Node * get_src_node() const
Return the source node of the current arc.
Definition tpl_graph.H:956
Node_Iterator(const List_Graph &g)
Construct an iterator on the nodes of g
Definition tpl_graph.H:800
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
Node_Arc_Iterator(typename GT::Node *p, Show_Arc sa=Show_Arc())
Construct and filtered iterator according to condition sa.
Definition tpl_graph.H:1139
Node_Arc_Iterator()=default
Type of set: p's arcs.
typename Itor::Item_Type Item_Type
Definition tpl_graph.H:1129
typename Itor::Set_Type Set_Type
type of element: Arc*
Definition tpl_graph.H:1130
Filter of painter arcs with that are set the Spanning_Tree control bit.
Definition tpl_graph.H:3913
bool operator()(typename GT::Arc *a) noexcept
Definition tpl_graph.H:3921
Distance::Distance_Type dist
Accumulative distance from the first seen arc until the last seen.
Definition tpl_graph.H:3915
Path_Desc(Node *_node=nullptr, Arc *_arc=nullptr) noexcept
Definition tpl_graph.H:2791
bool operator==(const Path_Desc &r) const noexcept
Definition tpl_graph.H:2795
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:614
Distance accessor.
Common node iterator for graph having its node derived from Dlink class.
Definition graph-dry.H:298
gsl_rng * r
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).
DynList< int > l
Treap< int >::Node * last_node
Definition writeTreap.C:60