65#ifndef TPL_MAT_GRAPH_H
66#define TPL_MAT_GRAPH_H
79namespace matgraph_detail
95 <<
"Node index " << i <<
" out of range [0, " <<
nodes.size() <<
")";
96 return nodes.access(i);
107template <
typename GT>
112 <<
"Node index " << i <<
" out of range [0, " <<
nodes.size() <<
")";
113 return nodes.access(i);
124template <
typename GT>
157 <<
"Matrix index (" << i <<
", " << j <<
") out of range [0, " << n <<
")";
170template <
typename Entry>
189template <
typename Entry>
247template <
class GT,
class SA = Dft_Show_Arc<GT>>
335 return matgraph_detail::get_node<GT>(
nodes, i);
358 Mat_Entry* entry = matgraph_detail::read_matrix<Mat_Entry>(
363 return entry ? entry->
arc :
nullptr;
377 return entry ? entry->
arc :
nullptr;
392template <
class GT,
class SA>
397 copy_list_graph(*
other.lgraph);
401template <
class GT,
class SA>
409template <
class GT,
class SA>
420 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
421 nodes[i] = it.get_current_node_ne();
427 for (
typename GT::Node_Iterator
nit(g);
nit.has_curr();
nit.next_ne())
429 Node* src =
nit.get_current_node_ne();
434 Arc* arc =
ait.get_current_arc_ne();
486template <
typename GT,
class SA = Dft_Show_Arc<GT>>
508 mutable size_t n = 0;
523 for (
size_t i = 0; i <
n; ++i)
526 for (
size_t j = 0; j <
n; ++j)
529 if (
other.arcs.exist(idx))
541 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
542 ptr[i] = it.get_current_node_ne();
547 for (
size_t idx = 0; idx <
n; ++idx)
553 typename GT::Arc* arc = it.get_current_arc_ne();
554 typename GT::Node* tgt = it.get_tgt_node();
555 const long j = matgraph_detail::node_index<GT>(ptr,
n, tgt);
626 <<
"Node index " << i <<
" out of range";
691template <
class GT,
typename __Entry_Type,
class SA = Dft_Show_Arc<GT>>
723 <<
"Matrices refer to different graphs";
733 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
734 nodes[i] = it.get_current_node_ne();
759 return matgraph_detail::get_node<GT>(
nodes, i);
770 return matgraph_detail::get_node<GT>(
nodes, i);
818 return (*
this)(matgraph_detail::node_index<GT>(
nodes,
num_nodes, src),
831 return (*
this)(matgraph_detail::node_index<GT>(
nodes,
num_nodes, src),
899 template <
class Operation>
913 template <
class Operation>
928 template <
class Operation>
936 template <
class Operation>
942template <
class GT,
typename __Entry_Type,
class SA>
943template <
class Operation>
947 for (
typename GT::Node_Iterator
nit(*lgraph);
nit.has_curr();
nit.next_ne())
949 Node* src =
nit.get_current_node_ne();
954 Arc* arc = at.get_current_arc_ne();
955 Node* tgt = at.get_tgt_node();
965template <
class GT,
typename __Entry_Type,
class SA>
966template <
class Operation>
970 for (
typename GT::Node_Iterator
nit(*lgraph);
nit.has_curr();
nit.next_ne())
972 Node* src =
nit.get_current_node_ne();
977 Arc* arc = at.get_current_arc_ne();
987template <
class GT,
typename __Entry_Type,
class SA>
988template <
class Operation>
993 for (
long s = 0; s < n; ++s)
995 Node* src_node = matgraph_detail::get_node<GT>(
nodes, s);
996 for (
long t = 0; t < n; ++t)
998 Node* tgt_node = matgraph_detail::get_node<GT>(
nodes, t);
1000 Operation()(*
this, src_node, tgt_node, s, t, entry);
1005template <
class GT,
typename __Entry_Type,
class SA>
1006template <
class Operation>
1011 for (
long s = 0; s < n; ++s)
1013 Node* src_node = matgraph_detail::get_node<GT>(
nodes, s);
1014 for (
long t = 0; t < n; ++t)
1016 Node* tgt_node = matgraph_detail::get_node<GT>(
nodes, t);
1018 Operation()(*
this, src_node, tgt_node, s, t, entry, ptr);
1066template <
class GT,
class SA = Dft_Show_Arc<GT>>
1083 mutable size_t n = 0;
1091 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1096 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1098 typename GT::Node* src = it.get_current_node_ne();
1099 const size_t src_idx = matgraph_detail::node_index<Graph_Type>(
nodes,
n, src);
1103 typename GT::Node* tgt =
jt.get_tgt_node_ne();
1104 const size_t tgt_idx = matgraph_detail::node_index<Graph_Type>(
nodes,
n, tgt);
1216 <<
"No graph associated with bit matrix";
1217 return matgraph_detail::get_node<GT>(
nodes, i);
1229 <<
"No graph associated with bit matrix";
1230 return matgraph_detail::node_index<GT>(
nodes,
n, node);
1236 return Proxy(*
this, i, j);
1249 <<
"No graph associated with bit matrix";
1251 matgraph_detail::node_index<GT>(
nodes,
n, src_node),
1252 matgraph_detail::node_index<GT>(
nodes,
n, tgt_node));
1259 <<
"No graph associated with bit matrix";
1261 matgraph_detail::node_index<GT>(
nodes,
n, src_node),
1262 matgraph_detail::node_index<GT>(
nodes,
n, tgt_node));
1273template <
typename GT>
1277 return matgraph_detail::get_node<GT>(
nodes, i);
1280template <
typename GT>
1284 return matgraph_detail::node_index<GT>(
nodes, n, p);
1293template <
typename Entry>
1300template <
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.
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.
typename GT::Node_Type Node_Type
Node attribute type from graph.
Ady_Mat(GT &g)
Construct from graph without null value.
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).
typename GT::Node Node
Node type from graph.
typename GT::Arc_Type Arc_Type
Arc attribute type from graph.
Node * operator()(long i)
Get node pointer by index.
Ady_Mat(Ady_Mat &other)
Copy constructor.
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.
Ady_Mat(GT &g, const Entry_Type &null)
Construct from graph with null value.
Ady_Mat & copy(Ady_Mat &other)
Ady_Mat & operator=(Ady_Mat &other)
Copy assignment.
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.
void set_size(const size_t sz)
Resets the dimension of the array.
Bit matrix for graph connectivity.
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)
Proxy operator()(long i, long j)
Access bit by indices.
void copy_list_graph(GT &g)
Bit_Mat_Graph & operator=(GT &g)
Assign from graph.
Bit_Mat_Graph(GT &g)
Construct from graph.
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)
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 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 ...
void set_default_initial_value(const T &value) noexcept
Set the default value.
T & touch(const size_t i)
Touch the entry i.
T & access(const size_t i) const 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
Advances the iterator to the next filtered element (noexcept version).
size_t size() const noexcept
Count the number of elements of the list.
typename Node::Node_Type Node_Type
The arc class type.
Graph_Node< int > Node
The graph type.
Graph_Arc< int > 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.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
__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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Proxy & operator=(const Proxy &other)
Proxy(Bit_Mat_Graph &bitmat, long i, long j)
Proxy & operator=(int value)
Arc of graph implemented with double-linked adjacency lists.
Filtered iterator of adjacent arcs of a node.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.