Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
tpl_graph.H
1 # ifndef TPL_GRAPH_H
2 # define TPL_GRAPH_H
3 
4 # include <memory>
5 # include <bitArray.H>
6 # include <tpl_dynArray.H>
7 # include <tpl_sort_utils.H>
8 # include <tpl_dynMapTree.H>
9 # include <tpl_dynDlist.H>
10 # include <tpl_treapRk.H>
11 # include <aleph-graph.H>
12 # include <filter_iterator.H>
13 
14 
15 using namespace Aleph;
16 
17 namespace Aleph {
18 
19 template <typename Node_Info> class Graph_Node;
20 
21 template <typename Arc_Info> class Graph_Arc;
22 
23 class Arc_Node;
24 
25  template <class GT>
26 class Path;
27 
28  template <typename __Graph_Node, typename __Graph_Arc>
29 class List_Graph;
30 
31  template <typename __Graph_Node, typename __Graph_Arc>
33 
34  template <class GT>
35 class Mat_Graph;
36 
37  template <typename MT, typename Entry_Info, typename Copy>
38 class Ady_MaT;
39 
40 
77  template <typename __Node_Info = Empty_Class>
78 class Graph_Node : public Dlink
79 {
80  friend class Arc_Node;
81 
82 public:
83 
84  typedef __Node_Info Node_Info;
85 
86  GRAPH_NODE_COMMON(Graph_Node);
87 
97  Graph_Node() : num_arcs(0) { /* empty */ }
98 
99  Graph_Node(const Graph_Node &) : num_arcs(0) { /* empty */ }
100 
114  Graph_Node(const Node_Info & info) : node_info(info), num_arcs(0)
115  {
116  /* empty */
117  }
118 
134  Graph_Node(Graph_Node * node) : node_info(node->get_info()), num_arcs(0)
135  {
136  /* empty */
137  }
138 
139  Dlink arc_list;
140 };
141 
178  template <typename __Arc_Info = Empty_Class>
179 class Graph_Arc : public Dlink
180 {
181 public:
182 
183  typedef __Arc_Info Arc_Info;
184 
185  GRAPH_ARC_COMMON(Graph_Arc);
186 
187  Arc_Node * src_arc_node; // puntero al Arc_Node del nodo fuente
188  Arc_Node * tgt_arc_node; // puntero al Arc_Node del nodo destino
189 
190  Graph_Arc()
191  : src_node(NULL), tgt_node(NULL), src_arc_node(NULL), tgt_arc_node(NULL)
192  {
193  /* empty */
194  }
195 
196  Graph_Arc(const Arc_Info & info)
197  : arc_info(info),
198  src_node(NULL), tgt_node(NULL), src_arc_node(NULL), tgt_arc_node(NULL)
199  {
200  /* empty */
201  }
202 
203  Graph_Arc(void * src, void * tgt, const Arc_Info & data)
204  : arc_info(data),
205  src_node(src), tgt_node(tgt), src_arc_node(NULL), tgt_arc_node(NULL)
206  {
207  // empty
208  }
209 
210  Graph_Arc(void * src, void * tgt)
211  : src_node(src), tgt_node(tgt), src_arc_node(NULL), tgt_arc_node(NULL)
212  {
213  // empty
214  }
215 };
216 
217 class Arc_Node : public Dlink
218 {
219 public:
220  void * arc;
221  Arc_Node() : arc(NULL) {}
222  Arc_Node(void * __arc) : arc(__arc) {}
223 };
224 
253  template <typename __Graph_Node = Graph_Node<int>,
254  typename __Graph_Arc = Graph_Arc<int>>
255 class List_Graph
256 {
257  GRAPH_ATTR_COMMON(List_Graph);
258 
259  Dlink node_list; // lista de nodos
260  Dlink arc_list; // lista de arcos
261 
262  static Node * dlink_to_node(Dlink * p) { return (Node*) p; }
263 
264  static Arc * dlink_to_arc(Dlink * p) { return (Arc*) p; }
265 
266  static Arc_Node * dlink_to_arc_node(Dlink * p) { return (Arc_Node*) p; }
267 
268  static Arc * void_to_arc(Arc_Node * arc_node)
269  {
270  return (Arc*) arc_node->arc;
271  }
272 
273 private:
274 
275  template <class Cmp>
276  struct Cmp_Arc
277  {
278  Cmp & cmp;
279 
280  Cmp_Arc(Cmp && __cmp = Cmp()) : cmp(__cmp) { /* empty */ }
281 
282  Cmp_Arc(Cmp & __cmp) : cmp(__cmp) { /* empty */ }
283 
284  bool operator () (Dlink * d1, Dlink * d2) const
285  {
286  Arc * arc1 = dlink_to_arc(d1); // convertir dlink d1 de arco a Arc
287  Arc * arc2 = dlink_to_arc(d2); // convertir dlink d2 de arco a Arc
288  return cmp(arc1, arc2); // al tener dos arcos invocamos a Cmp
289  }
290  };
291 
292 public:
293 
310  inline virtual Node * insert_node(Node * node);
311 
324  inline virtual Node * insert_node(const Node_Type & node_info);
325 
326 
328  inline virtual Node * insert_node(Node_Type && node_info = Node_Type());
329 
343  inline virtual void remove_node(Node * node);
344 
354  inline Node * get_first_node() const;
355 
356  inline Arc * get_first_arc(Node *) const;
357 
380  inline virtual Arc *
381  insert_arc(Node * src_node, Node * tgt_node,
382  const typename Arc::Arc_Type & arc_info);
383 
384  inline virtual Arc *
385  insert_arc(Node * src_node, Node * tgt_node,
386  typename Arc::Arc_Type && arc_info);
387 
388  inline virtual Arc * insert_arc(Node * src_node, Node * tgt_node);
389 
400  inline virtual void remove_arc(Arc * arc);
401 
413  inline virtual void disconnect_arc(Arc * arc);
414 
432  inline virtual Arc * connect_arc(Arc * arc);
433 
449  inline Arc * get_first_arc() const;
450 
457  template <class Compare> inline
458  void sort_arcs(Compare && cmp = Compare());
459 
460  template <class Compare> inline
461  void sort_arcs(Compare & cmp);
462 
463  GRAPH_SEARCH_METHODS;
464 
475  inline List_Graph(const List_Graph & g);
476 
477  inline List_Graph(List_Graph && g);
478 
490  inline List_Graph & operator = (const List_Graph & g);
491 
492  inline List_Graph & operator = (List_Graph && g);
493 
496  inline virtual ~List_Graph();
497 
505  {
506  public:
507 
509  typedef Node * Item_Type;
510 
513 
514  Node_Iterator() { /* empty */ }
515 
516  inline Node_Iterator(List_Graph & _g);
517 
519  inline Node * get_current_node();
520 
522  Node * get_current() { return get_current_node(); }
523 
525  Node * get_curr() { return get_current_node(); }
526  };
527 
536  {
537  Node * src_node;
538 
539  public:
540 
542  typedef Arc * Item_Type;
543 
545  typedef Node * Set_Type;
546 
548  Node_Arc_Iterator() { /* empty */ }
549 
551  Node_Arc_Iterator(Node * src)
552  : Dlink::Iterator(&(src->arc_list)), src_node(src)
553  {
554  // empty
555  }
556 
558  inline Arc * get_current_arc() const;
559 
561  Arc * get_current() const { return get_current_arc(); }
562 
564  Arc * get_curr() const { return get_current_arc(); }
565 
567  inline Node * get_tgt_node() const;
568 
569  Arc_Node * get_current_arc_node() const
570  {
571  return dlink_to_arc_node(Dlink::Iterator::get_current());
572  }
573  };
574 
583  {
584  public:
585 
587  typedef Arc * Item_Type;
588 
591 
592  Arc_Iterator() { /* empty */ }
593 
594  inline Arc_Iterator(List_Graph & _g);
595 
597  Arc * get_current_arc() const ;
598 
600  Arc * get_current() const { return get_current_arc(); }
601 
603  Arc * get_curr() const { return get_current_arc(); }
604 
606  Node * get_src_node() const { return (Node*) get_current_arc()->src_node; }
607 
609  Node * get_tgt_node() const { return (Node*) get_current_arc()->tgt_node; }
610  };
611 
612  GRAPH_ITERATIVE_METHODS;
613 
614  List_Graph() : cookie(NULL), num_nodes(0), num_arcs(0), digraph(false)
615  {
616  // empty
617  }
618 
619  void swap(List_Graph & g)
620  {
621  common_swap(g);
622  node_list.swap(&g.node_list);
623  arc_list.swap(&g.arc_list);
624  }
625 
626  GRAPH_FUNCTIONAL_METHODS(List_Graph);
627 };
628 
629 
633  template <class GT>
635 {
636  bool operator () (typename GT::Arc * /* arc */) const
637  {
638  return true;
639  }
640 };
641 
642 
643  template <class GT, class SA1, class SA2>
645 {
646  SA1 & sa1;
647  SA2 & sa2;
648  typename GT::Node * s;
649 
650 public:
651 
652  Double_SA(SA1 && __sa1 = SA1(), SA2 && __sa2 = SA2())
653  : sa1(__sa1) , sa2(__sa2)
654  {
655  /* empty */
656  }
657 
658  Double_SA(SA1 & __sa1, SA2 & __sa2)
659  : sa1(__sa1) , sa2(__sa2)
660  {
661  /* empty */
662  }
663 
664  Double_SA(SA1 & __sa1, SA2 && __sa2 = SA2())
665  : sa1(__sa1) , sa2(__sa2)
666  {
667  /* empty */
668  }
669 
670  Double_SA(SA1 && __sa1, SA2 & __sa2)
671  : sa1(__sa1) , sa2(__sa2)
672  {
673  /* empty */
674  }
675 
676  bool operator () (typename GT::Arc * a)
677  {
678  return sa1(a) and sa2(a);
679  }
680 };
681 
693  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
695  public Filter_Iterator<typename GT::Node*,
696  typename GT::Node_Arc_Iterator,
697  Show_Arc>
698 {
699  typedef Filter_Iterator<typename GT::Node*,
700  typename GT::Node_Arc_Iterator,
701  Show_Arc> Filter_Itor;
702 public:
703 
704  typedef Filter_Iterator
705  <typename GT::Node*, typename GT::Node_Arc_Iterator, Show_Arc> Itor;
706 
708  typedef typename Itor::Item_Type Item_Type;
709 
711  typedef typename Itor::Set_Type Set_Type;
712 
713  Node_Arc_Iterator() { /* empty */ }
714 
720  Node_Arc_Iterator(typename GT::Node * p, Show_Arc && sa = Show_Arc())
721  : Itor(p, std::move(sa))
722  {
723  // empty
724  }
725 
731  Node_Arc_Iterator(typename GT::Node * p, Show_Arc & sa)
732  : Itor(p, sa)
733  {
734  // empty
735  }
736 };
737 
738 
750  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
751 class Arc_Iterator :
752  public Filter_Iterator<GT, typename GT::Arc_Iterator, Show_Arc>
753 {
754 public:
755 
757 
759  typedef typename Itor::Item_Type Item_Type;
760 
762  typedef typename Itor::Set_Type Set_Type;
763 
764  Arc_Iterator() { /* empty */ }
765 
771  Arc_Iterator(GT & g, Show_Arc && sa = Show_Arc())
772  : Itor(g, std::move(sa))
773  {
774  // empty
775  }
776 
782  Arc_Iterator(GT & g, Show_Arc & sa)
783  : Itor(g, sa)
784  {
785  // empty
786  }
787 };
788 
789 
793  template <class GT>
795 {
796  bool operator () (typename GT::Node *) const
797  {
798  return true;
799  }
800 };
801 
813  template <class GT, class Show_Node = Dft_Show_Node<GT>>
815  public Filter_Iterator<GT, typename GT::Node_Iterator, Show_Node>
816 {
817 public:
818 
820 
822  typedef typename Itor::Item_Type Item_Type;
823 
825  typedef typename Itor::Set_Type Set_Type;
826 
827  Node_Iterator() { /* empty */ }
828 
834  Node_Iterator(GT & g, Show_Node && sn = Show_Node())
835  : Itor (g, std::move(sn))
836  {
837  /* empty */
838  }
839 
845  Node_Iterator(GT & g, Show_Node & sn)
846  : Itor (g, sn)
847  {
848  /* empty */
849  }
850 };
851 
852 template <class GT, class SN = Dft_Show_Node<GT>>
853 void for_each_node(GT & g,
854  std::function<void(typename GT::Node*)> operation,
855  SN & sn)
856 {
857  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next())
858  operation(it.get_curr());
859 }
860 
861 template <class GT, class SN = Dft_Show_Node<GT>>
862 void for_each_node(GT & g,
863  std::function<void(typename GT::Node*)> operation,
864  SN && sn = SN())
865 {
866  for_each_node<GT, SN>(g, operation, sn);
867 }
868 
869 template <class GT, class SA = Dft_Show_Arc<GT>>
870 void for_each_arc(GT & g,
871  std::function<void(typename GT::Arc*)> operation,
872  SA & sa)
873 {
874  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next())
875  operation(it.get_curr());
876 }
877 
878 template <class GT, class SA = Dft_Show_Arc<GT>>
879 void for_each_arc(GT & g,
880  std::function<void(typename GT::Arc*)> operation,
881  SA && sa = SA())
882 {
883  for_each_arc<GT, SA>(g, operation, sa);
884 }
885 
886 template <class GT, class SA = Dft_Show_Arc<GT>>
887 void for_each_arc(GT&,
888  typename GT::Node * p,
889  std::function<void(typename GT::Arc*)> operation,
890  SA & sa)
891 {
892  for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next())
893  operation(it.get_curr());
894 }
895 
896 template <class GT, class SA = Dft_Show_Arc<GT>>
897 void for_each_arc(GT& g,
898  typename GT::Node * p,
899  std::function<void(typename GT::Arc*)> operation,
900  SA && sa = SA())
901 {
902  for_each_arc<GT, SA>(g, p, operation, sa);
903 }
904 
905 template <class GT, class SN = Dft_Show_Node<GT>>
906 bool forall_node(GT & g,
907  std::function<bool(typename GT::Node*)> operation,
908  SN & sn)
909 {
910  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next())
911  if (not operation(it.get_curr()))
912  return false;
913  return true;
914 }
915 
916 template <class GT, class SN = Dft_Show_Node<GT>>
917 bool forall_node(GT & g,
918  std::function<bool(typename GT::Node*)> operation,
919  SN && sn = SN())
920 {
921  return forall_node<GT, SN>(g, operation, sn);
922 }
923 
924 template <class GT, class SA = Dft_Show_Arc<GT>>
925 bool forall_arc(GT & g,
926  std::function<bool(typename GT::Arc*)> operation,
927  SA & sa)
928 {
929  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next())
930  if (not operation(it.get_curr()))
931  return false;
932  return true;
933 }
934 
935 template <class GT, class SA = Dft_Show_Arc<GT>>
936 bool forall_arc(GT & g,
937  std::function<bool(typename GT::Arc*)> operation,
938  SA && sa = SA())
939 {
940  return forall_arc<GT, SA>(g, operation, sa);
941 }
942 
943 template <class GT, class SA = Dft_Show_Arc<GT>>
944 bool forall_arc(typename GT::Node * p,
945  std::function<bool(typename GT::Arc*)> operation,
946  SA & sa)
947 {
948  for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next())
949  if (not operation(it.get_curr()))
950  return false;
951  return true;
952 }
953 
954 template <class GT, class SA = Dft_Show_Arc<GT>>
955 bool forall_arc(typename GT::Node * p,
956  std::function<bool(typename GT::Arc*)> operation,
957  SA && sa = SA())
958 {
959  return forall_arc<GT, SA>(p, operation, sa);
960 }
961 
962 template <class GT,
963  class SN = Dft_Show_Node<GT>,
964  template <typename> class Container = DynDlist,
965  typename T = typename GT::Node_Type>
966 Container<T> map_nodes(GT & g,
967  std::function<T(typename GT::Node *)> operation,
968  SN & sn)
969 {
970  Container<T> ret_val;
971  for_each_node<GT, SN>(g, [&ret_val, &operation] (typename GT::Node * p)
972  {
973  ret_val.append(operation(p));
974  }, sn);
975  return ret_val;
976 }
977 
978 template <class GT,
979  typename T = typename GT::Node_Type,
980  template <typename> class Container = DynDlist,
981  class SN = Dft_Show_Node<GT>>
982 Container<T> map_nodes(GT & g,
983  std::function<T(typename GT::Node *)> operation,
984  SN && sn = SN())
985 {
986  return map_nodes<GT, SN, Container, T>(g, operation, sn);
987 }
988 
989 template <class GT,
990  class SA = Dft_Show_Arc<GT>,
991  template <typename> class Container = DynDlist,
992  typename T = typename GT::Arc_Type>
993 Container<T> map_arcs(GT & g,
994  std::function<T(typename GT::Arc *)> operation,
995  SA & sa)
996 {
997  Container<T> ret_val;
998  for_each_arc<GT, SA>(g, [&ret_val, &operation] (typename GT::Arc * p)
999  {
1000  ret_val.append(operation(p));
1001  }, sa);
1002  return ret_val;
1003 }
1004 
1005 template <class GT,
1006  typename T = typename GT::Arc_Type,
1007  template <typename> class Container = DynDlist,
1008  class SA = Dft_Show_Arc<GT>>
1009 Container<T> map_arcs(GT & g,
1010  std::function<T(typename GT::Arc *)> operation,
1011  SA && sa = SA())
1012 {
1013  return map_arcs<GT, SA, Container, T>(g, operation, sa);
1014 }
1015 
1016 template <class GT,
1017  class SA = Dft_Show_Arc<GT>,
1018  template <typename> class Container = DynDlist,
1019  typename T = typename GT::Arc_Type>
1020 Container<T> map_node_arcs(GT & g,
1021  typename GT::Node * p,
1022  std::function<T(typename GT::Arc *)> operation,
1023  SA & sa)
1024 {
1025  Container<T> ret_val;
1026  for_each_arc<GT, SA>(g, p, [&ret_val, &operation] (typename GT::Arc * p)
1027  {
1028  ret_val.append(operation(p));
1029  }, sa);
1030  return ret_val;
1031 }
1032 
1033 template <class GT,
1034  typename T = typename GT::Arc_Type,
1035  template <typename> class Container = DynDlist,
1036  class SA = Dft_Show_Arc<GT>>
1037 Container<T> map_node_arcs(GT & g,
1038  typename GT::Node * p,
1039  std::function<T(typename GT::Arc *)> operation,
1040  SA && sa = SA())
1041 {
1042  return map_node_arcs<GT, SA, Container, T>(g, p, operation, sa);
1043 }
1044 
1045 template <class GT, typename T, class SN = Dft_Show_Node<GT>>
1046 T foldl_nodes(GT & g, const T & init,
1047  std::function<T(const T&, typename GT::Node*)> operation,
1048  SN & sn)
1049 {
1050  T ret_val = init;
1051  for_each_node<GT, SN>(g, [&ret_val, &operation] (typename GT::Node * p)
1052  {
1053  ret_val = operation(ret_val, p);
1054  }, sn);
1055  return ret_val;
1056 }
1057 
1058 template <class GT, typename T, class SN = Dft_Show_Node<GT>>
1059 T foldl_nodes(GT & g, const T & init,
1060  std::function<T(const T&, typename GT::Node*)> operation,
1061  SN && sn = SN())
1062 {
1063  return foldl_nodes<GT, T, SN>(g, init, operation, sn);
1064 }
1065 
1066 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1067 T foldl_arcs(GT & g, const T & init,
1068  std::function<T(const T&, typename GT::Arc*)> operation,
1069  SA & sa)
1070 {
1071  T ret_val = init;
1072  for_each_arc<GT, SA>(g, [&ret_val, &operation] (typename GT::Arc* a)
1073  {
1074  ret_val = operation(ret_val, a);
1075  }, sa);
1076  return ret_val;
1077 }
1078 
1079 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1080 T foldl_arcs(GT & g, const T & init,
1081  std::function<T(const T&, typename GT::Arc*)> operation,
1082  SA && sa = SA())
1083 {
1084  return foldl_arcs<GT, T, SA>(g, init, operation, sa);
1085 }
1086 
1087 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1088 T foldl_arcs(GT & g, typename GT::Node * p,
1089  const T & init,
1090  std::function<T(const T&, typename GT::Arc*)> operation,
1091  SA & sa)
1092 {
1093  T ret_val = init;
1094  for_each_arc<GT, SA>(g, p, [&ret_val, &operation] (typename GT::Arc* a)
1095  {
1096  ret_val = operation(ret_val, a);
1097  }, sa);
1098  return ret_val;
1099 }
1100 
1101 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1102 T foldl_arcs(GT & g, typename GT::Node * p,
1103  const T & init,
1104  std::function<T(const T&, typename GT::Arc*)> operation,
1105  SA && sa = SA())
1106 {
1107  return foldl_arcs<GT, T, SA>(g, p, init, operation, sa);
1108 }
1109 
1126 template <typename __Graph_Node = Graph_Node<int>,
1127  typename __Graph_Arc = Graph_Arc<int>>
1128 class List_Digraph : public List_Graph<__Graph_Node, __Graph_Arc>
1129 {
1131 
1132 public:
1133 
1134  typedef __Graph_Node Node;
1135 
1136  typedef __Graph_Arc Arc;
1137 
1138  List_Digraph()
1139  {
1140  this->digraph = true;
1141  }
1142 
1143  List_Digraph(const List_Digraph & dg) : GT()
1144  {
1145  this->digraph = true;
1146  *this = dg;
1147  }
1148 
1149  List_Digraph(List_Digraph && dg) : GT()
1150  {
1151  this->digraph = true;
1152  this->swap(dg);
1153  }
1154 
1155  List_Digraph & operator = (const List_Digraph & g)
1156  {
1157  if (this == &g)
1158  return *this;
1159 
1160  copy_graph(*this, const_cast<List_Digraph&>(g));
1161 
1162  return *this;
1163  }
1164 
1165  List_Digraph & operator = (List_Digraph && g)
1166  {
1167  this->swap(g);
1168 
1169  return *this;
1170  }
1171 };
1172 
1173 
1185  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1187  public Filter_Iterator<typename GT::Node*,
1188  typename GT::Out_Iterator,
1189  Show_Arc>
1190 {
1191 public:
1192 
1193  typedef Filter_Iterator
1194  <typename GT::Node*, typename GT::Out_Iterator, Show_Arc> Itor;
1195 
1196  Out_Iterator() { /* empty */ }
1197 
1203  Out_Iterator(typename GT::Node * p, Show_Arc && sa = Show_Arc())
1204  : Itor(p, sa)
1205  {
1206  // empty
1207  }
1208 
1214  Out_Iterator(typename GT::Node * p, Show_Arc & sa)
1215  : Itor(p, sa)
1216  {
1217  // empty
1218  }
1219 };
1220 
1232  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1233 class In_Iterator :
1234  public Filter_Iterator<typename GT::Node*,
1235  typename GT::In_Iterator,
1236  Show_Arc>
1237 {
1238 public:
1239 
1240  typedef Filter_Iterator
1241  <typename GT::Node*, typename GT::In_Iterator, Show_Arc> Itor;
1242 
1243  In_Iterator() { /* empty */ }
1244 
1250  In_Iterator(typename GT::Node * p, Show_Arc && sa = Show_Arc())
1251  : Itor(p, sa)
1252  {
1253  // empty
1254  }
1255 
1261  In_Iterator(typename GT::Node * p, Show_Arc & sa)
1262  : Itor(p, sa)
1263  {
1264  // empty
1265  }
1266 };
1267 
1313  template <class GT, class SA> typename GT::Arc *
1314 search_arc(GT & g, typename GT::Node * src_node,
1315  typename GT::Node * tgt_node);
1316 
1318  template <class GT>
1319 typename GT::Node * mapped_node(typename GT::Node * p)
1320 {
1321  return (typename GT::Node *) NODE_COOKIE(p);
1322 }
1323 
1325  template <class GT>
1326 typename GT::Arc * mapped_arc(typename GT::Arc * a)
1327 {
1328  return (typename GT::Arc *) ARC_COOKIE(a);
1329 }
1330 
1333  template <class GTSRC, class GTTGT>
1334 typename GTTGT::Node * mapped_node(typename GTSRC::Node * p)
1335 {
1336  return (typename GTTGT::Node *) NODE_COOKIE(p);
1337 }
1340  template <class GTSRC, class GTTGT>
1341 typename GTTGT::Arc * mapped_arc(typename GTSRC::Arc * a)
1342 {
1343  return (typename GTTGT::Arc *) ARC_COOKIE(a);
1344 }
1345 
1364  template <class GT>
1365 void copy_graph(GT & gtgt, GT & gsrc, const bool cookie_map = false);
1366 
1372 template <class GT> inline void clear_graph(GT & g);
1373 
1388  template <class GT, class Operation, class SN = Dft_Show_Node<GT>>
1390 {
1391  SN & sn;
1392 
1393 public:
1394 
1395  Operate_On_Nodes(SN & __sn) : sn(__sn) { /* empty */ }
1396 
1397  Operate_On_Nodes(SN && __sn = SN()) : sn(__sn) { /* empty */ }
1398 
1405  void operator () (GT & g, Operation op = Operation()) const
1406  {
1407  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next())
1408  op (g, it.get_curr());
1409  }
1410 
1420  void operator () (GT & g, void * ptr, Operation op = Operation()) const
1421  {
1422  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next())
1423  op (g, it.get_curr(), ptr);
1424  }
1425 };
1426 
1441  template <class GT, class Operation,
1442  class SA = Dft_Show_Arc<GT>>
1444 {
1445  SA & sa;
1446 
1447 public:
1448 
1449  Operate_On_Arcs(SA & __sa) : sa(__sa) { /* empty */ }
1450 
1451  Operate_On_Arcs(SA && __sa = SA()) : sa(__sa) { /* empty */ }
1452 
1459  void operator () (GT & g, Operation op = Operation()) const
1460  {
1461  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next())
1462  op (g, it.get_curr());
1463  }
1464 
1473  void operator () (GT & g, void * ptr, Operation op = Operation()) const
1474  {
1475  for (Arc_Iterator<GT, SA> itor(g, sa); itor.has_curr(); itor.next())
1476  op (g, itor.get_curr(), ptr);
1477  }
1478 
1485  void operator () (GT & g, typename GT::Node * p,
1486  Operation op = Operation()) const
1487  {
1488  for (Node_Arc_Iterator<GT, SA> it(p); it.has_curr(); it.next())
1489  op (g, it.get_current_arc());
1490  }
1491 
1501  void operator () (GT & g, typename GT::Node * node,
1502  void * ptr, Operation op = Operation()) const
1503  {
1504  for (Node_Arc_Iterator<GT, SA> it(node); it.has_current(); it.next())
1505  op (g, it.get_current_arc(), ptr);
1506  }
1507 };
1508 
1525  template <class GT>
1526 class Path
1527 {
1528 public:
1529 
1531  typedef typename GT::Node_Type Node_Type;
1533  typedef typename GT::Arc_Type Arc_Type;
1534 
1535 private:
1536 
1537  GT * g;
1538  void * cookie;
1539 
1540  typedef typename GT::Node Node;
1541  typedef typename GT::Arc Arc;
1542 
1543  struct Path_Desc
1544  {
1545  Node * node; // nodo origen
1546  Arc * arc; // arco adyacente
1547  Path_Desc(Node * _node = NULL, Arc * _arc = NULL)
1548  : node(_node), arc(_arc) {}
1549  bool operator == (const Path_Desc & r) const { return arc == r.arc; }
1550  };
1551 
1553 
1554 public:
1555 
1557  GT & get_graph() const { return *g; }
1558 
1559  void *& get_cookie() { return cookie; }
1560 
1562  bool inside_graph(GT & gr) const { return g == &gr; }
1563 
1569  Path(GT & _g, void * __cookie = NULL) : g(&_g), cookie(__cookie) {}
1570 
1572  Path() : g(NULL), cookie(NULL) { /* empty */ }
1573 
1580  void init(Node * start_node) { list.append(Path_Desc(start_node)); }
1581 
1590  Path(GT & _g, Node * start_node, void * __cookie = NULL)
1591  : g(&_g), cookie(__cookie) { init(start_node); }
1592 
1604  void set_graph(GT & __g, Node * start_node = NULL)
1605  {
1606  clear_path();
1607  g = &__g;
1608  if (start_node == NULL)
1609  return;
1610  init(start_node);
1611  }
1612 
1614  const size_t & size() const { return list.size(); }
1615 
1617  bool is_empty() const { return list.is_empty(); }
1618 
1620  void clear_path()
1621  {
1622  while (not list.is_empty())
1623  list.remove_first();
1624  }
1625 
1627  void clear() { clear_path(); }
1628 
1630  Path(const Path & path) : g(path.g), list(path.list) {}
1631 
1633  Path & operator = (const Path & path)
1634  {
1635  if (this == &path)
1636  return *this;
1637 
1638  clear_path();
1639  g = path.g;
1640  list = path.list;
1641  return *this;
1642  }
1643 
1671  void append(Arc * arc)
1672  {
1673  if (list.is_empty())
1674  throw std::domain_error("path is empty");
1675 
1676  Path_Desc & last_path_desc = list.get_last();
1677 
1678  if (g == NULL)
1679  throw std::domain_error("Graph has not been specified");
1680 
1681  last_path_desc.arc = arc;
1682  list.append(Path_Desc(g->get_connected_node(arc, last_path_desc.node)));
1683  }
1684 
1712  void append(Node * node)
1713  {
1714  if (list.is_empty())
1715  {
1716  init(node);
1717  return;
1718  }
1719 
1720  if (g == NULL)
1721  throw std::domain_error("Graph has not been specified");
1722 
1723  Node * last_node = get_last_node();
1724  Arc * arc = search_arc(*g, last_node, node);
1725 
1726  if (arc == NULL)
1727  throw std::domain_error("There is no an arc connecting to the node");
1728 
1729  append(arc);
1730  }
1731 
1758  void insert(Arc * arc)
1759  {
1760  if (list.is_empty())
1761  throw std::domain_error("path is empty");
1762 
1763  if (g == NULL)
1764  throw std::domain_error("Graph has not been specified");
1765 
1766  Path_Desc & first_path_desc = list.get_first();
1767 
1768  Path_Desc item(g->get_connected_node(arc, first_path_desc.node), arc);
1769 
1770  list.insert(item);
1771  }
1772 
1800  void insert(Node * node)
1801  {
1802  if (list.is_empty())
1803  {
1804  init(node);
1805  return;
1806  }
1807 
1808  if (g == NULL)
1809  throw std::domain_error("Graph has not been specified");
1810 
1811  Node * first_node = get_first_node();
1812  Arc * arc = search_arc(*g, node, first_node); // busque arco last_node-node
1813  if (arc == NULL)
1814  throw std::domain_error("There is no arc connecting node");
1815 
1816  insert(arc);
1817  }
1818 
1820  Node * get_first_node() const { return list.get_first().node; }
1821 
1823  Node * get_last_node() const { return list.get_last().node; }
1824 
1826  Arc * get_first_arc() { return list.get_first().arc; }
1827 
1829  Arc * get_last_arc()
1830  {
1831  if (list.is_unitarian())
1832  throw std::domain_error("Path with only a node (without any arc");
1833 
1834  typename DynDlist<Path_Desc>::Iterator it(list);
1835  it.reset_last();
1836  it.prev();
1837  return it.get_current().arc;
1838  }
1839 
1841  bool is_cycle() const { return get_first_node() == get_last_node(); }
1842 
1845  {
1846  list.remove_last();
1847  list.get_last().arc = NULL;
1848  }
1849 
1852  {
1853  list.remove_first();
1854  }
1855 
1857  void swap(Path & path)
1858  {
1859  std::swap(g, path.g);
1860  list.swap(path.list);
1861  }
1862 
1868  class Iterator : public DynDlist<Path_Desc>::Iterator
1869  {
1870  public:
1871 
1873  Iterator(const Path & path)
1874  : DynDlist<Path_Desc>::Iterator(path.list) { }
1875 
1876  private:
1877 
1878  Path_Desc & get_curr_path_desc() const
1879  {
1881  }
1882 
1883  public:
1884 
1887  Node * get_current_node() const { return this->get_curr_path_desc().node; }
1888 
1891  Arc * get_current_arc() const
1892  {
1893  if (this->is_in_last())
1894  throw std::overflow_error("Path iterator is in last node of path");
1895 
1896  return this->get_curr_path_desc().arc;
1897  }
1898 
1900  Node * get_current() const { return get_current_node(); }
1901 
1903  Node * get_curr() const { return get_current_node(); }
1904 
1908  bool has_current_arc() const
1909  {
1910  return this->has_current() and not this->is_in_last();
1911  }
1912 
1914  bool has_current_node() const { return this->has_current(); }
1915  };
1916 
1923  template <class Operation>
1924  void for_each_node(Operation & op) const
1925  {
1926  for (Iterator it(*this); it.has_current(); it.next())
1927  op (it.get_current_node());
1928  }
1929 
1930  template <class Operation>
1931  void for_each_node(Operation && op = Operation()) const
1932  {
1933  for_each_node<Operation>(op);
1934  }
1935 
1942  template <class Operation>
1943  void for_each_arc(Operation & op) const
1944  {
1945  for (Iterator it(*this); it.has_current_arc(); it.next())
1946  op (it.get_current_arc());
1947  }
1948 
1949  template <class Operation>
1950  void for_each_arc(Operation && op = Operation()) const
1951  {
1952  for_each_arc<Operation>(op);
1953  }
1954 
1956  bool contains_node(Node * node) const
1957  {
1958  for (Iterator it(*this); it.has_current_node(); it.next())
1959  if (it.get_current_node() == node)
1960  return true;
1961  return false;
1962  }
1963 
1965  bool contains_arc(Arc * arc) const
1966  {
1967  for (Iterator it(*this); it.has_current_arc(); it.next())
1968  if (it.get_current_arc() == arc)
1969  return true;
1970  return false;
1971  }
1972 
1973  template <template <typename> class Container = DynList>
1974  Container<Node*> nodes() const
1975  {
1976  Container<Node*> ret_val;
1977  for_each_node([&ret_val] (Node * p)
1978  {
1979  ret_val.append(p);
1980  });
1981 
1982  return ret_val;
1983  }
1984 
1985  template <template <typename> class Container = DynList>
1986  Container<Arc*> arcs() const
1987  {
1988  Container<Arc*> ret_val;
1989  for_each_arc([&ret_val] (Arc * a)
1990  {
1991  ret_val.append(a);
1992  });
1993 
1994  return ret_val;
1995  }
1996 
1997  bool operator == (const Path & p) const
1998  {
1999  return eq(this->list, p.list);
2000  }
2001 
2002  bool operator != (const Path & p) const
2003  {
2004  return not eq(this->list, p.list);
2005  }
2006 };
2007 
2008  template <class GT, class SA = Dft_Show_Arc<GT>> inline
2009 bool find_path_depth_first(GT & g, typename GT::Node * start_node,
2010  typename GT::Node * end_node, Path<GT> & path);
2011 
2012  template <class GT, class SA> inline
2013 bool __find_path_depth_first(GT& g, typename GT::Node * curr_node,
2014  typename GT::Arc * curr_arc,
2015  typename GT::Node * end_node, Path<GT> & curr_path)
2016 {
2017  if (curr_node == end_node) // ¿se alcanzó nodo final?
2018  { // sí, terminar añadir el arco y terminar
2019  curr_path.append(curr_arc);
2020  return true;
2021  }
2022 
2023  if (IS_NODE_VISITED(curr_node, Find_Path)) // ¿ha sido visitado?
2024  return false; // sí ==> desde él no hay camino
2025 
2026  curr_path.append(curr_arc); // añadir curr_arc al camino
2027  NODE_BITS(curr_node).set_bit(Find_Path, true);
2028 
2029  // buscar recursivamente a través de arcos de curr_node
2030  for (Node_Arc_Iterator<GT, SA> i(curr_node); i.has_current(); i.next())
2031  {
2032  typename GT::Arc * next_arc = i.get_current_arc();
2033  if (IS_ARC_VISITED(next_arc, Find_Path))
2034  continue;
2035 
2036  ARC_BITS(next_arc).set_bit(Find_Path, true);
2037  typename GT::Node * next_node = i.get_tgt_node();
2038  if (__find_path_depth_first<GT, SA> (g, next_node, next_arc,
2039  end_node, curr_path))
2040  {
2041  I(curr_path.get_last_node() == end_node);
2042  return true; // se encontró camino
2043  }
2044  }
2045 
2046  curr_path.remove_last_node();
2047 
2048  return false;
2049 }
2050 
2081  template <class GT, class SA> inline
2082 bool find_path_depth_first(GT & g, typename GT::Node * start_node,
2083  typename GT::Node * end_node, Path<GT> & path)
2084 {
2085  if (not path.inside_graph(g))
2086  throw std::invalid_argument("Path does not belong to graph");
2087 
2088  path.clear_path();
2089  path.init(start_node);
2090  g.reset_bit_nodes(Find_Path);
2091  g.reset_bit_arcs(Find_Path);
2092  NODE_BITS(start_node).set_bit(Find_Path, true);
2093 
2094  // explorar recursivamente cada arco de start_node
2095  for (Node_Arc_Iterator<GT, SA> i(start_node); i.has_current(); i.next())
2096  {
2097  typename GT::Arc * arc = i.get_current_arc();
2098  ARC_BITS(arc).set_bit(Find_Path, true);
2099  typename GT::Node * next_node = i.get_tgt_node();
2100  if (IS_NODE_VISITED(next_node, Find_Path))
2101  continue;
2102 
2103  if (__find_path_depth_first<GT, SA>(g, next_node, arc, end_node, path))
2104  return true;
2105  }
2106 
2107  return false;
2108 }
2109 
2110  template <class GTS, class GTT>
2111 void map_nodes(typename GTS::Node * p, typename GTT::Node * q)
2112 {
2113  I(p != NULL and q != NULL);
2114 
2115  if (NODE_COOKIE(p) == NULL)
2116  {
2117  NODE_COOKIE(p) = q;
2118  NODE_COOKIE(q) = p;
2119  return;
2120  }
2121 
2122  NODE_COOKIE(q) = NODE_COOKIE(p);
2123  NODE_COOKIE(p) = q;
2124 }
2125 
2126  template <class GTS, class GTT>
2127 void map_arcs(typename GTS::Arc * p, typename GTT::Arc * q)
2128 {
2129  I(p != NULL and q != NULL);
2130 
2131  if (ARC_COOKIE(p) == NULL)
2132  {
2133  ARC_COOKIE(p) = q;
2134  ARC_COOKIE(q) = p;
2135 
2136  return;
2137  }
2138 
2139  ARC_COOKIE(q) = ARC_COOKIE(p);
2140  ARC_COOKIE(p) = q;
2141 }
2142 
2144 //
2145 // Implementaciones de métodos de List_Graph
2146 //
2148 
2149  template <typename Node, typename Arc>
2151 {
2152  if (num_nodes == 0)
2153  throw std::range_error("Graph has not nodes");
2154 
2155  return dlink_to_node(const_cast<Dlink&>(node_list).get_next());
2156 }
2157 
2158  template <typename Node, typename Arc>
2160 {
2161  if (get_num_arcs() == 0)
2162  throw std::range_error("Graph has not arcs");
2163 
2164  return dlink_to_arc(const_cast<Dlink&>(arc_list).get_next());
2165 }
2166 
2167  template <typename Node, typename Arc>
2168 Arc * List_Graph<Node, Arc>::get_first_arc(Node * node) const
2169 {
2170  if (get_num_arcs(node) == 0)
2171  throw std::range_error("node has not arcs");
2172 
2173  void * arc = dlink_to_arc_node(node->arc_list.get_next())->arc;
2174  return reinterpret_cast <Arc *> (arc);
2175 }
2176  template <typename Node, typename Arc>
2178  : Dlink::Iterator(&_g.node_list)
2179 {
2180  // empty
2181 }
2182 
2183  template <typename Node, typename Arc>
2185 {
2186  return dlink_to_node(Dlink::Iterator::get_current());
2187 }
2188 
2189  template <typename Node, typename Arc>
2191  : Dlink::Iterator(&_g.arc_list)
2192 {
2193  // empty
2194 }
2195 
2196  template <typename Node, typename Arc>
2198 {
2199  return dlink_to_arc(const_cast<Dlink*>(Dlink::Iterator::get_current()));
2200 }
2201 
2202  template <typename Node, typename Arc>
2204 {
2205  return static_cast<Arc*>(get_current_arc_node()->arc);
2206 }
2207 
2208  template <typename Node, typename Arc>
2210 {
2211  return static_cast<Node*>(get_current_arc()->get_connected_node(src_node));
2212 }
2213 
2214  template <typename Node, typename Arc>
2216 {
2217  ++num_nodes;
2218  node_list.append(node);
2219  return node;
2220 }
2221 
2222  template <typename Node, typename Arc> Node *
2223 List_Graph<Node, Arc>::insert_node(const typename Node::Node_Type & node_info)
2224 {
2225  return List_Graph<Node, Arc>::insert_node( new Node (node_info) );
2226 }
2227 
2228  template <typename Node, typename Arc> Node *
2229 List_Graph<Node, Arc>::insert_node(typename Node::Node_Type && node_info)
2230 {
2232  (new Node (std::move(node_info)) );
2233 }
2234 
2235  template <typename Node, typename Arc> Arc *
2236 List_Graph<Node, Arc>::insert_arc(Node * src_node, Node * tgt_node)
2237 {
2238  // paso 1: apartar memoria para Arc e iniciar
2239  unique_ptr<Arc> arc ( new Arc ); // apartar memoria para arco
2240  arc->src_node = src_node; // Iniciar sus campos
2241  arc->tgt_node = tgt_node;
2242 
2243  // paso 3: (parcial): apartar Arc_Node de src_node
2244  unique_ptr<Arc_Node> src_arc_node (new Arc_Node (arc.get()));
2245 
2246  // paso 2: si es grafo ==> apartar Arc_Node de tgt_node
2247  if (not digraph) // si es digrafo ==> no insertar en otro nodo
2248  { // inserción en nodo destino
2249  if (src_node == tgt_node) // ¿es un lazo?
2250  arc->tgt_arc_node = src_arc_node.get();
2251  else
2252  { // apartar arco nodo para tgt_node
2253  unique_ptr<Arc_Node> tgt_arc_node(new Arc_Node(arc.get()));
2254 
2255  // inserción en lista de adyacencia de tgt_node
2256  arc->tgt_arc_node = tgt_arc_node.get();
2257  tgt_node->arc_list.append(tgt_arc_node.get());
2258  tgt_node->num_arcs++;
2259  tgt_arc_node.release();
2260  }
2261  }
2262 
2263  // paso 3 (resto): inserción en lista adyacencia src_node
2264  arc->src_arc_node = src_arc_node.get();
2265  src_node->arc_list.append(src_arc_node.get());
2266  src_node->num_arcs++;
2267 
2268  arc_list.append(arc.get()); //paso 4:insertar en lista arcos grafo
2269  ++num_arcs;
2270  src_arc_node.release();
2271 
2272  return arc.release();
2273 }
2274 
2275 
2276  template <typename Node, typename Arc>
2278 {
2279  Node * src_node = get_src_node(arc);
2280  Node * tgt_node = get_tgt_node(arc);
2281  Arc_Node * src_arc_node = arc->src_arc_node;
2282  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
2283 
2284  if (not digraph) // si es digrafo ==> no hay que insertar en el otro nodo
2285  { // inserción en nodo destino
2286  if (src_node != tgt_node) // verificar si se trata de un ciclo
2287  { // inserción en lista de adyacencia de tgt_node
2288  tgt_node->arc_list.append(tgt_arc_node);
2289  tgt_node->num_arcs++;
2290  }
2291  }
2292 
2293  src_node->arc_list.append(src_arc_node);
2294  src_node->num_arcs++;
2295  arc_list.append(arc);
2296  ++num_arcs;
2297 
2298  return arc;
2299 }
2300 
2301  template <typename Node, typename Arc> Arc *
2302 List_Graph<Node, Arc>::insert_arc(Node * src_node, Node * tgt_node,
2303  const typename Arc::Arc_Type & arc_info)
2304 {
2305  Arc * arc = List_Graph<Node, Arc>::insert_arc(src_node, tgt_node);
2306  arc->get_info() = arc_info;
2307 
2308  return arc;
2309 }
2310 
2311  template <typename Node, typename Arc> Arc *
2312 List_Graph<Node, Arc>::insert_arc(Node * src_node, Node * tgt_node,
2313  typename Arc::Arc_Type && arc_info)
2314 {
2315  Arc * arc = List_Graph<Node, Arc>::insert_arc(src_node, tgt_node);
2316  arc->get_info() = std::move(arc_info);
2317 
2318  return arc;
2319 }
2320 
2321 
2322  template <typename Node, typename Arc>
2324 { // paso 1: eliminar Arc_node de src_node
2325  Node * src_node = get_src_node(arc);
2326  Arc_Node * src_arc_node = arc->src_arc_node;
2327 
2328  src_arc_node->del(); // desenlaza src_node de la lista de nodos
2329  src_node->num_arcs--; // decrementa contador de arcos de src_node
2330  delete src_arc_node; // entrega memoria
2331 
2332  if (not digraph)
2333  { // eliminación arco en nodo destino
2334  Node * tgt_node = get_tgt_node(arc);
2335  if (src_node != tgt_node) // verificar eliminación de ciclo
2336  { // paso 2: eliminar Arc_node de tgt_node
2337  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
2338  tgt_arc_node->del();
2339  tgt_node->num_arcs--;
2340  delete tgt_arc_node;
2341  }
2342  }
2343 
2344  // eliminación de arco del grafo
2345  arc->del(); // desenlazar arc de lista de arcos de grafo
2346  --num_arcs;
2347  delete arc;
2348 }
2349 
2350  template <typename Node, typename Arc>
2352 { // paso (1): eliminación del Arc_node del nodo origen src_node
2353  Node * src_node = get_src_node(arc);
2354  Arc_Node * src_arc_node = arc->src_arc_node;
2355  src_arc_node->del(); // desenlaza src_node de la lista de nodos
2356  src_node->num_arcs--; // decrementa contador de arcos de src_node
2357 
2358  if (not digraph)
2359  { // eliminación arco en nodo destino
2360  Node * tgt_node = get_tgt_node(arc);
2361  if (src_node != tgt_node) // verificar eliminación de ciclo
2362  { // paso (2): eliminación del Arc_node del nodo destino tgt_node
2363  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
2364  tgt_arc_node->del();
2365  tgt_node->num_arcs--;
2366  }
2367  }
2368 
2369  // eliminación de arco del grafo
2370  arc->del(); // desenlazar arc de la lista de arcos del grafo
2371  --num_arcs;
2372 }
2373 
2374  template <typename Node, typename Arc>
2376 {
2377  if (not digraph)
2378  // Eliminar arcos adyacentes a node
2379  while (not node->arc_list.is_empty()) // mientras queden arcos
2380  { // obtener Arc_Node
2381  Arc_Node * arc_node =
2382  dlink_to_arc_node(node->arc_list.get_next());
2383  Arc * arc = void_to_arc(arc_node); // obtener el arco
2384  remove_arc(arc); // eliminarlo del grafo
2385  }
2386  else
2387  for (Arc_Iterator it(*this); it.has_current();)
2388  {
2389  Arc * arc = it.get_current();
2390  if (get_src_node(arc) == node or get_tgt_node(arc) == node)
2391  {
2392  it.next();
2393  remove_arc(arc);
2394  }
2395  else
2396  it.next();
2397  }
2398 
2399  // en este punto el nodo ya no tiene arcos
2400  node->del(); // desenlazar nodo de lista de nodos del grafo
2401  --num_nodes;
2402  delete node;
2403 }
2404 
2405  template <typename Node, typename Arc>
2407 {
2408  init();
2409  copy_graph(*this, const_cast<List_Graph<Node, Arc> &>(g));
2410 }
2411 
2412  template <typename Node, typename Arc>
2414 {
2415  init();
2416  swap(g);
2417 }
2418 
2419  template <typename Node, typename Arc>
2422 {
2423  copy_graph(*this, const_cast<List_Graph<Node, Arc> &>(g));
2424 
2425  return *this;
2426 }
2427 
2428 
2429  template <typename Node, typename Arc>
2431  (List_Graph<Node, Arc> && g)
2432 {
2433  swap(g);
2434  return *this;
2435 }
2436 
2437  template <typename Node, typename Arc>
2439 {
2440  clear_graph(*this);
2441 }
2442 
2443  template <typename Node, typename Arc>
2444  template <class Compare>
2446 {
2447  mergesort<Dlink, Cmp_Arc<Compare>>(arc_list, Cmp_Arc<Compare>(cmp));
2448 }
2449 
2450  template <typename Node, typename Arc>
2451  template <class Compare>
2452 void List_Graph<Node, Arc>::sort_arcs(Compare & cmp)
2453 {
2454  Cmp_Arc<Compare> comp(cmp);
2455  mergesort<Dlink, Cmp_Arc<Compare>>(arc_list, comp);
2456 }
2457 
2458 GRAPH_METHODS_IMPLS(List_Graph);
2459 
2460  template <class GT, class SA>
2461 typename GT::Arc * search_arc(GT & g,
2462  typename GT::Node * src, typename GT::Node * tgt)
2463 {
2464  I(src != NULL and tgt != NULL);
2465 
2466  // escoger nodo con menos arcos
2467  if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
2468  std::swap(tgt, src);
2469 
2470  for (Node_Arc_Iterator<GT, SA> itor(src); itor.has_curr(); itor.next())
2471  if (itor.get_tgt_node() == tgt)
2472  return itor.get_current_arc();
2473 
2474  return NULL;
2475 }
2476 
2477 
2478  template <class GT>
2479 typename GT::Arc * search_arc(GT & g, typename GT::Node * src_node,
2480  typename GT::Node * tgt_node)
2481 {
2482  return search_arc<GT, Dft_Show_Arc<GT>>(g, src_node, tgt_node);
2483 }
2484 
2485  template <class GT>
2486 void clear_graph(GT & g)
2487 {
2488  for (typename GT::Arc_Iterator it(g); it.has_current(); ) // eliminar arcos
2489  {
2490  typename GT::Arc * arc = it.get_current();
2491  it.next();
2492  g.remove_arc(arc);
2493  }
2494 
2495  for (typename GT::Node_Iterator it(g); it.has_current(); ) // eliminar nodos
2496  {
2497  typename GT::Node * p = it.get_current();
2498  it.next(); // avanzar antes de borrar (consistencia iterador)
2499  g.remove_node(p); // eliminarlo del grafo
2500  }
2501 }
2502 
2503 
2504  template <class GT>
2505 void copy_graph(GT & gtgt, GT & gsrc, const bool cookie_map)
2506 {
2507  IG(not gtgt.is_digraph() and gsrc.is_digraph(),
2508  gtgt.is_digraph() != gsrc.is_digraph());
2509 
2510  try
2511  {
2512  clear_graph(gtgt); // limpiar this antes de copiar
2514 
2515  // fase 1: recorrer nodos de src_graph e insertar copia en this
2516  for (typename GT::Node_Iterator it(gsrc); it.has_curr(); it.next())
2517  {
2518  typename GT::Node * src_node = it.get_current_node();
2519  unique_ptr<typename GT::Node>
2520  tgt_node(new typename GT::Node(src_node));
2521  map.insert(src_node, tgt_node.get());
2522 
2523  typename GT::Node * tgt = tgt_node.release();
2524  gtgt.insert_node(tgt); // insertar en grafo destino
2525 
2526  if (cookie_map)
2527  GT::map_nodes(src_node, tgt);
2528  }
2529 
2530  I(gtgt.get_num_nodes() == gsrc.get_num_nodes());
2531 
2532  // fase 2: por cada arco de src_graph, crear en this un
2533  // arco que conecte a los nodos mapeados de map
2534  for (typename GT::Arc_Iterator it(gsrc); it.has_current(); it.next())
2535  {
2536  typename GT::Arc * src_arc = it.get_current_arc();
2537 
2538  // obtener imágenes de nodos en el grafo destino y crear arco
2539  typename GT::Node * src_node = map(gsrc.get_src_node(src_arc));
2540  typename GT::Node * tgt_node = map(gsrc.get_tgt_node(src_arc));
2541  typename GT::Arc * tgt_arc = gtgt.insert_arc(src_node, tgt_node);
2542  tgt_arc->get_info() = src_arc->get_info();
2543  if (cookie_map)
2544  GT::map_arcs(src_arc, tgt_arc);
2545  }
2546 
2547  I(gtgt.get_num_arcs() == gsrc.get_num_arcs());
2548  }
2549  catch (...)
2550  { // Si ocurre excepción se limpia this
2551  clear_graph(gtgt);
2552  throw;
2553  }
2554 }
2555 
2556  template <class GTT, class GTS>
2558 {
2559  void operator () (typename GTT::Node * tgt, typename GTS::Node * src)
2560  {
2561  tgt->get_info() = src->get_info();
2562  }
2563 };
2564 
2565 
2566  template <class GTT, class GTS>
2568 {
2569  void operator () (typename GTT::Arc * tgt, typename GTS::Arc * src)
2570  {
2571  tgt->get_info() = src->get_info();
2572  }
2573 };
2574 
2579  template <class GTT, class GTS,
2580  class Copy_Node = Dft_Copy_Node<GTT, GTS>,
2581  class Copy_Arc = Dft_Copy_Arc<GTT, GTS>>
2582 void inter_copy_graph(GTT & gtgt, GTS & gsrc, const bool cookie_map = false)
2583 {
2584  IG(not gtgt.is_digraph() and gsrc.is_digraph(),
2585  gtgt.is_digraph() != gsrc.is_digraph());
2586 
2587  try
2588  {
2589  clear_graph(gtgt); // limpiar this antes de copiar
2591 
2592  // fase 1: recorrer nodos de src_graph e insertar copia en this
2593  for (typename GTS::Node_Iterator it(gsrc); it.has_current(); it.next())
2594  {
2595  typename GTS::Node * src_node = it.get_current_node();
2596  unique_ptr<typename GTT::Node> tgt_node(new typename GTT::Node);
2597  Copy_Node () (tgt_node.get(), src_node);
2598  map.insert(src_node, tgt_node.get());
2599 
2600  typename GTT::Node * tgt = tgt_node.release();
2601  gtgt.insert_node(tgt); // insertar en grafo destino
2602 
2603  if (cookie_map)
2604  map_nodes<GTS, GTT>(src_node, tgt);
2605  }
2606 
2607  I(gtgt.get_num_nodes() == gsrc.get_num_nodes());
2608 
2609  // fase 2: por cada arco de src_graph, crear en this un
2610  // arco que conecte a los nodos mapeados de map
2611  for (typename GTS::Arc_Iterator it(gsrc); it.has_current(); it.next())
2612  {
2613  typename GTS::Arc * src_arc = it.get_current_arc();
2614 
2615  // obtener imágenes de nodos en el grafo destino y crear arco
2616  typename GTT::Node * src_node = map[gsrc.get_src_node(src_arc)];
2617  typename GTT::Node * tgt_node = map[gsrc.get_tgt_node(src_arc)];
2618  typename GTT::Arc * tgt_arc = gtgt.insert_arc(src_node, tgt_node);
2619  Copy_Arc () (tgt_arc, src_arc);
2620  if (cookie_map)
2621  map_arcs<GTS, GTT>(src_arc, tgt_arc);
2622  }
2623 
2624  I(gtgt.get_num_arcs() == gsrc.get_num_arcs());
2625  }
2626  catch (...)
2627  { // Si ocurre excepción se limpia this
2628  clear_graph(gtgt);
2629  throw;
2630  }
2631 }
2632 
2633 
2653 template <class GT,
2654  class SN = Dft_Show_Node<GT>,
2655  class SA = Dft_Show_Arc<GT>
2656  >
2658 {
2659  SN & sn;
2660  SA & sa;
2661 
2662 public:
2663 
2669  Copy_Graph(SA & __sa, SN & __sn)
2670  : sn(__sn), sa(__sa)
2671  {
2672  // empty
2673  }
2674 
2676  Copy_Graph(SA && __sa = SA(), SN && __sn = SN())
2677  : sn(__sn), sa(__sa)
2678  {
2679  // empty
2680  }
2681 
2683  Copy_Graph(SA & __sa, SN && __sn = SN())
2684  : sn(__sn), sa(__sa)
2685  {
2686  // empty
2687  }
2688 
2689 private:
2690 
2691  void copy(GT & gtgt, GT & gsrc, const bool cookie_map)
2692  {
2693  IG(not gtgt.is_digraph() and gsrc.is_digraph(),
2694  gtgt.is_digraph() != gsrc.is_digraph());
2695 
2696  try
2697  {
2698  clear_graph(gtgt); // limpiar this antes de copiar
2700 
2701  // fase 1: recorrer nodos de src_graph e insertar copia en this
2702  for (Node_Iterator<GT, SN> it(gsrc, sn); it.has_curr(); it.next())
2703  {
2704  typename GT::Node * src_node = it.get_curr();
2705  unique_ptr<typename GT::Node>
2706  tgt_node(new typename GT::Node(src_node));
2707  map.insert(src_node, tgt_node.get());
2708 
2709  typename GT::Node * tgt = tgt_node.release();
2710  gtgt.insert_node(tgt); // insertar en grafo destino
2711 
2712  if (cookie_map)
2713  GT::map_nodes(src_node, tgt);
2714  }
2715 
2716  // fase 2: por cada arco de src_graph, crear en this un
2717  // arco que conecte a los nodos mapeados de map
2718  for (Arc_Iterator<GT, SA> it(gsrc, sa); it.has_curr(); it.next())
2719  {
2720  typename GT::Arc * src_arc = it.get_curr();
2721 
2722  // obtener imágenes de nodos en el grafo destino y crear arco
2723  typename GT::Node * src_node = map[gsrc.get_src_node(src_arc)];
2724  typename GT::Node * tgt_node = map[gsrc.get_tgt_node(src_arc)];
2725  typename GT::Arc * tgt_arc =
2726  gtgt.insert_arc(src_node, tgt_node, src_arc->get_info());
2727 
2728  if (cookie_map)
2729  GT::map_arcs(src_arc, tgt_arc);
2730  }
2731  }
2732  catch (...)
2733  { // Si ocurre excepción se limpia this
2734  clear_graph(gtgt);
2735  throw;
2736  }
2737  }
2738 
2739 public:
2740 
2753  void operator () (GT & gtgt, GT & gsrc, const bool cookie_map = true)
2754  {
2755  copy(gtgt, gsrc, cookie_map);
2756  }
2757 };
2758 
2759 } // end namespace Aleph
2760 
2761 # endif /* TPL_GRAPH_H */
Definition: tpl_graph.H:2657
Node * get_tgt_node() const
Retorna el nodo destino del arco actual (sólo cuenta si es digrafo)
Definition: tpl_graph.H:609
Copy_Graph(SA &__sa, SN &__sn)
Definition: tpl_graph.H:2669
Node_Arc_Iterator(Node *src)
Instancia un iterador sobre el nodo _src_node.
Definition: tpl_graph.H:551
void inter_copy_graph(GTT &gtgt, GTS &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:2582
Node * get_curr() const
Definition: tpl_graph.H:1903
Definition: tpl_graph.H:29
virtual void remove_node(Node *node)
Definition: tpl_graph.H:2375
Node * get_current()
Definition: tpl_graph.H:522
const size_t & size() const
Retorna la longitud del camino en nodos.
Definition: tpl_graph.H:1614
Node_Iterator(GT &g, Show_Node &sn)
Definition: tpl_graph.H:845
Node * get_current_node() const
Definition: tpl_graph.H:1887
void insert(Node *node)
Definition: tpl_graph.H:1800
Node * get_first_node() const
Definition: tpl_graph.H:2150
virtual void remove_arc(Arc *arc)
Definition: tpl_graph.H:2323
void sort_arcs(Compare &&cmp=Compare())
Definition: tpl_graph.H:2445
#define NODE_COOKIE(p)
Definition: aleph-graph.H:248
virtual ~List_Graph()
Definition: tpl_graph.H:2438
Definition: tpl_dynMapTree.H:260
Path()
Constructor vacío.
Definition: tpl_graph.H:1572
Node * Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:545
Out_Iterator(typename GT::Node *p, Show_Arc &&sa=Show_Arc())
Definition: tpl_graph.H:1203
Definition: tpl_graph.H:38
Definition: tpl_graph.H:21
bool is_cycle() const
Retorna true si el camino conforma un ciclo.
Definition: tpl_graph.H:1841
Definition: tpl_graph.H:217
bool is_empty() const
Retorna true si el camino está vacío.
Definition: tpl_graph.H:1617
size_type size()
Retorna la cantidad de elementos que tiene la lista.
Definition: List.H:340
Definition: tpl_dynDlist.H:26
Node * Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_graph.H:509
Definition: tpl_graph.H:751
Definition: tpl_graph.H:2567
Definition: tpl_graph.H:1389
Node * get_curr()
Definition: tpl_graph.H:525
Path(const Path &path)
Constructor copia de camino; todos los nodos y arcos se copian.
Definition: tpl_graph.H:1630
void insert(Arc *arc)
Definition: tpl_graph.H:1758
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:242
void swap(list &c)
Definition: List.H:486
Arc * get_current_arc() const
Retorna un puntero al arco actual.
Node * get_first_node() const
Retorna el primer nodo del camino.
Definition: tpl_graph.H:1820
In_Iterator(typename GT::Node *p, Show_Arc &&sa=Show_Arc())
Definition: tpl_graph.H:1250
Itor::Set_Type Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:825
List_Graph Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:512
Itor::Item_Type Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_graph.H:822
Copy_Graph(SA &__sa, SN &&__sn=SN())
Definition: tpl_graph.H:2683
#define ARC_COOKIE(p)
Definition: aleph-graph.H:281
Copy_Graph(SA &&__sa=SA(), SN &&__sn=SN())
Definition: tpl_graph.H:2676
Definition: tpl_graph.H:794
Node_Arc_Iterator()
Instancia un iterador vacío (inválido).
Definition: tpl_graph.H:548
bool inside_graph(GT &gr) const
retorna true si el camino this refiere al grafo gr
Definition: tpl_graph.H:1562
Arc_Iterator(GT &g, Show_Arc &sa)
Definition: tpl_graph.H:782
Itor::Item_Type Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_graph.H:708
void clear_graph(GT &g)
Definition: tpl_graph.H:2486
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:275
It::Item_Type Item_Type
Tipo de elemento que retorna get_current()
Definition: filter_iterator.H:93
Itor::Set_Type Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:762
Definition: tpl_graph.H:1186
Out_Iterator(typename GT::Node *p, Show_Arc &sa)
Definition: tpl_graph.H:1214
Definition: tpl_graph.H:26
Node_Arc_Iterator(typename GT::Node *p, Show_Arc &sa)
Definition: tpl_graph.H:731
bool find_path_depth_first(GT &g, typename GT::Node *start_node, typename GT::Node *end_node, Path< GT > &path)
Definition: tpl_graph.H:2082
Node_Arc_Iterator(typename GT::Node *p, Show_Arc &&sa=Show_Arc())
Definition: tpl_graph.H:720
Arc * get_last_arc()
Retorna el último arco del camino.
Definition: tpl_graph.H:1829
List_Graph & operator=(const List_Graph &g)
Definition: tpl_graph.H:2421
void remove_first_node()
Elimina el primer nodo del camino.
Definition: tpl_graph.H:1851
void swap(Path &path)
Intercambias los contenidos del camino this con el de path.
Definition: tpl_graph.H:1857
GT::Arc_Type Arc_Type
El tipo de atributo que contienen los arcos del camino.
Definition: tpl_graph.H:1533
void remove_last_node()
Elimina el último nodo del camino.
Definition: tpl_graph.H:1844
Arc * get_current_arc() const
Definition: tpl_graph.H:1891
Node * get_src_node() const
Retorna el nodo origen del arco actual (sólo cuenta si es digrafo)
Definition: tpl_graph.H:606
Definition: tpl_graph.H:582
Graph_Node()
Definition: tpl_graph.H:97
Definition: tpl_graph.H:634
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Graph_Node(const Node_Info &info)
Definition: tpl_graph.H:114
bool contains_node(Node *node) const
Retorna true si nodo pertenece al camino; false de lo contrario.
Definition: tpl_graph.H:1956
In_Iterator(typename GT::Node *p, Show_Arc &sa)
Definition: tpl_graph.H:1261
void for_each_node(Operation &op) const
Definition: tpl_graph.H:1924
Arc * get_current_arc() const
Retorna el arco actual.
Arc * Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_graph.H:587
Node * get_last_node() const
Retorna el último nodo del camino.
Definition: tpl_graph.H:1823
Arc * get_curr() const
Definition: tpl_graph.H:603
Definition: tpl_graph.H:504
bool contains_arc(Arc *arc) const
Retorna true si nodo pertenece al camino; false de lo contrario.
Definition: tpl_graph.H:1965
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const typename Arc::Arc_Type &arc_info)
Definition: tpl_graph.H:2302
Path(GT &_g, Node *start_node, void *__cookie=NULL)
Definition: tpl_graph.H:1590
bool has_current_arc() const
Definition: tpl_graph.H:1908
#define NODE_BITS(p)
Definition: aleph-graph.H:221
Definition: tpl_graph.H:1233
Path & operator=(const Path &path)
Asignación de camino; limpia this y copia todos los nodos y arcos de path.
Definition: tpl_graph.H:1633
Iterator(const Path &path)
Instancia un iterador sobre el camino path.
Definition: tpl_graph.H:1873
Definition: tpl_graph.H:535
Arc * get_curr() const
Definition: tpl_graph.H:564
Definition: tpl_graph.H:644
Arc * get_current() const
Retorna un puntero al arco actual.
Definition: tpl_graph.H:600
void init(Node *start_node)
Definition: tpl_graph.H:1580
void clear_path()
Limpia el camino (se eliminan todos sus nodos y arcos).
Definition: tpl_graph.H:1620
GT::Node_Type Node_Type
El tipo de atributo que contienen los nodos del camino.
Definition: tpl_graph.H:1531
virtual Arc * connect_arc(Arc *arc)
Definition: tpl_graph.H:2277
Definition: tpl_graph.H:19
Definition: tpl_graph.H:814
void operator()(GT &g, Operation op=Operation()) const
Definition: tpl_graph.H:1459
Arc * get_first_arc() const
Definition: tpl_graph.H:2159
#define ARC_BITS(p)
Definition: aleph-graph.H:266
void copy_graph(GT &gtgt, GT &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:2505
Arc_Iterator(GT &g, Show_Arc &&sa=Show_Arc())
Definition: tpl_graph.H:771
Arc * Item_Type
El tipo de dato que retorna get_current().
Definition: tpl_graph.H:542
T & get_current() const
Retorna el elemento actual del iterador.
Definition: tpl_dynDlist.H:427
Graph_Node(Graph_Node *node)
Definition: tpl_graph.H:134
Arc * get_first_arc()
Retorna el primer arco del camino.
Definition: tpl_graph.H:1826
GT::Arc * search_arc(GT &g, typename GT::Node *src_node, typename GT::Node *tgt_node)
Definition: tpl_graph.H:2461
Definition: tpl_graph.H:32
GT & get_graph() const
Obtiene una referencia al grafo sobre el cual se encuentra el camino.
Definition: tpl_graph.H:1557
Definition: ahDry.H:13
Node_Iterator(GT &g, Show_Node &&sn=Show_Node())
Definition: tpl_graph.H:834
Definition: filter_iterator.H:42
virtual void disconnect_arc(Arc *arc)
Definition: tpl_graph.H:2351
Arc * get_current() const
Definition: tpl_graph.H:561
iterator insert(const iterator &pos, const T &value)
Definition: List.H:525
Path(GT &_g, void *__cookie=NULL)
Definition: tpl_graph.H:1569
Definition: Map.H:56
void append(Arc *arc)
Definition: tpl_graph.H:1671
Definition: tpl_graph.H:694
Itor::Item_Type Item_Type
Tipo de elemento que retorna get_current()
Definition: tpl_graph.H:759
List_Graph Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:590
Node * get_tgt_node() const
Retorna el nodo destino del arco actual.
Key * insert(const Key &key, const Type &data)
Definition: tpl_dynMapTree.H:74
Definition: tpl_graph.H:35
Definition: List.H:23
Node * get_current() const
Definition: tpl_graph.H:1900
Definition: tpl_graph.H:1868
void clear()
Definition: tpl_graph.H:1627
Node * get_current_node()
retorna el nodo actual.
Itor::Set_Type Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_graph.H:711
void for_each_arc(Operation &op) const
Definition: tpl_graph.H:1943
void set_graph(GT &__g, Node *start_node=NULL)
Definition: tpl_graph.H:1604
void append(Node *node)
Definition: tpl_graph.H:1712
Definition: tpl_graph.H:2557
bool has_current_node() const
Retorna true si el iterador tiene nodo actual; false de lo contrario.
Definition: tpl_graph.H:1914
Definition: tpl_graph.H:1443
void operator()(GT &g, Operation op=Operation()) const
Definition: tpl_graph.H:1405
virtual Node * insert_node(Node *node)
Definition: tpl_graph.H:2215

Leandro Rabindranath León