DeSiGNAR  0.5a
Data Structures General Library
graph.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGGRAPH_H
26 # define DSGGRAPH_H
27 
28 # include <nodesdef.H>
29 # include <graphutilities.H>
30 # include <italgorithms.H>
31 # include <sort.H>
32 
33 namespace Designar
34 {
35  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
36  class Graph;
37 
38  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
39  class Digraph;
40 
41  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
42  class GraphNode : public BaseGraphNode<NodeInfo, CommonNodeArc>
43  {
44  friend class Graph<NodeInfo, ArcInfo, GraphInfo>;
45  friend class DLNode<GraphNode>;
47  using Base::Base;
48  };
49 
50  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
51  class DigraphNode : public BaseGraphNode<NodeInfo, CommonNodeArc>
52  {
53  friend class Digraph<NodeInfo, ArcInfo, GraphInfo>;
54  friend class DLNode<DigraphNode>;
56  using Base::Base;
57  };
58 
59  template <class Node, typename NodeInfo, typename ArcInfo, typename GraphInfo>
60  class GraphArc : public BaseGraphArc<Node, ArcInfo, CommonNodeArc>
61  {
62  friend class Graph<NodeInfo, ArcInfo, GraphInfo>;
63  friend class DLNode<GraphArc>;
65  using Base::Base;
66 
67  protected:
68  DLNode<DLNode<GraphArc> *> * arc_in_src_node = nullptr;
69  DLNode<DLNode<GraphArc> *> * arc_in_tgt_node = nullptr;
70  };
71 
72  template <class Node, typename NodeInfo, typename ArcInfo, typename GraphInfo>
73  class DigraphArc : public BaseGraphArc<Node, ArcInfo, CommonNodeArc>
74  {
75  friend class Digraph<NodeInfo, ArcInfo, GraphInfo>;
76  friend class DLNode<DigraphArc>;
78  using Base::Base;
79 
80  protected:
81  DLNode<DLNode<DigraphArc> *> * arc_in_arc_list = nullptr;
82  };
83 
84  template <class GT, class Node, class Arc,
85  typename NodeInfoType, typename ArcInfoType>
86  class BaseGraph
87  {
88  GT & me()
89  {
90  return *static_cast<GT *>(this);
91  }
92 
93  const GT & const_me() const
94  {
95  return *static_cast<const GT *>(this);
96  }
97 
98  public:
99  static void copy_graph(const GT &, GT &);
100 
101  Node * nth_node(nat_t i)
102  {
103  return nth_it(me().nodes_begin(), me().nodes_end(), i);
104  }
105 
106  Node * nth_node(nat_t i) const
107  {
108  return nth_it(const_me().nodes_begin(), const_me().nodes_end(), i);
109  }
110 
111  template <class Op>
112  void for_each_node(Op & op) const
113  {
114  for_each_it(const_me().nodes_begin(), const_me().nodes_end(), op);
115  }
116 
117  template <class Op>
118  void for_each_node(Op && op = Op()) const
119  {
120  for_each_it(const_me().nodes_begin(), const_me().nodes_end(),
121  std::forward<Op>(op));
122  }
123 
124  template <class ContainerRet = SLList<Node *>, class Pred>
125  ContainerRet filter_nodes(Pred & pred) const
126  {
127  return filter_it(const_me().nodes_begin(), const_me().nodes_end(), pred);
128  }
129 
130  template <class ContainerRet = SLList<Node *>, class Pred>
131  ContainerRet filter_nodes(Pred && pred = Pred()) const
132  {
133  return filter_it(const_me().nodes_begin(), const_me().nodes_end(),
134  std::forward<Pred>(pred));
135  }
136 
137  template <typename RetT = NodeInfoType,
138  class ContainerRet = SLList<RetT>, class Op>
139  ContainerRet map_nodes(Op & op) const
140  {
141  return map_it<ContainerRet>(const_me().nodes_begin(),
142  const_me().nodes_end(), op);
143  }
144 
145  template <typename RetT = NodeInfoType,
146  class ContainerRet = SLList<RetT>, class Op>
147  ContainerRet map_nodes(Op && op = Op()) const
148  {
149  return map_it<ContainerRet>(const_me().nodes_begin(),
150  const_me().nodes_end(), std::forward<Op>(op));
151  }
152 
153  template <typename RetT = NodeInfoType,
154  class ContainerRet = SLList<RetT>,
155  class Op, class Pred>
156  ContainerRet map_nodes_if(Op & op, Pred & pred) const
157  {
158  return map_if_it<ContainerRet>(const_me().nodes_begin(),
159  const_me().nodes_end(),
160  op, pred);
161  }
162 
163  template <typename RetT = NodeInfoType,
164  class ContainerRet = SLList<RetT>,
165  class Op, class Pred>
166  ContainerRet map_nodes_if(Op & op, Pred && pred = Pred()) const
167  {
168  return map_if_it<ContainerRet>(const_me().nodes_begin(),
169  const_me().nodes_end(),
170  op, std::forward<Pred>(pred));
171  }
172 
173  template <typename RetT = NodeInfoType,
174  class ContainerRet = SLList<RetT>,
175  class Op, class Pred>
176  ContainerRet map_nodes_if(Op && op, Pred & pred) const
177  {
178  return map_if_it<ContainerRet>(const_me().nodes_begin(),
179  const_me().nodes_end(),
180  std::forward<Op>(op), pred);
181  }
182 
183  template <typename RetT = NodeInfoType,
184  class ContainerRet = SLList<RetT>,
185  class Op, class Pred>
186  ContainerRet map_nodes_if(Op && op = Op(), Pred && pred = Pred()) const
187  {
188  return map_if_it<ContainerRet>(const_me().nodes_begin(),
189  const_me().nodes_end(),
190  std::forward<Op>(op),
191  std::forward<Pred>(pred));
192  }
193 
194  template <typename RetT, class Op>
195  RetT fold_nodes(const RetT & init_val, Op & op) const
196  {
197  return fold_it<RetT>(const_me().nodes_begin(), const_me().nodes_end(),
198  init_val, op);
199  }
200 
201  template <typename RetT, class Op>
202  RetT fold_nodes(const RetT & init_val, Op && op = Op()) const
203  {
204  return fold_it<RetT>(const_me().nodes_begin(), const_me().nodes_end(),
205  init_val, std::forward<Op>(op));
206  }
207 
208  template <typename RetT, class Op>
209  RetT fold_nodes(RetT && init_val, Op & op) const
210  {
211  return fold_it<RetT>(const_me().nodes_begin(), const_me().nodes_end(),
212  std::forward<RetT>(init_val), op);
213  }
214 
215  template <typename RetT, class Op>
216  RetT fold_nodes(RetT && init_val, Op && op = Op()) const
217  {
218  return fold_it<RetT>(const_me().nodes_begin(), const_me().nodes_end(),
219  std::forward<RetT>(init_val),
220  std::forward<Op>(op));
221  }
222 
223  template <class Pred>
224  bool all_nodes(Pred & pred) const
225  {
226  return all_it(const_me().nodes_begin(), const_me().nodes_end(), pred);
227  }
228 
229  template <class Pred>
230  bool all_nodes(Pred && pred = Pred()) const
231  {
232  return all_it(const_me().nodes_begin(), const_me().nodes_end(),
233  std::forward<Pred>(pred));
234  }
235 
236  template <class Pred>
237  bool exists_node(Pred & pred) const
238  {
239  return exists_it(const_me().nodes_begin(), const_me().nodes_end(), pred);
240  }
241 
242  template <class Pred>
243  bool exists_node(Pred && pred = Pred()) const
244  {
245  return exists_it(const_me().nodes_begin(), const_me().nodes_end(),
246  std::forward<Pred>(pred));
247  }
248 
249  template <class Pred>
250  bool none_node(Pred & pred) const
251  {
252  return none_it(const_me().nodes_begin(), const_me().nodes_end(), pred);
253  }
254 
255  template <class Pred>
256  bool none_node(Pred && pred = Pred()) const
257  {
258  return none_it(const_me().nodes_begin(), const_me().nodes_end(),
259  std::forward<Pred>(pred));
260  }
261 
262  template <class Pred>
263  Node * search_node(Pred & pred) const
264  {
265  for (auto it = const_me().nodes_begin();
266  it != const_me().nodes_end(); ++it)
267  if (pred(*it))
268  return *it;
269  return nullptr;
270  }
271 
272  template <class Pred>
273  Node * search_node(Pred && pred = Pred()) const
274  {
275  return search_node<Pred>(pred);
276  }
277 
278  template <class Pred>
279  bool remove_first_node_if(Pred & pred)
280  {
281  return remove_first_if_it(me().nodes_begin(), me().nodes_end(), pred);
282  }
283 
284  template <class Pred>
285  bool remove_first_node_if(Pred && pred = Pred())
286  {
287  return remove_first_if_it(me().nodes_begin(), me().nodes_end(),
288  std::forward<Pred>(pred));
289  }
290 
291  template <class Pred>
292  void remove_node_if(Pred & pred)
293  {
294  remove_if_it(me().nodes_begin(), me().nodes_end(), pred);
295  }
296 
297  template <class Pred>
298  void remove_node_if(Pred && pred = Pred())
299  {
300  remove_if_it(me().nodes_begin(), me().nodes_end(),
301  std::forward<Pred>(pred));
302  }
303 
305  {
306  return to_list_it<Node *>(const_me().nodes_begin(),
307  const_me().nodes_end());
308  }
309 
310  Arc * nth_arc(nat_t i)
311  {
312  return nth_it(me().arcs_begin(), me().arcs_end(), i);
313  }
314 
315  Arc * nth_arc(nat_t i) const
316  {
317  return nth_it(const_me().arcs_begin(), const_me().arcs_end(), i);
318  }
319 
320  template <class Op>
321  void for_each_arc(Op & op) const
322  {
323  for_each_it(const_me().arcs_begin(), const_me().arcs_end(), op);
324  }
325 
326  template <class Op>
327  void for_each_arc(Op && op) const
328  {
329  for_each_it(const_me().arcs_begin(), const_me().arcs_end(),
330  std::forward<Op>(op));
331  }
332 
333  template <class ContainerRet = SLList<Arc *>, class Pred>
334  ContainerRet filter_arcs(Pred & pred) const
335  {
336  return filter_it(const_me().arcs_begin(), const_me().arcs_end(), pred);
337  }
338 
339  template <class ContainerRet = SLList<Arc *>, class Pred>
340  ContainerRet filter_arcs(Pred && pred = Pred()) const
341  {
342  return filter_it(const_me().arcs_begin(), const_me().arcs_end(),
343  std::forward<Pred>(pred));
344  }
345 
346  template <typename RetT = ArcInfoType,
347  class ContainerRet = SLList<RetT>, class Op>
348  ContainerRet map_arcs(Op & op) const
349  {
350  return map_it<ContainerRet>(const_me().arcs_begin(),
351  const_me().arcs_end(), op);
352  }
353 
354  template <typename RetT = ArcInfoType,
355  class ContainerRet = SLList<RetT>, class Op>
356  ContainerRet map_arcs(Op && op = Op()) const
357  {
358  return map_it<ContainerRet>(const_me().arcs_begin(),
359  const_me().arcs_end(), std::forward<Op>(op));
360  }
361 
362  template <typename RetT = ArcInfoType,
363  class ContainerRet = SLList<RetT>,
364  class Op, class Pred>
365  ContainerRet map_arcs_if(Op & op, Pred & pred) const
366  {
367  return map_if_it<ContainerRet>(const_me().arcs_begin(),
368  const_me().arcs_end(),
369  op, pred);
370  }
371 
372  template <typename RetT = ArcInfoType,
373  class ContainerRet = SLList<RetT>,
374  class Op, class Pred>
375  ContainerRet map_arcs_if(Op & op, Pred && pred = Pred()) const
376  {
377  return map_if_it<ContainerRet>(const_me().arcs_begin(),
378  const_me().arcs_end(),
379  op, std::forward<Pred>(pred));
380  }
381 
382  template <typename RetT = ArcInfoType,
383  class ContainerRet = SLList<RetT>,
384  class Op, class Pred>
385  ContainerRet map_arcs_if(Op && op, Pred & pred) const
386  {
387  return map_if_it<ContainerRet>(const_me().arcs_begin(),
388  const_me().arcs_end(),
389  std::forward<Op>(op), pred);
390  }
391 
392  template <typename RetT = ArcInfoType,
393  class ContainerRet = SLList<RetT>,
394  class Op, class Pred>
395  ContainerRet map_arcs_if(Op && op = Op(), Pred && pred = Pred()) const
396  {
397  return map_if_it<ContainerRet>(const_me().arcs_begin(),
398  const_me().arcs_end(),
399  std::forward<Op>(op),
400  std::forward<Pred>(pred));
401  }
402 
403  template <typename RetT, class Op>
404  RetT fold_arcs(const RetT & init_val, Op & op) const
405  {
406  return fold_it<RetT>(const_me().arcs_begin(), const_me().arcs_end(),
407  init_val, op);
408  }
409 
410  template <typename RetT, class Op>
411  RetT fold_arcs(const RetT & init_val, Op && op = Op()) const
412  {
413  return fold_it<RetT>(const_me().arcs_begin(), const_me().arcs_end(),
414  init_val, std::forward<Op>(op));
415  }
416 
417  template <typename RetT, class Op>
418  RetT fold_arcs(RetT && init_val, Op & op) const
419  {
420  return fold_it<RetT>(const_me().arcs_begin(), const_me().arcs_end(),
421  std::forward<RetT>(init_val), op);
422  }
423 
424  template <typename RetT, class Op>
425  RetT fold_arcs(RetT && init_val, Op && op = Op()) const
426  {
427  return fold_it<RetT>(const_me().arcs_begin(), const_me().arcs_end(),
428  std::forward<RetT>(init_val),
429  std::forward<Op>(op));
430  }
431 
432  template <class Pred>
433  bool all_arcs(Pred & pred) const
434  {
435  return all_it(const_me().arcs_begin(), const_me().arcs_end(), pred);
436  }
437 
438  template <class Pred>
439  bool all_arcs(Pred && pred) const
440  {
441  return all_it(const_me().arcs_begin(), const_me().arcs_end(),
442  std::forward<Pred>(pred));
443  }
444 
445  template <class Pred>
446  bool exists_arc(Pred & pred) const
447  {
448  return exists_it(const_me().arcs_begin(), const_me().arcs_end(), pred);
449  }
450 
451  template <class Pred>
452  bool exists_arc(Pred && pred) const
453  {
454  return exists_it(const_me().arcs_begin(), const_me().arcs_end(),
455  std::forward<Pred>(pred));
456  }
457 
458  template <class Pred>
459  bool none_arc(Pred & pred) const
460  {
461  return none_it(const_me().arcs_begin(), const_me().arcs_end(), pred);
462  }
463 
464  template <class Pred>
465  bool none_arc(Pred && pred) const
466  {
467  return none_it(const_me().arcs_begin(), const_me().arcs_end(),
468  std::forward<Pred>(pred));
469  }
470 
471  template <class Pred>
472  Arc * search_arc(Pred & pred) const
473  {
474  for (auto it = const_me().arcs_begin();
475  it != const_me().arcs_end(); ++it)
476  if (pred(*it))
477  return *it;
478  return nullptr;
479  }
480 
481  template <class Pred>
482  Arc * search_arc(Pred && pred) const
483  {
484  return search_arc<Pred>(pred);
485  }
486 
487  template <class Pred>
488  bool remove_first_arc_if(Pred & pred)
489  {
490  return remove_first_if_it(me().arcs_begin(), me().arcs_end(), pred);
491  }
492 
493  template <class Pred>
494  bool remove_first_arc_if(Pred && pred)
495  {
496  return remove_first_if_it(me().arcs_begin(), me().arcs_end(),
497  std::forward<Pred>(pred));
498  }
499 
500  template <class Pred>
501  void remove_arc_if(Pred & pred)
502  {
503  remove_if_it(me().arcs_begin(), me().arcs_end(), pred);
504  }
505 
506  template <class Pred>
507  void remove_arc_if(Pred && pred)
508  {
509  remove_if_it(me().arcs_begin(), me().arcs_end(), std::forward<Pred>(pred));
510  }
511 
513  {
514  return to_list_it<Arc *>(const_me().arcs_begin(), const_me().arcs_end());
515  }
516 
517  Arc * nth_adjacent_arc(Node * p, nat_t i)
518  {
519  return nth_it(me().arcs_begin(p), me().arcs_end(p), i);
520  }
521 
522  Arc * nth_adjacent_arc(Node * p, nat_t i) const
523  {
524  return nth_it(const_me().arcs_begin(p), const_me().arcs_end(p), i);
525  }
526 
527  template <class Op>
528  void for_each_adjacent_arc(Node * p, Op & op) const
529  {
530  for_each_it(const_me().arcs_begin(p), const_me().arcs_end(p), op);
531  }
532 
533  template <class Op>
534  void for_each_adjacent_arc(Node * p, Op && op) const
535  {
536  for_each_it(const_me().arcs_begin(p), const_me().arcs_end(p),
537  std::forward<Op>(op));
538  }
539 
540  template <class ContainerRet = SLList<Arc *>, class Pred>
541  ContainerRet filter_adjacent_arcs(Node * p, Pred & pred) const
542  {
543  return filter_it(const_me().arcs_begin(p), const_me().arcs_end(p), pred);
544  }
545 
546  template <class ContainerRet = SLList<ArcInfoType>, class Pred>
547  ContainerRet filter_adjacent_arcs(Node * p, Pred && pred = Pred()) const
548  {
549  return filter_it(const_me().arcs_begin(p), const_me().arcs_end(p),
550  std::forward<Pred>(pred));
551  }
552 
553  template <typename RetT = ArcInfoType,
554  class ContainerRet = SLList<RetT>, class Op>
555  ContainerRet map_adjacent_arcs(Node * p, Op & op) const
556  {
557  return map_it<ContainerRet>(const_me().arcs_begin(p),
558  const_me().arcs_end(p), op);
559  }
560 
561  template <typename RetT = ArcInfoType,
562  class ContainerRet = SLList<RetT>, class Op>
563  ContainerRet map_adjacent_arcs(Node * p, Op && op = Op()) const
564  {
565  return map_it<ContainerRet>(const_me().arcs_begin(p),
566  const_me().arcs_end(p),
567  std::forward<Op>(op));
568  }
569 
570  template <typename RetT = ArcInfoType,
571  class ContainerRet = SLList<RetT>,
572  class Op, class Pred>
573  ContainerRet map_adjacent_arcs_if(Node * p, Op & op, Pred & pred) const
574  {
575  return map_if_it<ContainerRet>(const_me().arcs_begin(p),
576  const_me().arcs_end(p),
577  op, pred);
578  }
579 
580  template <typename RetT = ArcInfoType,
581  class ContainerRet = SLList<RetT>,
582  class Op, class Pred>
583  ContainerRet
584  map_adjacent_arcs_if(Node * p, Op & op, Pred && pred = Pred()) const
585  {
586  return map_if_it<ContainerRet>(const_me().arcs_begin(p),
587  const_me().arcs_end(p),
588  op, std::forward<Pred>(pred));
589  }
590 
591  template <typename RetT = ArcInfoType,
592  class ContainerRet = SLList<RetT>,
593  class Op, class Pred>
594  ContainerRet map_adjacent_arcs_if(Node * p, Op && op, Pred & pred) const
595  {
596  return map_if_it<ContainerRet>(const_me().arcs_begin(p),
597  const_me().arcs_end(p),
598  std::forward<Op>(op), pred);
599  }
600 
601  template <typename RetT = ArcInfoType,
602  class ContainerRet = SLList<RetT>,
603  class Op, class Pred>
604  ContainerRet
605  map_adjacent_arcs_if(Node * p, Op && op = Op(), Pred && pred = Pred()) const
606  {
607  return map_if_it<ContainerRet>(const_me().arcs_begin(p),
608  const_me().arcs_end(p),
609  std::forward<Op>(op),
610  std::forward<Pred>(pred));
611  }
612 
613  template <typename RetT, class Op>
614  RetT fold_adjacent_arcs(Node * p, const RetT & init_val, Op & op) const
615  {
616  return fold_it<RetT>(const_me().arcs_begin(p), const_me().arcs_end(p),
617  init_val, op);
618  }
619 
620  template <typename RetT, class Op>
621  RetT fold_adjacent_arcs(Node * p, const RetT & init_val, Op && op = Op()) const
622  {
623  return fold_it<RetT>(const_me().arcs_begin(p), const_me().arcs_end(p),
624  init_val, std::forward<Op>(op));
625  }
626 
627  template <typename RetT, class Op>
628  RetT fold_adjacent_arcs(Node * p, RetT && init_val, Op & op) const
629  {
630  return fold_it<RetT>(const_me().arcs_begin(p), const_me().arcs_end(p),
631  std::forward<RetT>(init_val), op);
632  }
633 
634  template <typename RetT, class Op>
635  RetT fold_adjacent_arcs(Node * p, RetT && init_val, Op && op = Op()) const
636  {
637  return fold_it<RetT>(const_me().arcs_begin(p), const_me().arcs_end(p),
638  std::forward<RetT>(init_val),
639  std::forward<Op>(op));
640  }
641 
642  template <class Pred>
643  bool all_adjacent_arcs(Node * p, Pred & pred) const
644  {
645  return all_it(const_me().arcs_begin(p), const_me().arcs_end(p), pred);
646  }
647 
648  template <class Pred>
649  bool all_adjacent_arcs(Node * p, Pred && pred) const
650  {
651  return all_it(const_me().arcs_begin(p), const_me().arcs_end(p),
652  std::forward<Pred>(pred));
653  }
654 
655  template <class Pred>
656  bool exists_adjacent_arc(Node * p, Pred & pred) const
657  {
658  return exists_it(const_me().arcs_begin(p), const_me().arcs_end(p), pred);
659  }
660 
661  template <class Pred>
662  bool exists_adjacent_arc(Node * p, Pred && pred) const
663  {
664  return exists_it(const_me().arcs_begin(p), const_me().arcs_end(p),
665  std::forward<Pred>(pred));
666  }
667 
668  template <class Pred>
669  bool none_adjacent_arc(Node * p, Pred & pred) const
670  {
671  return none_it(const_me().arcs_begin(p), const_me().arcs_end(p), pred);
672  }
673 
674  template <class Pred>
675  bool none_adjacent_arc(Node * p, Pred && pred) const
676  {
677  return none_it(const_me().arcs_begin(p), const_me().arcs_end(p),
678  std::forward<Pred>(pred));
679  }
680 
681  template <class Pred>
682  Arc * search_adjacent_arc(Node * p, Pred & pred) const
683  {
684  for (auto it = const_me().arcs_begin(p);
685  it != const_me().arcs_end(p); ++it)
686  if (pred(*it))
687  return *it;
688  return nullptr;
689  }
690 
691  template <class Pred>
692  Arc * search_adjacent_arc(Node * p, Pred && pred) const
693  {
694  return search_adjacent_arc<Pred>(p, pred);
695  }
696 
697  template <class Pred>
698  bool remove_first_adjacent_arc_if(Node * p, Pred & pred)
699  {
700  return remove_first_if_it(me().arcs_begin(p), me().arcs_end(p), pred);
701  }
702 
703  template <class Pred>
704  bool remove_first_adjacent_arc_if(Node * p, Pred && pred)
705  {
706  return remove_first_if_it(me().arcs_begin(p), me().arcs_end(p),
707  std::forward<Pred>(pred));
708  }
709 
710  template <class Pred>
711  void remove_adjacent_arc_if(Node * p, Pred & pred)
712  {
713  remove_if_it(me().arcs_begin(p), me().arcs_end(p), pred);
714  }
715 
716  template <class Pred>
717  void remove_adjacent_arc_if(Node * p, Pred && pred)
718  {
719  remove_if_it(me().arcs_begin(p), me().arcs_end(p),
720  std::forward<Pred>(pred));
721  }
722 
723  SLList<Arc *> adjacent_arcs(Node * p) const
724  {
725  return to_list_it<Arc *>(const_me().arcs_begin(p), const_me().arcs_end(p));
726  }
727 
728  void reset_all_node_tag(GraphTag tag) const
729  {
730  for_each_node([&tag](Node * node)
731  {
732  node->unvisit(tag);
733  });
734  }
735 
736  void reset_all_node_tag() const
737  {
738  for_each_node([](Node * node)
739  {
740  node->reset_tag();
741  });
742  }
743 
744  void reset_all_arc_tag(GraphTag tag) const
745  {
746  for_each_arc([&tag](Arc * arc)
747  {
748  arc->unvisit(tag);
749  });
750  }
751 
752  void reset_all_arc_tag() const
753  {
754  for_each_arc([](Arc * arc)
755  {
756  arc->reset_tag();
757  });
758  }
759 
760  void reset_tag(GraphTag tag) const
761  {
762  reset_all_node_tag(tag);
763  reset_all_arc_tag(tag);
764  }
765 
766  void reset_all_tags() const
767  {
768  reset_all_node_tag();
769  reset_all_arc_tag();
770  }
771 
772  void reset_node_cookies() const
773  {
774  for_each_node([](Node * node)
775  {
776  node->cookie() = nullptr;
777  });
778  }
779 
780  void reset_arc_cookies() const
781  {
782  for_each_arc([](Arc * arc)
783  {
784  arc->cookie() = nullptr;
785  });
786  }
787 
788  void reset_node_counter() const
789  {
790  for_each_node([](Node * node)
791  {
792  node->counter() = 0;
793  });
794  }
795 
796  void reset_arc_counter() const
797  {
798  for_each_arc([](Arc * arc)
799  {
800  arc->counter() = 0;
801  });
802  }
803 
804  void reset_counters() const
805  {
806  reset_node_counter();
807  reset_arc_counter();
808  }
809 
810  void reset_cookies() const
811  {
812  reset_node_cookies();
813  reset_arc_cookies();
814  }
815 
816  void reset_nodes() const
817  {
818  for_each_node([](Node * node)
819  {
820  node->reset();
821  });
822  }
823 
824  void reset_arcs() const
825  {
826  for_each_arc([](Arc * arc)
827  {
828  arc->reset();
829  });
830  }
831  };
832 
833  template <typename GT, class Node, class Arc,
834  typename NodeInfoType, typename ArcInfoType>
836  copy_graph(const GT & src, GT & tgt)
837  {
839 
840  for (auto it = src.nodes_begin(); it != src.nodes_end(); ++it)
841  {
842  Node * p = it.get_current();
843  Node * q = tgt.insert_node(p->get_info());
844  map_nodes[p] = q;
845  }
846 
847  for (auto it = src.arcs_begin(); it != src.arcs_end(); ++it)
848  {
849  Arc * a = it.get_current();
850  Node * ssrc = a->get_src_node();
851  Node * stgt = a->get_tgt_node();
852  Node * tsrc = map_nodes[ssrc];
853  Node * ttgt = map_nodes[stgt];
854  tgt.insert_arc(tsrc, ttgt, a->get_info());
855  }
856  }
857 
858  template <typename NodeInfo,
859  typename ArcInfo = EmptyClass,
860  typename GraphInfo = EmptyClass>
861  class Graph : public BaseGraph<Graph<NodeInfo, ArcInfo, GraphInfo>,
862  GraphNode<NodeInfo, ArcInfo, GraphInfo>,
863  GraphArc<
864  GraphNode<NodeInfo, ArcInfo, GraphInfo>,
865  NodeInfo, ArcInfo, GraphInfo>,
866  NodeInfo, ArcInfo>
867  {
870  GraphArc<
871  GraphNode<NodeInfo, ArcInfo, GraphInfo>,
872  NodeInfo, ArcInfo, GraphInfo>,
873  NodeInfo, ArcInfo>;
874 
875  public:
876  using NodeInfoType = NodeInfo;
877  using ArcInfoType = ArcInfo;
878  using GraphInfoType = GraphInfo;
880 
881  using Node = GraphNode<NodeInfo, ArcInfo, GraphInfo>;
883 
884  protected:
886  using GArc = DLNode<Arc>;
888 
889  static GNode * dl_to_node(DL * ptr)
890  {
891  return static_cast<GNode *>(ptr);
892  }
893 
894  static GArc * dl_to_arc(DL * ptr)
895  {
896  return static_cast<GArc *>(ptr);
897  }
898 
899  static GAdArc * dl_to_adjacent_arc(DL * ptr)
900  {
901  return static_cast<GAdArc *>(ptr);
902  }
903 
904  static GNode * to_gnode(Node * node)
905  {
906  GNode * node_zero = 0;
907  nat_t off_set = (nat_t) &node_zero->get_item();
908  nat_t node_address = (nat_t) node;
909  return (GNode *) (node_address - off_set);
910  }
911 
912  static GArc * to_garc(Arc * arc)
913  {
914  GArc * arc_zero = 0;
915  nat_t off_set = (nat_t) &arc_zero->get_item();
916  nat_t arc_address = (nat_t) arc;
917  return (GArc *) (arc_address - off_set);
918  }
919 
920  GraphInfo info;
925 
927  {
928  node_list.insert_prev(p);
929  ++num_nodes;
930  return p;
931  }
932 
933  GArc * insert_garc(Node * src, Node * tgt)
934  {
935  GArc * arc = new GArc(Arc(src, tgt));
936 
937  GAdArc * arc_in_src_node = new GAdArc(arc);
938 
939  arc->get_item().arc_in_src_node = arc_in_src_node;
940  src->adjacent_arc_list.insert_prev(arc_in_src_node);
941  ++src->num_arcs;
942 
943  if (src == tgt)
944  arc->get_item().arc_in_tgt_node = arc_in_src_node;
945  else
946  {
947  GAdArc * arc_in_tgt_node = new GAdArc(arc);
948  arc->get_item().arc_in_tgt_node = arc_in_tgt_node;
949  tgt->adjacent_arc_list.insert_prev(arc_in_tgt_node);
950  ++tgt->num_arcs;
951  }
952 
953  arc_list.insert_prev(arc);
954  ++num_arcs;
955  return arc;
956  }
957 
958  void remove_arc(GArc * arc)
959  {
960  Node * src_node = arc->get_item().src_node;
961 
962  GAdArc * arc_in_src_node = arc->get_item().arc_in_src_node;
963  arc_in_src_node->del();
964  --src_node->num_arcs;
965  delete arc_in_src_node;
966 
967  Node * tgt_node = arc->get_item().tgt_node;
968 
969  if (src_node != tgt_node)
970  {
971  GAdArc * arc_in_tgt_node = arc->get_item().arc_in_tgt_node;
972  arc_in_tgt_node->del();
973  --tgt_node->num_arcs;
974  delete arc_in_tgt_node;
975  }
976 
977  arc->del();
978  --num_arcs;
979  delete arc;
980  }
981 
982  void remove_node(GNode *);
983 
984  public:
986  : info(), num_nodes(0), node_list(), num_arcs(0), arc_list()
987  {
988  // empty
989  }
990 
991  Graph(const GraphInfo & _info)
992  : info(_info), num_nodes(0), num_arcs(0)
993  {
994  // empty
995  }
996 
997  Graph(GraphInfo && _info)
998  : info(std::move(_info)), num_nodes(0), num_arcs(0)
999  {
1000  // empty
1001  }
1002 
1003  Graph(const Graph & g)
1004  : info(g.info), num_nodes(0), num_arcs(0)
1005  {
1006  Base::copy_graph(g, *this);
1007  }
1008 
1009  Graph(Graph && g)
1010  : Graph()
1011  {
1012  swap(g);
1013  }
1014 
1016  {
1017  clear();
1018  }
1019 
1020  Graph & operator = (const Graph & g)
1021  {
1022  if (this == &g)
1023  return *this;
1024 
1025  clear();
1026  Base::copy_graph(g, *this);
1027  info = g.info;
1028 
1029  return *this;
1030  }
1031 
1032  Graph & operator = (Graph && g)
1033  {
1034  swap(g);
1035  return *this;
1036  }
1037 
1038  void swap(Graph & g)
1039  {
1040  std::swap(info, g.info);
1041  std::swap(num_nodes, g.num_nodes);
1042  node_list.swap(g.node_list);
1043  std::swap(num_arcs, g.num_arcs);
1044  arc_list.swap(g.arc_list);
1045  }
1046 
1047  void clear();
1048 
1049  GraphInfo & get_info()
1050  {
1051  return info;
1052  }
1053 
1054  const GraphInfo & get_info() const
1055  {
1056  return info;
1057  }
1058 
1060  {
1061  if (node_list.is_empty())
1062  throw std::underflow_error("Graph has not nodes");
1063 
1064  return &dl_to_node(node_list.get_next())->get_item();
1065  }
1066 
1067  Node * get_first_node() const
1068  {
1069  if (node_list.is_empty())
1070  throw std::underflow_error("Graph has not nodes");
1071 
1072  return &dl_to_node(const_cast<DL &>(node_list).get_next())->get_item();
1073  }
1074 
1076  {
1077  if (arc_list.is_empty())
1078  throw std::underflow_error("Graph has not arcs");
1079 
1080  return &dl_to_arc(arc_list.get_next())->get_item();
1081  }
1082 
1083  Arc * get_first_arc() const
1084  {
1085  if (arc_list.is_empty())
1086  throw std::underflow_error("Graph has not arcs");
1087 
1088  return &dl_to_arc(const_cast<DL &>(arc_list).get_next())->get_item();
1089  }
1090 
1092  {
1093  return num_nodes;
1094  }
1095 
1097  {
1098  return num_arcs;
1099  }
1100 
1101  Node * insert_node()
1102  {
1103  GNode * node = insert_gnode(new GNode);
1104  return &node->get_item();
1105  }
1106 
1107  Node * insert_node(const NodeInfo & info)
1108  {
1109  GNode * node = insert_gnode(new GNode(Node(info)));
1110  return &node->get_item();
1111  }
1112 
1113  Node * insert_node(NodeInfo && info)
1114  {
1115  GNode * node = insert_gnode(new GNode(Node(std::forward<NodeInfo>(info))));
1116  return &node->get_item();
1117  }
1118 
1119  Arc * insert_arc(Node * s, Node * t)
1120  {
1121  GArc * arc = insert_garc(s, t);
1122  return &arc->get_item();
1123  }
1124 
1125  Arc * insert_arc(Node * src, Node * tgt, const ArcInfo & info)
1126  {
1127  Arc * arc = insert_arc(src, tgt);
1128  arc->get_info() = info;
1129  return arc;
1130  }
1131 
1132  Arc * insert_arc(Node * src, Node * tgt, ArcInfo && info)
1133  {
1134  Arc * arc = insert_arc(src, tgt);
1135  arc->get_info() = std::move(info);
1136  return arc;
1137  }
1138 
1139  void remove_arc(Arc * a)
1140  {
1141  GArc * arc = to_garc(a);
1142  remove_arc(arc);
1143  }
1144 
1145  void remove_node(Node * n)
1146  {
1147  GNode * node = to_gnode(n);
1148  remove_node(node);
1149  }
1150 
1151  class NodeIterator : public DL::Iterator,
1152  public BidirectionalIterator<NodeIterator, Node *, true>
1153  {
1154  friend class BasicIterator<NodeIterator, Node *, true>;
1155 
1156  using Base = DL::Iterator;
1157 
1158  Graph * graph_ptr;
1159 
1160  public:
1162  : Base(), graph_ptr(nullptr)
1163  {
1164  // empty
1165  }
1166 
1167  NodeIterator(const Graph & g)
1168  : Base(const_cast<DL *>(&g.node_list)),
1169  graph_ptr(const_cast<Graph *>(&g))
1170  {
1171  // empty
1172  }
1173 
1174  NodeIterator(const Graph & g, DL * curr)
1175  : Base(const_cast<DL *>(&g.node_list), curr),
1176  graph_ptr(const_cast<Graph *>(&g))
1177  {
1178  // empty
1179  }
1180 
1182  : Base(it), graph_ptr(it.graph_ptr)
1183  {
1184  // empty
1185  }
1186 
1188  : NodeIterator()
1189  {
1190  swap(it);
1191  }
1192 
1193  NodeIterator & operator = (const NodeIterator & it)
1194  {
1195  if (this == &it)
1196  return *this;
1197 
1198  (Base &) *this = it;
1199  graph_ptr = it.graph_ptr;
1200  return *this;
1201  }
1202 
1203  NodeIterator & operator = (NodeIterator && it)
1204  {
1205  swap(it);
1206  return *this;
1207  }
1208 
1209  void swap(NodeIterator & it)
1210  {
1211  Base::swap(it);
1212  std::swap(graph_ptr, it.graph_ptr);
1213  }
1214 
1215  Node * get_current()
1216  {
1217  return &dl_to_node(Base::get_current())->get_item();
1218  }
1219 
1220  Node * get_current() const
1221  {
1222  return &dl_to_node(Base::get_current())->get_item();
1223  }
1224 
1225  void del()
1226  {
1227  if (not Base::has_current())
1228  throw std::overflow_error("There is not current element");
1229 
1230  GNode * p = dl_to_node(Base::get_current());
1231  Base::next();
1232  graph_ptr->remove_node(p);
1233  }
1234  };
1235 
1236  class ArcIterator : public DL::Iterator,
1237  public BidirectionalIterator<ArcIterator, Arc *, true>
1238  {
1239  friend class BasicIterator<ArcIterator, Arc *, true>;
1240 
1241  using Base = DL::Iterator;
1242 
1243  Graph * graph_ptr;
1244 
1245  public:
1247  : Base(), graph_ptr(nullptr)
1248  {
1249  // empty
1250  }
1251 
1252  ArcIterator(const Graph & g)
1253  : Base(const_cast<DL *>(&g.arc_list)),
1254  graph_ptr(const_cast<Graph *>(&g))
1255  {
1256  // empty
1257  }
1258 
1259  ArcIterator(const Graph & g, DL * curr)
1260  : Base(const_cast<DL *>(&g.arc_list), curr),
1261  graph_ptr(const_cast<Graph *>(&g))
1262  {
1263  // empty
1264  }
1265 
1267  : Base(it), graph_ptr(it.graph_ptr)
1268  {
1269  // empty
1270  }
1271 
1273  : ArcIterator()
1274  {
1275  swap(it);
1276  }
1277 
1278  ArcIterator & operator = (const ArcIterator & it)
1279  {
1280  if (this == &it)
1281  return *this;
1282 
1283  (Base &) *this = it;
1284  graph_ptr = it.graph_ptr;
1285  return *this;
1286  }
1287 
1288  ArcIterator & operator = (ArcIterator && it)
1289  {
1290  swap(it);
1291  return *this;
1292  }
1293 
1294  void swap(ArcIterator & it)
1295  {
1296  Base::swap(it);
1297  std::swap(graph_ptr, it.graph_ptr);
1298  }
1299 
1300  Arc * get_current()
1301  {
1302  return &dl_to_arc(Base::get_current())->get_item();
1303  }
1304 
1305  Arc * get_current() const
1306  {
1307  return &dl_to_arc(Base::get_current())->get_item();
1308  }
1309 
1310  void del()
1311  {
1312  if (not Base::has_current())
1313  throw std::overflow_error("There is not current element");
1314 
1315  GArc * a = dl_to_arc(Base::get_current());
1316  Base::next();
1317  graph_ptr->remove_arc(a);
1318  }
1319  };
1320 
1322  public BidirectionalIterator<AdjacentArcIterator,
1323  Arc *, true>
1324  {
1325  friend class BasicIterator<AdjacentArcIterator, Arc *, true>;
1326 
1327  using Base = DL::Iterator;
1328 
1329  Graph * graph_ptr;
1330  Node * node_ptr;
1331 
1332  public:
1334  : Base(), graph_ptr(nullptr), node_ptr(nullptr)
1335  {
1336  // empty
1337  }
1338 
1339  AdjacentArcIterator(const Graph & g, Node * n)
1340  : Base(const_cast<DL *>(&n->adjacent_arc_list)),
1341  graph_ptr(const_cast<Graph *>(&g)), node_ptr(n)
1342  {
1343  // empty
1344  }
1345 
1346  AdjacentArcIterator(const Graph & g, Node * n, DL * curr)
1347  : Base(const_cast<DL *>(&n->adjacent_arc_list), curr),
1348  graph_ptr(const_cast<Graph *>(&g)), node_ptr(n)
1349  {
1350  // empty
1351  }
1352 
1354  : Base(it), graph_ptr(it.graph_ptr), node_ptr(it.node_ptr)
1355  {
1356  // empty
1357  }
1358 
1361  {
1362  swap(it);
1363  }
1364 
1365  AdjacentArcIterator & operator = (const AdjacentArcIterator & it)
1366  {
1367  if (this == &it)
1368  return *this;
1369 
1370  (Base &) *this = it;
1371  graph_ptr = it.graph_ptr;
1372  node_ptr = it.node_ptr;
1373  return *this;
1374  }
1375 
1377  {
1378  swap(it);
1379  return *this;
1380  }
1381 
1383  {
1384  Base::swap(it);
1385  std::swap(graph_ptr, it.graph_ptr);
1386  std::swap(node_ptr, it.node_ptr);
1387  }
1388 
1389  Arc * get_current()
1390  {
1391  return &dl_to_adjacent_arc(Base::get_current())->get_item()->get_item();
1392  }
1393 
1394  Arc * get_current() const
1395  {
1396  return &dl_to_adjacent_arc(Base::get_current())->get_item()->get_item();
1397  }
1398 
1399  Node * get_src_node()
1400  {
1401  return node_ptr;
1402  }
1403 
1404  Node * get_src_node() const
1405  {
1406  return node_ptr;
1407  }
1408 
1409  Node * get_tgt_node()
1410  {
1411  return get_current()->get_connected_node(node_ptr);
1412  }
1413 
1414  const Node * get_tgt_node() const
1415  {
1416  return get_current()->get_connected_node(node_ptr);
1417  }
1418  };
1419 
1421  {
1422  return NodeIterator(*this);
1423  }
1424 
1426  {
1427  return NodeIterator(*this);
1428  }
1429 
1431  {
1432  return NodeIterator(*this, &node_list);
1433  }
1434 
1435  const NodeIterator nodes_end() const
1436  {
1437  return NodeIterator(*this, const_cast<DL *>(&node_list));
1438  }
1439 
1441  {
1442  return ArcIterator(*this);
1443  }
1444 
1445  const ArcIterator arcs_begin() const
1446  {
1447  return ArcIterator(*this);
1448  }
1449 
1451  {
1452  return ArcIterator(*this, &arc_list);
1453  }
1454 
1455  const ArcIterator arcs_end() const
1456  {
1457  return ArcIterator(*this, const_cast<DL *>(&arc_list));
1458  }
1459 
1461  {
1462  return AdjacentArcIterator(*this, p);
1463  }
1464 
1465  const AdjacentArcIterator arcs_begin(Node * p) const
1466  {
1467  return AdjacentArcIterator(*this, p);
1468  }
1469 
1471  {
1472  return AdjacentArcIterator(*this, p, &p->adjacent_arc_list);
1473  }
1474 
1475  const AdjacentArcIterator arcs_end(Node * p) const
1476  {
1477  return AdjacentArcIterator(*this, p,
1478  const_cast<DL *>(&p->adjacent_arc_list));
1479  }
1480 
1481  Arc * search_arc(Node *, Node *);
1482 
1483  template <class Cmp>
1484  void sort_nodes(Cmp & cmp)
1485  {
1486  using C = PtrCmp<Node, Cmp>;
1487  C c(cmp);
1488  quicksort<Node, C>(*dl_to_node(&node_list), c);
1489  }
1490 
1491  template <class Cmp>
1492  void sort_nodes(Cmp && cmp = Cmp())
1493  {
1494  sort_nodes<Cmp>(cmp);
1495  }
1496 
1497  template <class Cmp>
1498  void sort_arcs(Cmp & cmp)
1499  {
1500  using C = PtrCmp<Arc, Cmp>;
1501  C c(cmp);
1502  quicksort<Arc, C>(*dl_to_arc(&arc_list), c);
1503  }
1504 
1505  template <class Cmp>
1506  void sort_arcs(Cmp && cmp = Cmp())
1507  {
1508  sort_arcs<Cmp>(cmp);
1509  }
1510 
1511  bool is_digraph() const { return false; }
1512  };
1513 
1514  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
1516  {
1517  DL & l = node->get_item().adjacent_arc_list;
1518 
1519  while (not l.is_empty())
1520  {
1521  GAdArc * adjacent_arc = dl_to_adjacent_arc(l.get_next());
1522  GArc * arc = adjacent_arc->get_item();
1523  remove_arc(arc);
1524  }
1525 
1526  node->del();
1527  --num_nodes;
1528  delete node;
1529  }
1530 
1531  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
1533  {
1534  while (not node_list.is_empty())
1535  {
1536  GNode * node = dl_to_node(node_list.get_next());
1537  remove_node(node);
1538  }
1539  }
1540 
1541  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
1544  {
1545  for (AdjacentArcIterator it(*this, s); it.has_current(); it.next())
1546  if (it.get_tgt_node() == t)
1547  return *it;
1548 
1549  for (AdjacentArcIterator it(*this, t); it.has_current(); it.next())
1550  if (it.get_tgt_node() == s)
1551  return *it;
1552 
1553  return nullptr;
1554  }
1555 
1556  template <typename NodeInfo,
1557  typename ArcInfo = EmptyClass,
1558  typename GraphInfo = EmptyClass>
1559  class Digraph : public BaseGraph<Digraph<NodeInfo, ArcInfo, GraphInfo>,
1560  DigraphNode<NodeInfo, ArcInfo, GraphInfo>,
1561  DigraphArc<
1562  DigraphNode<NodeInfo, ArcInfo, GraphInfo>,
1563  NodeInfo, ArcInfo, GraphInfo>,
1564  NodeInfo, ArcInfo>
1565  {
1568  DigraphArc<
1569  DigraphNode<NodeInfo, ArcInfo, GraphInfo>,
1570  NodeInfo, ArcInfo, GraphInfo>,
1571  NodeInfo, ArcInfo>;
1572 
1573  public:
1574  using NodeInfoType = NodeInfo;
1575  using ArcInfoType = ArcInfo;
1576  using GraphInfoType = GraphInfo;
1578 
1579  using Node = DigraphNode<NodeInfo, ArcInfo, GraphInfo>;
1581 
1582  protected:
1586 
1587  static GNode * dl_to_node(DL * ptr)
1588  {
1589  return static_cast<GNode *>(ptr);
1590  }
1591 
1592  static GArc * dl_to_arc(DL * ptr)
1593  {
1594  return static_cast<GArc *>(ptr);
1595  }
1596 
1597  static GAdArc * dl_to_adjacent_arc(DL * ptr)
1598  {
1599  return static_cast<GAdArc *>(ptr);
1600  }
1601 
1602  static GNode * to_gnode(Node * node)
1603  {
1604  GNode * node_zero = 0;
1605  nat_t off_set = (nat_t) &node_zero->get_item();
1606  nat_t node_address = (nat_t) node;
1607  return (GNode *) (node_address - off_set);
1608  }
1609 
1610  static GAdArc * to_garc(Arc * arc)
1611  {
1612  GAdArc * arc_zero = 0;
1613  nat_t off_set = (nat_t) &arc_zero->get_item();
1614  nat_t arc_address = (nat_t) arc;
1615  return (GAdArc *) (arc_address - off_set);
1616  }
1617 
1618  GraphInfo info;
1623 
1625  {
1626  node_list.insert_prev(p);
1627  ++num_nodes;
1628  return p;
1629  }
1630 
1631  GAdArc * insert_garc(Node * src, Node * tgt)
1632  {
1633  GAdArc * arc = new GAdArc(Arc(src, tgt));
1634 
1635  GArc * arc_in_arc_list = new GArc(arc);
1636 
1637  arc->get_item().arc_in_arc_list = arc_in_arc_list;
1638  src->adjacent_arc_list.insert_prev(arc);
1639  ++src->num_arcs;
1640 
1641  arc_list.insert_prev(arc_in_arc_list);
1642  ++num_arcs;
1643  return arc;
1644  }
1645 
1646  void remove_arc(GAdArc * arc)
1647  {
1648  GArc * arc_in_arc_list = arc->get_item().arc_in_arc_list;
1649 
1650  arc_in_arc_list->del();
1651  --num_arcs;
1652  delete arc_in_arc_list;
1653 
1654  Node * src_node = arc->get_item().src_node;
1655 
1656  arc->del();
1657  --src_node->num_arcs;
1658  delete arc;
1659  }
1660 
1661  void remove_node(GNode *);
1662 
1663  public:
1665  : info(), num_nodes(0), node_list(), num_arcs(0), arc_list()
1666  {
1667  // empty
1668  }
1669 
1670  Digraph(const GraphInfo & _info)
1671  : info(_info), num_nodes(0), num_arcs(0)
1672  {
1673  // empty
1674  }
1675 
1676  Digraph(GraphInfo && _info)
1677  : info(std::move(_info)), num_nodes(0), num_arcs(0)
1678  {
1679  // empty
1680  }
1681 
1682  Digraph(const Digraph & g)
1683  : info(g.info), num_nodes(0), num_arcs(0)
1684  {
1685  copy_graph(g, *this);
1686  }
1687 
1689  : Digraph()
1690  {
1691  swap(g);
1692  }
1693 
1695  {
1696  clear();
1697  }
1698 
1699  Digraph & operator = (const Digraph & g)
1700  {
1701  if (this == &g)
1702  return *this;
1703 
1704  clear();
1705  Base::copy_graph(g, *this);
1706  info = g.info;
1707 
1708  return *this;
1709  }
1710 
1711  Digraph & operator = (Digraph && g)
1712  {
1713  swap(g);
1714  return *this;
1715  }
1716 
1717  void swap(Digraph & g)
1718  {
1719  std::swap(info, g.info);
1720  std::swap(num_nodes, g.num_nodes);
1721  node_list.swap(g.node_list);
1722  std::swap(num_arcs, g.num_arcs);
1723  arc_list.swap(g.arc_list);
1724  }
1725 
1726  void clear();
1727 
1728  GraphInfo & get_info()
1729  {
1730  return info;
1731  }
1732 
1733  const GraphInfo & get_info() const
1734  {
1735  return info;
1736  }
1737 
1739  {
1740  if (node_list.is_empty())
1741  throw std::underflow_error("Graph has not nodes");
1742 
1743  return &dl_to_node(node_list.get_next())->get_item();
1744  }
1745 
1746  Node * get_first_node() const
1747  {
1748  if (node_list.is_empty())
1749  throw std::underflow_error("Graph has not nodes");
1750 
1751  return &dl_to_node(const_cast<DL &>(node_list).get_next())->get_item();
1752  }
1753 
1755  {
1756  if (arc_list.is_empty())
1757  throw std::underflow_error("Graph has not arcs");
1758 
1759  return &dl_to_arc(arc_list.get_next())->get_item()->get_item();
1760  }
1761 
1762  Arc * get_first_arc() const
1763  {
1764  if (arc_list.is_empty())
1765  throw std::underflow_error("Graph has not arcs");
1766 
1767  return &dl_to_arc(const_cast<DL &>(arc_list).get_next())->get_item()
1768  ->get_item();
1769  }
1770 
1772  {
1773  return num_nodes;
1774  }
1775 
1777  {
1778  return num_arcs;
1779  }
1780 
1781  Node * insert_node()
1782  {
1783  GNode * node = insert_gnode(new GNode);
1784  return &node->get_item();
1785  }
1786 
1787  Node * insert_node(const NodeInfo & info)
1788  {
1789  GNode * node = insert_gnode(new GNode(Node(info)));
1790  return &node->get_item();
1791  }
1792 
1793  Node * insert_node(NodeInfo && info)
1794  {
1795  GNode * node = insert_gnode(new GNode(Node(std::forward<NodeInfo>(info))));
1796  return &node->get_item();
1797  }
1798 
1799  Arc * insert_arc(Node * s, Node * t)
1800  {
1801  GAdArc * arc = insert_garc(s, t);
1802  return &arc->get_item();
1803  }
1804 
1805  Arc * insert_arc(Node * src, Node * tgt, const ArcInfo & info)
1806  {
1807  Arc * arc = insert_arc(src, tgt);
1808  arc->get_info() = info;
1809  return arc;
1810  }
1811 
1812  Arc * insert_arc(Node * src, Node * tgt, ArcInfo && info)
1813  {
1814  Arc * arc = insert_arc(src, tgt);
1815  arc->get_info() = std::move(info);
1816  return arc;
1817  }
1818 
1819  void remove_arc(Arc * a)
1820  {
1821  GAdArc * arc = to_garc(a);
1822  remove_arc(arc);
1823  }
1824 
1825  void remove_node(Node * n)
1826  {
1827  GNode * node = to_gnode(n);
1828  remove_node(node);
1829  }
1830 
1831  class NodeIterator : public DL::Iterator,
1832  public BidirectionalIterator<NodeIterator, Node *, true>
1833  {
1834  friend class BasicIterator<NodeIterator, Node *, true>;
1835 
1836  using Base = DL::Iterator;
1837 
1838  Digraph * graph_ptr;
1839 
1840  public:
1842  : Base(), graph_ptr(nullptr)
1843  {
1844  // empty
1845  }
1846 
1848  : Base(const_cast<DL *>(&g.node_list)),
1849  graph_ptr(const_cast<Digraph *>(&g))
1850  {
1851  // empty
1852  }
1853 
1854  NodeIterator(const Digraph & g, DL * curr)
1855  : Base(const_cast<DL *>(&g.node_list), curr),
1856  graph_ptr(const_cast<Digraph *>(&g))
1857  {
1858  // empty
1859  }
1860 
1862  : Base(it), graph_ptr(it.graph_ptr)
1863  {
1864  // empty
1865  }
1866 
1868  : NodeIterator()
1869  {
1870  swap(it);
1871  }
1872 
1873  NodeIterator & operator = (const NodeIterator & it)
1874  {
1875  if (this == &it)
1876  return *this;
1877 
1878  (Base &) *this = it;
1879  graph_ptr = it.graph_ptr;
1880  return *this;
1881  }
1882 
1883  NodeIterator & operator = (NodeIterator && it)
1884  {
1885  swap(it);
1886  return *this;
1887  }
1888 
1889  void swap(NodeIterator & it)
1890  {
1891  Base::swap(it);
1892  std::swap(graph_ptr, it.graph_ptr);
1893  }
1894 
1895  Node * get_current()
1896  {
1897  return &dl_to_node(Base::get_current())->get_item();
1898  }
1899 
1900  Node * get_current() const
1901  {
1902  return &dl_to_node(Base::get_current())->get_item();
1903  }
1904 
1905  void del()
1906  {
1907  if (not Base::has_current())
1908  throw std::overflow_error("There is not current element");
1909 
1910  GNode * p = dl_to_node(Base::get_current());
1911  Base::next();
1912  graph_ptr->remove_node(p);
1913  }
1914  };
1915 
1916  class ArcIterator : public DL::Iterator,
1917  public BidirectionalIterator<ArcIterator, Arc *, true>
1918  {
1919  friend class BasicIterator<ArcIterator, Arc *, true>;
1920 
1921  using Base = DL::Iterator;
1922 
1923  Digraph * graph_ptr;
1924 
1925  public:
1927  : Base(), graph_ptr(nullptr)
1928  {
1929  // empty
1930  }
1931 
1933  : Base(const_cast<DL *>(&g.arc_list)),
1934  graph_ptr(const_cast<Digraph *>(&g))
1935  {
1936  // empty
1937  }
1938 
1939  ArcIterator(const Digraph & g, DL * curr)
1940  : Base(const_cast<DL *>(&g.arc_list), curr),
1941  graph_ptr(const_cast<Digraph *>(&g))
1942  {
1943  // empty
1944  }
1945 
1947  : Base(it), graph_ptr(it.graph_ptr)
1948  {
1949  // empty
1950  }
1951 
1953  : ArcIterator()
1954  {
1955  swap(it);
1956  }
1957 
1958  ArcIterator & operator = (const ArcIterator & it)
1959  {
1960  if (this == &it)
1961  return *this;
1962 
1963  (Base &) *this = it;
1964  graph_ptr = it.graph_ptr;
1965  return *this;
1966  }
1967 
1968  ArcIterator & operator = (ArcIterator && it)
1969  {
1970  swap(it);
1971  return *this;
1972  }
1973 
1974  void swap(ArcIterator & it)
1975  {
1976  Base::swap(it);
1977  std::swap(graph_ptr, it.graph_ptr);
1978  }
1979 
1980  Arc * get_current()
1981  {
1982  return &dl_to_arc(Base::get_current())->get_item()->get_item();
1983  }
1984 
1985  Arc * get_current() const
1986  {
1987  return &dl_to_arc(Base::get_current())->get_item()->get_item();
1988  }
1989 
1990  void del()
1991  {
1992  if (not Base::has_current())
1993  throw std::overflow_error("There is not current element");
1994 
1995  GArc * a = dl_to_arc(Base::get_current());
1996  Base::next();
1997  graph_ptr->remove_arc(a->get_item());
1998  }
1999  };
2000 
2002  : public DL::Iterator,
2003  public BidirectionalIterator<AdjacentArcIterator, Arc *, true>
2004  {
2005  friend class BasicIterator<AdjacentArcIterator, Arc *, true>;
2006 
2007  using Base = DL::Iterator;
2008 
2009  Digraph * graph_ptr;
2010  Node * node_ptr;
2011 
2012  public:
2014  : Base(), graph_ptr(nullptr), node_ptr(nullptr)
2015  {
2016  // empty
2017  }
2018 
2019  AdjacentArcIterator(const Digraph & g, Node * n)
2020  : Base(const_cast<DL *>(&n->adjacent_arc_list)),
2021  graph_ptr(const_cast<Digraph *>(&g)), node_ptr(n)
2022  {
2023  // empty
2024  }
2025 
2026  AdjacentArcIterator(const Digraph & g, Node * n, DL * curr)
2027  : Base(const_cast<DL *>(&n->adjacent_arc_list), curr),
2028  graph_ptr(const_cast<Digraph *>(&g)), node_ptr(n)
2029  {
2030  // empty
2031  }
2032 
2034  : Base(it), graph_ptr(it.graph_ptr), node_ptr(it.node_ptr)
2035  {
2036  // empty
2037  }
2038 
2041  {
2042  swap(it);
2043  }
2044 
2045  AdjacentArcIterator & operator = (const AdjacentArcIterator & it)
2046  {
2047  if (this == &it)
2048  return *this;
2049 
2050  (Base &) *this = it;
2051  graph_ptr = it.graph_ptr;
2052  node_ptr = it.node_ptr;
2053  return *this;
2054  }
2055 
2057  {
2058  swap(it);
2059  return *this;
2060  }
2061 
2063  {
2064  Base::swap(it);
2065  std::swap(graph_ptr, it.graph_ptr);
2066  std::swap(node_ptr, it.node_ptr);
2067  }
2068 
2069  Arc * get_current()
2070  {
2071  return &dl_to_adjacent_arc(Base::get_current())->get_item();
2072  }
2073 
2074  const Arc * get_current() const
2075  {
2076  return &dl_to_adjacent_arc(Base::get_current())->get_item();
2077  }
2078 
2079  Node * get_src_node()
2080  {
2081  return node_ptr;
2082  }
2083 
2084  Node * get_src_node() const
2085  {
2086  return node_ptr;
2087  }
2088 
2089  Node * get_tgt_node()
2090  {
2091  return get_current()->get_tgt_node();
2092  }
2093 
2094  const Node * get_tgt_node() const
2095  {
2096  return get_current()->get_tgt_node();
2097  }
2098  };
2099 
2101  {
2102  return NodeIterator(*this);
2103  }
2104 
2106  {
2107  return NodeIterator(*this);
2108  }
2109 
2111  {
2112  return NodeIterator(*this, &node_list);
2113  }
2114 
2115  const NodeIterator nodes_end() const
2116  {
2117  return NodeIterator(*this, const_cast<DL *>(&node_list));
2118  }
2119 
2121  {
2122  return ArcIterator(*this);
2123  }
2124 
2125  const ArcIterator arcs_begin() const
2126  {
2127  return ArcIterator(*this);
2128  }
2129 
2131  {
2132  return ArcIterator(*this, &arc_list);
2133  }
2134 
2135  const ArcIterator arcs_end() const
2136  {
2137  return ArcIterator(*this, const_cast<DL *>(&arc_list));
2138  }
2139 
2141  {
2142  return AdjacentArcIterator(*this, p);
2143  }
2144 
2145  const AdjacentArcIterator arcs_begin(Node * p) const
2146  {
2147  return AdjacentArcIterator(*this, p);
2148  }
2149 
2151  {
2152  return AdjacentArcIterator(*this, p, &p->adjacent_arc_list);
2153  }
2154 
2155  const AdjacentArcIterator arcs_end(Node * p) const
2156  {
2157  return AdjacentArcIterator(*this, p,
2158  const_cast<DL *>(&p->adjacent_arc_list));
2159  }
2160 
2161  Arc * search_arc(Node *, Node *);
2162 
2163  template <class Cmp>
2164  void sort_nodes(Cmp & cmp)
2165  {
2166  quicksort<Node, Cmp>(*dl_to_node(&node_list), cmp);
2167  }
2168 
2169  template <class Cmp>
2170  void sort_nodes(Cmp && cmp = Cmp())
2171  {
2172  sort_nodes<Cmp>(cmp);
2173  }
2174 
2175  template <class Cmp>
2176  void sort_arcs(Cmp & cmp)
2177  {
2178  quicksort<Arc, Cmp>(*dl_to_arc(&arc_list), cmp);
2179  }
2180 
2181  template <class Cmp>
2182  void sort_arcs(Cmp && cmp = Cmp())
2183  {
2184  sort_arcs<Cmp>(cmp);
2185  }
2186 
2187  bool is_digraph() const { return true; }
2188  };
2189 
2190  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
2192  {
2193  DL * curr_link = arc_list.get_next();
2194 
2195  while (curr_link != &arc_list)
2196  {
2197  GArc * arc = dl_to_arc(curr_link);
2198  curr_link = curr_link->get_next(); // Muevo antes de eliminar
2199  if (arc->get_item()->get_item().src_node == &node->get_item() or
2200  arc->get_item()->get_item().tgt_node == &node->get_item())
2201  remove_arc(arc->get_item());
2202  }
2203 
2204  node->del();
2205  --num_nodes;
2206  delete node;
2207  }
2208 
2209  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
2211  {
2212  while (not arc_list.is_empty())
2213  {
2214  GAdArc * arc = dl_to_arc(arc_list.get_next())->get_item();
2215  remove_arc(arc);
2216  }
2217 
2218  while (not node_list.is_empty())
2219  {
2220  GNode * node = dl_to_node(node_list.get_next());
2221  remove_node(node);
2222  }
2223  }
2224 
2225  template <typename NodeInfo, typename ArcInfo, typename GraphInfo>
2228  {
2229  for (AdjacentArcIterator it(*this, s); it.has_current(); it.next())
2230  if (it.get_tgt_node() == t)
2231  return *it;
2232 
2233  return nullptr;
2234  }
2235 
2236 } // end namespace Designar
2237 
2238 # endif // DSGGRAPH_H
RetT fold_arcs(const RetT &init_val, Op &&op=Op()) const
Definition: graph.H:411
Node * insert_node(NodeInfo &&info)
Definition: graph.H:1793
AdjacentArcIterator(const AdjacentArcIterator &it)
Definition: graph.H:2033
ArcIterator arcs_end()
Definition: graph.H:1450
ArcIterator(ArcIterator &&it)
Definition: graph.H:1952
AdjacentArcIterator(const Graph &g, Node *n)
Definition: graph.H:1339
ArcInfo ArcInfoType
Definition: graph.H:1575
void del()
Definition: graph.H:1905
Node * get_src_node()
Definition: graph.H:1399
Definition: nodesdef.H:999
ArcIterator(const ArcIterator &it)
Definition: graph.H:1946
Definition: graph.H:36
const Arc * get_current() const
Definition: graph.H:2074
ContainerRet map_arcs_if(Op &&op, Pred &pred) const
Definition: graph.H:385
~Graph()
Definition: graph.H:1015
void swap(AdjacentArcIterator &it)
Definition: graph.H:1382
Node * get_first_node()
Definition: graph.H:1738
NodeIterator(NodeIterator &&it)
Definition: graph.H:1867
Digraph(const GraphInfo &_info)
Definition: graph.H:1670
bool remove_first_node_if(Pred &&pred=Pred())
Definition: graph.H:285
NodeIterator nodes_begin()
Definition: graph.H:1420
static GNode * to_gnode(Node *node)
Definition: graph.H:1602
ContainerRet map_nodes(Op &op) const
Definition: graph.H:139
void del()
Definition: graph.H:1310
ContainerRet map_nodes_if(Op &op, Pred &pred) const
Definition: graph.H:156
void for_each_adjacent_arc(Node *p, Op &op) const
Definition: graph.H:528
nat_t SetSizeType
Definition: graph.H:879
Arc * get_first_arc()
Definition: graph.H:1754
bool exists_arc(Pred &&pred) const
Definition: graph.H:452
bool none_it(const It &, const It &, Pred &)
Definition: italgorithms.H:325
void sort_nodes(Cmp &cmp)
Definition: graph.H:1484
void remove_node(GNode *)
Definition: graph.H:1515
void clear()
Definition: graph.H:2210
Arc * nth_adjacent_arc(Node *p, nat_t i)
Definition: graph.H:517
bool is_empty() const
Definition: nodesdef.H:153
Definition: graph.H:39
void remove_arc(GArc *arc)
Definition: graph.H:958
Node * get_tgt_node()
Definition: graph.H:1409
ContainerRet filter_arcs(Pred &pred) const
Definition: graph.H:334
AdjacentArcIterator(const Graph &g, Node *n, DL *curr)
Definition: graph.H:1346
ArcIterator()
Definition: graph.H:1246
Definition: nodesdef.H:113
static GAdArc * to_garc(Arc *arc)
Definition: graph.H:1610
Arc * get_current()
Definition: graph.H:1980
ContainerRet map_adjacent_arcs_if(Node *p, Op &&op, Pred &pred) const
Definition: graph.H:594
bool all_adjacent_arcs(Node *p, Pred &&pred) const
Definition: graph.H:649
Definition: sort.H:494
Node * get_first_node() const
Definition: graph.H:1067
T & get_item()
Definition: nodesdef.H:454
void for_each_node(Op &&op=Op()) const
Definition: graph.H:118
Arc * insert_arc(Node *src, Node *tgt, ArcInfo &&info)
Definition: graph.H:1132
void remove_arc(Arc *a)
Definition: graph.H:1819
Definition: graph.H:86
void remove_node_if(Pred &&pred=Pred())
Definition: graph.H:298
Node * search_node(Pred &pred) const
Definition: graph.H:263
bool remove_first_adjacent_arc_if(Node *p, Pred &&pred)
Definition: graph.H:704
AdjacentArcIterator(AdjacentArcIterator &&it)
Definition: graph.H:2039
Graph(const GraphInfo &_info)
Definition: graph.H:991
void del()
Definition: graph.H:1225
ContainerRet map_adjacent_arcs_if(Node *p, Op &op, Pred &&pred=Pred()) const
Definition: graph.H:584
ContainerRet filter_adjacent_arcs(Node *p, Pred &pred) const
Definition: graph.H:541
static GNode * to_gnode(Node *node)
Definition: graph.H:904
Arc * search_arc(Pred &pred) const
Definition: graph.H:472
void for_each_arc(Op &op) const
Definition: graph.H:321
NodeIterator(const Graph &g)
Definition: graph.H:1167
const AdjacentArcIterator arcs_end(Node *p) const
Definition: graph.H:2155
void reset_all_tags() const
Definition: graph.H:766
void sort_nodes(Cmp &cmp)
Definition: graph.H:2164
bool exists_adjacent_arc(Node *p, Pred &&pred) const
Definition: graph.H:662
Digraph(GraphInfo &&_info)
Definition: graph.H:1676
bool remove_first_adjacent_arc_if(Node *p, Pred &pred)
Definition: graph.H:698
void reset_arc_cookies() const
Definition: graph.H:780
const NodeIterator nodes_begin() const
Definition: graph.H:1425
bool exists_arc(Pred &pred) const
Definition: graph.H:446
nat_t num_arcs
Definition: graph.H:923
NodeIterator nodes_end()
Definition: graph.H:1430
bool all_nodes(Pred &&pred=Pred()) const
Definition: graph.H:230
Graph()
Definition: graph.H:985
bool none_arc(Pred &pred) const
Definition: graph.H:459
void sort_nodes(Cmp &&cmp=Cmp())
Definition: graph.H:1492
Definition: iterator.H:36
const NodeIterator nodes_begin() const
Definition: graph.H:2105
SLList< Arc * > arcs() const
Definition: graph.H:512
Arc * search_adjacent_arc(Node *p, Pred &&pred) const
Definition: graph.H:692
RetT fold_nodes(const RetT &init_val, Op &op) const
Definition: graph.H:195
const AdjacentArcIterator arcs_begin(Node *p) const
Definition: graph.H:2145
Definition: graph.H:1151
bool is_digraph() const
Definition: graph.H:2187
void sort_arcs(Cmp &&cmp=Cmp())
Definition: graph.H:2182
ContainerRet map_adjacent_arcs(Node *p, Op &op) const
Definition: graph.H:555
GraphInfo info
Definition: graph.H:1618
Arc * get_first_arc() const
Definition: graph.H:1762
ArcIterator arcs_end()
Definition: graph.H:2130
ContainerRet map_arcs(Op &op) const
Definition: graph.H:348
AdjacentArcIterator(const Digraph &g, Node *n)
Definition: graph.H:2019
AdjacentArcIterator()
Definition: graph.H:2013
static GNode * dl_to_node(DL *ptr)
Definition: graph.H:889
ArcIterator arcs_begin()
Definition: graph.H:2120
DL arc_list
Definition: graph.H:1622
Arc * nth_arc(nat_t i) const
Definition: graph.H:315
GNode * insert_gnode(GNode *p)
Definition: graph.H:926
RetT fold_nodes(RetT &&init_val, Op &&op=Op()) const
Definition: graph.H:216
Arc * get_current() const
Definition: graph.H:1394
ArcIterator(const ArcIterator &it)
Definition: graph.H:1266
RetT fold_adjacent_arcs(Node *p, RetT &&init_val, Op &&op=Op()) const
Definition: graph.H:635
ContainerRet filter_it(const It &, const It &, Pred &)
Definition: italgorithms.H:196
Arc * get_current()
Definition: graph.H:2069
const GraphInfo & get_info() const
Definition: graph.H:1733
Arc * get_current()
Definition: graph.H:1389
nat_t num_arcs
Definition: graph.H:1621
ArcIterator arcs_begin()
Definition: graph.H:1440
ContainerRet filter_adjacent_arcs(Node *p, Pred &&pred=Pred()) const
Definition: graph.H:547
Graph(GraphInfo &&_info)
Definition: graph.H:997
RetT & nth_it(const It &, const It &, nat_t)
Definition: italgorithms.H:172
nat_t get_num_arcs() const
Definition: graph.H:1096
~Digraph()
Definition: graph.H:1694
AdjacentArcIterator arcs_begin(Node *p)
Definition: graph.H:1460
NodeIterator()
Definition: graph.H:1841
bool all_adjacent_arcs(Node *p, Pred &pred) const
Definition: graph.H:643
ArcIterator(const Digraph &g, DL *curr)
Definition: graph.H:1939
ContainerRet map_arcs_if(Op &&op=Op(), Pred &&pred=Pred()) const
Definition: graph.H:395
Definition: graph.H:73
ContainerRet map_nodes_if(Op &op, Pred &&pred=Pred()) const
Definition: graph.H:166
AdjacentArcIterator()
Definition: graph.H:1333
const NodeIterator nodes_end() const
Definition: graph.H:1435
Arc * get_first_arc()
Definition: graph.H:1075
SLList< Node * > nodes() const
Definition: graph.H:304
static GNode * dl_to_node(DL *ptr)
Definition: graph.H:1587
void swap(ArcIterator &it)
Definition: graph.H:1974
AdjacentArcIterator(AdjacentArcIterator &&it)
Definition: graph.H:1359
Definition: graph.H:1321
nat_t num_arcs
Definition: nodesdef.H:1003
ArcIterator(ArcIterator &&it)
Definition: graph.H:1272
Node * get_current()
Definition: graph.H:1215
Node * search_node(Pred &&pred=Pred()) const
Definition: graph.H:273
DL arc_list
Definition: graph.H:924
void map_nodes(Node< SG > *p, Node< TG > *q)
Definition: graphutilities.H:99
Arc * get_current()
Definition: graph.H:1300
void remove_node(Node *n)
Definition: graph.H:1145
ContainerRet filter_nodes(Pred &pred) const
Definition: graph.H:125
ContainerRet map_arcs(Op &&op=Op()) const
Definition: graph.H:356
ContainerRet map_adjacent_arcs_if(Node *p, Op &&op=Op(), Pred &&pred=Pred()) const
Definition: graph.H:605
NodeIterator(NodeIterator &&it)
Definition: graph.H:1187
bool exists_node(Pred &&pred=Pred()) const
Definition: graph.H:243
void del()
Definition: nodesdef.H:208
ArcInfo & get_info()
Definition: nodesdef.H:1123
const AdjacentArcIterator arcs_begin(Node *p) const
Definition: graph.H:1465
NodeIterator nodes_begin()
Definition: graph.H:2100
DL node_list
Definition: graph.H:1620
RetT fold_arcs(RetT &&init_val, Op &&op=Op()) const
Definition: graph.H:425
void sort_arcs(Cmp &cmp)
Definition: graph.H:1498
RetT fold_adjacent_arcs(Node *p, RetT &&init_val, Op &op) const
Definition: graph.H:628
ContainerRet map_nodes(Op &&op=Op()) const
Definition: graph.H:147
const NodeIterator nodes_end() const
Definition: graph.H:2115
bool none_node(Pred &pred) const
Definition: graph.H:250
Node * get_src_node() const
Definition: graph.H:1404
NodeIterator(const Digraph &g, DL *curr)
Definition: graph.H:1854
void reset_arcs() const
Definition: graph.H:824
Arc * get_current() const
Definition: graph.H:1985
Definition: map.H:544
void reset_all_arc_tag() const
Definition: graph.H:752
GraphInfo GraphInfoType
Definition: graph.H:878
ContainerRet filter_arcs(Pred &&pred=Pred()) const
Definition: graph.H:340
Node * get_first_node()
Definition: graph.H:1059
const GraphInfo & get_info() const
Definition: graph.H:1054
GraphInfo info
Definition: graph.H:920
void swap(AdjacentArcIterator &it)
Definition: graph.H:2062
bool exists_it(const It &, const It &, Pred &)
Definition: italgorithms.H:310
ContainerRet map_arcs_if(Op &op, Pred &pred) const
Definition: graph.H:365
void reset_arc_counter() const
Definition: graph.H:796
Node * get_current()
Definition: graph.H:1895
nat_t SetSizeType
Definition: graph.H:1577
Node * get_src_node()
Definition: graph.H:2079
const ArcIterator arcs_end() const
Definition: graph.H:1455
void remove_arc(Arc *a)
Definition: graph.H:1139
bool none_node(Pred &&pred=Pred()) const
Definition: graph.H:256
static GArc * dl_to_arc(DL *ptr)
Definition: graph.H:1592
Arc * get_current() const
Definition: graph.H:1305
typename GT::Node Node
Definition: graphutilities.H:78
static GArc * dl_to_arc(DL *ptr)
Definition: graph.H:894
const ArcIterator arcs_begin() const
Definition: graph.H:2125
ContainerRet map_adjacent_arcs(Node *p, Op &&op=Op()) const
Definition: graph.H:563
NodeIterator(const Graph &g, DL *curr)
Definition: graph.H:1174
nat_t get_num_arcs() const
Definition: graph.H:1776
void swap(NodeIterator &it)
Definition: graph.H:1209
bool has_current() const
Definition: nodesdef.H:362
void for_each_it(const It &, const It &, Op &)
Definition: italgorithms.H:183
GAdArc * insert_garc(Node *src, Node *tgt)
Definition: graph.H:1631
bool exists_node(Pred &pred) const
Definition: graph.H:237
void for_each_arc(Op &&op) const
Definition: graph.H:327
RetT fold_nodes(const RetT &init_val, Op &&op=Op()) const
Definition: graph.H:202
bool none_adjacent_arc(Node *p, Pred &&pred) const
Definition: graph.H:675
static GAdArc * dl_to_adjacent_arc(DL *ptr)
Definition: graph.H:1597
bool exists_adjacent_arc(Node *p, Pred &pred) const
Definition: graph.H:656
void reset_tag(GraphTag tag) const
Definition: graph.H:760
Node * get_current() const
Definition: graph.H:1220
ArcInfo ArcInfoType
Definition: graph.H:877
Definition: italgorithms.H:33
Node * nth_node(nat_t i) const
Definition: graph.H:106
Definition: nodesdef.H:415
nat_t num_nodes
Definition: graph.H:1619
void swap(ArcIterator &it)
Definition: graph.H:1294
ArcIterator()
Definition: graph.H:1926
ContainerRet filter_nodes(Pred &&pred=Pred()) const
Definition: graph.H:131
NodeIterator(const NodeIterator &it)
Definition: graph.H:1861
ContainerRet map_nodes_if(Op &&op=Op(), Pred &&pred=Pred()) const
Definition: graph.H:186
static GArc * to_garc(Arc *arc)
Definition: graph.H:912
nat_t num_nodes
Definition: graph.H:921
Digraph()
Definition: graph.H:1664
GraphInfo & get_info()
Definition: graph.H:1049
Arc * insert_arc(Node *s, Node *t)
Definition: graph.H:1799
RetT fold_adjacent_arcs(Node *p, const RetT &init_val, Op &&op=Op()) const
Definition: graph.H:621
void reset_node_cookies() const
Definition: graph.H:772
ArcIterator(const Graph &g, DL *curr)
Definition: graph.H:1259
Arc * insert_arc(Node *s, Node *t)
Definition: graph.H:1119
Digraph(const Digraph &g)
Definition: graph.H:1682
Arc * search_arc(Node *, Node *)
Definition: graph.H:1543
void remove_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:375
Arc * get_first_arc() const
Definition: graph.H:1083
bool is_digraph() const
Definition: graph.H:1511
Definition: graph.H:60
Node * insert_node()
Definition: graph.H:1101
bool remove_first_arc_if(Pred &pred)
Definition: graph.H:488
ContainerRet map_nodes_if(Op &&op, Pred &pred) const
Definition: graph.H:176
void for_each_adjacent_arc(Node *p, Op &&op) const
Definition: graph.H:534
void reset_all_node_tag() const
Definition: graph.H:736
AdjacentArcIterator arcs_end(Node *p)
Definition: graph.H:1470
AdjacentArcIterator arcs_begin(Node *p)
Definition: graph.H:2140
void remove_node(GNode *)
Definition: graph.H:2191
Arc * insert_arc(Node *src, Node *tgt, const ArcInfo &info)
Definition: graph.H:1125
Node * get_tgt_node()
Definition: graph.H:2089
RetT fold_nodes(RetT &&init_val, Op &op) const
Definition: graph.H:209
Arc * search_adjacent_arc(Node *p, Pred &pred) const
Definition: graph.H:682
NodeInfo info
Definition: nodesdef.H:1002
bool all_arcs(Pred &pred) const
Definition: graph.H:433
const ArcIterator arcs_begin() const
Definition: graph.H:1445
Definition: graph.H:1236
const Node * get_tgt_node() const
Definition: graph.H:1414
void reset_cookies() const
Definition: graph.H:810
void remove_adjacent_arc_if(Node *p, Pred &&pred)
Definition: graph.H:717
void sort_arcs(Cmp &cmp)
Definition: graph.H:2176
bool remove_first_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:356
RetT fold_adjacent_arcs(Node *p, const RetT &init_val, Op &op) const
Definition: graph.H:614
RetT fold_arcs(RetT &&init_val, Op &op) const
Definition: graph.H:418
void reset_nodes() const
Definition: graph.H:816
Node * insert_node(const NodeInfo &info)
Definition: graph.H:1107
GArc * insert_garc(Node *src, Node *tgt)
Definition: graph.H:933
void reset_counters() const
Definition: graph.H:804
Definition: iterator.H:112
void remove_node(Node *n)
Definition: graph.H:1825
void remove_arc_if(Pred &&pred)
Definition: graph.H:507
Node * get_current() const
Definition: graph.H:1900
void reset_all_node_tag(GraphTag tag) const
Definition: graph.H:728
void sort_nodes(Cmp &&cmp=Cmp())
Definition: graph.H:2170
Definition: array.H:32
bool all_arcs(Pred &&pred) const
Definition: graph.H:439
Graph(Graph &&g)
Definition: graph.H:1009
static void copy_graph(const GT &, GT &)
Definition: graph.H:836
void remove_adjacent_arc_if(Node *p, Pred &pred)
Definition: graph.H:711
nat_t get_num_nodes() const
Definition: graph.H:1771
unsigned long int nat_t
Definition: types.H:50
GraphInfo & get_info()
Definition: graph.H:1728
Definition: types.H:60
static GAdArc * dl_to_adjacent_arc(DL *ptr)
Definition: graph.H:899
Digraph(Digraph &&g)
Definition: graph.H:1688
void reset_node_counter() const
Definition: graph.H:788
GNode * insert_gnode(GNode *p)
Definition: graph.H:1624
Arc * insert_arc(Node *src, Node *tgt, const ArcInfo &info)
Definition: graph.H:1805
Definition: graph.H:1831
void swap(Digraph &g)
Definition: graph.H:1717
SLList< Arc * > adjacent_arcs(Node *p) const
Definition: graph.H:723
NodeIterator()
Definition: graph.H:1161
Definition: graph.H:51
AdjacentArcIterator(const AdjacentArcIterator &it)
Definition: graph.H:1353
AdjacentArcIterator(const Digraph &g, Node *n, DL *curr)
Definition: graph.H:2026
void remove_arc_if(Pred &pred)
Definition: graph.H:501
DL adjacent_arc_list
Definition: nodesdef.H:1004
const AdjacentArcIterator arcs_end(Node *p) const
Definition: graph.H:1475
Node * get_src_node() const
Definition: graph.H:2084
DL node_list
Definition: graph.H:922
ArcIterator(const Graph &g)
Definition: graph.H:1252
Arc * nth_arc(nat_t i)
Definition: graph.H:310
void clear()
Definition: graph.H:1532
void swap(Graph &g)
Definition: graph.H:1038
void swap(NodeIterator &it)
Definition: graph.H:1889
bool all_nodes(Pred &pred) const
Definition: graph.H:224
Definition: graph.H:42
void remove_arc(GAdArc *arc)
Definition: graph.H:1646
RetT fold_arcs(const RetT &init_val, Op &op) const
Definition: graph.H:404
GraphTag
Definition: graphutilities.H:37
void reset_all_arc_tag(GraphTag tag) const
Definition: graph.H:744
void for_each_node(Op &op) const
Definition: graph.H:112
Node * nth_node(nat_t i)
Definition: graph.H:101
void sort_arcs(Cmp &&cmp=Cmp())
Definition: graph.H:1506
bool none_adjacent_arc(Node *p, Pred &pred) const
Definition: graph.H:669
void remove_node_if(Pred &pred)
Definition: graph.H:292
Arc * search_arc(Node *, Node *)
Definition: graph.H:2227
NodeIterator(const Digraph &g)
Definition: graph.H:1847
bool remove_first_node_if(Pred &pred)
Definition: graph.H:279
typename GT::Arc Arc
Definition: graphutilities.H:79
const ArcIterator arcs_end() const
Definition: graph.H:2135
ArcIterator(const Digraph &g)
Definition: graph.H:1932
Arc * nth_adjacent_arc(Node *p, nat_t i) const
Definition: graph.H:522
Node * get_first_node() const
Definition: graph.H:1746
Arc * search_arc(Pred &&pred) const
Definition: graph.H:482
bool none_arc(Pred &&pred) const
Definition: graph.H:465
void del()
Definition: graph.H:1990
nat_t get_num_nodes() const
Definition: graph.H:1091
ContainerRet map_adjacent_arcs_if(Node *p, Op &op, Pred &pred) const
Definition: graph.H:573
Definition: nodesdef.H:1049
const Node * get_tgt_node() const
Definition: graph.H:2094
bool remove_first_arc_if(Pred &&pred)
Definition: graph.H:494
Graph(const Graph &g)
Definition: graph.H:1003
Definition: graph.H:1916
GraphInfo GraphInfoType
Definition: graph.H:1576
DL *& get_next()
Definition: nodesdef.H:168
NodeIterator nodes_end()
Definition: graph.H:2110
NodeIterator(const NodeIterator &it)
Definition: graph.H:1181
Node * insert_node()
Definition: graph.H:1781
Node * insert_node(NodeInfo &&info)
Definition: graph.H:1113
void insert_prev(DL *node)
Definition: nodesdef.H:198
Node * insert_node(const NodeInfo &info)
Definition: graph.H:1787
Definition: nodesdef.H:293
void swap(DL *node)
Definition: nodesdef.H:229
AdjacentArcIterator arcs_end(Node *p)
Definition: graph.H:2150
bool all_it(const It &, const It &, Pred &)
Definition: italgorithms.H:295
ContainerRet map_arcs_if(Op &op, Pred &&pred=Pred()) const
Definition: graph.H:375
Arc * insert_arc(Node *src, Node *tgt, ArcInfo &&info)
Definition: graph.H:1812