Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph-dry.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef GRAPH_DRY_H
45# define GRAPH_DRY_H
46
47#include <concepts>
48
49// ============================================================================
50// C++20 Concepts for Graph Iterators
51// ============================================================================
52
151template <typename It>
152concept BasicGraphIterator = requires(It it, const It cit)
153{
154 { cit.has_curr() } -> std::convertible_to<bool>;
155 { it.next() } -> std::same_as<void>;
156 { it.get_curr() };
157};
158
171template <typename It>
173{
174 { it.reset_first() } -> std::same_as<void>;
175};
176
190template <typename It, typename Node>
191concept GraphNodeIterator = BasicGraphIterator<It> && requires(const It cit)
192{
193 { cit.get_curr() } -> std::convertible_to<Node*>;
194};
195
209template <typename It, typename Arc>
210concept GraphArcIterator = BasicGraphIterator<It> && requires(const It cit)
211{
212 { cit.get_curr() } -> std::convertible_to<Arc*>;
213};
214
233template <typename It, typename Node, typename Arc>
234concept NodeArcIterator = GraphArcIterator<It, Arc> && requires(const It cit)
235{
236 { cit.get_tgt_node() } -> std::convertible_to<Node*>;
237};
238
// end of GraphConcepts group
240
241// ============================================================================
242// End of C++20 Concepts
243// ============================================================================
244
254template <class GT>
255struct GTNodeIterator : public Dlink::Iterator
256{
257 using Node = typename GT::Node;
258
260 using Item_Type = Node *;
261
263 using Set_Type = GT;
264
265 GTNodeIterator() noexcept
266 { /* empty */
268
270 GTNodeIterator(Dlink & head) noexcept : Dlink::Iterator(head)
271 { /* empty */
272 }
273
275 Node * get_curr_ne() const noexcept
276 {
277 return static_cast<Node *>(Dlink::Iterator::get_curr_ne());
278 }
279
281 Node * get_curr() const { return static_cast<Node *>(Dlink::Iterator::get_curr()); }
282
284 Node * get_current_node() const { return get_curr(); }
285
286 Node * get_current_node_ne() const { return get_curr_ne(); }
287};
299template <class GT>
300struct GTArcIterator : public Dlink::Iterator
301{
302 using Node = typename GT::Node;
303 using Arc = typename GT::Arc;
304
306 using Item_Type = Arc *;
307
309 using Set_Type = GT;
310
311 GTArcIterator() noexcept
312 { /* empty */
313 }
314
316 GTArcIterator(Dlink & head) noexcept
317 : Dlink::Iterator(head)
318 { /* empty */
319 }
320
322 Arc * get_curr_ne() const noexcept
323 {
324 return static_cast<Arc *>(Dlink::Iterator::get_curr_ne());
325 }
326
328 Arc * get_curr() const { return static_cast<Arc *>(Dlink::Iterator::get_curr()); }
329
331 Arc * get_current_arc() const { return get_curr(); }
332
334 Arc * get_current_arc_ne() const noexcept { return get_curr_ne(); }
335
337 Node * get_src_node_ne() const noexcept
338 {
339 return static_cast<Node *>(get_curr_ne()->src_node);
340 }
341
343 Node * get_tgt_node_ne() const noexcept
344 {
345 return static_cast<Node *>(get_curr_ne()->tgt_node);
346 }
347
350 {
351 return static_cast<Node *>(get_curr()->src_node);
352 }
353
356 {
357 return static_cast<Node *>(get_curr()->tgt_node);
358 }
359};
360
361
366template <class GT, class Cmp>
368{
369 using Node = typename GT::Node;
370
372
373 Cmp_Dlink_Node(Cmp && __cmp = Cmp()) noexcept : cmp(__cmp)
374 { /* empty */
375 }
376
377 Cmp_Dlink_Node(Cmp & __cmp) noexcept : cmp(__cmp)
378 { /* empty */
379 }
380
381 bool operator ()(Dlink *d1, Dlink *d2) const noexcept
382 {
383 Node *p1 = static_cast<Node *>(d1);
384 Node *p2 = static_cast<Node *>(d2);
385 return cmp(p1, p2);
386 }
387};
388
393template <class GT, class Cmp>
395{
396 using Arc = typename GT::Arc;
397
399
400 Cmp_Dlink_Arc(Cmp && __cmp = Cmp()) noexcept : cmp(__cmp)
401 { /* empty */
402 }
403
404 Cmp_Dlink_Arc(Cmp & __cmp) noexcept : cmp(__cmp)
405 { /* empty */
406 }
407
408 bool operator ()(Dlink *d1, Dlink *d2) const noexcept
409 {
410 Arc *arc1 = static_cast<Arc *>(d1);
411 Arc *arc2 = static_cast<Arc *>(d2);
412 return cmp(arc1, arc2);
413 }
414};
415
433template <typename NodeInfo>
435{
436public:
441 Graph_Attr attrs;
442
444
445
454 size_t num_arcs = 0;
455
457
459
461
463 GTNodeCommon() noexcept = default;
464
466 GTNodeCommon(const NodeInfo & info) : node_info(info) {}
467
469 GTNodeCommon(NodeInfo && info) : node_info(std::move(info)) {}
470
472 GTNodeCommon(const GTNodeCommon & other) : node_info(other.node_info) {}
473
475 GTNodeCommon(GTNodeCommon && other) noexcept : node_info(std::move(other.node_info)) {}
476
479 {
480 if (this != &other)
481 node_info = other.node_info;
482 return *this;
483 }
484
487 {
488 if (this != &other)
489 node_info = std::move(other.node_info);
490 return *this;
491 }
492
494 NodeInfo &get_info() noexcept { return node_info; }
495
497 const NodeInfo &get_info() const noexcept { return node_info; }
498
500 unsigned int state() const noexcept { return NODE_BITS(this).state; }
501
503 void set_state(unsigned int s) noexcept
504 {
505 NODE_BITS(this).state = s;
506 }
507};
508
509
526template <typename ArcInfo>
528{
529public:
532
533 void *src_node = nullptr;
534 void *tgt_node = nullptr;
535
540 Graph_Attr attrs;
541
543
545 GTArcCommon() noexcept = default;
546
548 GTArcCommon(const ArcInfo & info) : arc_info(info) {}
549
551 GTArcCommon(ArcInfo && info) : arc_info(std::move(info)) {}
552
554 GTArcCommon(void *src, void *tgt, const ArcInfo & data)
555 : src_node(src), tgt_node(tgt), arc_info(data) {}
556
558 GTArcCommon(void *src, void *tgt, ArcInfo && data = ArcInfo())
559 : src_node(src), tgt_node(tgt), arc_info(std::move(data)) {}
560
563 : src_node(other.src_node), tgt_node(other.tgt_node), arc_info(other.arc_info) {}
564
566 GTArcCommon(GTArcCommon && other) noexcept
567 : src_node(other.src_node), tgt_node(other.tgt_node), arc_info(std::move(other.arc_info)) {}
568
571 {
572 if (this != &other)
573 arc_info = other.arc_info;
574 return *this;
575 }
576
578 GTArcCommon & operator=(GTArcCommon && other) noexcept
579 {
580 if (this != &other)
581 arc_info = std::move(other.arc_info);
582 return *this;
583 }
584
586 unsigned int state() const noexcept { return ARC_BITS(this).state; }
587
589 void set_state(unsigned int s) noexcept
590 {
591 ARC_BITS(this).state = s;
592 }
593
595 ArcInfo &get_info() noexcept { return arc_info; }
596
598 const ArcInfo &get_info() const noexcept { return arc_info; }
599
600 void * get_connected_node(void *node) noexcept
601 {
602 return src_node == node ? tgt_node : src_node;
603 }
604
605 void * get_img_node(void *node) noexcept
606 {
607 return src_node == node ? src_node : tgt_node;
608 }
609};
610
611
616template <class GT, class Node, class Arc>
618{
619 GT * me() { return static_cast<GT *>(this); }
620
621 const GT * const_me() const { return static_cast<const GT *>(this); }
622
623protected:
624 void *cookie = nullptr;
625 size_t num_nodes = 0;
626 size_t num_arcs = 0;
627 bool digraph = false;
628
629public:
630 using Node_Type = typename Node::Node_Type;
631 using Arc_Type = typename Arc::Arc_Type;
632
633protected:
634 void init() noexcept
635 {
636 num_nodes = num_arcs = 0;
637 cookie = nullptr;
638 digraph = false;
639 }
640
641 void common_swap(GT & g) noexcept
642 {
643 std::swap(num_nodes, g.num_nodes);
644 std::swap(num_arcs, g.num_arcs);
645 std::swap(digraph, g.digraph);
646 std::swap(cookie, g.cookie);
647 }
648
649public:
651 void *&get_cookie() noexcept { return cookie; }
652
654 void * get_cookie() const noexcept { return cookie; }
655
657 bool is_digraph() const noexcept { return digraph; }
658
692 void set_digraph(bool val) { digraph = val; } // TODO: delete after
693
695 [[nodiscard]] constexpr size_t get_num_nodes() const noexcept { return num_nodes; }
696
701 [[nodiscard]] constexpr bool is_empty() const noexcept { return num_nodes == 0; }
702
704 [[nodiscard]] constexpr size_t vsize() const noexcept { return get_num_nodes(); }
705
714 Node * get_node() const { return const_me()->get_first_node(); }
715
724 Node * get_arc() const { return const_me()->get_first_arc(); }
725
734 Node * get_arc(Node *p) { return const_me()->get_first_arc(p); }
735
737 Node * get_src_node(Arc *arc) const noexcept
738 {
739 return static_cast<Node *>(arc->src_node);
740 }
741
743 Node * get_tgt_node(Arc *arc) const noexcept
744 {
745 return static_cast<Node *>(arc->tgt_node);
746 }
747
778 Node * get_connected_node(Arc *arc, Node *node) const noexcept
779 {
780 return static_cast<Node *>(arc->get_connected_node(node));
781 }
782
784 [[nodiscard]] constexpr size_t get_num_arcs() const noexcept { return num_arcs; }
785
788 size_t get_num_arcs(Node *node) const noexcept
789 {
790 return node->num_arcs;
791 }
792
795 size_t degree(Node *p) const noexcept { return get_num_arcs(p); }
796
798 size_t esize() const noexcept { return get_num_arcs(); }
799
801 Bit_Fields &get_control_bits(Node *node) const noexcept
802 {
803 return NODE_BITS(node).reset();
804 }
805
807 void reset_bit(Node *node, int bit) const noexcept
808 {
809 NODE_BITS(node).reset(bit);
810 }
811
813 void reset_bits(Node *node) const noexcept
814 {
815 NODE_BITS(node).reset();
816 }
817
819 int get_bit(Node *node, int bit) const noexcept
820 {
821 return NODE_BITS(node).get_bit(bit);
822 }
823
825 void set_bit(Node *node, int bit, int value) const noexcept
826 {
827 NODE_BITS(node).set_bit(bit, value);
828 }
829
831 Bit_Fields &get_control_bits(Arc *arc) const noexcept
832 {
833 return ARC_BITS(arc);
834 }
835
837 void reset_bit(Arc *arc, int bit) const noexcept
838 {
839 ARC_BITS(arc).reset(bit);
840 }
841
843 void reset_bits(Arc *arc) const noexcept
844 {
845 ARC_BITS(arc).reset();
846 }
847
849 int get_bit(Arc *arc, int bit) const noexcept
850 {
851 return ARC_BITS(arc).get_bit(bit);
852 }
853
855 void set_bit(Arc *arc, int bit, int value) const noexcept
856 {
857 ARC_BITS(arc).set_bit(bit, value);
858 }
859
861 void *&get_cookie(Node *node) const noexcept
862 {
863 return NODE_COOKIE(node);
864 }
865
867 void *&get_cookie(Arc *arc) const noexcept
868 {
869 return ARC_COOKIE(arc);
870 }
871
873 long &get_counter(Node *node) const noexcept
874 {
875 return NODE_COUNTER(node);
876 }
877
879 void reset_counter(Node *node) const noexcept
880 {
881 NODE_COUNTER(node) = 0;
882 }
883
885 void reset_node_counters() const noexcept
886 {
887 for_each_node([this](auto p) { this->reset_counter(p); });
888 }
889
893 void reset_node(Node *p) const noexcept
894 {
895 p->attrs.reset();
896 }
897
899 long &get_counter(Arc *arc) const noexcept
900 {
901 return ARC_COUNTER(arc);
902 }
903
905 void reset_counter(Arc *arc) const noexcept
906 {
907 ARC_COUNTER(arc) = No_Visited;
908 }
909
911 void reset_arc_counters() const noexcept
912 {
913 for_each_arc([this](auto a) { this->reset_counter(a); });
914 }
915
919 void reset_arc(Arc *arc) const noexcept
920 {
921 arc->attrs.reset();
922 }
923
926 void reset_nodes() const
927 {
928 for_each_node([](auto p) { p->attrs.reset(); });
929 }
930
933 void reset_arcs() const
934 {
935 for_each_arc([](auto a) { a->attrs.reset(); });
936 }
937
999 template <class N1, class N2 = N1>
1000 static
1001 void map_nodes(N1 *p, N2 *q) noexcept
1002 {
1003 assert(p != nullptr and q != nullptr);
1004 // Use reinterpret_cast to preserve exact pointer values without any
1005 // implicit base class pointer adjustment from multiple inheritance
1006 if (NODE_COOKIE(p) == nullptr)
1007 {
1008 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
1009 NODE_COOKIE(q) = reinterpret_cast<void *>(p);
1010 return;
1011 }
1012 NODE_COOKIE(q) = NODE_COOKIE(p);
1013 NODE_COOKIE(p) = reinterpret_cast<void *>(q);
1014 }
1015
1030 template <class A1, class A2 = A1>
1031 static
1032 void map_arcs(A1 *p, A2 *q) noexcept
1033 {
1034 assert(p != nullptr and q != nullptr);
1035 if (ARC_COOKIE(p) == nullptr)
1036 {
1037 ARC_COOKIE(p) = q;
1038 ARC_COOKIE(q) = p;
1039 return;
1040 }
1041 ARC_COOKIE(q) = ARC_COOKIE(p);
1042 ARC_COOKIE(p) = q;
1043 }
1044
1046 void reset_bit_nodes(int bit) const noexcept
1047 {
1048 for_each_node([bit, this](auto p) { this->reset_bit(p, bit); });
1049 }
1050
1052 void reset_bit_arcs(int bit) const noexcept
1053 {
1054 for_each_arc([bit, this](auto a) { this->reset_bit(a, bit); });
1055 }
1056
1058 void reset_bit_nodes() const noexcept
1059 {
1060 for_each_node([this](auto p) { this->reset_bits(p); });
1061 }
1062
1064 void reset_bit_arcs() const noexcept
1065 {
1066 for_each_arc([this](auto a) { this->reset_bits(a); });
1067 }
1068
1070 void reset_counter_nodes() const noexcept
1071 {
1072 for_each_node([this](auto p) { this->reset_counter(p); });
1073 }
1074
1076 void reset_counter_arcs() const noexcept
1077 {
1078 for_each_arc([this](auto a) { this->reset_counter(a); });
1079 }
1080
1087 void reset_cookie_nodes() const noexcept
1088 {
1089 for_each_node([](auto p) { NODE_COOKIE(p) = nullptr; });
1090 }
1091
1098 void reset_cookie_arcs() const noexcept
1099 {
1100 for_each_arc([](auto a) { ARC_COOKIE(a) = nullptr; });
1101 }
1102
1122 Node * insert_node(const Node_Type & node_info)
1123 {
1124 return me()->insert_node(new Node(node_info));
1125 }
1126
1147 {
1148 return me()->insert_node(new Node(std::forward<Node_Type>(node_info)));
1149 }
1150
1171 template <typename... Args>
1172 Node * emplace_node(Args &&... args)
1173 {
1174 return me()->insert_node(Node_Type(args...));
1175 }
1176
1199 Arc * insert_arc(Node *src, Node *tgt, const Arc_Type & arc_info)
1200 {
1201 std::unique_ptr<Arc> arc(new Arc(arc_info));
1202 me()->insert_arc(src, tgt, arc.get());
1203 return arc.release();
1204 }
1205
1229 Arc * insert_arc(Node *src, Node *tgt, Arc_Type && arc_info = Arc_Type())
1230 {
1231 std::unique_ptr<Arc> arc(new Arc(std::forward<Arc_Type>(arc_info)));
1232 me()->insert_arc(src, tgt, arc.get());
1233 return arc.release();
1234 }
1235
1257 template <typename... Args>
1258 Arc * emplace_arc(Node *src, Node *tgt, Args &&... args)
1259 {
1260 return me()->insert_arc(src, tgt, Arc_Type(args...));
1261 }
1262
1309 template <class Operation>
1310 bool traverse_nodes(Operation & op) const
1311 {
1312 for (typename GT::Node_Iterator it(*const_me()); it.has_curr(); it.next_ne())
1313 if (not op(it.get_curr()))
1314 return false;
1315 return true;
1316 }
1317
1319 template <class Operation>
1320 bool traverse_nodes(Operation && op = Operation()) const
1321 {
1322 return traverse_nodes(op);
1323 }
1324
1374 template <class Operation>
1375 bool traverse_arcs(Operation & op) const
1376 {
1377 for (typename GT::Arc_Iterator it(*const_me()); it.has_curr(); it.next_ne())
1378 if (not op(it.get_curr()))
1379 return false;
1380 return true;
1381 }
1382
1384 template <class Operation>
1385 bool traverse_arcs(Operation && op = Operation()) const
1386 {
1387 return traverse_arcs(op);
1388 }
1389
1441 template <class Operation>
1442 bool traverse_arcs(Node *p, Operation & op) const
1443 {
1444 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
1445 if (not op(it.get_curr()))
1446 return false;
1447 return true;
1448 }
1449
1451 template <class Operation>
1452 bool traverse_arcs(Node *p, Operation && op = Operation()) const
1453 {
1454 return traverse_arcs(p, op);
1455 }
1456
1481 template <class Operation>
1482 void for_each_node(Operation & operation) const
1483 {
1484 for (typename GT::Node_Iterator it(*const_me()); it.has_curr(); it.next_ne())
1485 operation(it.get_curr());
1486 }
1487
1489 template <class Operation>
1490 void for_each_node(Operation && operation = Operation()) const
1491 {
1492 for_each_node(operation);
1493 }
1494
1519 template <class Operation>
1520 void for_each_arc(Operation & op) const
1521 {
1522 for (typename GT::Arc_Iterator it(*const_me()); it.has_curr(); it.next_ne())
1523 op(it.get_curr());
1524 }
1525
1527 template <class Operation>
1528 void for_each_arc(Operation && operation = Operation()) const
1529 {
1530 for_each_arc(operation);
1531 }
1532
1566 template <class Operation>
1567 void for_each_arc(Node *p, Operation & op) const
1568 {
1569 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
1570 op(it.get_curr());
1571 }
1572
1574 template <class Operation>
1575 void for_each_arc(Node *p, Operation && op = Operation()) const
1576 {
1577 for_each_arc(p, op);
1578 }
1579
1609 template <class Operation>
1610 bool all_nodes(Operation & op) const
1611 {
1612 return traverse_nodes(op);
1613 }
1614
1616 template <class Operation>
1617 bool all_nodes(Operation && op = Operation()) const
1618 {
1619 return all_nodes(op);
1620 }
1621
1651 template <class Operation>
1652 bool all_arcs(Operation & op) const
1653 {
1654 return traverse_arcs(op);
1655 }
1656
1658 template <class Operation>
1659 bool all_arcs(Operation && op = Operation()) const
1660 {
1661 return all_arcs(op);
1662 }
1663
1696 template <class Operation>
1697 bool all_arcs(Node *p, Operation & op) const
1698 {
1699 return traverse_arcs(p, op);
1700 }
1701
1703 template <class Operation>
1704 bool all_arcs(Node *p, Operation && op = Operation()) const
1705 {
1706 return all_arcs(p, op);
1707 }
1708
1748 template <typename T = Node_Type>
1749 auto nodes_map(std::function<T(Node *)> op) const
1750 {
1751 DynList<T> ret_val;
1752 for_each_node([&ret_val, &op](Node *p) { ret_val.append(op(p)); });
1753 return ret_val;
1754 }
1755
1795 template <typename T = Arc_Type>
1796 auto arcs_map(std::function<T(Arc *)> operation) const
1797 {
1798 DynList<T> ret_val;
1799 for_each_arc([&ret_val, &operation](Arc *p)
1800 {
1801 ret_val.append(operation(p));
1802 });
1803 return ret_val;
1804 }
1805
1846 template <typename T = Arc_Type>
1847 auto arcs_map(Node *p, std::function<T(Arc *)> operation) const
1848 {
1849 DynList<T> ret_val;
1850 for_each_arc(p, [&ret_val, &operation](Arc *a)
1851 {
1852 ret_val.append(operation(a));
1853 });
1854 return ret_val;
1855 }
1856
1891 template <typename T = Node_Type>
1892 T foldl_nodes(const T & init,
1893 std::function<T(const T &, Node *)> op) const
1894 {
1895 T ret = init;
1896 for_each_node([&ret, &op](Node *p) { ret = op(ret, p); });
1897 return ret;
1898 }
1899
1933 template <typename T = Arc_Type>
1934 T foldl_arcs(const T & init,
1935 std::function<T(const T &, Arc *)> op) const
1936 {
1937 T ret = init;
1938 for_each_arc([&ret, &op](Arc *p) { ret = op(ret, p); });
1939 return ret;
1940 }
1941
1976 template <typename T = Arc_Type>
1977 T foldl_arcs(Node *p, const T & init,
1978 std::function<T(const T &, Arc *)> op) const
1979 {
1980 T ret = init;
1981 for_each_arc(p, [&ret, &op](Arc *a) { ret = op(ret, a); });
1982 return ret;
1983 }
1984
2006 template <class Op>
2007 auto filter_nodes(Op & op) const
2008 {
2009 DynList<Node *> ret;
2010 for_each_node([&ret, &op](Node *p)
2011 {
2012 if (op(p))
2013 ret.append(p);
2014 });
2015 return ret;
2016 }
2017
2019 template <class Op>
2020 auto filter_nodes(Op && op) const
2021 {
2022 return filter_nodes(op);
2023 }
2024
2046 template <class Op>
2047 auto filter_arcs(Op & op) const
2048 {
2049 DynList<Arc *> ret;
2050 for_each_arc([&ret, &op](Arc *a)
2051 {
2052 if (op(a))
2053 ret.append(a);
2054 });
2055 return ret;
2056 }
2057
2059 template <class Op>
2060 auto filter_arcs(Op && op) const
2061 {
2062 return filter_arcs(op);
2063 }
2064
2095 template <class Op>
2096 auto filter_arcs(Node *p, Op & op) const
2097 {
2098 DynList<Arc *> ret;
2099 for_each_arc(p, [&ret, &op](Arc *a)
2100 {
2101 if (op(a))
2102 ret.append(a);
2103 });
2104 return ret;
2105 }
2106
2108 template <class Op>
2109 auto filter_arcs(Node *p, Op && op) const
2110 {
2111 return filter_arcs(p, op);
2112 }
2113
2132 template <class Operation>
2133 bool exists_node(Operation & op) const
2134 {
2135 return not traverse_nodes([&op](Node *p) { return not op(p); });
2136 }
2137
2139 template <class Operation>
2140 bool exists_node(Operation && op = Operation()) const
2141 {
2142 return exists_node(op);
2143 }
2144
2163 template <class Operation>
2164 bool exists_arc(Operation & op) const
2165 {
2166 return not traverse_arcs([&op](Arc *a) { return not op(a); });
2167 }
2168
2170 template <class Operation>
2171 bool exists_arc(Operation && op = Operation()) const
2172 {
2173 return exists_arc(op);
2174 }
2175
2198 template <class Operation>
2199 bool exists_arc(Node *p, Operation & op) const
2200 {
2201 return not traverse_arcs(p, [&op](Arc *a) { return not op(a); });
2202 }
2203
2205 template <class Operation>
2206 bool exists_arc(Node *p, Operation && op = Operation()) const
2207 {
2208 return exists_arc(p, op);
2209 }
2210
2225 template <class Operation>
2226 bool none_node(Operation & op) const
2227 {
2228 return not exists_node(op);
2229 }
2230
2232 template <class Operation>
2233 bool none_node(Operation && op) const
2234 {
2235 return none_node(op);
2236 }
2237
2252 template <class Operation>
2253 bool none_arc(Operation & op) const
2254 {
2255 return not exists_arc(op);
2256 }
2257
2259 template <class Operation>
2260 bool none_arc(Operation && op) const
2261 {
2262 return none_arc(op);
2263 }
2264
2272 template <class Operation>
2273 bool none_arc(Node *p, Operation & op) const
2274 {
2275 return not exists_arc(p, op);
2276 }
2277
2279 template <class Operation>
2280 bool none_arc(Node *p, Operation && op) const
2281 {
2282 return none_arc(p, op);
2283 }
2284
2301 template <class Operation = std::function<bool(Node*)>>
2302 size_t count_nodes(Operation op = [](Node*) { return true; }) const
2303 {
2304 size_t count = 0;
2305 for_each_node([&count, &op](Node *p) { if (op(p)) ++count; });
2306 return count;
2307 }
2308
2325 template <class Operation = std::function<bool(Arc*)>>
2326 size_t count_arcs(Operation op = [](Arc*) { return true; }) const
2327 {
2328 size_t count = 0;
2329 for_each_arc([&count, &op](Arc *a) { if (op(a)) ++count; });
2330 return count;
2331 }
2332
2339 template <class Operation = std::function<bool(Arc*)>>
2340 size_t count_arcs(Node *p, Operation op = [](Arc*) { return true; }) const
2341 {
2342 size_t count = 0;
2343 for_each_arc(p, [&count, &op](Arc *a) { if (op(a)) ++count; });
2344 return count;
2345 }
2346
2363 template <typename T = double, class Extract>
2364 T sum_arcs(Node *p, Extract extract) const
2365 {
2366 T sum = T{0};
2367 for_each_arc(p, [&sum, &extract](Arc *a) { sum += extract(a); });
2368 return sum;
2369 }
2370
2372 template <typename T = double>
2373 T sum_arcs(Node *p) const
2374 {
2375 T sum = T{0};
2376 for_each_arc(p, [&sum](Arc *a) { sum += static_cast<T>(a->get_info()); });
2377 return sum;
2378 }
2379
2395 template <class Compare = std::function<bool(Arc*, Arc*)>>
2396 Arc* min_arc(Node *p, Compare cmp = [](Arc *a, Arc *b) {
2397 return a->get_info() < b->get_info();
2398 }) const
2399 {
2400 Arc* result = nullptr;
2401 for_each_arc(p, [&result, &cmp](Arc *a) {
2402 if (result == nullptr or cmp(a, result))
2403 result = a;
2404 });
2405 return result;
2406 }
2407
2423 template <class Compare = std::function<bool(Arc*, Arc*)>>
2424 Arc* max_arc(Node *p, Compare cmp = [](Arc *a, Arc *b) {
2425 return a->get_info() < b->get_info();
2426 }) const
2427 {
2428 Arc* result = nullptr;
2429 for_each_arc(p, [&result, &cmp](Arc *a) {
2430 if (result == nullptr or cmp(result, a))
2431 result = a;
2432 });
2433 return result;
2434 }
2435
2441 template <class Compare = std::function<bool(Arc*, Arc*)>>
2442 Arc* min_arc(Compare cmp = [](Arc *a, Arc *b) {
2443 return a->get_info() < b->get_info();
2444 }) const
2445 {
2446 Arc* result = nullptr;
2447 for_each_arc([&result, &cmp](Arc *a) {
2448 if (result == nullptr or cmp(a, result))
2449 result = a;
2450 });
2451 return result;
2452 }
2453
2459 template <class Compare = std::function<bool(Arc*, Arc*)>>
2460 Arc* max_arc(Compare cmp = [](Arc *a, Arc *b) {
2461 return a->get_info() < b->get_info();
2462 }) const
2463 {
2464 Arc* result = nullptr;
2465 for_each_arc([&result, &cmp](Arc *a) {
2466 if (result == nullptr or cmp(result, a))
2467 result = a;
2468 });
2469 return result;
2470 }
2471
2487 template <class Operation>
2488 std::pair<DynList<Node*>, DynList<Node*>> partition_nodes(Operation op) const
2489 {
2490 DynList<Node*> yes, no;
2491 for_each_node([&yes, &no, &op](Node *p) {
2492 if (op(p))
2493 yes.append(p);
2494 else
2495 no.append(p);
2496 });
2497 return {std::move(yes), std::move(no)};
2498 }
2499
2510 template <class Operation>
2511 std::pair<DynList<Arc*>, DynList<Arc*>> partition_arcs(Operation op) const
2512 {
2513 DynList<Arc*> yes, no;
2514 for_each_arc([&yes, &no, &op](Arc *a) {
2515 if (op(a))
2516 yes.append(a);
2517 else
2518 no.append(a);
2519 });
2520 return {std::move(yes), std::move(no)};
2521 }
2522
2536 DynList<Node*> adjacent_nodes(Node *p) const
2537 {
2538 DynList<Node*> result;
2539 for_each_arc(p, [this, p, &result](Arc *a) {
2540 result.append(get_connected_node(a, p));
2541 });
2542 return result;
2543 }
2544
2561 template <class Op>
2562 Node * search_node(Op & op) const
2563 {
2564 for (typename GT::Node_Iterator it(*const_me()); it.has_curr(); it.next_ne())
2565 {
2566 auto p = it.get_curr();
2567 if (op(p))
2568 return p;
2569 }
2570 return nullptr;
2571 }
2572
2574 template <class Op>
2575 Node * search_node(Op && op) const
2576 {
2577 return search_node(op);
2578 }
2579
2591 Node * find_node(const Node_Type & info) const noexcept
2592 {
2593 return search_node([&info](auto p) { return p->get_info() == info; });
2594 }
2595
2612 template <class Op>
2613 Arc * search_arc(Op & op) const
2614 {
2615 for (typename GT::Arc_Iterator it(*const_me()); it.has_curr(); it.next_ne())
2616 {
2617 auto a = it.get_curr();
2618 if (op(a))
2619 return a;
2620 }
2621 return nullptr;
2622 }
2623
2625 template <class Op>
2626 Arc * search_arc(Op && op) const
2627 {
2628 return search_arc(op);
2629 }
2630
2642 Arc * find_arc(const Arc_Type & info) const noexcept
2643 {
2644 return search_arc([&info](auto a) { return a->get_info() == info; });
2645 }
2646
2665 template <class Operation>
2666 Arc * search_arc(Node *p, Operation & op) const
2667 {
2668 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
2669 {
2670 Arc *arc = it.get_curr();
2671 if (op(arc))
2672 return arc;
2673 }
2674 return nullptr;
2675 }
2676
2678 template <class Operation>
2679 Arc * search_arc(Node *p, Operation && op = Operation()) const
2680 {
2681 return search_arc(p, op);
2682 }
2683
2707 Arc * search_arc(Node *src, Node *tgt) const noexcept
2708 {
2709 for (typename GT::Node_Arc_Iterator it(src); it.has_curr(); it.next_ne())
2710 if (it.get_tgt_node_ne() == tgt)
2711 return it.get_curr();
2712 return nullptr;
2713 }
2714
2725 template <template <typename> class Container = Aleph::DynList>
2727 {
2729 for_each_node([&ret](Node *p) { ret.append(p); });
2730 return ret;
2731 }
2732
2743 template <template <typename> class Container = Aleph::DynList>
2745 {
2746 Container<Arc *> ret;
2747 for_each_arc([&ret](Arc *a) { ret.append(a); });
2748 return ret;
2749 }
2750
2761 template <template <typename> class Container = Aleph::DynList>
2763 {
2764 Container<Arc *> ret;
2765 this->for_each_arc(p, [&ret](Arc *a) { ret.append(a); });
2766 return ret;
2767 }
2768
2786 auto get_node_it() const noexcept
2787 {
2788 return typename GT::Node_Iterator(*const_me());
2789 }
2790
2808 auto get_arc_it() const noexcept
2809 {
2810 return typename GT::Arc_Iterator(*const_me());
2811 }
2812
2831 auto get_arc_it(Node *p) const noexcept
2832 {
2833 return typename GT::Node_Arc_Iterator(p);
2834 }
2835
2868 struct In_Filt
2869 {
2870 Node *tgt = nullptr;
2871
2873 In_Filt(Node *__tgt = nullptr) noexcept : tgt(__tgt)
2874 { /* empty */
2875 }
2876
2879 bool operator ()(Arc *a) const noexcept
2880 {
2881 assert(tgt);
2882 return a->tgt_node == tgt;
2883 }
2884
2886 Node * get_node(Arc *a) const noexcept
2887 {
2888 assert(tgt);
2889 return (typename GT::Node *) a->src_node;
2890 }
2891 };
2892
2926 {
2927 Node *src = nullptr;
2928
2930 Out_Filt(Node *__src) noexcept : src(__src)
2931 { /* empty */
2932 }
2933
2936 bool operator ()(Arc *a) const noexcept
2937 {
2938 assert(src);
2939 return a->src_node == src;
2940 }
2941
2943 Node * get_node(Arc *a) const noexcept
2944 {
2945 assert(src);
2946 return (Node *) a->tgt_node;
2947 }
2948 };
2949
2975 template <class Filter>
2977 {
2978 using Itor = Filter_Iterator<Node *, typename GT::Node_Arc_Iterator, Filter>;
2979
2980 Filter filt;
2982
2983 public:
2984 using Item_Type = typename Itor::Item_Type;
2985
2987
2990 {
2991 // empty
2992 }
2993
2994 void next_ne() noexcept { it.next_ne(); }
2995
2998 void next() { it.next(); }
2999
3002 void prev() { it.prev(); }
3003
3004 void prev_ne() { it.prev_ne(); }
3005
3007 bool has_curr() const noexcept { return it.has_curr(); }
3008
3011 typename GT::Arc * get_curr() const { return it.get_curr(); }
3012
3013 typename GT::Arc * get_curr_ne() const noexcept { return it.get_curr_ne(); }
3014
3016 auto get_current_arc() const { return get_curr(); }
3017
3018 auto get_current_arc_ne() const noexcept { return get_curr_ne(); }
3019
3022 typename GT::Node * get_node(typename GT::Arc *a) const noexcept
3023 {
3024 return filt.get_node(a);
3025 }
3026
3029 typename GT::Node * get_node_ne() const noexcept
3030 {
3031 return this->get_node(this->get_curr_ne());
3032 }
3033
3035 auto get_tgt_node_ne() const noexcept { return get_node_ne(); }
3036
3037 typename GT::Node * get_node() const
3038 {
3039 return this->get_node(this->get_curr());
3040 }
3041
3043 auto get_tgt_node() const { return get_node(); }
3044
3046 void reset_first() noexcept { it.reset_first(); }
3047
3049 void reset_last() noexcept { it.reset_last(); }
3050 };
3051
3053 // template <class Filt> using Filter_Iterator = Digraph_Iterator<Filt>;
3054
3055 // /** Iterator on incoming arcs of node */
3057 {
3058 using Digraph_Iterator<In_Filt>::Digraph_Iterator;
3059 };
3060
3061 // /** Iterator on incoming arcs of node */
3063 {
3064 using Digraph_Iterator<Out_Filt>::Digraph_Iterator;
3065 };
3066
3085 In_Iterator get_in_it(Node *p) const noexcept { return In_Iterator(p); }
3086
3105 Out_Iterator get_out_it(Node *p) const noexcept { return Out_Iterator(p); }
3106
3117 Arc * search_directed_arc(Node *src, Node *tgt) const noexcept
3118 {
3119 for (typename GT::Out_Iterator it(src); it.has_curr(); it.next_ne())
3120 if (it.get_tgt_node() == tgt)
3121 return it.get_curr();
3122 return nullptr;
3123 }
3124
3135 DynList<Node *> in_nodes(Node *p) const
3136 {
3137 DynList<Node *> ret;
3138 for (In_Iterator it(p); it.has_curr(); it.next_ne())
3139 ret.append(it.get_node());
3140 return ret;
3141 }
3142
3153 DynList<Node *> out_nodes(Node *p) const
3154 {
3155 DynList<Node *> ret;
3156 for (Out_Iterator it(p); it.has_curr(); it.next_ne())
3157 ret.append(it.get_node_ne());
3158 return ret;
3159 }
3160
3170 DynList<Arc *> out_arcs(Node *p) const
3171 {
3172 DynList<Arc *> ret;
3173 for (Out_Iterator it(p); it.has_curr(); it.next_ne())
3174 ret.append(it.get_curr());
3175 return ret;
3176 }
3177
3186 DynList<Arc *> in_arcs(Node *p) const
3187 {
3188 DynList<Arc *> ret;
3189 for (In_Iterator it(p); it.has_curr(); it.next_ne())
3190 ret.append(it.get_curr());
3191 return ret;
3192 }
3193
3195 using ArcPair = std::tuple<Arc *, Node *>;
3196
3209 auto in_pairs(Node *p) const
3210 {
3211 DynList<ArcPair> ret;
3212 for (In_Iterator it(p); it.has_curr(); it.next_ne())
3213 {
3214 auto a = it.get_curr();
3215 ret.append(std::make_tuple(a, (Node *) a->get_connected_node(p)));
3216 }
3217 return ret;
3218 }
3219
3232 auto out_pairs(Node *p) const
3233 {
3234 DynList<ArcPair> ret;
3235 for (Out_Iterator it(p); it.has_curr(); it.next_ne())
3236 {
3237 auto a = it.get_curr();
3238 ret.append(std::make_tuple(a, (Node *) a->get_connected_node(p)));
3239 }
3240 return ret;
3241 }
3242
3256 size_t in_degree(Node *p) const noexcept
3257 {
3258 size_t count = 0;
3259 for (In_Iterator it(p); it.has_curr(); it.next_ne())
3260 ++count;
3261 return count;
3262 }
3263
3279 size_t out_degree(Node *p) const noexcept
3280 {
3281 size_t count = 0;
3282 for (Out_Iterator it(p); it.has_curr(); it.next_ne())
3283 ++count;
3284 return count;
3285 }
3286
3305 template <class Itor, class Operation>
3306 bool traverse_arcs(Node *p, Operation & op) const
3307 {
3308 for (Itor it(p); it.has_curr(); it.next_ne())
3309 if (not op(it.get_curr()))
3310 return false;
3311 return true;
3312 }
3313
3315 template <class Itor, class Operation>
3316 void for_each_arc(Node *p, Operation & op) const
3317 {
3318 for (Itor it(p); it.has_curr(); it.next_ne())
3319 op(it.get_curr());
3320 }
3321
3324 template <class Op>
3325 bool traverse_in_arcs(Node *p, Op & op) const
3326 {
3327 return traverse_arcs<In_Iterator, Op>(p, op);
3328 }
3329
3331 template <class Op>
3332 bool traverse_in_arcs(Node *p, Op && op = Op()) const
3333 {
3334 return traverse_in_arcs(p, op);
3335 }
3336
3338 template <class Op>
3339 void for_each_in_arc(Node *p, Op & op) const
3340 {
3341 for_each_arc<In_Iterator>(p, op);
3342 }
3343
3345 template <class Op>
3346 void for_each_in_arc(Node *p, Op && op = Op()) const
3347 {
3348 for_each_in_arc(p, op);
3349 }
3350
3352 template <class Op>
3353 bool all_in_arcs(Node *p, Op & op) const
3354 {
3355 return traverse_in_arcs(p, [&op](auto a) { return op(a); });
3356 }
3357
3359 template <class Op>
3360 bool all_in_arcs(Node *p, Op && op = Op()) const
3361 {
3362 return all_in_arcs(p, op);
3363 }
3364
3367 template <class Op>
3368 bool exists_in_arc(Node *p, Op & op) const
3369 {
3370 return not traverse_in_arcs(p, [&op](auto a) { return not op(a); });
3371 }
3372
3374 template <class Op>
3375 bool exists_in_arc(Node *p, Op && op = Op()) const
3376 {
3377 return exists_in_arc(p, op);
3378 }
3379
3393 template <class Op>
3394 auto search_in_arc(Node *p, Op & op) const
3395 {
3396 Arc *ret = nullptr;
3397 traverse_in_arcs(p, [&op, &ret](auto a)
3398 {
3399 if (op(a))
3400 {
3401 ret = a;
3402 return false;
3403 }
3404 return true;
3405 });
3406 return ret;
3407 }
3408
3410 template <class Op>
3411 auto search_in_arc(Node *p, Op && op = Op()) const
3412 {
3413 return search_in_arc(p, op);
3414 }
3415
3424 template <typename T>
3425 auto in_arcs_map(Node *p, std::function<T(Arc *)> op) const
3426 {
3427 DynList<T> ret;
3428 for_each_in_arc(p, [&ret, &op](auto a) { ret.append(op(a)); });
3429 return ret;
3430 }
3431
3439 template <typename T = Arc_Type>
3440 T foldl_in_arcs(Node *p, const T & init,
3441 std::function<T(const T &, Arc *)> op) const
3442 {
3443 T ret = init;
3444 for_each_in_arc(p, [&ret, &op](auto a) { ret = op(ret, a); });
3445 return ret;
3446 }
3447
3455 template <class Op>
3456 DynList<Arc *> filter_in_arcs(Node *p, Op & op) const
3457 {
3458 DynList<Arc *> ret;
3459 for_each_in_arc(p, [&ret, &op](auto a)
3460 {
3461 if (op(a))
3462 ret.append(a);
3463 });
3464 return ret;
3465 }
3466
3468 template <class Op>
3469 auto filter_in_arcs(Node *p, Op && op = Op()) const
3470 {
3471 return filter_in_arcs(p, op);
3472 }
3473
3476 template <class Op>
3477 bool traverse_out_arcs(Node *p, Op & op) const
3478 {
3479 return traverse_arcs<Out_Iterator>(p, op);
3480 }
3481
3483 template <class Op>
3484 bool traverse_out_arcs(Node *p, Op && op = Op()) const
3485 {
3486 return traverse_out_arcs(p, op);
3487 }
3488
3490 template <class Op>
3491 void for_each_out_arc(Node *p, Op & op) const
3492 {
3493 for_each_arc<Out_Iterator>(p, op);
3494 }
3495
3497 template <class Op>
3498 void for_each_out_arc(Node *p, Op && op = Op()) const
3499 {
3500 for_each_out_arc(p, op);
3501 }
3502
3504 template <class Op>
3505 bool all_out_arcs(Node *p, Op & op) const
3506 {
3507 return traverse_out_arcs(p, [&op](auto a) { return op(a); });
3508 }
3509
3511 template <class Op>
3512 bool all_out_arcs(Node *p, Op && op = Op()) const
3513 {
3514 return all_out_arcs(p, op);
3515 }
3516
3519 template <class Op>
3520 bool exists_out_arc(Node *p, Op & op) const
3521 {
3522 return not traverse_out_arcs(p, [&op](auto a) { return not op(a); });
3523 }
3524
3526 template <class Op>
3527 bool exists_out_arc(Node *p, Op && op = Op()) const
3528 {
3529 return exists_out_arc(p, op);
3530 }
3531
3545 template <class Op>
3546 auto search_out_arc(Node *p, Op & op) const
3547 {
3548 typename GT::Arc *ret = nullptr;
3549 traverse_out_arcs(p, [&op, &ret](auto a)
3550 {
3551 if (op(a))
3552 {
3553 ret = a;
3554 return false;
3555 }
3556 return true;
3557 });
3558 return ret;
3559 }
3560
3562 template <class Op>
3563 auto search_out_arc(Node *p, Op && op = Op()) const
3564 {
3565 return search_out_arc(p, op);
3566 }
3567
3576 template <typename T = Arc_Type>
3577 auto out_arcs_map(Node *p, std::function<T(Arc *)> op) const
3578 {
3579 DynList<T> ret;
3580 for_each_out_arc(p, [&ret, &op](auto a) { ret.append(op(a)); });
3581 return ret;
3582 }
3583
3585 template <typename T = Arc_Type>
3586 T foldl_out_arcs(Node *p, const T & init,
3587 std::function<T(const T &, Arc *)> op) const
3588 {
3589 T ret = init;
3590 for_each_out_arc(p, [&ret, &op](auto a) { ret = op(ret, a); });
3591 return ret;
3592 }
3593
3601 template <class Op>
3602 DynList<Arc *> filter_out_arcs(Node *p, Op & op) const
3603 {
3604 DynList<Arc *> ret;
3605 for_each_out_arc(p, [&ret, &op](auto a)
3606 {
3607 if (op(a))
3608 ret.append(a);
3609 });
3610 return ret;
3611 }
3612
3614 template <class Op>
3615 auto filter_out_arcs(Node *p, Op && op = Op()) const
3616 {
3617 return filter_out_arcs(p, op);
3618 }
3619
3620 // ===========================================================================
3621 // Arc Removal Helpers (protected, for use in remove_node implementations)
3622 // ===========================================================================
3623
3624protected:
3639 template <class Predicate>
3640 DynList<Arc*> collect_arcs_if(Predicate pred) const
3641 {
3642 DynList<Arc*> result;
3643 for (typename GT::Arc_Iterator it(*const_me()); it.has_curr(); it.next_ne())
3644 if (pred(it.get_curr_ne()))
3645 result.append(it.get_curr_ne());
3646 return result;
3647 }
3648
3670 template <class Predicate>
3671 void remove_arcs_if(Predicate pred)
3672 {
3673 collect_arcs_if(pred).for_each([this](auto a) { me()->remove_arc(a); });
3674 }
3675
3676public:
3677 // ===========================================================================
3678 // Sorting Methods (for Dlink-based graphs)
3679 // ===========================================================================
3680
3699 template <class U>
3700 static constexpr bool has_node_dlink_v =
3701 requires(U & u) { { u.get_node_dlink() } -> std::same_as<Dlink&>; };
3702
3703 template <class U>
3704 static constexpr bool has_arc_dlink_v =
3705 requires(U & u) { { u.get_arc_dlink() } -> std::same_as<Dlink&>; };
3706
3707 template <class Compare>
3708 void sort_nodes(Compare & cmp) noexcept
3709 requires(has_node_dlink_v<GT>)
3710 {
3712 mergesort(me()->get_node_dlink(), c);
3713 }
3714
3716 template <class Compare>
3717 void sort_nodes(Compare && cmp = Compare()) noexcept
3718 requires(has_node_dlink_v<GT>)
3719 {
3720 sort_nodes(cmp);
3721 }
3722
3741 template <class Compare>
3742 void sort_arcs(Compare & cmp) noexcept
3743 requires(has_arc_dlink_v<GT>)
3744 {
3746 mergesort(me()->get_arc_dlink(), c);
3747 }
3748
3750 template <class Compare>
3751 void sort_arcs(Compare && cmp = Compare()) noexcept
3752 requires(has_arc_dlink_v<GT>)
3753 {
3754 sort_arcs(cmp);
3755 }
3756};
3757
3758
3759// ============================================================================
3760// Macro for Copy/Move/Swap Pattern
3761// ============================================================================
3762
3792#define ALEPH_GRAPH_COPY_MOVE_CTORS(GraphClass) \
3793 \
3794 GraphClass(const GraphClass & g) \
3795 { \
3796 copy_graph(*this, g); \
3797 } \
3798 \
3799 \
3800 GraphClass(GraphClass && g) noexcept \
3801 { \
3802 swap(g); \
3803 } \
3804 \
3805 \
3806 GraphClass & operator=(const GraphClass & g) \
3807 { \
3808 if (this == &g) \
3809 return *this; \
3810 copy_graph(*this, g); \
3811 return *this; \
3812 } \
3813 \
3814 \
3815 GraphClass & operator=(GraphClass && g) noexcept \
3816 { \
3817 swap(g); \
3818 return *this; \
3819 }
3820
3821
3822namespace Aleph
3823{
3824
3852template <class BaseGraph>
3853class Digraph : public BaseGraph
3854{
3855public:
3856 using GT = BaseGraph;
3857 using Node = typename BaseGraph::Node;
3858 using Arc = typename BaseGraph::Arc;
3859
3865 {
3866 this->digraph = true;
3867 }
3868
3876 Digraph(const Digraph & dg) : GT()
3877 {
3878 this->digraph = true;
3879 copy_graph(*this, dg, false);
3880 }
3881
3890 {
3891 this->digraph = true;
3892 this->swap(dg);
3893 }
3894
3904 {
3905 if (this == &g)
3906 return *this;
3907
3908 this->digraph = true;
3909 copy_graph(*this, g, false);
3910
3911 return *this;
3912 }
3913
3922 Digraph & operator = (Digraph && g) noexcept
3923 {
3924 this->digraph = true;
3925 this->swap(g);
3926
3927 return *this;
3928 }
3929};
3930
3931} // namespace Aleph
3932
3933
3934# endif // GRAPH_DRY_H
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3854
Digraph(const Digraph &dg)
Copy constructor.
Definition graph-dry.H:3876
Digraph(Digraph &&dg) noexcept
Move constructor.
Definition graph-dry.H:3889
Digraph & operator=(const Digraph &g)
Copy assignment operator.
Definition graph-dry.H:3903
typename BaseGraph::Arc Arc
Definition graph-dry.H:3858
typename BaseGraph::Node Node
Definition graph-dry.H:3857
Digraph() noexcept
Default constructor.
Definition graph-dry.H:3864
BaseGraph GT
Definition graph-dry.H:3856
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
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_first()
Resets the iterator to the first filtered element.
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:576
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
Definition tpl_graph.H:595
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:649
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
Common methods for the arc of a graph.
Definition graph-dry.H:528
GTArcCommon() noexcept=default
data contained in arc
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
void * tgt_node
Please don't use.
Definition graph-dry.H:534
void * get_connected_node(void *node) noexcept
Definition graph-dry.H:600
GTArcCommon(const GTArcCommon &other)
Copy constructor.
Definition graph-dry.H:562
GTArcCommon(void *src, void *tgt, const ArcInfo &data)
Construct with endpoints and info (copy)
Definition graph-dry.H:554
const ArcInfo & get_info() const noexcept
Return a constant reference to the arc data.
Definition graph-dry.H:598
GTArcCommon(GTArcCommon &&other) noexcept
Move constructor.
Definition graph-dry.H:566
Graph_Attr attrs
Please don't use.
Definition graph-dry.H:540
ArcInfo arc_info
Definition graph-dry.H:542
GTArcCommon(ArcInfo &&info)
Construct from info value (move)
Definition graph-dry.H:551
GTArcCommon & operator=(const GTArcCommon &other)
Copy assignment operator.
Definition graph-dry.H:570
void set_state(unsigned int s) noexcept
Set the state of arc to value s
Definition graph-dry.H:589
void * get_img_node(void *node) noexcept
Definition graph-dry.H:605
GTArcCommon(void *src, void *tgt, ArcInfo &&data=ArcInfo())
Construct with endpoints and info (move)
Definition graph-dry.H:558
unsigned int state() const noexcept
Return the state of arc.
Definition graph-dry.H:586
void * src_node
Definition graph-dry.H:533
GTArcCommon & operator=(GTArcCommon &&other) noexcept
Move assignment operator.
Definition graph-dry.H:578
Common attributes and methods for nodes (vertexes) belonging to graphs.
Definition graph-dry.H:435
NodeInfo Item_Type
Definition graph-dry.H:456
const NodeInfo & get_info() const noexcept
Return a constant reference to the data contained in the node.
Definition graph-dry.H:497
GTNodeCommon() noexcept=default
another alias for set type
NodeInfo node_info
Definition graph-dry.H:443
GTNodeCommon & operator=(const GTNodeCommon &other)
Copy assignment operator.
Definition graph-dry.H:478
GTNodeCommon(NodeInfo &&info)
Move constructor from info value.
Definition graph-dry.H:469
Graph_Attr attrs
Attributes of node.
Definition graph-dry.H:441
unsigned int state() const noexcept
Return the state's value.
Definition graph-dry.H:500
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
NodeInfo Node_Type
The node.
Definition graph-dry.H:460
GTNodeCommon & operator=(GTNodeCommon &&other) noexcept
Move assignment operator.
Definition graph-dry.H:486
GTNodeCommon(GTNodeCommon &&other) noexcept
Move constructor.
Definition graph-dry.H:475
GTNodeCommon(const GTNodeCommon &other)
Copy constructor.
Definition graph-dry.H:472
void set_state(unsigned int s) noexcept
Set the state to value s
Definition graph-dry.H:503
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:454
Special iterator for distinguishing input arcs of output ones.
Definition graph-dry.H:2977
void prev()
back to previous item.
Definition graph-dry.H:3002
typename Itor::Item_Type Item_Type
Definition graph-dry.H:2984
void next()
Advance to next arc.
Definition graph-dry.H:2998
Digraph_Iterator(Node *p)
Instantiate an filtered iterator for arcs on the node p
Definition graph-dry.H:2989
void reset_last() noexcept
Reset the iterator to last arc.
Definition graph-dry.H:3049
Filter_Iterator< Node *, typename GT::Node_Arc_Iterator, Filter > Itor
Definition graph-dry.H:2978
GT::Arc * get_curr_ne() const noexcept
Definition graph-dry.H:3013
GT::Arc * get_curr() const
Return the current arc.
Definition graph-dry.H:3011
GT::Node * get_node(typename GT::Arc *a) const noexcept
Return the node connected to p (passed during construction) and linked through a
Definition graph-dry.H:3022
Itor Iterator_Type
the type of items (Arc*)
Definition graph-dry.H:2986
auto get_tgt_node_ne() const noexcept
Backward-compatible alias: return target node (same as get_node_ne()).
Definition graph-dry.H:3035
auto get_current_arc_ne() const noexcept
Definition graph-dry.H:3018
GT::Node * get_node_ne() const noexcept
Return the node connected to p (passed during construction) and linked through the current arc.
Definition graph-dry.H:3029
void reset_first() noexcept
Reset the iterator to first arc.
Definition graph-dry.H:3046
bool has_curr() const noexcept
Return true is the iterator has a current arc.
Definition graph-dry.H:3007
auto get_tgt_node() const
Backward-compatible alias: return target node (same as get_node()).
Definition graph-dry.H:3043
GT::Node * get_node() const
Definition graph-dry.H:3037
Common methods to the Aleph-w ( ) graph classes.
Definition graph-dry.H:618
void init() noexcept
Definition graph-dry.H:634
T foldl_nodes(const T &init, std::function< T(const T &, Node *)> op) const
Folding of nodes on a graph.
Definition graph-dry.H:1892
void reset_bit_nodes(int bit) const noexcept
Reset bit to zero for all the nodes of graph.
Definition graph-dry.H:1046
Node * search_node(Op &&op) const
Overload of search_node(Op&) that accepts rvalues.
Definition graph-dry.H:2575
typename Arc::Arc_Type Arc_Type
Definition graph-dry.H:631
void for_each_arc(Node *p, Operation &op) const
Unconditionally traverse all the arcs adjacnt to a node and on each one perform an operation.
Definition graph-dry.H:1567
auto search_in_arc(Node *p, Op &op) const
Search an incoming arc to a node satisfaying a condition.
Definition graph-dry.H:3394
size_t count_nodes(Operation op=[](Node *) { return true;}) const
Count the nodes satisfying a condition.
Definition graph-dry.H:2302
T foldl_arcs(Node *p, const T &init, std::function< T(const T &, Arc *)> op) const
Folding of arcs of a node.
Definition graph-dry.H:1977
void for_each_out_arc(Node *p, Op &op) const
Perform op on each outcoming arc of node p
Definition graph-dry.H:3491
bool exists_arc(Operation &&op=Operation()) const
Overload of exists_arc(Operation&) that accepts rvalues.
Definition graph-dry.H:2171
bool exists_in_arc(Node *p, Op &op) const
Return true if it exists a incoming arc to p returning true for op
Definition graph-dry.H:3368
void * get_cookie() const noexcept
Return a constant reference to graph's cookie.
Definition graph-dry.H:654
Arc * emplace_arc(Node *src, Node *tgt, Args &&... args)
Insert a new arc in the graph by constructing its associated data in-place with the given args.
Definition graph-dry.H:1258
bool traverse_arcs(Node *p, Operation &op) const
Conditioned traversal of all the adjacent arcs of a node.
Definition graph-dry.H:1442
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:933
T foldl_arcs(const T &init, std::function< T(const T &, Arc *)> op) const
Folding of arcs on a graph.
Definition graph-dry.H:1934
auto filter_in_arcs(Node *p, Op &&op=Op()) const
Overload of filter_in_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3469
void reset_cookie_arcs() const noexcept
Reset all the cookies to `nullptr for all the arcs of graph.
Definition graph-dry.H:1098
Container< Arc * > arcs() const
Return a container with all the arcs of the graph.
Definition graph-dry.H:2744
int get_bit(Arc *arc, int bit) const noexcept
Get the control bit of arc
Definition graph-dry.H:849
Arc * find_arc(const Arc_Type &info) const noexcept
Find an arc mathing a content.
Definition graph-dry.H:2642
size_t get_num_arcs(Node *node) const noexcept
Return the total of arcs of a node.
Definition graph-dry.H:788
Node * find_node(const Node_Type &info) const noexcept
Find a node mathing a content.
Definition graph-dry.H:2591
Arc * search_arc(Node *p, Operation &&op=Operation()) const
Overload of search_arc(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:2679
size_t num_nodes
Definition graph-dry.H:625
bool none_node(Operation &op) const
Determine if no node satisfies a condition.
Definition graph-dry.H:2226
bool all_arcs(Operation &&op=Operation()) const
Overload of all_arcs(Operation&) that accepts rvalues.
Definition graph-dry.H:1659
bool exists_out_arc(Node *p, Op &op) const
Return true if it exists a outcoming arc to p returning true for op
Definition graph-dry.H:3520
Arc * search_arc(Op &&op) const
Overload of search_arc(Op&) that accepts rvalues.
Definition graph-dry.H:2626
void for_each_in_arc(Node *p, Op &op) const
Perform op on each incoming arc of node p
Definition graph-dry.H:3339
auto arcs_map(Node *p, std::function< T(Arc *)> operation) const
Map the adjacent arcs of a node to a specific range.
Definition graph-dry.H:1847
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:2808
auto filter_arcs(Op &op) const
Filter the arcs of graph satisfying a condition.
Definition graph-dry.H:2047
void reset_counter(Node *node) const noexcept
Reset the node counter to zero.
Definition graph-dry.H:879
void for_each_out_arc(Node *p, Op &&op=Op()) const
Overload of for_each_out_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3498
bool none_arc(Node *p, Operation &op) const
Determine if no arc adjacent to a node satisfies a condition.
Definition graph-dry.H:2273
Node * insert_node(Node_Type &&node_info=Node_Type())
Allocate a new node, set by moving its data content and insert it into the graph.
Definition graph-dry.H:1146
In_Iterator get_in_it(Node *p) const noexcept
Return an input iterator on the incoming arcs to p
Definition graph-dry.H:3085
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:1122
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:737
Node * get_node() const
Return any node in the graph.
Definition graph-dry.H:714
Out_Iterator get_out_it(Node *p) const noexcept
Return an output iterator on the incoming nodes to p
Definition graph-dry.H:3105
auto search_out_arc(Node *p, Op &op) const
Search an outcoming arc to a node satisfaying a condition.
Definition graph-dry.H:3546
std::tuple< Arc *, Node * > ArcPair
Pair of arc and node (topologically related)
Definition graph-dry.H:3195
bool traverse_arcs(Operation &op) const
Conditioned traversal of all the arcs of a graph.
Definition graph-dry.H:1375
DynList< Node * > in_nodes(Node *p) const
Return a list with the incoming nodes to p
Definition graph-dry.H:3135
T foldl_out_arcs(Node *p, const T &init, std::function< T(const T &, Arc *)> op) const
Fold-left over outcoming arcs of a node.
Definition graph-dry.H:3586
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
auto filter_arcs(Node *p, Op &op) const
Filter the arcs adjacent to a node satisfying a condition.
Definition graph-dry.H:2096
Arc * search_arc(Node *p, Operation &op) const
Linear search of an arc.
Definition graph-dry.H:2666
auto out_arcs_map(Node *p, std::function< T(Arc *)> op) const
Return a list of outcoming arcs of a node mapped to items of type given by transformation op.
Definition graph-dry.H:3577
auto filter_nodes(Op &&op) const
Overload of filter_nodes(Op&) that accepts rvalues.
Definition graph-dry.H:2020
bool traverse_in_arcs(Node *p, Op &&op=Op()) const
Overload of traverse_in_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3332
bool all_nodes(Operation &op) const
Check if all the nodes of graph satisfy an boolean condition.
Definition graph-dry.H:1610
void reset_bit_nodes() const noexcept
Reset all the bits for all the nodes of graph.
Definition graph-dry.H:1058
auto search_in_arc(Node *p, Op &&op=Op()) const
Overload of search_in_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3411
Arc * max_arc(Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the maximum arc in the entire graph.
Definition graph-dry.H:2460
bool traverse_arcs(Node *p, Operation &&op=Operation()) const
Overload of traverse_arcs(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:1452
auto search_out_arc(Node *p, Op &&op=Op()) const
Overload of search_out_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3563
void for_each_in_arc(Node *p, Op &&op=Op()) const
Overload of for_each_in_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3346
T sum_arcs(Node *p) const
Overload of sum_arcs(Node*, Extract) using the arc info as extractor.
Definition graph-dry.H:2373
Arc * insert_arc(Node *src, Node *tgt, Arc_Type &&arc_info=Arc_Type())
Create and insert a new arc linking two nodes and moving the received data.
Definition graph-dry.H:1229
void set_bit(Node *node, int bit, int value) const noexcept
Set the control bit of node to value
Definition graph-dry.H:825
DynList< Arc * > out_arcs(Node *p) const
Return a list with the outcoming arcs to p`.
Definition graph-dry.H:3170
bool exists_in_arc(Node *p, Op &&op=Op()) const
Overload of exists_in_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3375
void set_bit(Arc *arc, int bit, int value) const noexcept
Set the control bit of arc to value
Definition graph-dry.H:855
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:657
void reset_cookie_nodes() const noexcept
Reset all the cookies to `nullptr for all the nodes of graph.
Definition graph-dry.H:1087
void reset_counter(Arc *arc) const noexcept
Reset the acr counter to zero.
Definition graph-dry.H:905
size_t num_arcs
Definition graph-dry.H:626
bool none_arc(Operation &&op) const
Overload of none_arc(Operation&) that accepts rvalues.
Definition graph-dry.H:2260
auto arcs_map(std::function< T(Arc *)> operation) const
Map the arcs of a graph to a specific range.
Definition graph-dry.H:1796
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
Definition graph-dry.H:692
bool all_nodes(Operation &&op=Operation()) const
Overload of all_nodes(Operation&) that accepts rvalues.
Definition graph-dry.H:1617
bool traverse_out_arcs(Node *p, Op &&op=Op()) const
Overload of traverse_out_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3484
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Definition graph-dry.H:651
void for_each_arc(Node *p, Operation &op) const
Perform op on each arc of node p
Definition graph-dry.H:3316
size_t degree(Node *p) const noexcept
Return the total of arcs (or degree) of a node.
Definition graph-dry.H:795
bool exists_arc(Node *p, Operation &op) const
Determine if exists at least a arc adjacent to a node satisfying a condition.
Definition graph-dry.H:2199
void for_each_node(Operation &&operation=Operation()) const
Overload of for_each_node(Operation&) that accepts rvalues.
Definition graph-dry.H:1490
bool exists_node(Operation &op) const
Determine if exists at least a node satisfying a condition.
Definition graph-dry.H:2133
DynList< Arc * > filter_in_arcs(Node *p, Op &op) const
Filter the incoming arcs of a node.
Definition graph-dry.H:3456
bool all_in_arcs(Node *p, Op &op) const
Return true if op is true for all the incoming arcs to node p
Definition graph-dry.H:3353
void reset_bit_arcs() const noexcept
Reset all the bits for all the arcs of graph.
Definition graph-dry.H:1064
void for_each_arc(Operation &&operation=Operation()) const
Overload of for_each_arc(Operation&) that accepts rvalues.
Definition graph-dry.H:1528
static void map_arcs(A1 *p, A2 *q) noexcept
Map the arcs through their cookies.
Definition graph-dry.H:1032
auto in_pairs(Node *p) const
Return a list of pair incoming arcs and nodes.
Definition graph-dry.H:3209
void reset_counter_nodes() const noexcept
Reset all the counters to zero for all the nodes of graph.
Definition graph-dry.H:1070
Arc * search_arc(Node *src, Node *tgt) const noexcept
Search an arc linking two nodes.
Definition graph-dry.H:2707
bool traverse_in_arcs(Node *p, Op &op) const
Traverse the incoming arcs of node p executing the conditioned operation
Definition graph-dry.H:3325
bool none_node(Operation &&op) const
Overload of none_node(Operation&) that accepts rvalues.
Definition graph-dry.H:2233
typename Node::Node_Type Node_Type
Definition graph-dry.H:630
void for_each_arc(Node *p, Operation &&op=Operation()) const
Overload of for_each_arc(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:1575
void sort_arcs(Compare &cmp) noexcept
Sort all the arcs of the graph according to a specific criteria.
Definition graph-dry.H:3742
DynList< Arc * > in_arcs(Node *p) const
Return a list with the incoming arcs to p`.
Definition graph-dry.H:3186
auto filter_arcs(Op &&op) const
Overload of filter_arcs(Op&) that accepts rvalues.
Definition graph-dry.H:2060
Arc * max_arc(Node *p, Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the maximum arc adjacent to a node.
Definition graph-dry.H:2424
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:778
bool traverse_out_arcs(Node *p, Op &op) const
Traverse the outcoming arcs of node p executing the conditioned operation
Definition graph-dry.H:3477
size_t count_arcs(Operation op=[](Arc *) { return true;}) const
Count the arcs satisfying a condition.
Definition graph-dry.H:2326
void common_swap(GT &g) noexcept
Definition graph-dry.H:641
auto in_arcs_map(Node *p, std::function< T(Arc *)> op) const
Return a list of incoming arcs of a node mapped to items of type given by transformation op.
Definition graph-dry.H:3425
bool all_arcs(Node *p, Operation &&op=Operation()) const
Overload of all_arcs(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:1704
constexpr bool is_empty() const noexcept
Checks if the graph is empty (has no nodes).
Definition graph-dry.H:701
bool all_arcs(Node *p, Operation &op) const
Check if all the arcs adjacent to a node satisfy an boolean condition.
Definition graph-dry.H:1697
long & get_counter(Node *node) const noexcept
Get a modifiable reference to the counter of node
Definition graph-dry.H:873
bool all_out_arcs(Node *p, Op &op) const
Return true if op is true for all the outcoming arcs to node p
Definition graph-dry.H:3505
void reset_bit(Node *node, int bit) const noexcept
Reset the bit of node (to zero)
Definition graph-dry.H:807
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
Definition graph-dry.H:1052
GT * me()
Definition graph-dry.H:619
size_t out_degree(Node *p) const noexcept
Compute the output degree of a node.
Definition graph-dry.H:3279
void for_each_arc(Operation &op) const
Unconditionally traverse all the arcs of graph and on each one perform an operation.
Definition graph-dry.H:1520
Node * emplace_node(Args &&... args)
Insert a new node in the graph by constructing it in-place with the given args.
Definition graph-dry.H:1172
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:1199
void reset_node_counters() const noexcept
Reset all the node counters of graph to zero.
Definition graph-dry.H:885
Bit_Fields & get_control_bits(Node *node) const noexcept
Return a reference to control fields of node
Definition graph-dry.H:801
Node * get_arc(Node *p)
Return any arc adjacent to a node.
Definition graph-dry.H:734
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:784
Arc * search_arc(Op &op) const
Linear search of an arc.
Definition graph-dry.H:2613
void reset_counter_arcs() const noexcept
Reset all the counters to zero for all the arcs of graph.
Definition graph-dry.H:1076
void sort_arcs(Compare &&cmp=Compare()) noexcept
Definition graph-dry.H:3751
constexpr size_t vsize() const noexcept
Definition graph-dry.H:704
bool none_arc(Operation &op) const
Determine if no arc satisfies a condition.
Definition graph-dry.H:2253
bool none_arc(Node *p, Operation &&op) const
Overload of none_arc(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:2280
void reset_bits(Node *node) const noexcept
Reset all the control bits of node
Definition graph-dry.H:813
bool all_out_arcs(Node *p, Op &&op=Op()) const
Overload of all_out_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3512
Arc * search_directed_arc(Node *src, Node *tgt) const noexcept
Search a directed arc linking two nodes.
Definition graph-dry.H:3117
long & get_counter(Arc *arc) const noexcept
Get a modifiable reference to the counter of arc
Definition graph-dry.H:899
const GT * const_me() const
Definition graph-dry.H:621
size_t esize() const noexcept
Return the total of arcs of graph.
Definition graph-dry.H:798
void reset_arc(Arc *arc) const noexcept
Reset all the control attributes of arc.
Definition graph-dry.H:919
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Definition graph-dry.H:926
bool exists_arc(Node *p, Operation &&op=Operation()) const
Overload of exists_arc(Node*, Operation&) that accepts rvalues.
Definition graph-dry.H:2206
bool all_arcs(Operation &op) const
Check if all the arcs of graph satisfy a boolean condition.
Definition graph-dry.H:1652
void reset_node(Node *p) const noexcept
Reset all the control attributes of node p.
Definition graph-dry.H:893
size_t in_degree(Node *p) const noexcept
Compute the input degree of a node.
Definition graph-dry.H:3256
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:2786
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:743
Node * get_arc() const
Return any arc in the graph.
Definition graph-dry.H:724
auto filter_out_arcs(Node *p, Op &&op=Op()) const
Overload of filter_out_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3615
DynList< Arc * > collect_arcs_if(Predicate pred) const
Collect all arcs matching a predicate.
Definition graph-dry.H:3640
bool all_in_arcs(Node *p, Op &&op=Op()) const
Overload of all_in_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3360
void reset_bits(Arc *arc) const noexcept
Reset all the control bits of arc
Definition graph-dry.H:843
auto get_arc_it(Node *p) const noexcept
Obtains an iterator to the adjacent arcs of a node.
Definition graph-dry.H:2831
auto nodes_map(std::function< T(Node *)> op) const
Map the nodes of a graph to a specific range.
Definition graph-dry.H:1749
bool traverse_nodes(Operation &&op=Operation()) const
Overload of traverse_nodes(Operation&) that accepts rvalues.
Definition graph-dry.H:1320
DynList< Node * > out_nodes(Node *p) const
Return a list with the outcoming nodes to p
Definition graph-dry.H:3153
void *& get_cookie(Arc *arc) const noexcept
Get a modifiable reference to the cookie pointer of arc
Definition graph-dry.H:867
Container< Node * > nodes() const
Return a container with all the nodes of the graph.
Definition graph-dry.H:2726
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1001
Container< Arc * > arcs(Node *p) const
Return a container with all the arcs adjacent to a node.
Definition graph-dry.H:2762
bool exists_arc(Operation &op) const
Determine if exists at least a arc satisfying a condition.
Definition graph-dry.H:2164
Arc * min_arc(Node *p, Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the minimum arc adjacent to a node.
Definition graph-dry.H:2396
bool exists_node(Operation &&op=Operation()) const
Overload of exists_node(Operation&) that accepts rvalues.
Definition graph-dry.H:2140
Arc * min_arc(Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the minimum arc in the entire graph.
Definition graph-dry.H:2442
Node * search_node(Op &op) const
Linear search of a node.
Definition graph-dry.H:2562
auto filter_nodes(Op &op) const
Filter the nodes satisfying a condition.
Definition graph-dry.H:2007
void reset_arc_counters() const noexcept
Reset all the arc counters of graph to zero.
Definition graph-dry.H:911
auto filter_arcs(Node *p, Op &&op) const
Overload of filter_arcs(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:2109
void reset_bit(Arc *arc, int bit) const noexcept
Reset the bit of arc to zero.
Definition graph-dry.H:837
void sort_nodes(Compare &cmp) noexcept
Definition graph-dry.H:3708
T sum_arcs(Node *p, Extract extract) const
Sum values derived from arcs adjacent to a node.
Definition graph-dry.H:2364
T foldl_in_arcs(Node *p, const T &init, std::function< T(const T &, Arc *)> op) const
Fold the incoming arcs of a node.
Definition graph-dry.H:3440
std::pair< DynList< Node * >, DynList< Node * > > partition_nodes(Operation op) const
Partition nodes into two groups based on a predicate.
Definition graph-dry.H:2488
Bit_Fields & get_control_bits(Arc *arc) const noexcept
Return a reference to the control bits of arc
Definition graph-dry.H:831
bool traverse_arcs(Node *p, Operation &op) const
Traverse of arcs of a node according to specific arcs iterator.
Definition graph-dry.H:3306
DynList< Arc * > filter_out_arcs(Node *p, Op &op) const
Filter the outcoming arcs of a node.
Definition graph-dry.H:3602
void sort_nodes(Compare &&cmp=Compare()) noexcept
Definition graph-dry.H:3717
DynList< Node * > adjacent_nodes(Node *p) const
Get all adjacent nodes (neighbors) of a node.
Definition graph-dry.H:2536
void for_each_node(Operation &operation) const
Unconditionally traverse all the nodes of graph and on each one perform an operation.
Definition graph-dry.H:1482
static constexpr bool has_arc_dlink_v
Definition graph-dry.H:3704
void remove_arcs_if(Predicate pred)
Remove all arcs matching a predicate.
Definition graph-dry.H:3671
bool traverse_nodes(Operation &op) const
Conditioned traversal of all the nodes of a graph.
Definition graph-dry.H:1310
void *& get_cookie(Node *node) const noexcept
Get a modifiable reference to the cookie pointer of node
Definition graph-dry.H:861
bool exists_out_arc(Node *p, Op &&op=Op()) const
Overload of exists_out_arc(Node*, Op&) that accepts rvalues.
Definition graph-dry.H:3527
static constexpr bool has_node_dlink_v
Sort all the nodes of the graph according to a specific criteria.
Definition graph-dry.H:3700
auto out_pairs(Node *p) const
Return a list of pair outcoming arcs and nodes.
Definition graph-dry.H:3232
int get_bit(Node *node, int bit) const noexcept
Get the control bit of node
Definition graph-dry.H:819
size_t count_arcs(Node *p, Operation op=[](Arc *) { return true;}) const
Count arcs adjacent to a node satisfying a condition.
Definition graph-dry.H:2340
void * cookie
Definition graph-dry.H:624
bool traverse_arcs(Operation &&op=Operation()) const
Overload of traverse_arcs(Operation&) that accepts rvalues.
Definition graph-dry.H:1385
std::pair< DynList< Arc * >, DynList< Arc * > > partition_arcs(Operation op) const
Partition arcs into two groups based on a predicate.
Definition graph-dry.H:2511
Concept for basic graph iterators.
Definition graph-dry.H:152
Concept for graph arc iterators.
Definition graph-dry.H:210
Concept for graph node iterators.
Definition graph-dry.H:191
Concept for node adjacency iterators.
Definition graph-dry.H:234
Concept for resettable graph iterators.
Definition graph-dry.H:172
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COUNTER(p)
Get the counter of a node.
#define ARC_COUNTER(p)
Return the counter of arc p.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
#define NODE_BITS(p)
Get the control bits of a node.
void copy_graph(GT &gtgt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Definition tpl_graph.H:3574
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:121
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.
Empty_Class ArcInfo
string NodeInfo
Empty placeholder class with no data members.
Definition ahDefs.H:105
Common arc iterator for graph having its arcs derived from Dlink class.
Definition graph-dry.H:301
Arc * get_curr() const
Return current arc.
Definition graph-dry.H:328
typename GT::Node Node
Definition graph-dry.H:302
Arc * Item_Type
The type of item that returns the iterator.
Definition graph-dry.H:306
Arc * get_curr_ne() const noexcept
Return current arc without exception.
Definition graph-dry.H:322
GTArcIterator() noexcept
Definition graph-dry.H:311
Node * get_tgt_node() const
Return the target node of current arc (if it is a directed graph)
Definition graph-dry.H:355
Arc * get_current_arc() const
Return the current arc.
Definition graph-dry.H:331
typename GT::Arc Arc
Definition graph-dry.H:303
Node * get_src_node_ne() const noexcept
Return the source node of current arc (if it is a directed graph)
Definition graph-dry.H:337
Arc * get_current_arc_ne() const noexcept
Return the current arc without exception.
Definition graph-dry.H:334
Node * get_tgt_node_ne() const noexcept
Return the target node of current arc (if it is a directed graph)
Definition graph-dry.H:343
Node * get_src_node() const
Return the source node of current arc (if it is a directed graph)
Definition graph-dry.H:349
GTArcIterator(Dlink &head) noexcept
Build a iterator for all the arcs of g.
Definition graph-dry.H:316
Common node iterator for graph having its node derived from Dlink class.
Definition graph-dry.H:256
GTNodeIterator() noexcept
Definition graph-dry.H:265
Node * Item_Type
The type of item that returns the iterator.
Definition graph-dry.H:260
Node * get_current_node() const
Return the current node.
Definition graph-dry.H:284
GTNodeIterator(Dlink &head) noexcept
Build a iterator for all the nodes of g.
Definition graph-dry.H:270
Node * get_curr_ne() const noexcept
Return the current node without exception.
Definition graph-dry.H:275
typename GT::Node Node
Definition graph-dry.H:257
Node * get_current_node_ne() const
Definition graph-dry.H:286
Node * get_curr() const
Return the current node.
Definition graph-dry.H:281
Filter for input arcs of a node.
Definition graph-dry.H:2869
Node * get_node(Arc *a) const noexcept
Return the source node of arc a
Definition graph-dry.H:2886
In_Filt(Node *__tgt=nullptr) noexcept
target node of iteration
Definition graph-dry.H:2873
bool operator()(Arc *a) const noexcept
Return true if the arc a is incoming arc to tgt; false otherwise.
Definition graph-dry.H:2879
Alias for Digraph_Iterator
Definition graph-dry.H:3057
Filter for output arcs of a node.
Definition graph-dry.H:2926
Out_Filt(Node *__src) noexcept
source node of iteration
Definition graph-dry.H:2930
Node * get_node(Arc *a) const noexcept
Return the source node of arc a (whose target is tgt)
Definition graph-dry.H:2943
bool operator()(Arc *a) const noexcept
Return true if a is a outcoming arc from src; false otherwise.
Definition graph-dry.H:2936