65#ifndef TPL_MAT_GRAPH_H
66#define TPL_MAT_GRAPH_H
80namespace matgraph_detail
92[[nodiscard]]
inline typename GT::Node*
96 <<
"Node index " << i <<
" out of range [0, " <<
nodes.size() <<
")";
97 return nodes.access(i);
108template <
typename GT>
109[[nodiscard]]
inline typename GT::Node*
113 <<
"Node index " << i <<
" out of range [0, " <<
nodes.size() <<
")";
114 return nodes.access(i);
125template <
typename GT>
126[[nodiscard]]
inline long
140[[nodiscard]]
inline long
154[[nodiscard]]
inline long
158 <<
"Matrix index (" << i <<
", " << j <<
") out of range [0, " << n <<
")";
171template <
typename Entry>
172[[nodiscard]]
inline Entry*
176 if (not mat.
exist(index))
190template <
typename Entry>
248template <
class GT,
class SA = Dft_Show_Arc<GT>>
336 return matgraph_detail::get_node<GT>(
nodes, i);
359 Mat_Entry* entry = matgraph_detail::read_matrix<Mat_Entry>(
364 return entry ? entry->
arc :
nullptr;
378 return entry ? entry->
arc :
nullptr;
393template <
class GT,
class SA>
398 copy_list_graph(*other.
lgraph);
402template <
class GT,
class SA>
410template <
class GT,
class SA>
421 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
422 nodes[i] = it.get_current_node_ne();
428 for (
typename GT::Node_Iterator nit(g); nit.has_curr(); nit.next_ne())
430 Node* src = nit.get_current_node_ne();
431 const long src_idx = matgraph_detail::node_index<GT>(
nodes,
num_nodes, src);
435 Arc* arc = ait.get_current_arc_ne();
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));
487template <
typename GT,
class SA = Dft_Show_Arc<GT>>
509 mutable size_t n = 0;
524 for (
size_t i = 0; i <
n; ++i)
527 for (
size_t j = 0; j <
n; ++j)
542 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
543 ptr[i] = it.get_current_node_ne();
548 for (
size_t idx = 0; idx <
n; ++idx)
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);
627 <<
"Node index " << i <<
" out of range";
693template <
class GT,
typename __Entry_Type,
class SA = Dft_Show_Arc<GT>>
726 <<
"Matrices refer to different graphs";
736 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
737 nodes[i] = it.get_current_node_ne();
765 return matgraph_detail::get_node<GT>(
nodes, i);
776 return matgraph_detail::get_node<GT>(
nodes, i);
824 return (*
this)(matgraph_detail::node_index<GT>(
nodes,
num_nodes, src),
837 return (*
this)(matgraph_detail::node_index<GT>(
nodes,
num_nodes, src),
920 template <
class Operation>
934 template <
class Operation>
949 template <
class Operation>
957 template <
class Operation>
963template <
class GT,
typename __Entry_Type,
class SA>
964template <
class Operation>
968 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
970 Node* src = nit.get_current_node_ne();
971 const long src_idx = matgraph_detail::node_index<Graph_Type>(
nodes,
num_nodes, src);
975 Arc* arc = at.get_current_arc_ne();
976 Node* tgt = at.get_tgt_node();
981 Operation()(*
this, arc, src_idx, tgt_idx, entry);
986template <
class GT,
typename __Entry_Type,
class SA>
987template <
class Operation>
991 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
993 Node* src = nit.get_current_node_ne();
994 const long src_idx = matgraph_detail::node_index<Graph_Type>(
nodes,
num_nodes, src);
998 Arc* arc = at.get_current_arc_ne();
999 Node* tgt = at.get_tgt_node();
1000 const long tgt_idx = matgraph_detail::node_index<Graph_Type>(
nodes,
num_nodes, tgt);
1003 Operation()(*
this, arc, src_idx, tgt_idx, entry, ptr);
1008template <
class GT,
typename __Entry_Type,
class SA>
1009template <
class Operation>
1014 for (
long s = 0; s < n; ++s)
1016 Node* src_node = matgraph_detail::get_node<GT>(
nodes, s);
1017 for (
long t = 0; t < n; ++t)
1019 Node* tgt_node = matgraph_detail::get_node<GT>(
nodes, t);
1021 Operation()(*
this, src_node, tgt_node, s, t, entry);
1026template <
class GT,
typename __Entry_Type,
class SA>
1027template <
class Operation>
1032 for (
long s = 0; s < n; ++s)
1034 Node* src_node = matgraph_detail::get_node<GT>(
nodes, s);
1035 for (
long t = 0; t < n; ++t)
1037 Node* tgt_node = matgraph_detail::get_node<GT>(
nodes, t);
1039 Operation()(*
this, src_node, tgt_node, s, t, entry, ptr);
1087template <
class GT,
class SA = Dft_Show_Arc<GT>>
1104 mutable size_t n = 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());
1119 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
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);
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);
1141 bit_index(matgraph_detail::index_array(i, j, bitmat.
n))
1245 <<
"No graph associated with bit matrix";
1246 return matgraph_detail::get_node<GT>(
nodes, i);
1258 <<
"No graph associated with bit matrix";
1259 return matgraph_detail::node_index<GT>(
nodes,
n, node);
1265 return Proxy(*
this, i, j);
1278 <<
"No graph associated with bit matrix";
1280 matgraph_detail::node_index<GT>(
nodes,
n, src_node),
1281 matgraph_detail::node_index<GT>(
nodes,
n, tgt_node));
1288 <<
"No graph associated with bit matrix";
1290 matgraph_detail::node_index<GT>(
nodes,
n, src_node),
1291 matgraph_detail::node_index<GT>(
nodes,
n, tgt_node));
1302template <
typename GT>
1303[[nodiscard]]
inline typename GT::Node*
1306 return matgraph_detail::get_node<GT>(
nodes, i);
1309template <
typename GT>
1310[[nodiscard]]
inline long
1313 return matgraph_detail::node_index<GT>(
nodes, n, p);
1316[[nodiscard]]
inline long
1322template <
typename Entry>
1323[[nodiscard]]
inline Entry*
1329template <
typename Entry>
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.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t value
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.
Ady_Mat(const Ady_Mat &other)
Copy constructor: the graph, the entries, the null value and the filter of other.
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.
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.
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.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Adjacency matrix mapped to an adjacency list graph.
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.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
__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)
DynArray< Graph::Node * > nodes
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.
Proxy & operator=(const Proxy &other)
Proxy(Bit_Mat_Graph &bitmat, long i, long j)
Proxy & operator=(int value)
Filtered iterator of adjacent arcs of a node.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.