Aleph-w  1.9
General library for algorithms and data structures
tpl_graph.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_GRAPH_H
28 # define TPL_GRAPH_H
29 
30 # include <memory>
31 # include <bitArray.H>
32 # include <tpl_dynArray.H>
33 # include <tpl_sort_utils.H>
34 # include <tpl_dynMapTree.H>
35 # include <tpl_dynDlist.H>
36 # include <tpl_treapRk.H>
37 # include <filter_iterator.H>
38 # include <aleph-graph.H>
39 # include <graph-dry.H>
40 
41 
42 using namespace Aleph;
43 
44 namespace Aleph {
45 
46 template <typename Node_Info> struct Graph_Node;
47 
48 template <typename Arc_Info> struct Graph_Arc;
49 
50 class Arc_Node;
51 
52  template <class GT>
53 class Path;
54 
55  template <typename __Graph_Node, typename __Graph_Arc>
56 class List_Graph;
57 
58  template <typename __Graph_Node, typename __Graph_Arc>
60 
61  template <class GT>
62 class Mat_Graph;
63 
64  template <typename MT, typename Entry_Info, typename Copy>
65 class Ady_MaT;
66 
67 
95  template <typename __Node_Info = Empty_Class>
96 struct Graph_Node
97  : public Dlink,
98  public GTNodeCommon<__Node_Info>
99 {
100  friend class GTNodeCommon<__Node_Info>;
101  friend class Arc_Node;
102 
103  using Base = GTNodeCommon<__Node_Info>;
104  using Node_Info = __Node_Info;
105 
115  Graph_Node(const Node_Info & info) noexcept : Base(info) { /* empty */ }
116 
126  Graph_Node(Node_Info && info = Node_Info()) noexcept : Base(move(info))
127  {
128  /* empty */
129  }
130 
131  Graph_Node(const Graph_Node & node) noexcept : Graph_Node(node.node_info)
132  {
133  // empty
134  }
135 
136  Graph_Node & operator = (const Graph_Node & node)
137  noexcept(std::is_nothrow_copy_assignable<Node_Info>::value)
138  {
139  if (&node == this)
140  return *this;
141  this->node_info = node.node_info;
142  return *this;
143  }
144 
161  noexcept(std::is_nothrow_copy_assignable<Node_Info>::value)
162  : Base(node->get_info())
163  {
164  /* empty */
165  }
166 
167  Dlink arc_list;
168 };
169 
196  template <typename __Arc_Info = Empty_Class>
197 struct Graph_Arc
198  : public Dlink,
199  public GTArcCommon<__Arc_Info>
200 {
201  friend class GTArcCommon<__Arc_Info>;
202 
203  using Base = GTArcCommon<__Arc_Info>;
204 
205  using Arc_Info = __Arc_Info;
206 
207  Arc_Node * src_arc_node = nullptr; // pointer to source node
208  Arc_Node * tgt_arc_node = nullptr; // pointer to target node
209 
219  Graph_Arc(const Arc_Info & info)
220  noexcept(noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value))
221  : Base(info)
222  {
223  /* empty */
224  }
225 
235  Graph_Arc(Arc_Info && info = Arc_Info())
236  noexcept(noexcept(std::is_nothrow_move_constructible<Arc_Info>::value))
237  : Base(move(info))
238  {
239  /* empty */
240  }
241 
242  Graph_Arc(const Graph_Arc & arc)
243  noexcept(noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value))
244  : Graph_Arc(arc.arc_info)
245  {
246  /* empty */
247  }
248 
249  Graph_Arc & operator = (const Graph_Arc & arc)
250  noexcept(std::is_nothrow_copy_constructible<Arc_Info>::value)
251  {
252  if (&arc == this)
253  return *this;
254  this->arc_info = arc.arc_info;
255  return *this;
256  }
257 };
258 
259 class Arc_Node : public Dlink
260 {
261 public:
262  void * arc = nullptr;
263  Arc_Node() noexcept : arc(nullptr) {}
264  Arc_Node(void * __arc) noexcept : arc(__arc) {}
265 };
266 
403  template <typename __Graph_Node = Graph_Node<unsigned long>,
404  typename __Graph_Arc = Graph_Arc<unsigned long>>
405 class List_Graph
406  : public GraphCommon<List_Graph<__Graph_Node, __Graph_Arc>,
407  __Graph_Node, __Graph_Arc>
408 {
409 public:
410 
411  using GT = List_Graph;
412 
413  using Node = __Graph_Node;
414  using Arc = __Graph_Arc;
415 
417  using Node_Type = typename Node::Node_Type;
418 
420  using Arc_Type = typename Arc::Arc_Type;
421 
422  friend class GraphCommon<List_Graph<__Graph_Node, __Graph_Arc>,
423  __Graph_Node, __Graph_Arc>;
424 
426  __Graph_Node, __Graph_Arc>;
427 
428  using CommonBase::insert_node;
429  using CommonBase::insert_arc;
430 
431 private:
432 
433  Dlink node_list; // lista de nodos
434  Dlink arc_list; // lista de arcos
435 
436  static Node * dlink_to_node(Dlink * p) noexcept { return (Node*) p; }
437 
438  static Arc * dlink_to_arc(Dlink * p) noexcept { return (Arc*) p; }
439 
440  static Arc_Node * dlink_to_arc_node(Dlink * p) noexcept
441  {
442  return (Arc_Node*) p;
443  }
444 
445  static Arc * void_to_arc(Arc_Node * arc_node) noexcept
446  {
447  return (Arc*) arc_node->arc;
448  }
449 
450 public:
451 
474  virtual Node * insert_node(Node * node) noexcept
475  {
476  this->num_nodes++;
477  node_list.append(node);
478  return node;
479  }
480 
493  virtual void remove_node(Node * node) noexcept
494  {
495  if (not this->digraph)
496  while (not node->arc_list.is_empty()) // remove each arc related to node
497  {
498  Arc_Node * arc_node = dlink_to_arc_node(node->arc_list.get_next());
499  Arc * arc = void_to_arc(arc_node);
500  remove_arc(arc);
501  }
502  else // Scan all the arcs and remove those related to node
503  for (Arc_Iterator it(*this); it.has_curr();)
504  {
505  Arc * a = it.get_curr_ne();
506  if (this->get_src_node(a) == node or this->get_tgt_node(a) == node)
507  {
508  it.next_ne();
509  remove_arc(a);
510  }
511  else
512  it.next_ne();
513  }
514 
515  // At this point the node has not more arcs
516  node->del(); // unlink it from arc_list
517  this->num_nodes--;
518  delete node;
519  }
520 
533  {
534  if (this->num_nodes == 0)
535  throw std::range_error("Graph has not nodes");
536 
537  return dlink_to_node(const_cast<Dlink&>(node_list).get_next());
538  }
539 
552  Arc * get_first_arc(Node * node) const
553  {
554  if (get_num_arcs(node) == 0)
555  throw std::range_error("node has not arcs");
556 
557  void * arc = dlink_to_arc_node(node->arc_list.get_next())->arc;
558  return reinterpret_cast <Arc *> (arc);
559  }
560 
561 private:
562 
563  Arc * insert_arc(Node * src_node, Node * tgt_node, void * a)
564  {
565  Arc * arc = (Arc *) a;
566  arc->src_node = src_node;
567  arc->tgt_node = tgt_node;
568 
569  // paso 3: (parcial): apartar Arc_Node de src_node
570  unique_ptr<Arc_Node> src_arc_node (new Arc_Node (arc));
571 
572  // paso 2: si es grafo ==> apartar Arc_Node de tgt_node
573  if (not this->digraph) // si es digrafo ==> no insertar en otro nodo
574  { // inserción en nodo destino
575  if (src_node == tgt_node) // ¿es un lazo?
576  arc->tgt_arc_node = src_arc_node.get();
577  else
578  { // apartar arco nodo para tgt_node
579  unique_ptr<Arc_Node> tgt_arc_node(new Arc_Node(arc));
580 
581  // inserción en lista de adyacencia de tgt_node
582  arc->tgt_arc_node = tgt_arc_node.get();
583  tgt_node->arc_list.append(tgt_arc_node.get());
584  tgt_node->num_arcs++;
585  tgt_arc_node.release();
586  }
587  }
588 
589  // paso 3 (resto): inserción en lista adyacencia src_node
590  arc->src_arc_node = src_arc_node.get();
591  src_node->arc_list.append(src_arc_node.get());
592  src_node->num_arcs++;
593 
594  arc_list.append(arc); //paso 4:insertar en lista arcos grafo
595  this->num_arcs++;
596  src_arc_node.release();
597 
598  return arc;
599  }
600 
601 public:
602 
608  virtual void remove_arc(Arc * arc) noexcept
609  { // paso 1: eliminar Arc_node de src_node
610  Node * src_node = this->get_src_node(arc);
611  Arc_Node * src_arc_node = arc->src_arc_node;
612 
613  src_arc_node->del(); // desenlaza src_node de la lista de nodos
614  src_node->num_arcs--; // decrementa contador de arcos de src_node
615  delete src_arc_node; // entrega memoria
616 
617  if (not this->digraph)
618  { // eliminación arco en nodo destino
619  Node * tgt_node = this->get_tgt_node(arc);
620  if (src_node != tgt_node) // verificar eliminación de ciclo
621  { // paso 2: eliminar Arc_node de tgt_node
622  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
623  tgt_arc_node->del();
624  tgt_node->num_arcs--;
625  delete tgt_arc_node;
626  }
627  }
628 
629  // eliminación de arco del grafo
630  arc->del(); // desenlazar arc de lista de arcos de grafo
631  this->num_arcs--;
632  delete arc;
633  }
634 
656  virtual void disconnect_arc(Arc * arc) noexcept
657  {
658  Node * src_node = this->get_src_node(arc);
659  Arc_Node * src_arc_node = arc->src_arc_node;
660  src_arc_node->del(); // desenlaza src_node de la lista de nodos
661  src_node->num_arcs--; // decrementa contador de arcos de src_node
662 
663  if (not this->digraph)
664  { // eliminación arco en nodo destino
665  Node * tgt_node = this->get_tgt_node(arc);
666  if (src_node != tgt_node) // verificar eliminación de ciclo
667  { // paso (2): eliminación del Arc_node del nodo destino tgt_node
668  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
669  tgt_arc_node->del();
670  tgt_node->num_arcs--;
671  }
672  }
673 
674  // eliminación de arco del grafo
675  arc->del(); // desenlazar arc de la lista de arcos del grafo
676  this->num_arcs--;
677  }
678 
689  virtual Arc * connect_arc(Arc * arc) noexcept
690  {
691  Node * src_node = this->get_src_node(arc);
692  Node * tgt_node = this->get_tgt_node(arc);
693  Arc_Node * src_arc_node = arc->src_arc_node;
694  Arc_Node * tgt_arc_node = arc->tgt_arc_node;
695 
696  if (not this->digraph) // si es digrafo ==> no hay que insertar en otro nodo
697  { // inserción en nodo destino
698  if (src_node != tgt_node) // verificar si se trata de un ciclo
699  { // inserción en lista de adyacencia de tgt_node
700  tgt_node->arc_list.append(tgt_arc_node);
701  tgt_node->num_arcs++;
702  }
703  }
704 
705  src_node->arc_list.append(src_arc_node);
706  src_node->num_arcs++;
707  arc_list.append(arc);
708  this->num_arcs++;
709 
710  return arc;
711  }
712 
724  Arc * get_first_arc() const
725  {
726  if (this->get_num_arcs() == 0)
727  throw std::range_error("Graph has not arcs");
728 
729  return dlink_to_arc(const_cast<Dlink&>(arc_list).get_next());
730  }
731 
753  template <class Compare> inline
754  void sort_nodes(Compare & cmp) noexcept
755  {
757  mergesort(node_list, c);
758  }
759 
761  template <class Compare> inline
762  void sort_nodes(Compare && cmp = Compare()) noexcept
763  {
764  sort_nodes(cmp);
765  }
766 
788  template <class Compare> inline
789  void sort_arcs(Compare & cmp) noexcept
790  {
792  mergesort(arc_list, c);
793  }
794 
796  template <class Compare> inline
797  void sort_arcs(Compare && cmp = Compare()) noexcept
798  {
799  sort_arcs(cmp);
800  }
801 
818  {
819  copy_graph(*this, g);
820  }
821 
831  List_Graph(List_Graph && g) noexcept
832  {
833  swap(g);
834  }
835 
845  List_Graph & operator = (const List_Graph & g)
846  {
847  if (this == &g)
848  return *this;
849 
850  copy_graph(*this, g);
851  return *this;
852  }
853 
859  List_Graph & operator = (List_Graph && g) noexcept
860  {
861  swap(g);
862  return *this;
863  }
864 
866  virtual ~List_Graph()
867  {
868  clear_graph(*this);
869  }
870 
877  struct Node_Iterator : public GTNodeIterator<List_Graph>
878  {
879  using Base = GTNodeIterator<List_Graph>;
880 
883  : Base(const_cast<Dlink&>(g.node_list))
884  {
885  // empty
886  }
887  };
888 
913 {
914  Node * src_node;
915 
916 public:
917 
918  using Item_Type = Arc *;
919 
920  using Set_Type = Node *;
921 
922  Node_Arc_Iterator() noexcept { /* empty */ }
923 
928  Node_Arc_Iterator(Node * src) noexcept
929  : Dlink::Iterator(&(src->arc_list)), src_node(src)
930  {
931  // empty
932  }
933 
934  Arc_Node * get_current_arc_node() const
935  {
936  return dlink_to_arc_node(Dlink::Iterator::get_curr());
937  }
938 
939  Arc_Node * get_current_arc_node_ne() const noexcept
940  {
941  return dlink_to_arc_node(Dlink::Iterator::get_curr_ne());
942  }
943 
945  Arc * get_curr() const
946  {
947  return static_cast<Arc*>(get_current_arc_node()->arc);
948  }
949 
951  Arc * get_curr_ne() const noexcept
952  {
953  return static_cast<Arc*>(get_current_arc_node_ne()->arc);
954  }
955 
958  Arc * get_current_arc() const { return get_curr(); }
959 
960  Arc * get_current_arc_ne() const noexcept { return get_curr_ne(); }
961 
965  Node * get_tgt_node() const
966  {
967  return (Node*) get_current_arc()->get_connected_node(src_node);
968  }
969 
970  Node * get_tgt_node_ne() const noexcept
971  {
972  return (Node*) get_current_arc_ne()->get_connected_node(src_node);
973  }
974 
975  Node * get_node() const { return get_tgt_node(); }
976 
977  Node * get_node_ne() const noexcept { return get_tgt_node_ne(); }
978 };
979 
989  {
990  using Item_Type = Arc *;
991 
993 
994  Arc_Iterator() noexcept { /* empty */ }
995 
997  Arc_Iterator(const List_Graph & g) noexcept
998  : Dlink::Iterator(&const_cast<Dlink&>(g.arc_list))
999  {
1000  // empty
1001  }
1002 
1004  Arc * get_current_arc_ne() const noexcept
1005  {
1006  return dlink_to_arc(const_cast<Dlink*>(Dlink::Iterator::get_curr_ne()));
1007  }
1008 
1009  Arc * get_current_arc() const
1010  {
1011  return dlink_to_arc(const_cast<Dlink*>(Dlink::Iterator::get_curr()));
1012  }
1013 
1014  Arc * get_curr() const { return get_current_arc(); }
1015 
1016  Arc * get_curr_ne() const noexcept { return get_current_arc_ne(); }
1017 
1021  Node * get_src_node() const
1022  {
1023  return (Node*) get_current_arc()->src_node;
1024  }
1025 
1026  Node * get_src_node_ne() const
1027  {
1028  return (Node*) get_current_arc_ne()->src_node;
1029  }
1030 
1034  Node * get_tgt_node() const { return (Node*) get_current_arc()->tgt_node; }
1035 
1036  Node * get_tgt_node_ne() const
1037  {
1038  return (Node*) get_current_arc_ne()->tgt_node;
1039  }
1040  };
1041 
1043  List_Graph() noexcept { /* empty */ }
1044 
1046  void swap(List_Graph & g) noexcept
1047  {
1048  this->common_swap(g);
1049  node_list.swap(&g.node_list);
1050  arc_list.swap(&g.arc_list);
1051  }
1052 };
1053 
1054 
1062  template <class GT>
1064 {
1065  bool operator () (typename GT::Arc * /* arc */) const noexcept
1066  {
1067  return true;
1068  }
1069 
1070  void set_cookie(void*) noexcept { /* empty */ }
1071 };
1072 
1176  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1178  public Filter_Iterator<typename GT::Node*,
1179  typename GT::Node_Arc_Iterator,
1180  Show_Arc>
1181 {
1182  using Filter_Itor = Filter_Iterator<typename GT::Node*,
1183  typename GT::Node_Arc_Iterator,
1184  Show_Arc>;
1185 
1186  using Itor = Filter_Iterator<typename GT::Node*,
1187  typename GT::Node_Arc_Iterator,
1188  Show_Arc>;
1189 
1190  using Item_Type = typename Itor::Item_Type;
1191  using Set_Type = typename Itor::Set_Type;
1192 
1193  Node_Arc_Iterator() noexcept { /* empty */ }
1194 
1200  Node_Arc_Iterator(typename GT::Node * p, Show_Arc sa = Show_Arc())
1201  noexcept(noexcept(Itor(p, sa)))
1202  : Itor(p, sa)
1203  {
1204  // empty
1205  }
1206 };
1207 
1224  template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1225 struct Arc_Iterator :
1226  public Filter_Iterator<GT, typename GT::Arc_Iterator, Show_Arc>
1227 {
1229 
1230  using Item_Type = typename Itor::Item_Type;
1231  using Set_Type = typename Itor::Set_Type;
1232 
1233  Arc_Iterator() noexcept { /* empty */ }
1234 
1240  Arc_Iterator(const GT & g, Show_Arc sa = Show_Arc())
1241  noexcept(noexcept(Itor(g, sa)))
1242  : Itor(g, sa)
1243  {
1244  // empty
1245  }
1246 };
1247 
1248 
1249 
1256  template <class GT>
1258 {
1259  bool operator () (typename GT::Node *) const noexcept
1260  {
1261  return true;
1262  }
1263 };
1264 
1269  template <class GT, class Show_Node = Dft_Show_Node<GT>>
1271  public Filter_Iterator<GT, typename GT::Node_Iterator, Show_Node>
1272 {
1273 public:
1274 
1276 
1277  using Item_Type = typename Itor::Item_Type;
1278 
1279  using Set_Type = typename Itor::Set_Type;
1280 
1281  Node_Iterator() noexcept { /* empty */ }
1282 
1288  Node_Iterator(const GT & g, Show_Node sn = Show_Node())
1289  noexcept(noexcept(Itor(g, sn)))
1290  : Itor (g, sn)
1291  {
1292  /* empty */
1293  }
1294 };
1295 
1305 template <class GT, class SN = Dft_Show_Node<GT>>
1306 void for_each_node(const GT & g,
1307  std::function<void(typename GT::Node*)> operation,
1308  SN sn = SN())
1309  noexcept(noexcept(operation) and noexcept(sn))
1310 {
1311  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1312  operation(it.get_curr_ne());
1313 }
1314 
1324 template <class GT, class SA = Dft_Show_Arc<GT>>
1325 void for_each_arc(const GT & g,
1326  std::function<void(typename GT::Arc*)> operation,
1327  SA sa = SA())
1328  noexcept(noexcept(operation) and noexcept(sa))
1329 {
1330  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1331  operation(it.get_curr_ne());
1332 }
1333 
1344 template <class GT, class SA = Dft_Show_Arc<GT>>
1345 void for_each_arc(const GT&,
1346  typename GT::Node * p,
1347  std::function<void(typename GT::Arc*)> operation,
1348  SA sa = SA())
1349  noexcept(noexcept(operation) and noexcept(sa))
1350 {
1351  for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1352  operation(it.get_curr_ne());
1353 }
1354 
1365 template <class GT, class SN = Dft_Show_Node<GT>>
1366 bool forall_node(const GT & g,
1367  std::function<bool(typename GT::Node*)> cond,
1368  SN sn = SN())
1369  noexcept(noexcept(cond) and noexcept(sn))
1370 {
1371  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
1372  if (not cond(it.get_curr_ne()))
1373  return false;
1374  return true;
1375 }
1376 
1387 template <class GT, class SA = Dft_Show_Arc<GT>>
1388 bool forall_arc(const GT & g,
1389  std::function<bool(typename GT::Arc*)> cond,
1390  SA sa = SA())
1391  noexcept(noexcept(cond) and noexcept(sa))
1392 {
1393  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
1394  if (not cond(it.get_curr_ne()))
1395  return false;
1396  return true;
1397 }
1398 
1409 template <class GT, class SA = Dft_Show_Arc<GT>>
1410 bool forall_arc(typename GT::Node * p,
1411  std::function<bool(typename GT::Arc*)> cond,
1412  SA sa = SA())
1413  noexcept(noexcept(cond) and noexcept(sa))
1414 {
1415  for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1416  if (not cond(it.get_curr_ne()))
1417  return false;
1418  return true;
1419 }
1420 
1421 
1432 template <class GT, typename T,
1433  template <typename> class Container = DynList,
1434  class SN = Dft_Show_Node<GT>>
1435 Container<T> nodes_map(GT & g,
1436  std::function<T(typename GT::Node *)> transformation,
1437  SN sn = SN())
1438 {
1439  Container<T> ret_val;
1440  for_each_node<GT, SN>(g, [&ret_val, &transformation] (typename GT::Node * p)
1441  {
1442  ret_val.append(transformation(p));
1443  }, sn);
1444  return ret_val;
1445 }
1446 
1456 template <class GT, typename T,
1457  template <typename> class Container = DynList,
1458  class SA = Dft_Show_Arc<GT>>
1459 Container<T> arcs_map(GT & g,
1460  std::function<T(typename GT::Arc *)> transformation,
1461  SA sa = SA())
1462 {
1463  Container<T> ret_val;
1464  for_each_arc<GT, SA>(g, [&ret_val, &transformation] (typename GT::Arc * p)
1465  {
1466  ret_val.append(transformation(p));
1467  }, sa);
1468  return ret_val;
1469 }
1470 
1481 template <class GT, typename T,
1482  template <typename> class Container = DynList,
1483  class SA = Dft_Show_Arc<GT>>
1484 Container<T> arcs_map(GT & g,
1485  typename GT::Node * p,
1486  std::function<T(typename GT::Arc *)> transformation,
1487  SA sa = SA())
1488 {
1489  Container<T> ret_val;
1490  for_each_arc<GT, SA>(g, p, [&ret_val, &transformation] (typename GT::Arc * p)
1491  {
1492  ret_val.append(transformation(p));
1493  }, sa);
1494  return ret_val;
1495 }
1496 
1507 template <class GT, typename T, class SN = Dft_Show_Node<GT>>
1508 T foldl_nodes(GT & g, const T & init,
1509  std::function<T(const T&, typename GT::Node*)> operation,
1510  SN sn = SN())
1511  noexcept(noexcept(operation) and noexcept(sn) and
1512  std::is_nothrow_copy_assignable<T>::value)
1513 {
1514  T ret_val = init;
1515  for_each_node<GT, SN>(g, [&ret_val, &operation] (typename GT::Node * p)
1516  {
1517  ret_val = operation(ret_val, p);
1518  }, sn);
1519  return ret_val;
1520 }
1521 
1531 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1532 T foldl_arcs(GT & g, const T & init,
1533  std::function<T(const T&, typename GT::Arc*)> operation,
1534  SA sa = SA())
1535  noexcept(noexcept(operation) and noexcept(sa) and
1536  std::is_nothrow_copy_assignable<T>::value)
1537 {
1538  T ret_val = init;
1539  for_each_arc<GT, SA>(g, [&ret_val, &operation] (typename GT::Arc* a)
1540  {
1541  ret_val = operation(ret_val, a);
1542  }, sa);
1543  return ret_val;
1544 }
1545 
1556 template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
1557 T foldl_arcs(GT & g, typename GT::Node * p,
1558  const T & init,
1559  std::function<T(const T&, typename GT::Arc*)> operation,
1560  SA sa = SA())
1561  noexcept(noexcept(operation) and noexcept(sa) and
1562  std::is_nothrow_copy_assignable<T>::value)
1563 {
1564  T ret_val = init;
1565  for_each_arc<GT, SA>(g, p, [&ret_val, &operation] (typename GT::Arc* a)
1566  {
1567  ret_val = operation(ret_val, a);
1568  }, sa);
1569  return ret_val;
1570 }
1571 
1581 template <typename __Graph_Node = Graph_Node<int>,
1582  typename __Graph_Arc = Graph_Arc<int>>
1583 class List_Digraph : public List_Graph<__Graph_Node, __Graph_Arc>
1584 {
1585 public:
1586 
1588 
1589  List_Digraph() noexcept
1590  {
1591  this->digraph = true;
1592  }
1593 
1594  List_Digraph(const List_Digraph & dg) : GT()
1595  {
1596  this->digraph = true;
1597  *this = dg;
1598  }
1599 
1600  List_Digraph(List_Digraph && dg) : GT()
1601  {
1602  this->digraph = true;
1603  this->swap(dg);
1604  }
1605 
1606  List_Digraph & operator = (const List_Digraph & g)
1607  {
1608  if (this == &g)
1609  return *this;
1610 
1611  copy_graph(*this, (const List_Digraph &) g);
1612  return *this;
1613  }
1614 
1615  List_Digraph & operator = (List_Digraph && g)
1616  {
1617  this->swap(g);
1618  return *this;
1619  }
1620 };
1621 
1622 
1628 template <class GT>
1629 using ArcPair = tuple<typename GT::Arc*, typename GT::Node*>;
1630 
1638 template <class GT> class __Out_Filt
1639 {
1640  typename GT::Node * src = nullptr;
1641 
1642 public:
1643 
1644  __Out_Filt(typename GT::Node * __src) noexcept : src(__src) { /* empty */ }
1645 
1646  bool operator () (typename GT::Arc * a) const noexcept
1647  {
1648  assert(src);
1649  return a->src_node == src;
1650  }
1651 
1652  typename GT::Node * get_node(typename GT::Arc * a) const noexcept
1653  {
1654  assert(src);
1655  return (typename GT::Node *) a->tgt_node;
1656  }
1657 };
1658 
1659 
1667 template <class GT> class __In_Filt
1668 {
1669  typename GT::Node * tgt = nullptr;
1670 
1671 public:
1672 
1673  __In_Filt(typename GT::Node * __tgt) noexcept : tgt(__tgt) { /* empty */ }
1674 
1675  bool operator () (typename GT::Arc * a) const noexcept
1676  {
1677  assert(tgt);
1678  return a->tgt_node == tgt;
1679  }
1680 
1681  typename GT::Node * get_node(typename GT::Arc * a) const noexcept
1682  {
1683  assert(tgt);
1684  return (typename GT::Node *) a->src_node;
1685  }
1686 };
1687 
1692 template <class GT, class Filter>
1694 {
1695  using Itor = Filter_Iterator<typename GT::Node*,
1696  typename GT::Node_Arc_Iterator, Filter>;
1697 
1698  Filter filt;
1699  Itor it;
1700 
1701 public:
1702 
1703  using Item_Type = typename Itor::Item_Type;
1704 
1706 
1708  Digraph_Iterator(typename GT::Node * p)
1709  noexcept(noexcept(Filter(p)) and noexcept(Itor(p, filt)))
1710  : filt(p), it(p, filt)
1711  {
1712  // empty
1713  }
1714 
1717  void next() { it.next(); }
1718 
1719  void next_ne() noexcept { it.next_ne(); }
1720 
1723  void prev() { it.next(); }
1724 
1726  bool has_curr() const noexcept { return it.has_curr(); }
1727 
1730  typename GT::Arc * get_curr() const { return it.get_curr(); }
1731 
1734  typename GT::Arc * get_curr_ne() const noexcept { return it.get_curr_ne(); }
1735 
1737  auto get_current_arc() const { return get_curr(); }
1738 
1741  typename GT::Node * get_node(typename GT::Arc * a) const noexcept
1742  {
1743  return filt.get_node(a);
1744  }
1745 
1747  typename GT::Node * get_node() const
1748  {
1749  return this->get_node(this->get_curr());
1750  }
1751 
1753  auto get_tgt_node() const { return get_node(); }
1754 
1756  typename GT::Node * get_node_ne() const noexcept
1757  {
1758  return this->get_node(this->get_curr_ne());
1759  }
1760 
1762  auto get_tgt_node_ne() const noexcept { return get_node_ne(); }
1763 
1765  void reset_first() noexcept { it.reset_first(); }
1766 
1768  void reset_last() noexcept { it.reset_last(); }
1769 
1771  void end() noexcept { put_itor_at_the_end(*this); }
1772 };
1773 
1774 
1779 template <class GT>
1780 using __Out_Iterator = typename GT::Out_Iterator;
1781 
1782 
1787 template <class GT>
1788 using __In_Iterator = typename GT::In_Iterator;
1789 
1790 
1795 template <class GT, class Show_Arc = Dft_Show_Arc<GT>>
1796 struct Out_Iterator : public
1797  Filter_Iterator<typename GT::Node*, typename GT::Out_Iterator, Show_Arc>
1798 {
1799  using Base =
1801  using Base::Base;
1802 };
1803 
1804 
1809 template <class GT, class SA = Dft_Show_Arc<GT>>
1810 class In_Iterator : public
1811  Filter_Iterator<typename GT::Node*, typename GT::In_Iterator, SA>
1812 {
1813  using Base =
1815  using Base::Base;
1816 };
1817 
1818 
1828 template <class GT, class SA = Dft_Show_Arc<GT>>
1829 DynList<typename GT::Node*> out_nodes(typename GT::Node * p, SA sa = SA())
1830 {
1832  for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1833  ret.append(it.get_node_ne());
1834  return ret;
1835 }
1836 
1845 template <class GT, class SA = Dft_Show_Arc<GT>>
1846 DynList<typename GT::Node*> in_nodes(typename GT::Node * p, SA sa = SA())
1847 {
1849  for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1850  ret.append(it.get_node_ne());
1851  return ret;
1852 }
1853 
1861 template <class GT, class SA = Dft_Show_Arc<GT>>
1862 DynList<typename GT::Arc*> out_arcs(typename GT::Node * p, SA sa = SA())
1863 {
1865  for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1866  ret.append(it.get_curr_ne());
1867  return ret;
1868 }
1869 
1877 template <class GT, class SA = Dft_Show_Arc<GT>>
1878 DynList<typename GT::Arc*> in_arcs(typename GT::Node * p, SA sa = SA())
1879 {
1881  for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1882  ret.append(it.get_curr_ne());
1883  return ret;
1884 }
1885 
1892 template <class GT, class SA = Dft_Show_Arc<GT>>
1893 DynList<typename GT::Arc*> arcs(typename GT::Node * p, SA sa = SA())
1894 {
1896  for (Node_Arc_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1897  ret.append(it.get_curr_ne());
1898  return ret;
1899 }
1900 
1907 template <class GT, class SA = Dft_Show_Arc<GT>>
1908 DynList<ArcPair<GT>> in_pairs(typename GT::Node * p, SA sa = SA())
1909 {
1910  DynList<ArcPair<GT>> ret;
1911  for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1912  {
1913  typename GT::Arc * a = it.get_curr_ne();
1914  ret.append(make_tuple(a, (typename GT::Node*) a->get_connected_node(p)));
1915  }
1916  return ret;
1917 }
1918 
1926 template <class GT, class SA = Dft_Show_Arc<GT>>
1927 DynList<ArcPair<GT>> out_pairs(typename GT::Node * p, SA sa = SA())
1928 {
1929  DynList<ArcPair<GT>> ret;
1930  for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1931  {
1932  typename GT::Arc * a = it.get_curr_ne();
1933  ret.append(make_tuple(a, (typename GT::Node*) a->get_connected_node(p)));
1934  }
1935  return ret;
1936 }
1937 
1947 template <class GT, class SA = Dft_Show_Arc<GT>>
1948 size_t in_degree(typename GT::Node * p, SA sa = SA())
1949 {
1950  size_t count = 0;
1951  for (In_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1952  ++count;
1953  return count;
1954 }
1955 
1956 template <class GT>
1957 size_t in_degree(typename GT::Node * p)
1958 {
1959  size_t count = 0;
1960  for (__In_Iterator<GT> it(p); it.has_curr(); it.next_ne())
1961  ++count;
1962  return count;
1963 }
1964 
1975 template <class GT, class SA = Dft_Show_Arc<GT>>
1976 size_t out_degree(typename GT::Node * p, SA sa = SA())
1977 {
1978  size_t count = 0;
1979  for (Out_Iterator<GT, SA> it(p, sa); it.has_curr(); it.next_ne())
1980  ++count;
1981  return count;
1982 }
1983 
1984 template <class GT>
1985 size_t out_degree(typename GT::Node * p)
1986 {
1987  size_t count = 0;
1988  for (__Out_Iterator<GT> it(p); it.has_curr(); it.next_ne())
1989  ++count;
1990  return count;
1991 }
1992 
1993 
2016 template <class GT, class Itor, class Operation> inline
2017 bool traverse_arcs(typename GT::Node * p, Operation op = Operation())
2018  noexcept(noexcept(op))
2019 {
2020  for (Itor it(p); it.has_curr(); it.next_ne())
2021  if (not op(it.get_curr_ne()))
2022  return false;
2023  return true;
2024 }
2025 
2043 template <class GT, class Itor, class Operation> inline
2044 void for_each_arc(typename GT::Node * p, Operation op = Operation())
2045  noexcept(noexcept(op))
2046 {
2047  for (Itor it(p); it.has_curr(); it.next_ne())
2048  op(it.get_curr_ne());
2049 }
2050 
2051 
2052  // Functional operations on input arcs
2053 
2063 template <class GT, class Op> inline
2064 bool traverse_in_arcs(typename GT::Node * p, Op op = Op())
2065  noexcept(noexcept(op))
2066 {
2067  return traverse_arcs<GT, __In_Iterator<GT>, Op>(p, op);
2068 }
2069 
2076 template <class GT, class Op> inline
2077 void for_each_in_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2078 {
2079  for_each_arc<GT, __In_Iterator<GT>, Op>(p, op);
2080 }
2081 
2092 template <class GT, class Op> inline
2093 bool all_in_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2094 {
2095  return traverse_in_arcs(p, [&op] (auto a) { return op(a); });
2096 }
2097 
2108 template <class GT, class Op> inline
2109 bool exists_in_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2110 {
2111  return not traverse_in_arcs<GT, Op>(p, [&op] (auto a) { return not op(a); });
2112 }
2113 
2125 template <class GT, class Op> inline
2126 auto search_in_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2127 {
2128  typename GT::Arc * ret = nullptr;
2129  traverse_in_arcs(p, [&op, &ret] (auto a)
2130  {
2131  if (op(a))
2132  {
2133  ret = a;
2134  return false;
2135  }
2136  return true;
2137  });
2138  return ret;
2139 }
2140 
2154 template <class GT, typename T> inline
2155 auto map_in_arcs(typename GT::Node * p, std::function<T(typename GT::Arc*)> op)
2156  noexcept(noexcept(op))
2157 {
2158  DynList<T> ret;
2159  for_each_in_arc(p, [&ret, &op] (auto a) { ret.append(op(a)); });
2160  return ret;
2161 }
2162 
2179 template <class GT, typename T> inline
2180 T foldl_in_arcs(typename GT::Node * p, const T & init,
2181  std::function<T(const T&, typename GT::Arc*)> op)
2182  noexcept(noexcept(op))
2183 {
2184  T ret = init;
2185  for_each_in_arc(p, [&ret, &op] (auto a) { ret = op(ret, a); });
2186  return ret;
2187 }
2188 
2195 template <class GT, class Op> inline
2196 DynList<typename GT::Arc*> filter_in_arcs(typename GT::Node * p, Op cond)
2197  noexcept(noexcept(cond))
2198 {
2200  for_each_in_arc(p, [&ret, &cond] (auto a)
2201  {
2202  if (cond(a))
2203  ret.append(a);
2204  });
2205  return ret;
2206 }
2207 
2208 
2209  // Functional operation on output arcs
2210 
2220 template <class GT, class Op> inline
2221 bool traverse_out_arcs(typename GT::Node * p, Op op = Op())
2222  noexcept(noexcept(op))
2223 {
2224  return traverse_arcs<GT, __Out_Iterator<GT>, Op>(p, op);
2225 }
2226 
2233 template <class GT, class Op> inline
2234 void for_each_out_arc(typename GT::Node * p, Op op = Op())
2235  noexcept(noexcept(op))
2236 {
2237  for_each_arc<GT, __Out_Iterator<GT>, Op>(p, op);
2238 }
2239 
2250 template <class GT, class Op> inline
2251 bool all_out_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2252 {
2253  return traverse_out_arcs(p, [&op] (auto a) { return op(a); });
2254 }
2255 
2266 template <class GT, class Op> inline
2267 bool exists_out_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2268 {
2269  return not traverse_out_arcs<GT, Op>(p, [&op] (auto a) { return not op(a); });
2270 }
2271 
2283 template <class GT, class Op> inline
2284 auto search_out_arc(typename GT::Node * p, Op op = Op()) noexcept(noexcept(op))
2285 {
2286  typename GT::Arc * ret = nullptr;
2287  __traverse_out_arcs(p, [&op, &ret] (auto a)
2288  {
2289  if (op(a))
2290  {
2291  ret = a;
2292  return false;
2293  }
2294  return true;
2295  });
2296  return ret;
2297 }
2298 
2312 template <class GT, typename T> inline
2313 auto map_out_arcs(typename GT::Node * p, std::function<T(typename GT::Arc*)> op)
2314  noexcept(noexcept(op))
2315 {
2316  DynList<T> ret;
2317  for_each_out_arc(p, [&ret, &op] (auto a) { ret.append(op(a)); });
2318  return ret;
2319 }
2320 
2337 template <class GT, typename T> inline
2338 T foldl_out_arcs(typename GT::Node * p, const T & init,
2339  std::function<T(const T&, typename GT::Arc*)> op)
2340  noexcept(noexcept(op))
2341 {
2342  T ret = init;
2343  for_each_out_arc(p, [&ret, &op] (auto a) { ret = op(ret, a); });
2344  return ret;
2345 }
2346 
2353 template <class GT, class Op> inline
2354 DynList<typename GT::Arc*> filter_out_arcs(typename GT::Node * p, Op cond)
2355  noexcept(noexcept(cond))
2356 {
2358  for_each_out_arc(p, [&ret, &cond] (auto a)
2359  {
2360  if (cond(a))
2361  ret.append(a);
2362  });
2363  return ret;
2364 }
2365 
2379  template <class GT, class SA = Dft_Show_Arc<GT>>
2380  typename GT::Arc *
2381 search_arc(const GT & g,
2382  typename GT::Node * src, typename GT::Node * tgt,
2383  SA sa = SA()) noexcept
2384 {
2385  assert(src != nullptr and tgt != nullptr);
2386 
2387  if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
2388  std::swap(tgt, src); // select the node with less arcs
2389 
2390  for (Node_Arc_Iterator<GT, SA> itor(src, sa); itor.has_curr(); itor.next_ne())
2391  if (itor.get_tgt_node_ne() == tgt)
2392  return itor.get_current_arc_ne();
2393 
2394  return nullptr;
2395 }
2396 
2406 template <class GT, class SA = Dft_Show_Arc<GT>>
2407  typename GT::Arc *
2409  typename GT::Node * src, typename GT::Node * tgt,
2410  SA sa = SA()) noexcept
2411 {
2412  assert(src != nullptr and tgt != nullptr);
2413 
2414  for (Node_Arc_Iterator<GT, SA> it(src, sa); it.has_curr(); it.next_ne())
2415  {
2416  typename GT::Arc * a = it.get_curr_ne();
2417  if (a->src_node == src and a->tgt_node == tgt)
2418  return a;
2419  }
2420 
2421  return nullptr;
2422 }
2423 
2428  template <class GT>
2429 typename GT::Node * mapped_node(typename GT::Node * p) noexcept
2430 {
2431  return (typename GT::Node *) NODE_COOKIE(p);
2432 }
2433 
2435  template <class GT>
2436 typename GT::Arc * mapped_arc(typename GT::Arc * a) noexcept
2437 {
2438  return (typename GT::Arc *) ARC_COOKIE(a);
2439 }
2440 
2442  template <class GTSRC, class GTTGT>
2443 typename GTTGT::Node * mapped_node(typename GTSRC::Node * p) noexcept
2444 {
2445  return (typename GTTGT::Node *) NODE_COOKIE(p);
2446 }
2447 
2449  template <class GTSRC, class GTTGT>
2450 typename GTTGT::Arc * mapped_arc(typename GTSRC::Arc * a) noexcept
2451 {
2452  return (typename GTTGT::Arc *) ARC_COOKIE(a);
2453 }
2454 
2472  template <class GT> inline
2473 void copy_graph(GT & gtgt, const GT & gsrc, const bool cookie_map = false);
2474 
2480 template <class GT> inline void clear_graph(GT & g) noexcept;
2481 
2492  template <class GT, class Operation, class SN = Dft_Show_Node<GT>>
2494 {
2495  SN sn;
2496 
2497 public:
2498 
2500  Operate_On_Nodes(SN __sn = SN())
2501  noexcept(std::is_nothrow_constructible<SN>::value)
2502  : sn(__sn) { /* empty */ }
2503 
2509  void operator () (const GT & g, Operation op = Operation())
2510  noexcept(noexcept(op))
2511  {
2512  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
2513  op (g, it.get_curr_ne());
2514  }
2515 
2521  void operator () (GT & g, Operation op = Operation()) noexcept(noexcept(op))
2522  {
2523  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
2524  op (g, it.get_curr_ne());
2525  }
2526 };
2527 
2528 
2539  template <class GT, class Operation, class SA = Dft_Show_Arc<GT>>
2541 {
2542  SA sa;
2543 
2544 public:
2545 
2547  Operate_On_Arcs(SA __sa = SA())
2548  noexcept(std::is_nothrow_constructible<SA>::value)
2549  : sa(__sa) { /* empty */ }
2550 
2556  void operator () (const GT & g, Operation op = Operation()) const
2557  noexcept(noexcept(op))
2558  {
2559  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2560  op (g, it.get_curr_ne());
2561  }
2562 
2563  void operator () (GT & g, Operation op = Operation()) const
2564  noexcept(noexcept(op))
2565  {
2566  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
2567  op (g, it.get_curr_ne());
2568  }
2569 
2576  void operator () (const GT & g, typename GT::Node * p,
2577  Operation op = Operation()) const noexcept(noexcept(op))
2578  {
2579  for (Node_Arc_Iterator<GT, SA> it(p); it.has_curr(); it.next_ne())
2580  op (g, it.get_current_arc_ne());
2581  }
2582 
2583  void operator () (GT & g, typename GT::Node * p,
2584  Operation op = Operation()) const
2585  noexcept(noexcept(op))
2586  {
2587  for (Node_Arc_Iterator<GT, SA> it(p); it.has_curr(); it.next_ne())
2588  op (g, it.get_current_arc_ne());
2589  }
2590 };
2591 
2631  template <class GT>
2632 class Path
2633 {
2634 public:
2635 
2637  using Node_Type = typename GT::Node_Type;
2638 
2640  using Arc_Type = typename GT::Arc_Type;
2641 
2642 private:
2643 
2644  const GT * g = nullptr;
2645 
2646  using Node = typename GT::Node;
2647  using Arc = typename GT::Arc;
2648 
2649  struct Path_Desc
2650  {
2651  Node * node; // nodo origen
2652  Arc * arc; // arco adyacente
2653 
2654  Path_Desc(Node * _node = nullptr, Arc * _arc = nullptr) noexcept
2655  : node(_node), arc(_arc) {}
2656 
2657  bool operator == (const Path_Desc & r) const noexcept
2658  {
2659  if (not (node->get_info() == r.node->get_info()))
2660  return false;
2661 
2662  if (arc == nullptr)
2663  return r.arc == nullptr;
2664 
2665  if (r.arc == nullptr)
2666  return false;
2667 
2668  return arc->get_info() == r.arc->get_info();
2669  }
2670  };
2671 
2673 
2674  void check_graph()
2675  {
2676  if (g == nullptr)
2677  throw std::domain_error("Path: Graph has not been specified");
2678  }
2679 
2680 public:
2681 
2683  bool check() const
2684  {
2685  auto l = list;
2686  while (not l.is_unitarian_or_empty())
2687  {
2688  auto d = l.remove_first();
2689  auto nxt = l.get_first().node;
2690  if (nxt == g->get_connected_node(d.arc, d.node))
2691  return false;
2692  }
2693 
2694  return true;
2695  }
2696 
2698  bool check_directed() const
2699  {
2700  return list.all([this] (auto d) { return g->get_src_node(d.arc) == d.node; })
2701  and list.get_last().arc == nullptr;
2702  }
2703 
2705  const GT & get_graph() const noexcept { return *g; }
2706 
2708  bool inside_graph(const GT & gr) const noexcept { return g == &gr; }
2709 
2711  Path(const GT & __g) noexcept : g(&__g) {}
2712 
2713  Path() noexcept : g(nullptr) { /* empty */ }
2714 
2722  void init(Node * start_node) { list.append(Path_Desc(start_node)); }
2723 
2730  Path(const GT & _g, Node * start_node) : g(&_g) { init(start_node); }
2731 
2745  void set_graph(const GT & __g, Node * start_node = nullptr)
2746  {
2747  empty();
2748  g = &__g;
2749  if (start_node == nullptr)
2750  return;
2751  init(start_node);
2752  }
2753 
2755  size_t size() const noexcept { return list.size(); }
2756 
2758  bool is_empty() const noexcept { return list.is_empty(); }
2759 
2761  void empty()
2762  {
2763  check_graph();
2764  while (not list.is_empty())
2765  list.remove_first();
2766  }
2767 
2769  Path(const Path & path) : g(path.g), list(path.list) {}
2770 
2772  Path(Path && path) : g(path.g), list(move(path.list)) {}
2773 
2775  Path & operator = (const Path & path)
2776  {
2777  if (this == &path)
2778  return *this;
2779 
2780  empty();
2781  g = path.g;
2782  list = path.list;
2783  return *this;
2784  }
2785 
2787  Path & operator = (Path && path)
2788  {
2789  empty();
2790  std::swap(g, path.g);
2791  std::swap(list, path.list);
2792  return *this;
2793  }
2794 
2808  void append(Arc * arc)
2809  {
2810  check_graph();
2811 
2812  if (list.is_empty())
2813  throw std::domain_error("path is empty");
2814 
2815  auto & last_path_desc = list.get_last();
2816  auto last_node = last_path_desc.node;
2817  if (arc->src_node != last_node and arc->tgt_node != last_node)
2818  throw std::invalid_argument("arc has not link to last node of path");
2819 
2820  last_path_desc.arc = arc;
2821  list.append(Path_Desc(g->get_connected_node(arc, last_node)));
2822  }
2823 
2841  void append(Node * node)
2842  {
2843  check_graph();
2844 
2845  if (list.is_empty())
2846  {
2847  init(node);
2848  return;
2849  }
2850 
2851  Node * last_node = get_last_node();
2852  Arc * arc = search_arc(*g, last_node, node);
2853 
2854  if (arc == nullptr)
2855  throw std::invalid_argument("There is no an arc connecting to the node");
2856 
2857  append(arc);
2858  }
2859 
2880  void append_directed(Node * p)
2881  {
2882  check_graph();
2883  if (list.is_empty())
2884  {
2885  init(p);
2886  return;
2887  }
2888 
2889  auto & last_path_desc = list.get_last();
2890  Node * last_node = last_path_desc.node;
2891  Arc * arc = search_directed_arc(*g, last_node, p);
2892 
2893  if (arc == nullptr)
2894  throw std::invalid_argument("There is no an arc connecting to the node");
2895 
2896  assert(arc->src_node == last_path_desc.node);
2897 
2898  last_path_desc.arc = arc;
2899  list.append(Path_Desc(static_cast<Node*>(arc->tgt_node)));
2900  }
2901 
2920  void append_directed(Arc * arc)
2921  {
2922  check_graph();
2923 
2924  if (list.is_empty())
2925  throw std::domain_error("path is empty");
2926 
2927  auto & last_path_desc = list.get_last();
2928  Node * last_node = last_path_desc.node;
2929  if (arc->src_node != last_node)
2930  throw std::invalid_argument("The arc does not connect the last node");
2931 
2932  last_path_desc.arc = arc;
2933  list.append(Path_Desc(static_cast<Node*>(arc->tgt_node)));
2934  }
2935 
2951  void insert(Arc * arc)
2952  {
2953  check_graph();
2954 
2955  if (list.is_empty())
2956  throw std::domain_error("path is empty");
2957 
2958  auto & first_path_desc = list.get_first();
2959  auto first_node = first_path_desc.node;
2960  if (arc->src_node != first_node and arc->tgt_node != first_node)
2961  throw std::invalid_argument("arc has not link to first node of path");
2962 
2963  Path_Desc item(g->get_connected_node(arc, first_node), arc);
2964  list.insert(item);
2965  }
2966 
2982  void insert(Node * node)
2983  {
2984  check_graph();
2985 
2986  if (list.is_empty())
2987  {
2988  init(node);
2989  return;
2990  }
2991 
2992  Node * first_node = get_first_node();
2993  Arc * arc = search_arc(*g, node, first_node); // busque arco first_node-node
2994  if (arc == nullptr)
2995  throw std::domain_error("There is no arc connecting node");
2996 
2997  Path_Desc item(node, arc);
2998  list.insert(item);
2999  }
3000 
3019  void insert_directed(Node * p)
3020  {
3021  check_graph();
3022 
3023  if (list.is_empty())
3024  {
3025  init(p);
3026  return;
3027  }
3028 
3029  Node * first_node = get_first_node();
3030  Arc * arc = search_directed_arc(*g, p, first_node);
3031  if (arc == nullptr)
3032  throw std::domain_error("There is no an arc connecting to the node");
3033 
3034  list.insert(Path_Desc(p, arc));
3035  }
3036 
3052  void insert_directed(Arc * arc)
3053  {
3054  check_graph();
3055 
3056  if (list.is_empty())
3057  throw std::domain_error("path is empty");
3058 
3059  auto & first_path_desc = list.get_first();
3060  Node * first_node = first_path_desc.node;
3061  if (arc->tgt_node != first_node)
3062  throw std::invalid_argument("The arc does not connect the first node");
3063 
3064  list.insert(Path_Desc(static_cast<Node*>(arc->src_node), arc));
3065  }
3066 
3069  Node * get_first_node() const { return list.get_first().node; }
3070 
3073  Node * get_last_node() const
3074  {
3075  auto & last_path_desc = list.get_last();
3076  assert(last_path_desc.arc == nullptr);
3077  return last_path_desc.node;
3078  }
3079 
3082  Arc * get_first_arc() const { return list.get_first().arc; }
3083 
3086  Arc * get_last_arc() const
3087  {
3088  if (list.is_unitarian())
3089  throw std::domain_error("Path with only a node (without any arc");
3090 
3091  typename DynDlist<Path_Desc>::Iterator it(list);
3092  it.reset_last();
3093  it.prev();
3094  return it.get_curr().arc;
3095  }
3096 
3098  bool is_cycle() const noexcept { return get_first_node() == get_last_node(); }
3099 
3105  Node * remove_last_node() noexcept(noexcept(list.remove_last()))
3106  {
3107  auto d = list.remove_last();
3108  list.get_last().arc = nullptr;
3109  return d.node;
3110  }
3111 
3117  Node * remove_first_node() noexcept(noexcept(list.remove_first()))
3118  {
3119  auto d = list.remove_first();
3120  return d.node;
3121  }
3122 
3124  void swap(Path & path) noexcept
3125  {
3126  std::swap(g, path.g);
3127  list.swap(path.list);
3128  }
3129 
3135  class Iterator : public DynDlist<Path_Desc>::Iterator
3136  {
3137  public:
3138 
3140  Iterator(const Path & path) noexcept
3141  : DynDlist<Path_Desc>::Iterator(path.list) { }
3142 
3143  private:
3144 
3145  Path_Desc & get_curr_path_desc_ne() const noexcept
3146  {
3148  }
3149 
3150  Path_Desc & get_curr_path_desc() const
3151  {
3153  }
3154 
3155  public:
3156 
3159  Node * get_current_node() const { return this->get_curr_path_desc().node; }
3160 
3161  Node * get_current_node_ne() const noexcept
3162  {
3163  return this->get_curr_path_desc_ne().node;
3164  }
3165 
3176  Arc * get_current_arc() const
3177  {
3178  if (this->is_in_last())
3179  throw std::overflow_error("Path iterator is in last node of path");
3180 
3181  return this->get_curr_path_desc().arc;
3182  }
3183 
3184  Arc * get_current_arc_ne() const noexcept
3185  {
3186  return this->get_curr_path_desc_ne().arc;
3187  }
3188 
3190  Node * get_curr_ne() const noexcept { return get_current_node_ne(); }
3191 
3192  Node * get_curr() const { return get_current_node(); }
3193 
3204  pair<Node*, Arc*> get_pair() const
3205  {
3206  return make_pair(get_current_node(), get_current_arc());
3207  }
3208 
3219  tuple<Node*, Arc*> get_tuple() const
3220  {
3221  return make_tuple(get_current_node(), get_current_arc());
3222  }
3223 
3224  tuple<Node*, Arc*> get_tuple_ne() const noexcept
3225  {
3226  return make_tuple(get_current_node_ne(), get_current_arc_ne());
3227  }
3228 
3237  bool has_current_arc() const noexcept
3238  {
3239  return this->has_curr() and not this->is_in_last();
3240  }
3241 
3243  bool has_current_node() const noexcept { return this->has_curr(); }
3244  };
3245 
3247  Iterator get_it() const { return Iterator(*this); }
3248 
3253  template <class Operation>
3254  void for_each_node(Operation op = Operation()) const noexcept(noexcept(op))
3255  {
3256  for (Iterator it(*this); it.has_current_node(); it.next_ne())
3257  op (it.get_current_node_ne());
3258  }
3259 
3264  template <class Operation>
3265  void for_each_arc(Operation op = Operation()) const noexcept(noexcept(op))
3266  {
3267  for (Iterator it(*this); it.has_current_arc(); it.next_ne())
3268  op (it.get_current_arc_ne());
3269  }
3270 
3272  bool contains_node(Node * node) const noexcept
3273  {
3274  for (Iterator it(*this); it.has_current_node(); it.next_ne())
3275  if (it.get_current_node_ne() == node)
3276  return true;
3277  return false;
3278  }
3279 
3281  bool contains_arc(Arc * arc) const noexcept
3282  {
3283  for (Iterator it(*this); it.has_current_arc(); it.next_ne())
3284  if (it.get_current_arc_ne() == arc)
3285  return true;
3286  return false;
3287  }
3288 
3291  {
3292  DynList<Node*> ret_val;
3293  for_each_node([&ret_val] (Node * p) { ret_val.append(p); });
3294  return ret_val;
3295  }
3296 
3299  {
3300  DynList<Arc*> ret_val;
3301  for_each_arc([&ret_val] (Arc * a) { ret_val.append(a); });
3302  return ret_val;
3303  }
3304 
3315  bool operator == (const Path & p) const noexcept
3316  {
3317  return eq(this->list, p.list);
3318  }
3319 
3321  bool operator != (const Path & p) const noexcept
3322  {
3323  return not eq(this->list, p.list);
3324  }
3325 };
3326 
3327  template <class GT> inline
3328 Path<GT> find_path_depth_first(const GT & g, typename GT::Node * start_node,
3329  typename GT::Node * end_node);
3330 
3331  template <class GT> static inline
3332 bool __find_path_depth_first(const GT & g, typename GT::Node * curr_node,
3333  typename GT::Arc * curr_arc,
3334  typename GT::Node * end_node, Path<GT> & path)
3335 {
3336  if (curr_node == end_node) // this test must be first in order to find cycles
3337  {
3338  path.append(curr_arc);
3339  return true;
3340  }
3341 
3342  if (IS_NODE_VISITED(curr_node, Find_Path))
3343  return false;
3344 
3345  path.append(curr_arc);
3346  NODE_BITS(curr_node).set_bit(Find_Path, true);
3347 
3348  for (auto it = g.get_arc_it(curr_node); it.has_curr(); it.next_ne())
3349  {
3350  auto next_arc = it.get_curr_ne();
3351  if (IS_ARC_VISITED(next_arc, Find_Path))
3352  continue;
3353 
3354  ARC_BITS(next_arc).set_bit(Find_Path, true);
3355  auto next_node = it.get_tgt_node();
3356  if (__find_path_depth_first<GT>(g, next_node, next_arc, end_node, path))
3357  {
3358  assert(path.get_last_node() == end_node);
3359  return true;
3360  }
3361  }
3362 
3363  path.remove_last_node();
3364 
3365  return false;
3366 }
3367 
3382  template <class GT> inline
3383 Path<GT> find_path_depth_first(const GT & g, typename GT::Node * start_node,
3384  typename GT::Node * end_node)
3385 {
3386  Path<GT> path(g, start_node);
3387 
3388  g.reset_bit_nodes(Find_Path);
3389  g.reset_bit_arcs(Find_Path);
3390  NODE_BITS(start_node).set_bit(Find_Path, true);
3391 
3392  for (auto it = g.get_arc_it(start_node); it.has_curr(); it.next_ne())
3393  {
3394  auto arc = it.get_current_arc_ne();
3395  ARC_BITS(arc).set_bit(Find_Path, true);
3396  auto next_node = it.get_tgt_node();
3397  if (IS_NODE_VISITED(next_node, Find_Path))
3398  continue;
3399 
3400  if (__find_path_depth_first<GT>(g, next_node, arc, end_node, path))
3401  return path;
3402  }
3403 
3404  path.empty();
3405 
3406  return path;
3407 }
3408 
3418  template <class GTS, class GTT>
3419 void map_nodes(typename GTS::Node * p, typename GTT::Node * q) noexcept
3420 {
3421  assert(p != nullptr and q != nullptr);
3422 
3423  if (NODE_COOKIE(p) == nullptr)
3424  {
3425  NODE_COOKIE(p) = q;
3426  NODE_COOKIE(q) = p;
3427  return;
3428  }
3429 
3430  NODE_COOKIE(q) = NODE_COOKIE(p);
3431  NODE_COOKIE(p) = q;
3432 }
3433 
3444  template <class GTS, class GTT>
3445 void map_arcs(typename GTS::Arc * p, typename GTT::Arc * q) noexcept
3446 {
3447  assert(p != nullptr and q != nullptr);
3448 
3449  if (ARC_COOKIE(p) == nullptr)
3450  {
3451  ARC_COOKIE(p) = q;
3452  ARC_COOKIE(q) = p;
3453 
3454  return;
3455  }
3456 
3457  ARC_COOKIE(q) = ARC_COOKIE(p);
3458  ARC_COOKIE(p) = q;
3459 }
3460 
3461 
3462  template <class GT>
3463 void clear_graph(GT & g) noexcept
3464 {
3465  for (typename GT::Arc_Iterator it(g); it.has_curr(); ) // eliminar arcos
3466  {
3467  typename GT::Arc * arc = it.get_curr_ne();
3468  it.next_ne();
3469  g.remove_arc(arc);
3470  }
3471 
3472  for (typename GT::Node_Iterator it(g); it.has_curr(); ) // eliminar nodos
3473  {
3474  typename GT::Node * p = it.get_curr_ne();
3475  it.next_ne(); // avanzar antes de borrar (consistencia iterador)
3476  g.remove_node(p); // eliminarlo del grafo
3477  }
3478 }
3479 
3480 
3481  template <class GT>
3482 void copy_graph(GT & gtgt, const GT & gsrc, const bool cookie_map)
3483 {
3484  try
3485  {
3486  clear_graph(gtgt); // limpiar this antes de copiar
3488 
3489  // fase 1: recorrer nodos de src_graph e insertar copia en this
3490  for (typename GT::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
3491  {
3492  typename GT::Node * src_node = it.get_current_node_ne();
3493  unique_ptr<typename GT::Node>
3494  tgt_node(new typename GT::Node(src_node->get_info()));
3495  map.insert(src_node, tgt_node.get());
3496 
3497  typename GT::Node * tgt = tgt_node.release();
3498  assert(tgt->get_info() == src_node->get_info());
3499  gtgt.insert_node(tgt); // insertar en grafo destino
3500 
3501  if (cookie_map)
3502  GT::map_nodes(src_node, tgt);
3503  }
3504 
3505  assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
3506 
3507  // fase 2: por cada arco de src_graph, crear en this un
3508  // arco que conecte a los nodos mapeados de map
3509  for (typename GT::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
3510  {
3511  typename GT::Arc * src_arc = it.get_current_arc_ne();
3512 
3513  // obtener imágenes de nodos en el grafo destino y crear arco
3514  typename GT::Node * src_node = map[gsrc.get_src_node(src_arc)];
3515  typename GT::Node * tgt_node = map[gsrc.get_tgt_node(src_arc)];
3516  typename GT::Arc * tgt_arc = gtgt.insert_arc(src_node, tgt_node);
3517  // tgt_arc->get_info() = src_arc->get_info();
3518  *tgt_arc = *src_arc;
3519  if (cookie_map)
3520  GT::map_arcs(src_arc, tgt_arc);
3521  }
3522 
3523  assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
3524  }
3525  catch (...)
3526  { // Si ocurre excepción se limpia this
3527  clear_graph(gtgt);
3528  throw;
3529  }
3530 }
3531 
3536  template <class GTT, class GTS>
3538 {
3539  void operator () (typename GTT::Node * tgt, typename GTS::Node * src) noexcept
3540  {
3541  tgt->get_info() = src->get_info();
3542  }
3543 };
3544 
3545 
3550  template <class GTT, class GTS>
3552 {
3553  void operator () (typename GTT::Arc * tgt, typename GTS::Arc * src)
3554  {
3555  tgt->get_info() = src->get_info();
3556  }
3557 };
3558 
3567  template <class GTT, class GTS,
3568  class Copy_Node = Dft_Copy_Node<GTT, GTS>,
3569  class Copy_Arc = Dft_Copy_Arc<GTT, GTS>>
3570 void inter_copy_graph(GTT & gtgt, const GTS & gsrc,
3571  const bool cookie_map = false)
3572 {
3573  try
3574  {
3575  clear_graph(gtgt); // limpiar this antes de copiar
3577 
3578  // fase 1: recorrer nodos de src_graph e insertar copia en this
3579  for (typename GTS::Node_Iterator it(gsrc); it.has_curr(); it.next_ne())
3580  {
3581  typename GTS::Node * src_node = it.get_current_node_ne();
3582  unique_ptr<typename GTT::Node> tgt_node(new typename GTT::Node);
3583  Copy_Node () (tgt_node.get(), src_node);
3584  map.insert(src_node, tgt_node.get());
3585 
3586  typename GTT::Node * tgt = tgt_node.release();
3587  gtgt.insert_node(tgt); // insertar en grafo destino
3588 
3589  if (cookie_map)
3590  map_nodes<GTS, GTT>(src_node, tgt);
3591  }
3592 
3593  assert(gtgt.get_num_nodes() == gsrc.get_num_nodes());
3594 
3595  // fase 2: por cada arco de src_graph, crear en this un
3596  // arco que conecte a los nodos mapeados de map
3597  for (typename GTS::Arc_Iterator it(gsrc); it.has_curr(); it.next_ne())
3598  {
3599  typename GTS::Arc * src_arc = it.get_current_arc_ne();
3600 
3601  // obtener imágenes de nodos en el grafo destino y crear arco
3602  typename GTT::Node * src_node = map[gsrc.get_src_node(src_arc)];
3603  typename GTT::Node * tgt_node = map[gsrc.get_tgt_node(src_arc)];
3604  typename GTT::Arc * tgt_arc = gtgt.insert_arc(src_node, tgt_node);
3605  Copy_Arc () (tgt_arc, src_arc);
3606  if (cookie_map)
3607  map_arcs<GTS, GTT>(src_arc, tgt_arc);
3608  }
3609 
3610  assert(gtgt.get_num_arcs() == gsrc.get_num_arcs());
3611  }
3612  catch (...)
3613  { // Si ocurre excepción se limpia this
3614  clear_graph(gtgt);
3615  throw;
3616  }
3617 }
3618 
3619 
3632 template <class GT, class SN = Dft_Show_Node<GT>, class SA = Dft_Show_Arc<GT>>
3634 {
3635  SN sn;
3636  SA sa;
3637 
3638 public:
3639 
3645  Copy_Graph(SA __sa = SA(), SN __sn = SN()) : sn(__sn), sa(__sa)
3646  {
3647  // empty
3648  }
3649 
3650 private:
3651 
3652  void copy(GT & gtgt, const GT & gsrc, const bool cookie_map)
3653  {
3654  try
3655  {
3656  clear_graph(gtgt); // limpiar this antes de copiar
3658 
3659  // fase 1: recorrer nodos de src_graph e insertar copia en this
3660  for (Node_Iterator<GT, SN> it(gsrc, sn); it.has_curr(); it.next_ne())
3661  {
3662  typename GT::Node * src_node = it.get_curr_ne();
3663  unique_ptr<typename GT::Node>
3664  tgt_node(new typename GT::Node(src_node));
3665  map.insert(src_node, tgt_node.get());
3666 
3667  typename GT::Node * tgt = tgt_node.release();
3668  gtgt.insert_node(tgt); // insertar en grafo destino
3669 
3670  if (cookie_map)
3671  GT::map_nodes(src_node, tgt);
3672  }
3673 
3674  // fase 2: por cada arco de src_graph, crear en this un
3675  // arco que conecte a los nodos mapeados de map
3676  for (Arc_Iterator<GT, SA> it(gsrc, sa); it.has_curr(); it.next_ne())
3677  {
3678  typename GT::Arc * src_arc = it.get_curr_ne();
3679 
3680  // obtener imágenes de nodos en el grafo destino y crear arco
3681  typename GT::Node * src_node = map[gsrc.get_src_node(src_arc)];
3682  typename GT::Node * tgt_node = map[gsrc.get_tgt_node(src_arc)];
3683  typename GT::Arc * tgt_arc =
3684  gtgt.insert_arc(src_node, tgt_node, src_arc->get_info());
3685 
3686  if (cookie_map)
3687  GT::map_arcs(src_arc, tgt_arc);
3688  }
3689  }
3690  catch (...)
3691  { // Si ocurre excepción se limpia this
3692  clear_graph(gtgt);
3693  throw;
3694  }
3695  }
3696 
3697 public:
3698 
3706  void operator () (GT & gtgt, GT & gsrc, const bool cookie_map = true)
3707  {
3708  copy(gtgt, gsrc, cookie_map);
3709  }
3710 };
3711 
3722 template <class GT, class Distance>
3724 {
3726  typename Distance::Distance_Type dist;
3727 
3728  Painted_Min_Spanning_Tree() noexcept : dist(0) { /* empty */ }
3729 
3730  bool operator () (typename GT::Arc * a) noexcept
3731  {
3732  if (not IS_ARC_VISITED(a, Aleph::Spanning_Tree))
3733  return false;
3734 
3735  dist = dist + ARC_DIST(a);
3736 
3737  return true;
3738  }
3739 };
3740 
3741 
3761 template <class GT> inline
3762 bool are_equal(const GT & g1, const GT & g2);
3763 
3764 
3765 
3766 } // end namespace Aleph
3767 
3768 # endif /* TPL_GRAPH_H */
Definition: tpl_graph.H:3633
Node_Arc_Iterator(Node *src) noexcept
Definition: tpl_graph.H:928
T foldl_arcs(GT &g, typename GT::Node *p, const T &init, std::function< T(const T &, typename GT::Arc *)> operation, SA sa=SA()) noexcept(noexcept(operation) and noexcept(sa) and std::is_nothrow_copy_assignable< T >::value)
Definition: tpl_graph.H:1557
void for_each_in_arc(typename GT::Node *p, Op op=Op()) noexcept(noexcept(op))
Definition: tpl_graph.H:2077
__Node_Info & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition: graph-dry.H:242
GT::Arc * search_directed_arc(const GT &, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Definition: tpl_graph.H:2408
DynList< ArcPair< GT > > in_pairs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1908
void clear_graph(GT &g) noexcept
Definition: tpl_graph.H:3463
Definition: tpl_graph.H:1225
void sort_nodes(Compare &cmp) noexcept
Definition: tpl_graph.H:754
Graph_Node(Node_Info &&info=Node_Info()) noexcept
Definition: tpl_graph.H:126
Definition: tpl_graph.H:56
void swap(DynDlist &l) noexcept
Definition: tpl_dynDlist.H:357
Arc_Iterator(const GT &g, Show_Arc sa=Show_Arc()) noexcept(noexcept(Itor(g, sa)))
Definition: tpl_graph.H:1240
Node * get_first_node() const
Definition: tpl_graph.H:532
size_t size() const noexcept
Return the path length in nodes.
Definition: tpl_graph.H:2755
Arc_Iterator(const List_Graph &g) noexcept
Initialize an iterator for all the arc of g
Definition: tpl_graph.H:997
Node * get_first_node() const
Definition: tpl_graph.H:3069
void prev()
Definition: tpl_graph.H:1723
Pair * insert(const Key &key, const Type &data)
Definition: tpl_dynMapTree.H:112
Arc_Iterator() noexcept
The type of set.
Definition: tpl_graph.H:994
void insert(Node *node)
Definition: tpl_graph.H:2982
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition: tpl_graph.H:3243
#define NODE_COOKIE(p)
Definition: aleph-graph.H:333
Node_Arc_Iterator() noexcept
The container type (a node)
Definition: tpl_graph.H:922
DynList< Arc * > arcs() const
Return a list with the arcs of path (order according to the path)
Definition: tpl_graph.H:3298
Definition: tpl_dynMapTree.H:328
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition: tpl_graph.H:1046
auto get_tgt_node_ne() const noexcept
Definition: tpl_graph.H:1762
List_Graph(const List_Graph &g)
Definition: tpl_graph.H:817
Node * get_current_node() const
Definition: tpl_graph.H:3159
void insert_directed(Arc *arc)
Definition: tpl_graph.H:3052
Definition: tpl_graph.H:3551
Definition: tpl_graph.H:2493
Path(const Path &path)
Copy constructor.
Definition: tpl_graph.H:2769
Node_Arc_Iterator(typename GT::Node *p, Show_Arc sa=Show_Arc()) noexcept(noexcept(Itor(p, sa)))
Definition: tpl_graph.H:1200
void insert(Arc *arc)
Definition: tpl_graph.H:2951
void sort_nodes(Compare &&cmp=Compare()) noexcept
Definition: tpl_graph.H:762
bool traverse_arcs(typename GT::Node *p, Operation op=Operation()) noexcept(noexcept(op))
Definition: tpl_graph.H:2017
Definition: tpl_graph.H:988
#define IS_NODE_VISITED(p, bit)
Definition: aleph-graph.H:327
bool traverse_in_arcs(typename GT::Node *p, Op op=Op()) noexcept(noexcept(op))
Definition: tpl_graph.H:2064
GT::Node * get_node_ne() const noexcept
Return the connected node to current arc.
Definition: tpl_graph.H:1756
Node * get_curr_ne() const noexcept
Definition: tpl_graph.H:3190
Node * get_last_node() const
Definition: tpl_graph.H:3073
T & get_last() const
Return a modifiable reference to last item in the list.
Definition: tpl_dynDlist.H:240
auto get_tgt_node() const
Definition: tpl_graph.H:1753
bool contains_node(Node *node) const noexcept
Return true if node belongs to the path.
Definition: tpl_graph.H:3272
Graph_Arc(Arc_Info &&info=Arc_Info()) noexcept(noexcept(std::is_nothrow_move_constructible< Arc_Info >::value))
Definition: tpl_graph.H:235
Definition: tpl_graph.H:1693
void reset_first()
Coloca el iterador sobre el primer elemento de la secuencia.
Definition: filter_iterator.H:184
size_t out_degree(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1976
GT::Node * mapped_node(typename GT::Node *p) noexcept
Definition: tpl_graph.H:2429
DynList< ArcPair< GT > > out_pairs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1927
Graph_Node(Graph_Node *node) noexcept(std::is_nothrow_copy_assignable< Node_Info >::value)
Definition: tpl_graph.H:160
GT::Arc * get_curr() const
Definition: tpl_graph.H:1730
DynList< typename GT::Node * > out_nodes(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1829
size_t in_degree(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1948
void copy_graph(GT &gtgt, const GT &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:3482
Node * remove_last_node() noexcept(noexcept(list.remove_last()))
Definition: tpl_graph.H:3105
#define ARC_COOKIE(p)
Definition: aleph-graph.H:366
void next()
Definition: tpl_graph.H:1717
auto get_current_arc() const
Definition: tpl_graph.H:1737
Definition: tpl_graph.H:1257
void map_arcs(typename GTS::Arc *p, typename GTT::Arc *q) noexcept
Definition: tpl_graph.H:3445
void append_directed(Arc *arc)
Definition: tpl_graph.H:2920
Node_Iterator(const GT &g, Show_Node sn=Show_Node()) noexcept(noexcept(Itor(g, sn)))
Definition: tpl_graph.H:1288
Definition: graph-dry.H:272
pair< Node *, Arc * > get_pair() const
Definition: tpl_graph.H:3204
Arc * get_curr_ne() const noexcept
Definition: tpl_graph.H:951
#define IS_ARC_VISITED(p, bit)
Definition: aleph-graph.H:360
void insert_directed(Node *p)
Definition: tpl_graph.H:3019
Definition: graph-dry.H:329
Path(const GT &__g) noexcept
Construct a empty path on graph __g
Definition: tpl_graph.H:2711
Arc * get_last_arc() const
Definition: tpl_graph.H:3086
T remove_last()
Definition: tpl_dynDlist.H:292
It::Item_Type Item_Type
Tipo de elemento que retorna get_curr()
Definition: filter_iterator.H:123
typename Itor::Set_Type Set_Type
The element type: Node*.
Definition: tpl_graph.H:1279
tuple< typename GT::Arc *, typename GT::Node * > ArcPair
Definition: tpl_graph.H:1629
size_t num_arcs
data associated to the node. Access it with get_info()
Definition: graph-dry.H:227
Definition: tpl_graph.H:53
Definition: graph-dry.H:42
DynList< typename GT::Arc * > arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1893
void map_nodes(typename GTS::Node *p, typename GTT::Node *q) noexcept
Definition: tpl_graph.H:3419
Copy_Graph(SA __sa=SA(), SN __sn=SN())
Definition: tpl_graph.H:3645
void reset_last()
Coloca el iterador sobre el último elemento de la secuencia.
Definition: filter_iterator.H:187
Digraph_Iterator(typename GT::Node *p) noexcept(noexcept(Filter(p)) and noexcept(Itor(p, filt)))
Iterator type.
Definition: tpl_graph.H:1708
virtual Node * insert_node(Node *node) noexcept
Definition: tpl_graph.H:474
Definition: ah-comb.H:35
void swap(Path &path) noexcept
Fast spat between two paths (constant time)
Definition: tpl_graph.H:3124
typename Itor::Set_Type Set_Type
type of element: Arc*
Definition: tpl_graph.H:1191
void for_each_node(const GT &g, std::function< void(typename GT::Node *)> operation, SN sn=SN()) noexcept(noexcept(operation) and noexcept(sn))
Definition: tpl_graph.H:1306
Definition: graph-dry.H:206
Graph_Arc(const Arc_Info &info) noexcept(noexcept(std::is_nothrow_copy_constructible< Arc_Info >::value))
Definition: tpl_graph.H:219
typename Itor::Set_Type Set_Type
Type of element: Arc*.
Definition: tpl_graph.H:1231
Node * get_tgt_node() const
Definition: tpl_graph.H:1034
T & get_first() const
Return a modifiable reference to first item in the list.
Definition: tpl_dynDlist.H:232
List_Graph() noexcept
Construct an empty graph.
Definition: tpl_graph.H:1043
NodeInfo Node_Type
The node.
Definition: graph-dry.H:233
virtual void remove_arc(Arc *arc) noexcept
Definition: tpl_graph.H:608
bool all(Operation &operation) const noexcept(noexcept(operation))
Definition: htlist.H:719
void sort_arcs(Compare &cmp) noexcept
Definition: tpl_graph.H:789
Definition: tpl_graph.H:1063
void for_each_node(Operation op=Operation()) const noexcept(noexcept(op))
Definition: tpl_graph.H:3254
bool has_curr() const noexcept
Return true the iterator has an current arc.
Definition: tpl_graph.H:1726
Operate_On_Arcs(SA __sa=SA()) noexcept(std::is_nothrow_constructible< SA >::value)
Initialize the functor with the filter sa
Definition: tpl_graph.H:2547
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:169
Path(const GT &_g, Node *start_node)
Definition: tpl_graph.H:2730
void sort_arcs(Compare &&cmp=Compare()) noexcept
Definition: tpl_graph.H:797
const GT & get_graph() const noexcept
Get a constant reference to the graph.
Definition: tpl_graph.H:2705
Arc * get_first_arc(Node *node) const
Definition: tpl_graph.H:552
Arc * get_current_arc() const
Definition: tpl_graph.H:3176
virtual void disconnect_arc(Arc *arc) noexcept
Definition: tpl_graph.H:656
bool check() const
Return true if the path is consistent.
Definition: tpl_graph.H:2683
void inter_copy_graph(GTT &gtgt, const GTS &gsrc, const bool cookie_map=false)
Definition: tpl_graph.H:3570
void reset_first() noexcept
Reset the iterator to the first arc.
Definition: tpl_graph.H:1765
void append_directed(Node *p)
Definition: tpl_graph.H:2880
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Definition: tpl_graph.H:3383
Definition: tpl_graph.H:3723
Iterator get_it() const
Returns an iterator on the path.
Definition: tpl_graph.H:3247
Node * get_tgt_node() const
Definition: tpl_graph.H:965
bool forall_arc(typename GT::Node *p, std::function< bool(typename GT::Arc *)> cond, SA sa=SA()) noexcept(noexcept(cond) and noexcept(sa))
Definition: tpl_graph.H:1410
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition: graph-dry.H:447
Arc * get_first_arc() const
Definition: tpl_graph.H:3082
Definition: tpl_graph.H:1796
Arc * get_first_arc() const
Definition: tpl_graph.H:724
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Definition: tpl_graph.H:2381
void for_each_arc(typename GT::Node *p, Operation op=Operation()) noexcept(noexcept(op))
Definition: tpl_graph.H:2044
GT::Node * get_node() const
Return the connected node to current arc.
Definition: tpl_graph.H:1747
#define NODE_BITS(p)
Definition: aleph-graph.H:305
Definition: tpl_graph.H:1810
void reset_last() noexcept
Reset the iterator to the last arc.
Definition: tpl_graph.H:1768
Arc * get_current_arc_ne() const noexcept
Return the current arc. Throw overflow_error if there is no one.
Definition: tpl_graph.H:1004
Node_Iterator() noexcept
The set: the arcs of a graph.
Definition: tpl_graph.H:1281
Definition: tpl_graph.H:912
virtual ~List_Graph()
Destructor: all the nodes and arcs and removed and freed.
Definition: tpl_graph.H:866
void for_each_arc(Operation op=Operation()) const noexcept(noexcept(op))
Definition: tpl_graph.H:3265
Definition: tpl_graph.H:877
void init(Node *start_node)
Definition: tpl_graph.H:2722
void set_graph(const GT &__g, Node *start_node=nullptr)
Definition: tpl_graph.H:2745
typename GT::Node_Type Node_Type
The type of dfata stored in the nodes.
Definition: tpl_graph.H:2637
size_t get_num_arcs() const noexcept
Definition: graph-dry.H:494
Definition: tpl_graph.H:1667
Definition: tpl_graph.H:1270
bool is_empty() const noexcept
Return true if the path is empty.
Definition: tpl_graph.H:2758
GT::Arc * get_curr_ne() const noexcept
Definition: tpl_graph.H:1734
Path(Path &&path)
Move constructor.
Definition: tpl_graph.H:2772
Arc * get_curr() const
Definition: tpl_graph.H:945
Iterator(const Path &path) noexcept
Create an iterator on the first node of path
Definition: tpl_graph.H:3140
#define ARC_BITS(p)
Definition: aleph-graph.H:351
bool contains_arc(Arc *arc) const noexcept
Return true if arc belongs to the path.
Definition: tpl_graph.H:3281
const size_t & size() const noexcept
Return the number of elements (constant time)
Definition: tpl_dynDlist.H:74
List_Graph(List_Graph &&g) noexcept
Definition: tpl_graph.H:831
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition: graph-dry.H:453
__Euclidian_Arc Arc
The node class type.
Definition: tpl_graph.H:414
Node * remove_first_node() noexcept(noexcept(list.remove_first()))
Definition: tpl_graph.H:3117
__Euclidian_Node Node
The graph type.
Definition: tpl_graph.H:413
Definition: tpl_graph.H:59
Graph_Node(const Node_Info &info) noexcept
The type of data stored in the node.
Definition: tpl_graph.H:115
Operate_On_Nodes(SN __sn=SN()) noexcept(std::is_nothrow_constructible< SN >::value)
Initialize the functor with the filter sa
Definition: tpl_graph.H:2500
typename GT::Out_Iterator __Out_Iterator
Definition: tpl_graph.H:1780
T & get_curr() const
Definition: tpl_dynDlist.H:495
void for_each_out_arc(typename GT::Node *p, Op op=Op()) noexcept(noexcept(op))
Definition: tpl_graph.H:2234
Definition: ahDry.H:41
typename GT::Arc_Type Arc_Type
The type of data stored in the arc.
Definition: tpl_graph.H:2640
virtual void remove_node(Node *node) noexcept
Definition: tpl_graph.H:493
Definition: filter_iterator.H:68
Node * get_src_node() const
Definition: tpl_graph.H:1021
Arc_Iterator() noexcept
Type of set all the arcs.
Definition: tpl_graph.H:1233
bool is_cycle() const noexcept
Return true if this is a cycle.
Definition: tpl_graph.H:3098
typename GT::In_Iterator __In_Iterator
Definition: tpl_graph.H:1788
Definition: tpl_graph.H:46
void empty()
Clean the path: all the nodes and arc are removed.
Definition: tpl_graph.H:2761
T & append(const T &item)
Definition: htlist.H:1471
Arc * insert_arc(Node *src, Node *tgt, const Arc_Type &arc_info)
Definition: graph-dry.H:899
Definition: Map.H:82
void append(Arc *arc)
Definition: tpl_graph.H:2808
Definition: tpl_graph.H:1177
Distance::Distance_Type dist
Accumutive distance from the first seen arc until the last seen.
Definition: tpl_graph.H:3726
Definition: tpl_graph.H:48
Node_Arc_Iterator() noexcept
Type of set: p&#39;s arcs.
Definition: tpl_graph.H:1193
bool traverse_out_arcs(typename GT::Node *p, Op op=Op()) noexcept(noexcept(op))
Definition: tpl_graph.H:2221
T foldl_nodes(GT &g, const T &init, std::function< T(const T &, typename GT::Node *)> operation, SN sn=SN()) noexcept(noexcept(operation) and noexcept(sn) and std::is_nothrow_copy_assignable< T >::value)
Definition: tpl_graph.H:1508
tuple< Node *, Arc * > get_tuple() const
Definition: tpl_graph.H:3219
GT::Node * get_node(typename GT::Arc *a) const noexcept
Definition: tpl_graph.H:1741
void end() noexcept
Put the iterator in end state.
Definition: tpl_graph.H:1771
virtual Arc * connect_arc(Arc *arc) noexcept
Definition: tpl_graph.H:689
Definition: List.H:49
T remove_first()
Definition: tpl_dynDlist.H:280
bool has_current_arc() const noexcept
Definition: tpl_graph.H:3237
bool forall_node(const GT &g, std::function< bool(typename GT::Node *)> cond, SN sn=SN()) noexcept(noexcept(cond) and noexcept(sn))
Definition: tpl_graph.H:1366
Definition: tpl_graph.H:3135
T & insert(const T &item)
Definition: tpl_dynDlist.H:127
void append(Node *node)
Definition: tpl_graph.H:2841
Definition: tpl_graph.H:1638
Definition: tpl_graph.H:3537
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition: tpl_graph.H:3290
Container< T > nodes_map(GT &g, std::function< T(typename GT::Node *)> transformation, SN sn=SN())
Definition: tpl_graph.H:1435
Definition: tpl_graph.H:2540
bool check_directed() const
Return true if the directed path is consistent.
Definition: tpl_graph.H:2698
DynList< typename GT::Arc * > in_arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1878
DynList< typename GT::Arc * > out_arcs(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1862
bool inside_graph(const GT &gr) const noexcept
Return true if this is on graph gr
Definition: tpl_graph.H:2708
Node_Iterator(const List_Graph &g)
Construct an iterator on the nodes of g
Definition: tpl_graph.H:882
T & append(const T &item)
Definition: tpl_dynDlist.H:149
Arc * get_current_arc() const
Definition: tpl_graph.H:958
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Definition: tpl_graph.H:1846
Container< T > arcs_map(GT &g, typename GT::Node *p, std::function< T(typename GT::Arc *)> transformation, SA sa=SA())
Definition: tpl_graph.H:1484

Leandro Rabindranath León