Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_matgraph.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
65#ifndef TPL_MAT_GRAPH_H
66#define TPL_MAT_GRAPH_H
67
68#include <utility>
69#include <tpl_graph.H>
70#include <tpl_sort_utils.H>
71#include <ah-errors.H>
72
73namespace Aleph
74{
75
76// =============================================================================
77// Internal Helper Functions
78// =============================================================================
79
80namespace matgraph_detail
81{
82
91template <typename GT>
92[[nodiscard]] inline typename GT::Node*
94{
95 ah_out_of_range_error_if(i < 0 or static_cast<size_t>(i) >= nodes.size())
96 << "Node index " << i << " out of range [0, " << nodes.size() << ")";
97 return nodes.access(i);
98}
99
108template <typename GT>
109[[nodiscard]] inline typename GT::Node*
111{
112 ah_out_of_range_error_if(i < 0 or static_cast<size_t>(i) >= nodes.size())
113 << "Node index " << i << " out of range [0, " << nodes.size() << ")";
114 return nodes.access(i);
115}
116
125template <typename GT>
126[[nodiscard]] inline long
128{
129 return Aleph::binary_search(nodes, p, 0, n - 1);
130}
131
140[[nodiscard]] inline long
141index_array(long i, long j, long n) noexcept
142{
143 return i + j * n;
144}
145
154[[nodiscard]] inline long
155checked_index_array(long i, long j, long n)
156{
157 ah_out_of_range_error_if(i < 0 or i >= n or j < 0 or j >= n)
158 << "Matrix index (" << i << ", " << j << ") out of range [0, " << n << ")";
159 return index_array(i, j, n);
160}
161
171template <typename Entry>
172[[nodiscard]] inline Entry*
173read_matrix(const DynArray<Entry>& mat, long i, long j, long n)
174{
175 const long index = checked_index_array(i, j, n);
176 if (not mat.exist(index))
177 return nullptr;
178 return &const_cast<Entry&>(mat.access(index));
179}
180
190template <typename Entry>
191inline void
192write_matrix(DynArray<Entry>& mat, long i, long j, long n, const Entry& entry)
193{
194 mat[checked_index_array(i, j, n)] = entry;
195}
196
197} // namespace matgraph_detail
198
199
200// =============================================================================
201// Map_Matrix_Graph - Mapped Adjacency Matrix
202// =============================================================================
203
248template <class GT, class SA = Dft_Show_Arc<GT>>
250{
251public:
253 using Graph_Type = GT;
254
256 using Node_Type = typename GT::Node_Type;
257
259 using Arc_Type = typename GT::Arc_Type;
260
262 using Node = typename GT::Node;
263
265 using Arc = typename GT::Arc;
266
267private:
269 {
270 Arc* arc = nullptr;
271 Mat_Entry() = default;
272 explicit Mat_Entry(Arc* a) : arc(a) {}
273 };
274
276 GT* lgraph = nullptr;
277 mutable size_t num_nodes = 0;
278 SA sa;
280
281public:
289 void copy_list_graph(GT& g);
290
297 Map_Matrix_Graph(GT& g, SA&& __sa = SA())
298 : lgraph(&g), num_nodes(g.get_num_nodes()), sa(std::move(__sa))
299 {
301 }
302
309 Map_Matrix_Graph(GT& g, SA& __sa)
310 : lgraph(&g), num_nodes(g.get_num_nodes()), sa(__sa)
311 {
313 }
314
317 : lgraph(other.lgraph), num_nodes(other.num_nodes), sa(other.sa)
318 {
319 copy_list_graph(*other.lgraph);
320 }
321
324
327
334 [[nodiscard]] Node* operator()(long i)
335 {
336 return matgraph_detail::get_node<GT>(nodes, i);
337 }
338
345 [[nodiscard]] long operator()(Node* node) const
346 {
347 return matgraph_detail::node_index<GT>(nodes, num_nodes, node);
348 }
349
357 [[nodiscard]] Arc* operator()(Node* src_node, Node* tgt_node) const
358 {
359 Mat_Entry* entry = matgraph_detail::read_matrix<Mat_Entry>(
360 mat,
361 matgraph_detail::node_index<GT>(nodes, num_nodes, src_node),
362 matgraph_detail::node_index<GT>(nodes, num_nodes, tgt_node),
363 num_nodes);
364 return entry ? entry->arc : nullptr;
365 }
366
375 [[nodiscard]] Arc* operator()(long i, long j) const
376 {
377 Mat_Entry* entry = matgraph_detail::read_matrix<Mat_Entry>(mat, i, j, num_nodes);
378 return entry ? entry->arc : nullptr;
379 }
380
382 [[nodiscard]] GT& get_list_graph() noexcept { return *lgraph; }
383
385 [[nodiscard]] const GT& get_list_graph() const noexcept { return *lgraph; }
386
388 [[nodiscard]] size_t get_num_nodes() const noexcept { return num_nodes; }
389};
390
391// Implementation
392
393template <class GT, class SA>
396{
397 if (this != &other)
398 copy_list_graph(*other.lgraph);
399 return *this;
400}
401
402template <class GT, class SA>
405{
406 copy_list_graph(g);
407 return *this;
408}
409
410template <class GT, class SA>
411void
413{
414 lgraph = &g;
416 nodes.cut();
417 mat.cut();
418
419 // Copy nodes to array
420 long i = 0;
421 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
422 nodes[i] = it.get_current_node_ne();
423
424 // Sort for binary search
426
427 // Copy arcs to matrix
428 for (typename GT::Node_Iterator nit(g); nit.has_curr(); nit.next_ne())
429 {
430 Node* src = nit.get_current_node_ne();
431 const long src_idx = matgraph_detail::node_index<GT>(nodes, num_nodes, src);
432
433 for (Node_Arc_Iterator<GT, SA> ait(src, sa); ait.has_curr(); ait.next_ne())
434 {
435 Arc* arc = ait.get_current_arc_ne();
436 Node* tgt = g.get_connected_node(arc, src);
437 const long tgt_idx = matgraph_detail::node_index<GT>(nodes, num_nodes, tgt);
438 matgraph_detail::write_matrix<Mat_Entry>(mat, src_idx, tgt_idx, num_nodes, Mat_Entry(arc));
439 }
440 }
441}
442
443
444// =============================================================================
445// Matrix_Graph - Attribute-Copying Adjacency Matrix
446// =============================================================================
447
487template <typename GT, class SA = Dft_Show_Arc<GT>>
489{
490public:
492 using Graph_Type = GT;
493
495 using Node_Type = typename GT::Node_Type;
496
498 using Arc_Type = typename GT::Arc_Type;
499
501 using Node = typename GT::Node;
502
504 using Arc = typename GT::Arc;
505
506private:
509 mutable size_t n = 0;
511 SA sa;
512
513 void copy(Matrix_Graph& other)
514 {
515 if (this == &other)
516 return;
517
518 n = other.n;
519 Null_Value = other.Null_Value;
520 nodes.cut();
521 arcs.cut();
523
524 for (size_t i = 0; i < n; ++i)
525 {
526 nodes[i] = other.nodes[i];
527 for (size_t j = 0; j < n; ++j)
528 {
529 const long idx = matgraph_detail::index_array(i, j, n);
530 if (other.arcs.exist(idx))
531 arcs.touch(idx) = other.arcs.access(idx);
532 }
533 }
534 }
535
536 void copy(GT& g)
537 {
538 n = g.get_num_nodes();
540
541 long i = 0;
542 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
543 ptr[i] = it.get_current_node_ne();
544
545 quicksort(ptr);
547
548 for (size_t idx = 0; idx < n; ++idx)
549 {
550 typename GT::Node* src = ptr[idx];
551 nodes[idx] = src->get_info();
552 for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
553 {
554 typename GT::Arc* arc = it.get_current_arc_ne();
555 typename GT::Node* tgt = it.get_tgt_node();
556 const long j = matgraph_detail::node_index<GT>(ptr, n, tgt);
557 arcs[matgraph_detail::index_array(idx, j, n)] = arc->get_info();
558 }
559 }
560 }
561
562public:
564 [[nodiscard]] size_t get_num_nodes() const noexcept { return n; }
565
567 [[nodiscard]] const Arc_Type& null_value() const noexcept { return Null_Value; }
568
576 Matrix_Graph(GT& g, const Arc_Type& null, SA&& __sa = SA())
577 : Null_Value(null), sa(std::move(__sa))
578 {
579 copy(g);
580 }
581
582 Matrix_Graph(GT& g, const Arc_Type& null, SA& __sa)
583 : Null_Value(null), sa(__sa)
584 {
585 copy(g);
586 }
587
589 Matrix_Graph(Matrix_Graph& other) { copy(other); }
590
593 {
594 copy(other);
595 return *this;
596 }
597
600 {
601 copy(g);
602 return *this;
603 }
604
612 [[nodiscard]] const Arc_Type& operator()(long i, long j) const
613 {
614 const long idx = matgraph_detail::checked_index_array(i, j, n);
615 return arcs.exist(idx) ? arcs.access(idx) : Null_Value;
616 }
617
624 [[nodiscard]] Node_Type& operator()(long i) const
625 {
626 ah_out_of_range_error_if(i < 0 or static_cast<size_t>(i) >= n)
627 << "Node index " << i << " out of range";
628 return const_cast<Node_Type&>(nodes.access(i));
629 }
630
637 [[nodiscard]] Arc_Type& operator()(long i, long j)
638 {
640 }
641};
642
643
644// =============================================================================
645// Ady_Mat - Auxiliary Adjacency Matrix
646// =============================================================================
647
693template <class GT, typename __Entry_Type, class SA = Dft_Show_Arc<GT>>
695{
696public:
698 using Graph_Type = GT;
699
701 using Entry_Type = __Entry_Type;
702
704 using Node_Type = typename GT::Node_Type;
705
707 using Arc_Type = typename GT::Arc_Type;
708
710 using Node = typename GT::Node;
711
713 using Arc = typename GT::Arc;
714
715private:
716 GT* lgraph = nullptr;
717 SA sa;
720 mutable size_t num_nodes = 0;
722
723 void test_same_graph(const Ady_Mat& other) const
724 {
726 << "Matrices refer to different graphs";
727 }
728
729 void copy_nodes(GT& g)
730 {
731 lgraph = &g;
733 nodes.cut();
734
735 long i = 0;
736 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
737 nodes[i] = it.get_current_node_ne();
738
740 }
741
742 Ady_Mat& copy(const Ady_Mat& other)
743 {
744 if (this == &other)
745 return *this;
746
747 test_same_graph(other);
748 sa = other.sa;
749 nodes = other.nodes;
750 mat = other.mat;
751 num_nodes = other.num_nodes;
752 Null_Value = other.Null_Value;
753 return *this;
754 }
755
756public:
763 [[nodiscard]] Node* operator()(long i)
764 {
765 return matgraph_detail::get_node<GT>(nodes, i);
766 }
767
774 [[nodiscard]] Node* operator()(long i) const
775 {
776 return matgraph_detail::get_node<GT>(nodes, i);
777 }
778
785 [[nodiscard]] long operator()(Node* node) const
786 {
787 return matgraph_detail::node_index<GT>(nodes, num_nodes, node);
788 }
789
797 [[nodiscard]] Entry_Type& operator()(long i, long j)
798 {
800 }
801
809 [[nodiscard]] const Entry_Type& operator()(long i, long j) const
810 {
811 const long index = matgraph_detail::checked_index_array(i, j, num_nodes);
812 return mat.exist(index) ? mat.access(index) : Null_Value;
813 }
814
822 [[nodiscard]] Entry_Type& operator()(Node* src, Node* tgt)
823 {
824 return (*this)(matgraph_detail::node_index<GT>(nodes, num_nodes, src),
825 matgraph_detail::node_index<GT>(nodes, num_nodes, tgt));
826 }
827
835 [[nodiscard]] const Entry_Type& operator()(Node* src, Node* tgt) const
836 {
837 return (*this)(matgraph_detail::node_index<GT>(nodes, num_nodes, src),
838 matgraph_detail::node_index<GT>(nodes, num_nodes, tgt));
839 }
840
842 [[nodiscard]] GT& get_list_graph() noexcept { return *lgraph; }
843
845 [[nodiscard]] const GT& get_list_graph() const noexcept { return *lgraph; }
846
848 [[nodiscard]] const Entry_Type& null_value() const noexcept { return Null_Value; }
849
851 [[nodiscard]] size_t get_num_nodes() const noexcept { return num_nodes; }
852
864 explicit Ady_Mat(GT& g, SA arc_filter = SA())
865 : lgraph(&g), sa(std::move(arc_filter)), num_nodes(g.get_num_nodes())
866 {
867 copy_nodes(g);
868 }
869
878 Ady_Mat(GT& g, const Entry_Type& null, SA arc_filter = SA())
879 : lgraph(&g), sa(std::move(arc_filter)), num_nodes(g.get_num_nodes()),
880 Null_Value(null)
881 {
882 copy_nodes(g);
883 }
884
886 void set_null_value(const Entry_Type& null) noexcept { Null_Value = null; }
887
890 Ady_Mat(const Ady_Mat& other)
891 : lgraph(other.lgraph), sa(other.sa), nodes(other.nodes),
892 mat(other.mat), num_nodes(other.num_nodes),
894 {}
895
901 Ady_Mat& operator=(const Ady_Mat& other) { return copy(other); }
902
920 template <class Operation>
922
934 template <class Operation>
935 void operate_all_arcs_list_graph(void* ptr);
936
949 template <class Operation>
951
957 template <class Operation>
958 void operate_all_arcs_matrix(void* ptr);
959};
960
961// Implementation of operate_* methods
962
963template <class GT, typename __Entry_Type, class SA>
964template <class Operation>
965void
967{
968 for (typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
969 {
970 Node* src = nit.get_current_node_ne();
971 const long src_idx = matgraph_detail::node_index<Graph_Type>(nodes, num_nodes, src);
972
973 for (Node_Arc_Iterator<GT, SA> at(src, sa); at.has_curr(); at.next_ne())
974 {
975 Arc* arc = at.get_current_arc_ne();
976 Node* tgt = at.get_tgt_node();
977 const long tgt_idx =
978 matgraph_detail::node_index<Graph_Type>(nodes, num_nodes, tgt);
979 Entry_Type& entry =
980 mat.touch(matgraph_detail::index_array(src_idx, tgt_idx, num_nodes));
981 Operation()(*this, arc, src_idx, tgt_idx, entry);
982 }
983 }
984}
985
986template <class GT, typename __Entry_Type, class SA>
987template <class Operation>
988void
990{
991 for (typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
992 {
993 Node* src = nit.get_current_node_ne();
994 const long src_idx = matgraph_detail::node_index<Graph_Type>(nodes, num_nodes, src);
995
996 for (Node_Arc_Iterator<GT, SA> at(src, sa); at.has_curr(); at.next_ne())
997 {
998 Arc* arc = at.get_current_arc_ne();
999 Node* tgt = at.get_tgt_node(); // the other end, also from the target
1000 const long tgt_idx = matgraph_detail::node_index<Graph_Type>(nodes, num_nodes, tgt);
1001 Entry_Type& entry =
1002 mat.touch(matgraph_detail::index_array(src_idx, tgt_idx, num_nodes));
1003 Operation()(*this, arc, src_idx, tgt_idx, entry, ptr);
1004 }
1005 }
1006}
1007
1008template <class GT, typename __Entry_Type, class SA>
1009template <class Operation>
1010void
1012{
1013 const long n = num_nodes;
1014 for (long s = 0; s < n; ++s)
1015 {
1016 Node* src_node = matgraph_detail::get_node<GT>(nodes, s);
1017 for (long t = 0; t < n; ++t)
1018 {
1019 Node* tgt_node = matgraph_detail::get_node<GT>(nodes, t);
1021 Operation()(*this, src_node, tgt_node, s, t, entry);
1022 }
1023 }
1024}
1025
1026template <class GT, typename __Entry_Type, class SA>
1027template <class Operation>
1028void
1030{
1031 const long n = num_nodes;
1032 for (long s = 0; s < n; ++s)
1033 {
1034 Node* src_node = matgraph_detail::get_node<GT>(nodes, s);
1035 for (long t = 0; t < n; ++t)
1036 {
1037 Node* tgt_node = matgraph_detail::get_node<GT>(nodes, t);
1039 Operation()(*this, src_node, tgt_node, s, t, entry, ptr);
1040 }
1041 }
1042}
1043
1044
1045// =============================================================================
1046// Bit_Mat_Graph - Bit Matrix for Connectivity
1047// =============================================================================
1048
1087template <class GT, class SA = Dft_Show_Arc<GT>>
1089{
1090public:
1093
1095 using Node = typename GT::Node;
1096
1098 using Arc = typename GT::Arc;
1099
1100private:
1102 GT* lgraph = nullptr;
1104 mutable size_t n = 0;
1105
1106 // Sets the bits of the arcs of g that sa shows, on a matrix whose bits
1107 // are clear
1108 void copy_list_graph(GT& g, const SA & sa)
1109 {
1110 n = g.get_num_nodes();
1111 nodes.cut();
1112
1113 long i = 0;
1114 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1115 nodes[i++] = static_cast<typename GT::Node*>(it.get_current_node_ne());
1116
1118
1119 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1120 {
1121 typename GT::Node* src = it.get_current_node_ne();
1122 const size_t src_idx = matgraph_detail::node_index<Graph_Type>(nodes, n, src);
1123
1124 for (Node_Arc_Iterator<GT, SA> jt(src, sa); jt.has_curr(); jt.next_ne())
1125 {
1126 typename GT::Node* tgt = jt.get_tgt_node_ne();
1127 const size_t tgt_idx = matgraph_detail::node_index<Graph_Type>(nodes, n, tgt);
1128 bit_array[matgraph_detail::index_array(src_idx, tgt_idx, n)] = 1;
1129 }
1130 }
1131 }
1132
1134 struct Proxy
1135 {
1137 const size_t bit_index;
1138
1139 Proxy(Bit_Mat_Graph& bitmat, long i, long j)
1140 : bit_array(bitmat.bit_array),
1141 bit_index(matgraph_detail::index_array(i, j, bitmat.n))
1142 {}
1143
1144 Proxy& operator=(const Proxy& other)
1145 {
1146 bit_array[bit_index] = other.bit_array[other.bit_index];
1147 return *this;
1148 }
1149
1151 {
1153 return *this;
1154 }
1155
1156 operator int() const { return bit_array[bit_index]; }
1157 };
1158
1159public:
1161 [[nodiscard]] size_t get_num_nodes() const noexcept { return n; }
1162
1164 Bit_Mat_Graph() = default;
1165
1171 explicit Bit_Mat_Graph(GT& g, SA sa = SA())
1173 {
1174 copy_list_graph(g, sa);
1175 }
1176
1179 : bit_array(other.bit_array), lgraph(other.lgraph),
1180 nodes(other.nodes), n(other.n)
1181 {}
1182
1187 explicit Bit_Mat_Graph(size_t dim)
1188 : bit_array(dim * dim), nodes(dim), n(dim)
1189 {}
1190
1200 void set_list_graph(GT& g, SA sa = SA())
1201 {
1202 const size_t new_n = g.get_num_nodes();
1203 bit_array = BitArray(new_n * new_n); // set_size() would keep the old bits
1204 lgraph = &g;
1205 copy_list_graph(g, sa);
1206 }
1207
1209 [[nodiscard]] GT* get_list_graph() noexcept { return lgraph; }
1210
1212 [[nodiscard]] const GT* get_list_graph() const noexcept { return lgraph; }
1213
1216 {
1217 if (this != &other)
1218 {
1219 lgraph = other.lgraph;
1220 bit_array = other.bit_array;
1221 nodes = other.nodes;
1222 n = other.n;
1223 }
1224 return *this;
1225 }
1226
1230 {
1231 set_list_graph(g);
1232 return *this;
1233 }
1234
1242 [[nodiscard]] Node* operator()(long i)
1243 {
1244 ah_domain_error_if(lgraph == nullptr)
1245 << "No graph associated with bit matrix";
1246 return matgraph_detail::get_node<GT>(nodes, i);
1247 }
1248
1255 [[nodiscard]] long operator()(Node* node) const
1256 {
1257 ah_domain_error_if(lgraph == nullptr)
1258 << "No graph associated with bit matrix";
1259 return matgraph_detail::node_index<GT>(nodes, n, node);
1260 }
1261
1263 Proxy operator()(long i, long j)
1264 {
1265 return Proxy(*this, i, j);
1266 }
1267
1269 Proxy operator()(long i, long j) const
1270 {
1271 return Proxy(const_cast<Bit_Mat_Graph&>(*this), i, j);
1272 }
1273
1275 Proxy operator()(Node* src_node, Node* tgt_node)
1276 {
1277 ah_domain_error_if(lgraph == nullptr)
1278 << "No graph associated with bit matrix";
1279 return Proxy(*this,
1280 matgraph_detail::node_index<GT>(nodes, n, src_node),
1281 matgraph_detail::node_index<GT>(nodes, n, tgt_node));
1282 }
1283
1285 Proxy operator()(Node* src_node, Node* tgt_node) const
1286 {
1287 ah_domain_error_if(lgraph == nullptr)
1288 << "No graph associated with bit matrix";
1289 return Proxy(const_cast<Bit_Mat_Graph&>(*this),
1290 matgraph_detail::node_index<GT>(nodes, n, src_node),
1291 matgraph_detail::node_index<GT>(nodes, n, tgt_node));
1292 }
1293};
1294
1295
1296// =============================================================================
1297// Legacy Helper Functions (for backward compatibility)
1298// =============================================================================
1299
1301
1302template <typename GT>
1303[[nodiscard]] inline typename GT::Node*
1304get_node(DynArray<typename GT::Node*>& nodes, long i)
1305{
1306 return matgraph_detail::get_node<GT>(nodes, i);
1307}
1308
1309template <typename GT>
1310[[nodiscard]] inline long
1311node_index(const DynArray<typename GT::Node*>& nodes, long n, typename GT::Node* p)
1312{
1313 return matgraph_detail::node_index<GT>(nodes, n, p);
1314}
1315
1316[[nodiscard]] inline long
1317index_array(long i, long j, long n)
1318{
1319 return matgraph_detail::index_array(i, j, n);
1320}
1321
1322template <typename Entry>
1323[[nodiscard]] inline Entry*
1324read_matrix(const DynArray<Entry>& mat, long i, long j, long n)
1325{
1326 return matgraph_detail::read_matrix(mat, i, j, n);
1327}
1328
1329template <typename Entry>
1330inline void
1331write_matrix(DynArray<Entry>& mat, long i, long j, long n, const Entry& entry)
1332{
1333 matgraph_detail::write_matrix(mat, i, j, n, entry);
1334}
1335
1337
1338} // end namespace Aleph
1339
1340#endif // TPL_MAT_GRAPH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
int num_nodes
Definition btreepic.C:410
size_t size_t int32_t value
Definition ca-c-api.h:116
Auxiliary adjacency matrix with custom entry type.
Entry_Type & operator()(Node *src, Node *tgt)
Access entry by node pointers (modifiable).
void operate_all_arcs_list_graph()
Apply operation to all arcs from the graph.
Ady_Mat & copy(const Ady_Mat &other)
typename GT::Node_Type Node_Type
Node attribute type from graph.
const Entry_Type & operator()(Node *src, Node *tgt) const
Access entry by node pointers (const).
const GT & get_list_graph() const noexcept
Get const reference to underlying graph.
long operator()(Node *node) const
Get index of node.
Entry_Type & operator()(long i, long j)
Access entry by indices (modifiable).
Ady_Mat(GT &g, const Entry_Type &null, SA arc_filter=SA())
Construct from graph with null value.
typename GT::Node Node
Node type from graph.
Ady_Mat & operator=(const Ady_Mat &other)
Copy assignment from a matrix of the same graph: its entries, its null value and its filter.
typename GT::Arc_Type Arc_Type
Arc attribute type from graph.
Node * operator()(long i)
Get node pointer by index.
DynArray< Node * > nodes
Ady_Mat(const Ady_Mat &other)
Copy constructor: the graph, the entries, the null value and the filter of other.
void copy_nodes(GT &g)
SA sa
the filter of the arcs that the operations visit
Ady_Mat(GT &g, SA arc_filter=SA())
Construct from graph without null value.
void set_null_value(const Entry_Type &null) noexcept
Set the null value.
typename GT::Arc Arc
Arc type from graph.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
void operate_all_arcs_matrix()
Apply operation to all matrix entries.
Entry_Type Null_Value
const Entry_Type & null_value() const noexcept
Get the null value.
void test_same_graph(const Ady_Mat &other) const
GT & get_list_graph() noexcept
Get reference to underlying graph.
Node * operator()(long i) const
Get node pointer by index (const).
DynArray< Entry_Type > mat
const Entry_Type & operator()(long i, long j) const
Access entry by indices (const).
__Entry_Type Entry_Type
Entry type stored in matrix.
Contiguous array of bits.
Definition bitArray.H:201
Bit matrix for graph connectivity.
void copy_list_graph(GT &g, const SA &sa)
long operator()(Node *node) const
Get index of node.
DynArray< typename GT::Node * > nodes
Proxy operator()(Node *src_node, Node *tgt_node) const
Access bit by node pointers (const)
Proxy operator()(long i, long j) const
Access bit by indices (const)
const GT * get_list_graph() const noexcept
Get pointer to associated graph (const)
Bit_Mat_Graph()=default
Default constructor (empty matrix)
typename GT::Arc Arc
Arc type.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Bit_Mat_Graph(GT &g, SA sa=SA())
Construct from graph.
Proxy operator()(long i, long j)
Access bit by indices.
Bit_Mat_Graph & operator=(GT &g)
Assign from graph, with the filter SA(), as set_list_graph(): also when the matrix is already of g,...
Bit_Mat_Graph & operator=(const Bit_Mat_Graph &other)
Copy assignment.
Node * operator()(long i)
Get node by index.
GT * get_list_graph() noexcept
Get pointer to associated graph (nullptr if none)
typename GT::Node Node
Node type.
void set_list_graph(GT &g, SA sa=SA())
Associate with a graph.
Bit_Mat_Graph(const Bit_Mat_Graph &other)
Copy constructor.
Bit_Mat_Graph(size_t dim)
Construct with dimension only.
Proxy operator()(Node *src_node, Node *tgt_node)
Access bit by node pointers.
void set_default_initial_value(const T &value) noexcept(std::is_nothrow_copy_assignable_v< T >)
Set the default value.
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
T & touch(const size_t i)
Touch the entry i.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:447
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Adjacency matrix mapped to an adjacency list graph.
DynArray< Node * > nodes
typename GT::Node_Type Node_Type
Node attribute type from the graph.
const GT & get_list_graph() const noexcept
Get reference to underlying graph (const)
Map_Matrix_Graph(Map_Matrix_Graph &other)
Copy constructor.
typename GT::Node Node
Node type from the graph.
GT & get_list_graph() noexcept
Get reference to underlying graph.
Node * operator()(long i)
Get node pointer by index.
long operator()(Node *node) const
Get index of node.
Map_Matrix_Graph & operator=(Map_Matrix_Graph &other)
Copy assignment.
Map_Matrix_Graph(GT &g, SA &&__sa=SA())
Construct from adjacency list graph.
DynArray< Mat_Entry > mat
typename GT::Arc_Type Arc_Type
Arc attribute type from the graph.
Arc * operator()(Node *src_node, Node *tgt_node) const
Get arc by node pointers.
Arc * operator()(long i, long j) const
Get arc by indices.
typename GT::Arc Arc
Arc type from the graph.
Map_Matrix_Graph(GT &g, SA &__sa)
Construct from adjacency list graph.
void copy_list_graph(GT &g)
Copy graph structure to matrix.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Adjacency matrix storing copies of graph attributes.
Matrix_Graph(Matrix_Graph &other)
Copy constructor.
typename GT::Node_Type Node_Type
Node attribute type.
DynArray< Arc_Type > arcs
Matrix_Graph & operator=(Matrix_Graph &other)
Copy assignment.
void copy(Matrix_Graph &other)
Matrix_Graph(GT &g, const Arc_Type &null, SA &&__sa=SA())
Construct from graph with null value.
typename GT::Arc Arc
Arc type.
Matrix_Graph(GT &g, const Arc_Type &null, SA &__sa)
typename GT::Node Node
Node type.
const Arc_Type & null_value() const noexcept
Get the null value indicating no arc.
Node_Type & operator()(long i) const
Get node attribute by index.
typename GT::Arc_Type Arc_Type
Arc attribute type.
Matrix_Graph & operator=(GT &g)
Assign from graph.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
const Arc_Type & operator()(long i, long j) const
Get arc attribute by indices (const).
DynArray< Node_Type > nodes
Arc_Type & operator()(long i, long j)
Get/set arc attribute by indices.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
long node_index(const DynArray< typename GT::Node * > &nodes, long n, typename GT::Node *p)
Find index of node in sorted array using binary search.
GT::Node * get_node(DynArray< typename GT::Node * > &nodes, long i)
Get node pointer from sorted node array by index.
long index_array(long i, long j, long n) noexcept
Convert 2D matrix indices to 1D array index.
long checked_index_array(long i, long j, long n)
Validate and convert indices to linear index.
void write_matrix(DynArray< Entry > &mat, long i, long j, long n, const Entry &entry)
Write entry to matrix.
Entry * read_matrix(const DynArray< Entry > &mat, long i, long j, long n)
Read matrix entry if it exists.
Main namespace for Aleph-w library functions.
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Definition ahAlgo.H:1284
STL namespace.
Proxy for bit access.
Proxy & operator=(const Proxy &other)
Proxy(Bit_Mat_Graph &bitmat, long i, long j)
Proxy & operator=(int value)
Mat_Entry(Arc *a)
Arc * arc
Mat_Entry()=default
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.