27 # ifndef TPL_MAT_GRAPH_H 28 # define TPL_MAT_GRAPH_H 30 # include <tpl_graph.H> 31 # include <tpl_sort_utils.H> 33 using namespace Aleph;
38 template <
typename GT>
inline static 42 if (i >= nodes.
size())
43 throw std::out_of_range(
"Index of node out of range");
48 template <
typename GT>
inline static long 50 const long & n,
typename GT::Node * p)
52 return Aleph::binary_search(nodes, p, 0, n - 1);
56 index_array(
const long & i,
const long & j,
const long & n)
59 throw std::out_of_range(
"Matrix index out of range");
64 template <
typename Entry>
66 const long & i,
const long & j,
69 const long index = index_array(i, j, n);
70 if (not mat.
exist(index))
73 return &
const_cast<Entry&
>(mat.
access(index));
76 template <
typename Entry>
83 mat[index_array(i, j, n)] = entry;
135 template <
class GT,
class SA = Dft_Show_Arc<GT> >
149 typedef typename GT::Node
Node;
152 typedef typename GT::Arc
Arc;
158 mutable size_t num_nodes;
164 Mat_Entry(Arc * __arc =
nullptr) : arc(__arc) { }
183 : lgraph(&g), num_nodes(g.get_num_nodes()), sa(__sa)
258 inline Arc *
operator () (Node * src_node, Node * tgt_node)
const;
275 inline Arc *
operator () (
const long & i,
const long & j)
const;
286 template <
class GT,
class SA>
typename GT::Node *
289 return get_node <GT> (nodes, i);
292 template <
class GT,
class SA>
long 295 return node_index <GT> (nodes, num_nodes, node);
298 template <
class GT,
class SA>
typename GT::Arc *
301 Mat_Entry * mat_entry = read_matrix<Mat_Entry>(mat, i, j, num_nodes);
302 if (mat_entry ==
nullptr)
305 return mat_entry->arc;
308 template <
class GT,
class SA>
typename GT::Arc *
311 Mat_Entry * mat_entry =
312 read_matrix<Mat_Entry>(mat, node_index<GT>(nodes, num_nodes, src_node),
313 node_index<GT>(nodes, num_nodes, tgt_node),
315 if (mat_entry ==
nullptr)
318 return mat_entry->arc;
331 template <
class GT,
class SA>
337 template <
class GT,
class SA>
342 num_nodes = g.get_num_nodes();
346 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
347 nodes[i] = it.get_current_node_ne();
350 for (
typename GT::Node_Iterator nit(g); nit.has_curr(); nit.next_ne())
352 Node * src = nit.get_current_node_ne();
353 const long src_idx = node_index<GT>(nodes, num_nodes, src);
358 Arc * arc = ait.get_current_arc_ne();
359 Node * tgt = g.get_connected_node(arc, src);
360 const long tgt_idx = node_index<GT>(nodes, num_nodes, tgt);
361 write_matrix<Mat_Entry>(mat, src_idx, tgt_idx, num_nodes, arc);
395 template <
typename GT,
class SA = Dft_Show_Arc<GT> >
412 typedef typename GT::Node
Node;
415 typedef typename GT::Arc
Arc;
422 mutable Arc_Type Null_Value;
431 Null_Value = mat.Null_value;
437 for (
int i = 0; i < n; ++i)
439 nodes[i] = mat.nodes[i];
440 for (
int j = 0; j < n; ++j)
442 const long mat_index = index_array(i, j, n);
443 if (not arcs.
exist(mat_index))
445 arcs.
touch(mat_index) = mat.arcs.
access(mat_index);
452 n = g.get_num_nodes();
456 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
457 ptr[i] = it.get_current_node_ne();
463 for (i = 0; i < n; ++i)
465 typename GT::Node * src = ptr[i];
466 nodes[i] = src->get_info();
469 typename GT::Arc * arc = it.get_current_arc_ne();
470 typename GT::Node * tgt = it.get_tgt_node();
471 const long j = node_index<GT>(ptr, n, tgt);
472 const long mat_index = index_array(i, j, n);
473 arcs[mat_index] = arc->get_info();
504 : Null_Value(null), sa(__sa)
510 : Null_Value(null), sa(__sa)
581 const Arc_Type &
operator () (
const long & i,
const long & j)
const 583 const long mat_index = index_array(i, j, n);
584 if (not arcs.
exist(mat_index))
587 return arcs.
access(mat_index);
602 throw std::out_of_range(
"node index out of range");
604 return const_cast<Node_Type &
>(nodes.
access(i));
625 const long mat_index = index_array(i, j, n);
627 return arcs.
touch(mat_index);
657 template <
class GT,
typename __Entry_Type,
class SA = Dft_Show_Arc<GT> >
677 typedef typename GT::Node
Node;
680 typedef typename GT::Arc
Arc;
687 mutable size_t num_nodes;
688 mutable Entry_Type Null_Value;
690 void test_same_graph(
Ady_Mat & mat)
692 if (lgraph == mat.lgraph)
696 std::domain_error(
"mat does not refers the same GT than this");
699 void copy_nodes(GT & g)
702 num_nodes = g.get_num_nodes();
707 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne(), ++i)
708 nodes[i] = it.get_current_node_ne();
718 test_same_graph(mat);
719 Null_Value = mat.Null_Value;
731 return Aleph::get_node<GT>(nodes, i);
738 return node_index<GT>(nodes, num_nodes, node);
757 return mat.touch(index_array(i, j, num_nodes));
774 const Entry_Type &
operator () (
const long & i,
const long & j)
const 776 const long index = index_array(i, j, num_nodes);
778 if (mat.exist(index))
779 return mat.access(index);
803 return (*
this)(node_index <GT> (nodes, num_nodes, src),
804 node_index <GT> (nodes, num_nodes, tgt));
826 return (*
this)(node_index <GT> (nodes, num_nodes, src),
827 node_index <GT> (nodes, num_nodes, tgt));
876 : lgraph(&g), num_nodes(lgraph->
get_num_nodes()), Null_Value(null)
913 template <
class Operation>
void operate_all_arcs_list_graph();
944 template <
class Operation>
void operate_all_arcs_list_graph(
void * ptr);
965 template <
class Operation>
void operate_all_arcs_matrix();
991 template <
class Operation>
992 void operate_all_arcs_matrix(
void * ptr);
996 template <
class GT,
typename __Entry_Type,
class SA>
997 template <
class Operation>
1000 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
1002 Node * src = nit.get_current_node_ne();
1003 const long src_idx = node_index<Graph_Type>(nodes, num_nodes, src);
1008 Arc * arc = at.get_current_arc_ne();
1009 const long tgt_idx =
1010 node_index<Graph_Type>(nodes, num_nodes, arc->get_tgt_node());
1012 mat.
touch(index_array(src_idx, tgt_idx, num_nodes));
1014 Operation () (*
this, arc, src_idx, tgt_idx, entry);
1019 template <
class GT,
typename __Entry_Type,
class SA>
1020 template <
class Operation>
1023 const long & n = num_nodes;
1024 for (
int s = 0; s < n; ++s)
1026 Node * src_node = get_node<GT>(nodes, s);
1027 for (
int t = 0; t < n; ++t)
1029 Node * tgt_node = get_node<GT>(nodes, t);
1032 Operation () (*
this, src_node, tgt_node, s, t, entry);
1037 template <
class GT,
typename __Entry_Type,
class SA>
1038 template <
class Operation>
1041 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_curr(); nit.next_ne())
1043 Node * src = nit.get_current_node_ne();
1044 const long src_idx = node_index<Graph_Type>(nodes, num_nodes, src);
1049 Arc * arc = at.get_current_arc_ne();
1050 Node * tgt = lgraph->get_tgt_node(arc);
1051 const long tgt_idx = node_index<Graph_Type>(nodes, num_nodes, tgt);
1053 mat.
touch(index_array(src_idx, tgt_idx, num_nodes));
1055 Operation () (*
this, arc, src_idx, tgt_idx, entry, ptr);
1060 template <
class GT,
typename __Entry_Type,
class SA>
1061 template <
class Operation>
1064 const long & n = num_nodes;
1066 for (
int s = 0; s < n; ++s)
1068 Node * src_node = get_node<GT>(nodes, s);
1070 for (
int t = 0; t < n; ++t)
1072 Node * tgt_node = get_node<GT>(nodes, t);
1074 Operation () (*
this, src_node, tgt_node, s, t, entry, ptr);
1098 template <
class GT,
class SA = Dft_Show_Arc<GT> >
1121 n = g.get_num_nodes();
1126 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1127 nodes[i++] = static_cast<typename GT::Node*>(it.get_current_node_ne());
1132 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
1134 typename GT::Node * src = it.get_current_node_ne();
1135 const size_t src_idx = node_index<Graph_Type>(nodes, n, src);
1140 typename GT::Node * tgt = jt.get_tgt_node_ne();
1141 const size_t tgt_idx = node_index<Graph_Type>(nodes, n, tgt);
1142 bit_array[index_array(src_idx, tgt_idx, n)] = 1;
1151 const size_t bit_index;
1153 Proxy(
Bit_Mat_Graph & __bitmat,
const long & i,
const long & j)
1154 : bit_array(__bitmat.bit_array), bit_index(index_array(i, j, __bitmat.n))
1161 bit_array[bit_index] = proxy.bit_array[proxy.bit_index];
1166 bit_array[bit_index] = i;
1171 operator int ()
const 1173 return bit_array[bit_index];
1195 : bit_array(bitmat.bit_array), lgraph(bitmat.lgraph),
1196 nodes(bitmat.nodes), n(bitmat.n)
1203 : bit_array(dim*dim), lgraph(nullptr), nodes(dim), n(dim)
1212 const size_t & n = g.get_num_nodes();
1225 if (
this == &bitmat)
1228 lgraph = bitmat.lgraph;
1229 bit_array = bitmat.bit_array;
1249 if (lgraph ==
nullptr)
1250 throw std::domain_error(
"There is no a GT object");
1252 return Aleph::get_node<GT>(nodes, i);
1258 if (lgraph ==
nullptr)
1259 throw std::domain_error(
"There is no a GT object");
1261 return node_index<GT>(nodes, n, node);
1264 Proxy
operator () (
const long & i,
const long & j)
1266 return Proxy(*
this, i, j);
1269 Proxy
operator () (
const long & i,
const long & j)
const 1271 return Proxy(const_cast<Bit_Mat_Graph&>(*
this), i, j);
1274 Proxy
operator () (Node * src_node, Node * tgt_node)
1276 if (lgraph ==
nullptr)
1277 throw std::domain_error(
"There is no a GT object");
1279 return Proxy(*
this, node_index<GT>(nodes, n, src_node),
1280 node_index<GT>(nodes, n, tgt_node));
1283 Proxy
operator () (Node * src_node, Node * tgt_node)
const 1285 if (lgraph ==
nullptr)
1286 throw std::domain_error(
"There is no a GT object");
1288 return Proxy(*
this, node_index<GT>(nodes, n, src_node),
1289 node_index<GT>(nodes, n, tgt_node));
1295 # endif // TPL_MAT_GRAPH_H Matrix_Graph(Matrix_Graph &mat)
Definition: tpl_matgraph.H:523
const Arc_Type & null_value() const
El valor constante que representa la entrada nula en la matriz.
Definition: tpl_matgraph.H:484
GT Graph_Type
El tipo de grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:663
GT::Arc Arc
El tipo de arco usado en la representación con listas de adyacencia.
Definition: tpl_matgraph.H:680
const size_t & get_num_nodes() const
Definition: tpl_matgraph.H:282
void cut(const size_t new_dim=0)
Definition: tpl_dynArray.H:1104
GT::Node Node
El tipo de nodo que se maneja el objeto GT.
Definition: tpl_matgraph.H:149
GT Graph_Type
El tipo de Graph_List asociado a la matriz.
Definition: tpl_matgraph.H:140
Ady_Mat(GT &g)
Definition: tpl_matgraph.H:855
GT & get_list_graph()
Retorna una referencia al grafo representado con lista de adyacencia.
Definition: tpl_matgraph.H:831
GT * get_list_graph()
Definition: tpl_matgraph.H:1220
Bit_Mat_Graph()
Constructor vacÃo.
Definition: tpl_matgraph.H:1183
Definition: bitArray.H:135
T & access(const size_t i) const noexcept
Definition: tpl_dynArray.H:874
T & touch(const size_t i)
Definition: tpl_dynArray.H:958
GT::Node Node
El tipo de nodo en GT.
Definition: tpl_matgraph.H:1107
Definition: tpl_matgraph.H:136
void operate_all_arcs_matrix()
Definition: tpl_matgraph.H:1021
Definition: tpl_matgraph.H:396
Bit_Mat_Graph(const Bit_Mat_Graph &bitmat)
Constructor copia.
Definition: tpl_matgraph.H:1194
Map_Matrix_Graph & operator=(Map_Matrix_Graph &mat)
Asignación de matriz de adyacencia.
Definition: tpl_matgraph.H:322
GT::Node_Type Node_Type
Definition: tpl_matgraph.H:670
Ady_Mat(GT &g, const Entry_Type &null)
Definition: tpl_matgraph.H:875
Ady_Mat(Ady_Mat &mat)
Constructor copia.
Definition: tpl_matgraph.H:885
const size_t & get_num_nodes() const
Definition: tpl_matgraph.H:838
bool exist(const size_t i) const
Definition: tpl_dynArray.H:895
GT::Node Node
El tipo de nodo usado en la representación con listas de adyacencia.
Definition: tpl_matgraph.H:677
Definition: tpl_matgraph.H:1099
DynList< typename GT::Arc * > arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1893
GT::Node Node
El tipo de Graph_Node que usado en el GT.
Definition: tpl_matgraph.H:412
void operate_all_arcs_list_graph()
Definition: tpl_matgraph.H:998
Definition: tpl_matgraph.H:658
GT::Arc_Type Arc_Type
Definition: tpl_matgraph.H:674
GT Graph_Type
El tipo de grafo GT.
Definition: tpl_matgraph.H:1104
GT::Node_Type Node_Type
Definition: tpl_matgraph.H:405
GT::Arc_Type Arc_Type
Definition: tpl_matgraph.H:409
void set_size(const size_t sz)
Reajusta la dimensión del arreglo.
Definition: bitArray.H:244
__Entry_Type Entry_Type
El tipo de dato que alberga la matriz.
Definition: tpl_matgraph.H:666
const size_t & get_num_nodes() const
Retorna el número de nodos del grafo (dimensión de la matriz).
Definition: tpl_matgraph.H:1180
size_t size() const noexcept
Definition: tpl_dynArray.H:641
Bit_Mat_Graph(const size_t &dim)
Constructor especificando una dimensión.
Definition: tpl_matgraph.H:1202
Bit_Mat_Graph(GT &g)
Definition: tpl_matgraph.H:1187
Node * operator()(const long &i)
Definition: tpl_matgraph.H:287
Map_Matrix_Graph(GT &g, SA &&__sa=SA())
Constructor a partir de un grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:176
Definition: tpl_dynArray.H:159
Matrix_Graph(GT &g, const Arc_Type &null, SA &&__sa=SA())
Definition: tpl_matgraph.H:503
void set_default_initial_value(const T &value) noexcept
Definition: tpl_dynArray.H:659
GT Graph_Type
El tipo de grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:401
GT::Arc_Type Arc_Type
El tipo atributo de arco que guarda los nodos en el objeto GT.
Definition: tpl_matgraph.H:146
void copy_list_graph(GT &g)
Definition: tpl_matgraph.H:338
Definition: tpl_graph.H:1177
GT::Arc Arc
El tipo de arco en GT.
Definition: tpl_matgraph.H:1110
const size_t & get_num_nodes() const
Retorna el número de nodos del grafo (que es la dimensión de la matriz)
Definition: tpl_matgraph.H:481
void set_null_value(const Entry_Type &null)
Declara un valor nulo para la matriz de adyacencia auxiliar.
Definition: tpl_matgraph.H:882
Map_Matrix_Graph(Map_Matrix_Graph &mat)
Constructor copia.
Definition: tpl_matgraph.H:189
void set_list_graph(GT &g)
Definition: tpl_matgraph.H:1210
GT::Arc Arc
El tipo de Graph_Arc que usado en el GT.
Definition: tpl_matgraph.H:415
const Entry_Type & null_value() const
Retorna el valor considerado como nulo.
Definition: tpl_matgraph.H:834
GT::Node_Type Node_Type
El tipo atributo de nodo que guarda los nodos en el objeto GT.
Definition: tpl_matgraph.H:143
GT::Arc Arc
El tipo de arco que se maneja el objeto GT.
Definition: tpl_matgraph.H:152
GT & get_list_graph()
Retorna una referencia al grafo representado con listas enlazadas.
Definition: tpl_matgraph.H:278