1 # ifndef TPL_MAT_GRAPH_H
2 # define TPL_MAT_GRAPH_H
4 # include <tpl_graph.H>
5 # include <tpl_sort_utils.H>
12 template <
typename GT>
inline static
16 if (i >= nodes.
size())
17 throw std::out_of_range(
"Index of node out of range");
22 template <
typename GT>
inline static long
24 const long & n,
typename GT::Node * p)
26 return Aleph::binary_search<typename GT::Node*>(nodes, p, 0, n - 1);
30 index_array(
const long & i,
const long & j,
const long & n)
33 throw std::out_of_range(
"Matrix index out of range");
38 template <
typename Entry>
40 const long & i,
const long & j,
43 const long index = index_array(i, j, n);
44 if (not mat.
exist(index))
47 return &
const_cast<Entry&
>(mat.
access(index));
50 template <
typename Entry>
57 mat[index_array(i, j, n)] = entry;
109 template <
class GT,
class SA = Dft_Show_Arc<GT> >
123 typedef typename GT::Node
Node;
126 typedef typename GT::Arc
Arc;
132 mutable size_t num_nodes;
138 Mat_Entry(
Arc * __arc = NULL) : arc(__arc) { }
260 template <
class GT,
class SA>
typename GT::Node *
263 return get_node <GT> (nodes, i);
266 template <
class GT,
class SA>
long
269 return node_index <GT> (nodes, num_nodes, node);
272 template <
class GT,
class SA>
typename GT::Arc *
275 Mat_Entry * mat_entry = read_matrix<Mat_Entry>(mat, i, j, num_nodes);
276 if (mat_entry == NULL)
279 return mat_entry->arc;
282 template <
class GT,
class SA>
typename GT::Arc *
285 Mat_Entry * mat_entry =
286 read_matrix<Mat_Entry>(mat, node_index<GT>(nodes, num_nodes, src_node),
287 node_index<GT>(nodes, num_nodes, tgt_node),
289 if (mat_entry == NULL)
292 return mat_entry->arc;
301 copy_list_graph(*(mat.lgraph));
305 template <
class GT,
class SA>
311 template <
class GT,
class SA>
316 num_nodes = g.get_num_nodes();
320 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next(), ++i)
321 nodes[i] = it.get_current_node();
324 for (
typename GT::Node_Iterator nit(g); nit.has_current(); nit.next())
326 Node * src = nit.get_current_node();
327 const long src_idx = node_index<GT>(nodes, num_nodes, src);
332 Arc * arc = ait.get_current_arc();
333 Node * tgt = g.get_connected_node(arc, src);
334 const long tgt_idx = node_index<GT>(nodes, num_nodes, tgt);
335 write_matrix<Mat_Entry>(mat, src_idx, tgt_idx, num_nodes, arc);
369 template <
typename GT,
class SA = Dft_Show_Arc<GT> >
386 typedef typename GT::Node
Node;
389 typedef typename GT::Arc
Arc;
405 Null_Value = mat.Null_value;
411 for (
int i = 0; i < n; ++i)
413 nodes[i] = mat.nodes[i];
414 for (
int j = 0; j < n; ++j)
416 const long mat_index = index_array(i, j, n);
417 if (not arcs.
exist(mat_index))
419 arcs.
touch(mat_index) = mat.arcs.
access(mat_index);
426 n = g.get_num_nodes();
430 for (
typename GT::Node_Iterator it(g); it.has_current(); it.next(), ++i)
431 ptr[i] = it.get_current_node();
437 for (i = 0; i < n; ++i)
439 typename GT::Node * src = ptr[i];
440 nodes[i] = src->get_info();
443 typename GT::Arc * arc = it.get_current_arc();
444 typename GT::Node * tgt = it.get_tgt_node();
445 const long j = node_index<GT>(ptr, n, tgt);
446 const long mat_index = index_array(i, j, n);
447 arcs[mat_index] = arc->get_info();
478 : Null_Value(null), sa(__sa)
484 : Null_Value(null), sa(__sa)
557 const long mat_index = index_array(i, j, n);
558 if (not arcs.
exist(mat_index))
561 return arcs.
access(mat_index);
576 throw std::out_of_range(
"node index out of range");
599 const long mat_index = index_array(i, j, n);
601 return arcs.
touch(mat_index);
631 template <
class GT,
typename __Entry_Type,
class SA = Dft_Show_Arc<GT> >
651 typedef typename GT::Node
Node;
654 typedef typename GT::Arc
Arc;
661 mutable size_t num_nodes;
664 void test_same_graph(
Ady_Mat & mat)
666 if (lgraph == mat.lgraph)
670 std::domain_error(
"mat does not refers the same GT than this");
673 void copy_nodes(GT & g)
676 num_nodes = g.get_num_nodes();
681 for (
typename GT::Node_Iterator it(g); it.has_current(); it.next(), ++i)
682 nodes[i] = it.get_current_node();
692 test_same_graph(mat);
693 Null_Value = mat.Null_Value;
705 return Aleph::get_node<GT>(nodes, i);
712 return node_index<GT>(nodes, num_nodes, node);
731 return mat.touch(index_array(i, j, num_nodes));
750 const long index = index_array(i, j, num_nodes);
752 if (mat.exist(index))
753 return mat.access(index);
777 return (*
this)(node_index <GT> (nodes, num_nodes, src),
778 node_index <GT> (nodes, num_nodes, tgt));
800 return (*
this)(node_index <GT> (nodes, num_nodes, src),
801 node_index <GT> (nodes, num_nodes, tgt));
849 : lgraph(&g), num_nodes(lgraph->
get_num_nodes()), Null_Value(null)
964 template <
class Operation>
969 template <
class GT,
typename __Entry_Type,
class SA>
970 template <
class Operation>
973 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_current(); nit.next())
975 Node * src = nit.get_current_node();
976 const long src_idx = node_index<Graph_Type>(nodes, num_nodes, src);
981 Arc * arc = at.get_current_arc();
983 node_index<Graph_Type>(nodes, num_nodes, arc->get_tgt_node());
985 mat.
touch(index_array(src_idx, tgt_idx, num_nodes));
987 Operation () (*
this, arc, src_idx, tgt_idx, entry);
992 template <
class GT,
typename __Entry_Type,
class SA>
993 template <
class Operation>
996 const long & n = num_nodes;
997 for (
int s = 0; s < n; ++s)
999 Node * src_node = get_node<GT>(nodes, s);
1000 for (
int t = 0; t < n; ++t)
1002 Node * tgt_node = get_node<GT>(nodes, t);
1005 Operation () (*
this, src_node, tgt_node, s, t, entry);
1010 template <
class GT,
typename __Entry_Type,
class SA>
1011 template <
class Operation>
1014 for (
typename GT::Node_Iterator nit(*lgraph); nit.has_current(); nit.next())
1016 Node * src = nit.get_current_node();
1017 const long src_idx = node_index<Graph_Type>(nodes, num_nodes, src);
1022 Arc * arc = at.get_current_arc();
1023 Node * tgt = lgraph->get_tgt_node(arc);
1024 const long tgt_idx = node_index<Graph_Type>(nodes, num_nodes, tgt);
1026 mat.
touch(index_array(src_idx, tgt_idx, num_nodes));
1028 Operation () (*
this, arc, src_idx, tgt_idx, entry, ptr);
1033 template <
class GT,
typename __Entry_Type,
class SA>
1034 template <
class Operation>
1037 const long & n = num_nodes;
1039 for (
int s = 0; s < n; ++s)
1041 Node * src_node = get_node<GT>(nodes, s);
1043 for (
int t = 0; t < n; ++t)
1045 Node * tgt_node = get_node<GT>(nodes, t);
1047 Operation () (*
this, src_node, tgt_node, s, t, entry, ptr);
1071 template <
class GT,
class SA = Dft_Show_Arc<GT> >
1092 void copy_list_graph(GT & g)
1094 n = g.get_num_nodes();
1099 for (
typename GT::Node_Iterator it(g); it.has_current(); it.next())
1100 nodes[i++] = static_cast<typename GT::Node*>(it.get_current_node());
1105 for (
typename GT::Node_Iterator it(g); it.has_current(); it.next())
1107 typename GT::Node * src = it.get_current_node();
1108 const size_t src_idx = node_index<Graph_Type>(nodes, n, src);
1113 typename GT::Node * tgt = jt.get_tgt_node();
1114 const size_t tgt_idx = node_index<Graph_Type>(nodes, n, tgt);
1115 bit_array[index_array(src_idx, tgt_idx, n)] = 1;
1124 const size_t bit_index;
1126 Proxy(
Bit_Mat_Graph & __bitmat,
const long & i,
const long & j)
1127 : bit_array(__bitmat.bit_array), bit_index(index_array(i, j, __bitmat.n))
1134 bit_array[bit_index] = proxy.bit_array[proxy.bit_index];
1139 bit_array[bit_index] = i;
1144 operator int ()
const
1146 return bit_array[bit_index];
1168 : bit_array(bitmat.bit_array), lgraph(bitmat.lgraph),
1169 nodes(bitmat.nodes), n(bitmat.n)
1176 : bit_array(dim*dim), lgraph(NULL), nodes(dim), n(dim)
1185 const size_t & n = g.get_num_nodes();
1198 if (
this == &bitmat)
1201 lgraph = bitmat.lgraph;
1202 bit_array = bitmat.bit_array;
1223 throw std::domain_error(
"There is no a GT object");
1225 return Aleph::get_node<GT>(nodes, i);
1232 throw std::domain_error(
"There is no a GT object");
1234 return node_index<GT>(nodes, n, node);
1237 Proxy
operator () (
const long & i,
const long & j)
1239 return Proxy(*
this, i, j);
1242 Proxy
operator () (
const long & i,
const long & j)
const
1244 return Proxy(const_cast<Bit_Mat_Graph&>(*
this), i, j);
1250 throw std::domain_error(
"There is no a GT object");
1252 return Proxy(*
this, node_index<GT>(nodes, n, src_node),
1253 node_index<GT>(nodes, n, tgt_node));
1259 throw std::domain_error(
"There is no a GT object");
1261 return Proxy(*
this, node_index<GT>(nodes, n, src_node),
1262 node_index<GT>(nodes, n, tgt_node));
1268 # endif // TPL_MAT_GRAPH_H
Matrix_Graph(Matrix_Graph &mat)
Definition: tpl_matgraph.H:497
Node * operator()(const long &i)
Definition: tpl_matgraph.H:1220
GT Graph_Type
El tipo de grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:637
GT::Arc Arc
El tipo de arco usado en la representación con listas de adyacencia.
Definition: tpl_matgraph.H:654
void cut(const size_t new_dim=0)
Definition: tpl_dynArray.H:1040
GT::Node Node
El tipo de nodo que se maneja el objeto GT.
Definition: tpl_matgraph.H:123
GT Graph_Type
El tipo de Graph_List asociado a la matriz.
Definition: tpl_matgraph.H:114
Ady_Mat(GT &g)
Definition: tpl_matgraph.H:829
GT & get_list_graph()
Retorna una referencia al grafo representado con lista de adyacencia.
Definition: tpl_matgraph.H:805
const size_t & get_num_nodes() const
Definition: tpl_matgraph.H:256
GT * get_list_graph()
Definition: tpl_matgraph.H:1193
Bit_Mat_Graph()
Constructor vacío.
Definition: tpl_matgraph.H:1156
size_t size() const
Retorna dimensión actual (más lejano índice escrito)
Definition: tpl_dynArray.H:523
Definition: bitArray.H:96
void quicksort(T *a, const int l, const int r, Compare &cmp)
Definition: tpl_sort_utils.H:1213
T & touch(const size_t i)
Definition: tpl_dynArray.H:915
GT::Node Node
El tipo de nodo en GT.
Definition: tpl_matgraph.H:1080
Definition: tpl_matgraph.H:110
const size_t & get_num_nodes() const
Definition: tpl_matgraph.H:812
void operate_all_arcs_matrix()
Definition: tpl_matgraph.H:994
Definition: tpl_matgraph.H:370
Bit_Mat_Graph(const Bit_Mat_Graph &bitmat)
Constructor copia.
Definition: tpl_matgraph.H:1167
Map_Matrix_Graph & operator=(Map_Matrix_Graph &mat)
Asignación de matriz de adyacencia.
Definition: tpl_matgraph.H:296
Matrix_Graph & operator=(Matrix_Graph &mat)
Definition: tpl_matgraph.H:514
void set_default_initial_value(const T &value)
Definition: tpl_dynArray.H:541
GT::Node_Type Node_Type
Definition: tpl_matgraph.H:644
Ady_Mat(GT &g, const Entry_Type &null)
Definition: tpl_matgraph.H:848
Ady_Mat(Ady_Mat &mat)
Constructor copia.
Definition: tpl_matgraph.H:858
const size_t & get_num_nodes() const
Retorna el número de nodos del grafo (dimensión de la matriz).
Definition: tpl_matgraph.H:1153
bool exist(const size_t i) const
Definition: tpl_dynArray.H:854
Node * operator()(const long &i)
Definition: tpl_matgraph.H:703
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:455
GT::Node Node
El tipo de nodo usado en la representación con listas de adyacencia.
Definition: tpl_matgraph.H:651
Definition: tpl_graph.H:1186
Definition: tpl_matgraph.H:1072
GT::Node Node
El tipo de Graph_Node que usado en el GT.
Definition: tpl_matgraph.H:386
void operate_all_arcs_list_graph()
Definition: tpl_matgraph.H:971
Definition: tpl_matgraph.H:632
GT::Arc_Type Arc_Type
Definition: tpl_matgraph.H:648
GT Graph_Type
El tipo de grafo GT.
Definition: tpl_matgraph.H:1077
GT::Node_Type Node_Type
Definition: tpl_matgraph.H:379
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
const Arc_Type & null_value() const
El valor constante que representa la entrada nula en la matriz.
Definition: tpl_matgraph.H:458
GT::Arc_Type Arc_Type
Definition: tpl_matgraph.H:383
void set_size(const size_t sz)
Reajusta la dimensión del arreglo.
Definition: bitArray.H:203
__Entry_Type Entry_Type
El tipo de dato que alberga la matriz.
Definition: tpl_matgraph.H:640
Bit_Mat_Graph(const size_t &dim)
Constructor especificando una dimensión.
Definition: tpl_matgraph.H:1175
Bit_Mat_Graph(GT &g)
Definition: tpl_matgraph.H:1160
Node * operator()(const long &i)
Definition: tpl_matgraph.H:261
Map_Matrix_Graph(GT &g, SA &&__sa=SA())
Constructor a partir de un grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:150
const Entry_Type & null_value() const
Retorna el valor considerado como nulo.
Definition: tpl_matgraph.H:808
const Arc_Type & operator()(const long &i, const long &j) const
Definition: tpl_matgraph.H:555
Bit_Mat_Graph & operator=(const Bit_Mat_Graph &bitmat)
Asignación de matriz.
Definition: tpl_matgraph.H:1196
Matrix_Graph(GT &g, const Arc_Type &null, SA &&__sa=SA())
Definition: tpl_matgraph.H:477
GT Graph_Type
El tipo de grafo representado con listas de adyacencia.
Definition: tpl_matgraph.H:375
T & access(const size_t i)
Definition: tpl_dynArray.H:793
GT::Arc_Type Arc_Type
El tipo atributo de arco que guarda los nodos en el objeto GT.
Definition: tpl_matgraph.H:120
void copy_list_graph(GT &g)
Definition: tpl_matgraph.H:312
GT::Arc Arc
El tipo de arco en GT.
Definition: tpl_matgraph.H:1083
void set_null_value(const Entry_Type &null)
Declara un valor nulo para la matriz de adyacencia auxiliar.
Definition: tpl_matgraph.H:855
Map_Matrix_Graph(Map_Matrix_Graph &mat)
Constructor copia.
Definition: tpl_matgraph.H:163
void set_list_graph(GT &g)
Definition: tpl_matgraph.H:1183
GT::Arc Arc
El tipo de Graph_Arc que usado en el GT.
Definition: tpl_matgraph.H:389
GT::Node_Type Node_Type
El tipo atributo de nodo que guarda los nodos en el objeto GT.
Definition: tpl_matgraph.H:117
GT::Arc Arc
El tipo de arco que se maneja el objeto GT.
Definition: tpl_matgraph.H:126
GT & get_list_graph()
Retorna una referencia al grafo representado con listas enlazadas.
Definition: tpl_matgraph.H:252