Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
generate_graph.H
1 # ifndef GENERATE_GRAPH_H
2 # define GENERATE_GRAPH_H
3 
4 # include <fstream>
5 # include <tpl_dynArray.H>
6 # include <tpl_sort_utils.H>
7 # include <tpl_graph.H>
8 
9 using namespace std;
10 using namespace Aleph;
11 
12 namespace Aleph {
13 
14 
15  template <class GT, class SA> inline static
16 bool is_there_a_double_arc(GT * g,
17  typename GT::Node * src,
18  typename GT::Node * tgt)
19 {
20  if (not g->is_digraph())
21  return false;
22 
23  return search_arc<GT,SA>(*g, src, tgt) != NULL and
24  search_arc<GT,SA>(*g, tgt, src) != NULL;
25 }
26 
27 
28  template <class GT>
29 static int search_node(DynArray<typename GT::Node *> & nodes,
30  typename GT::Node * p)
31 {
32  return sequential_search(nodes, p, 0, nodes.size() - 1);
33 }
34 
35 
72 template <class GT,
73  class Write_Node, class Write_Arc,
74  class Shade_Node, class Shade_Arc, class SA>
75 void generate_graphpic(GT & g,
76  const double & xdist,
77  const double & /* ydist no usado por ahora */,
78  std::ostream & output)
79 {
81 
82  typename GT::Node_Iterator it(g);
83  for (int i = 0; it.has_current(); it.next(), ++i)
84  {
85  typename GT::Node * p = it.get_current_node();
86 
87  nodes[i] = p;
88 
89  if (Shade_Node() (p).size() != 0)
90  output << Shade_Node() (p) << " " << i << endl;
91 
92  const string text_node = Write_Node () (p);
93 
94  if (text_node.size() == 0)
95  continue;
96 
97  output << "NODE-TEXT " << i << " \"" << text_node << "\" 0 0" << endl;
98  }
99 
100  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
101  {
102  typename GT::Arc * a = it.get_current_arc();
103 
104  typename GT::Node * src = g.get_src_node(a);
105  typename GT::Node * tgt = g.get_tgt_node(a);
106 
107  const int src_idx = search_node <GT> (nodes, src);
108  const int tgt_idx = search_node <GT> (nodes, tgt);
109 
110  if (is_there_a_double_arc <GT, SA> (&g, src, tgt))
111  output << "CURVE-ARC " << src_idx << " " << tgt_idx << " "
112  << xdist/5 << " L" << endl;
113  else
114  output << "ARC " << src_idx << " " << tgt_idx << endl;
115 
116  if ( Shade_Arc()(a).size() != 0)
117  output << Shade_Arc()(a) << " "
118  << src_idx << " " << tgt_idx << " " << endl;
119 
120  const string text_arc = Write_Arc() (a);
121 
122  if (text_arc.size() == 0)
123  continue;
124 
125  output << "ARC-TEXT " << src_idx << " " << tgt_idx << " \""
126  << text_arc << "\" 0 0 " << endl;
127  }
128 }
129 
167 template <class GT,
168  class Write_Node,
169  class Write_Arc,
170  class Shade_Node,
171  class Shade_Arc,
172  class Dashed_Node,
173  class Dashed_Arc,
174  class SA,
175  class SN>
176 void generate_graphviz(GT & g, std::ostream & output,
177  const string & rankdir = "TB",
178  float ranksep = 0.2,
179  float nodesep = 0.2)
180 {
181  output << "// Generated by generate_graphviz() from Aleph library. See at:" << endl
182  << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << endl
183  << "// for documentation" << endl
184  << "// Copyleft Leandro Leon lrleon@ula.ve" << endl
185  << "// for using of graphviz system. See at http://graphviz.org/"
186  << endl << endl;
187  string arc_str;
188  if (g.is_digraph())
189  {
190  arc_str = " -> ";
191  output << "digraph {" << endl;
192  }
193  else
194  {
195  arc_str = " -- ";
196  output << "graph {" << endl;
197  }
198  output << endl
199  << "rankdir = " << rankdir << endl
200  << "style = none" << endl
201  << "truecolor=false" << endl
202  << "ranksep = " << ranksep << endl
203  << "nodesep = " << nodesep << endl << endl;
204 
206 
207  Node_Iterator<GT, SN> it(g);
208  for (int i = 0; it.has_current(); it.next(), ++i)
209  {
210  output << i << " [ ";
211 
212  typename GT::Node * p = it.get_current_node();
213  nodes[i] = p;
214 
215  if (Shade_Node () (p))
216  output << "style = bold ";
217 
218  const string text_node = Write_Node () (p);
219 
220  if (text_node.size() != 0)
221  output << "label = \"" << text_node << "\"";
222  output << "]" << endl;
223  }
224 
225  output << endl;
226 
227  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
228  {
229  typename GT::Arc * a = it.get_current_arc();
230 
231  typename GT::Node * src = g.get_src_node(a);
232  typename GT::Node * tgt = g.get_tgt_node(a);
233 
234  int src_idx = search_node <GT> (nodes, src);
235  int tgt_idx = search_node <GT> (nodes, tgt);
236 
237  output << src_idx << arc_str << tgt_idx << " [";
238 
239  if (Shade_Arc () (a))
240  output << "style = bold ";
241 
242  const string text_arc = Write_Arc() (a);
243 
244  if (text_arc.size() != 0)
245  output << "label = \"" << text_arc << "\"";
246  output <<"]" << endl;
247  }
248 
249  output << "}" << endl;
250 }
251 
252 
282 template <class GT,
283  class Node_Attr,
284  class Arc_Attr,
285  class SN,
286  class SA>
287 void generate_graphviz(GT & g, std::ostream & out,
288  Node_Attr node_attr = Node_Attr(),
289  Arc_Attr arc_attr = Arc_Attr(),
290  const string & rankdir = "TB")
291 {
292  out << "// Generated by generate_graphviz() from Aleph library" << endl
293  << "// See at:"
294  << "// http://webdelprofesor.ula.ve/ingenieria/lrleon/aleph/html/index.html" << endl
295  << "// for documentation of Aleph library" << endl
296  << "// Copyleft Leandro Leon lrleon@ula.ve" << endl
297  << "// for using of graphviz system. See at http://graphviz.org/"
298  << endl << endl
299  << (g.is_digraph() ? "digraph {" : "graph {") << endl
300  << endl
301  << "rankdir = " << rankdir << endl
302  << endl
303  << "// Node list" << endl
304  << endl;
305 
307 
308  Node_Iterator<GT, SN> it(g);
309  for (int i = 0; it.has_current(); it.next(), ++i)
310  {
311  typename GT::Node * p = it.get_current_node();
312 
313  nodes_table.insert(p, i);
314 
315  out << i << " [ ";
316 
317  node_attr (g, p, out);
318 
319  out << "]" << endl;
320  }
321 
322  out << endl
323  << endl
324  << "// Arc list" << endl
325  << endl;
326 
327  const string arrow = g.is_digraph() ? "->" : "--";
328 
329  for (Arc_Iterator<GT, SA> it(g); it.has_current(); it.next())
330  {
331  typename GT::Arc * a = it.get_current_arc();
332 
333  typename GT::Node * src = g.get_src_node(a);
334  typename GT::Node * tgt = g.get_tgt_node(a);
335 
336  const int src_idx = nodes_table.find(src);
337  const int tgt_idx = nodes_table.find(tgt);
338 
339  out << src_idx << arrow << tgt_idx << " [";
340  arc_attr (g, a, out) ;
341  out << "]" << endl;
342  }
343 
344  out << "}" << endl;
345 }
346 
347  template <class GT>
349 {
350  void operator () (GT&, typename GT::Node * p, std::ostream & out)
351  {
352  out << "label = \"" << p->get_info() << " \"";
353  }
354 };
355 
356  template <class GT>
358 {
359  void operator () (GT&, typename GT::Arc * a, std::ostream & out)
360  {
361  out << "label = \"" << a->get_info() << " \"";
362  }
363 };
364 
413 template <class GT,
414  class Node_Attr = Dft_Node_Attr<GT>,
415  class Arc_Attr = Dft_Arc_Attr<GT>,
416  class SN = Dft_Show_Node<GT>,
417  class SA = Dft_Show_Arc<GT> >
419 {
420 
421 public:
422 
432  void operator () (GT & g, std::ostream & out,
433  const Node_Attr & node_attr = Node_Attr(),
434  const Arc_Attr & arc_attr = Arc_Attr(),
435  const string & rankdir = "TB")
436  {
437  generate_graphviz <GT, Node_Attr, Arc_Attr, SN, SA>
438  (g, out, node_attr, arc_attr, rankdir);
439  }
440 };
441 
442 
443 
444  template <class GT>
446 {
447  bool operator () (typename GT::Node *) const { return false; }
448 
449  bool operator () (typename GT::Arc *) const { return false; }
450 };
451 
452 
483 template <class GT,
484  class Write_Node,
485  class Write_Arc,
486  class Shade_Node = Dummy_Attr<GT>,
487  class Shade_Arc = Dummy_Attr<GT>,
488  class Dashed_Node = Dummy_Attr<GT>,
489  class Dashed_Arc = Dummy_Attr<GT>,
490  class SA = Dft_Show_Arc<GT>,
491  class SN = Dft_Show_Node<GT> >
493 {
494 
495 public:
496 
506  void operator () (GT & g, std::ostream & out,
507  const string & rankdir = "TB",
508  float ranksep = 0.4, float nodesep = 0.4)
509  {
510  generate_graphviz <GT, Write_Node, Write_Arc, Shade_Node,
511  Shade_Arc, Dashed_Node, Dashed_Arc, SA, SN> (g, out, rankdir,
512  ranksep, nodesep);
513  }
514 
515 };
516 
555  template <class GT,
556  class Write_Node, class Write_Arc,
557  class Shade_Node, class Shade_Arc, class SA>
558 void generate_cross_graph(GT & g,
559  const size_t & nodes_by_level,
560  const double & xdist,
561  const double & ydist,
562  std::ostream & out)
563 {
564  if (g.is_digraph())
565  out << "cross-net-digraph ";
566  else
567  out << "cross-net-graph ";
568 
569  out << g.get_num_nodes() << " " << nodes_by_level << " "
570  << xdist << " " << ydist << endl
571  << endl;
572 
573  generate_graphpic<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, SA>
574  (g, xdist, ydist, out);
575 }
576 
577  template <class GT,
578  class Write_Node, class Write_Arc,
579  class Shade_Node, class Shade_Arc>
580 void generate_cross_graph(GT & g,
581  const size_t & nodes_by_level,
582  const double & xdist,
583  const double & ydist,
584  std::ostream & out)
585 {
586  typedef Dft_Show_Arc<GT> DSA;
587  generate_cross_graph<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, DSA>
588  (g, nodes_by_level, xdist, ydist, out);
589 }
628  template <class GT,
629  class Write_Node, class Write_Arc,
630  class Shade_Node, class Shade_Arc, class SA>
631 void generate_net_graph(GT & g,
632  const size_t & nodes_by_level,
633  const double & xdist,
634  const double & ydist,
635  std::ostream & out)
636 {
637  if (g.is_digraph())
638  out << "net-digraph ";
639  else
640  out << "net-graph ";
641 
642  out << g.get_num_nodes() << " " << nodes_by_level << " "
643  << xdist << " " << ydist << endl
644  << endl;
645 
646  generate_graphpic<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, SA>
647  (g, xdist, ydist, out);
648 }
649 
650  template <class GT,
651  class Write_Node, class Write_Arc,
652  class Shade_Node, class Shade_Arc>
653 void generate_net_graph(GT & g,
654  const size_t & nodes_by_level,
655  const double & xdist,
656  const double & ydist,
657  std::ostream & out)
658 {
659  typedef Dft_Show_Arc<GT> DSA;
660  generate_net_graph<GT, Write_Node, Write_Arc, Shade_Node, Shade_Arc, DSA>
661  (g, nodes_by_level, xdist, ydist, out);
662 
663 }
664 
665 template <class GT> struct __Shade_Node
666 {
667  string operator () (typename GT::Node *) const
668  {
669  return "";
670  }
671 };
672 
673 
674 template <class GT> struct __Shade_Arc
675 {
676  string operator () (typename GT::Arc *) const
677  {
678  return "";
679  }
680 };
681 
682 
683  template <class GT, class Write_Node, class Write_Arc, class SA>
684 void generate_cross_graph(GT & g,
685  const size_t & nodes_by_level,
686  const double & xdist,
687  const double & ydist,
688  std::ostream & out)
689 {
691  <GT, Write_Node, Write_Arc, __Shade_Node<GT>, __Shade_Arc<GT>, SA>
692  (g, nodes_by_level, xdist, ydist, out);
693 }
694 
695  template <class GT, class Write_Node, class Write_Arc, class SA>
696 void generate_net_graph(GT & g,
697  const size_t & nodes_by_level,
698  const double & xdist,
699  const double & ydist,
700  std::ostream & out)
701 {
703  <GT, Write_Node, Write_Arc, __Shade_Node<GT>, __Shade_Arc<GT>, SA>
704  (g, nodes_by_level, xdist, ydist, out);
705 }
706 
707 
708  template <class GT, class Write_Node, class Write_Arc>
709 void generate_cross_graph(GT & g,
710  const size_t & nodes_by_level,
711  const double & xdist,
712  const double & ydist,
713  std::ostream & out)
714 {
715  typedef Dft_Show_Arc<GT> DSA;
717  <GT, Write_Node, Write_Arc, __Shade_Node<GT>, __Shade_Arc<GT>, DSA>
718  (g, nodes_by_level, xdist, ydist, out);
719 }
720 
721  template <class GT, class Write_Node, class Write_Arc>
722 void generate_net_graph(GT & g,
723  const size_t & nodes_by_level,
724  const double & xdist,
725  const double & ydist,
726  std::ostream & out)
727 {
728  typedef Dft_Show_Arc<GT> DSA;
730  <GT, Write_Node, Write_Arc, __Shade_Node<GT>, __Shade_Arc<GT>, DSA>
731  (g, nodes_by_level, xdist, ydist, out);
732 }
733 
734 
735 } // end namespace Aleph
736 
737 
738 # endif // GENERATE_GRAPH_H
Definition: generate_graph.H:445
int sequential_search(T *a, const T &x, const int l, const int r, Equal &eq)
Definition: tpl_sort_utils.H:260
void generate_graphpic(GT &g, const double &xdist, const double &, std::ostream &output)
Definition: generate_graph.H:75
size_t size() const
Retorna dimensión actual (más lejano índice escrito)
Definition: tpl_dynArray.H:523
Definition: tpl_graph.H:751
Definition: generate_graph.H:348
Definition: generate_graph.H:492
Definition: generate_graph.H:665
Definition: generate_df_tree.H:41
Definition: tpl_graph.H:794
void generate_graphviz(GT &g, std::ostream &out, Node_Attr node_attr=Node_Attr(), Arc_Attr arc_attr=Arc_Attr(), const string &rankdir="TB")
Definition: generate_graph.H:287
Definition: tpl_graph.H:634
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Definition: tpl_dynMapTree.H:289
void generate_net_graph(GT &g, const size_t &nodes_by_level, const double &xdist, const double &ydist, std::ostream &out)
Definition: generate_graph.H:631
Definition: tpl_graph.H:814
Definition: generate_graph.H:674
void generate_cross_graph(GT &g, const size_t &nodes_by_level, const double &xdist, const double &ydist, std::ostream &out)
Definition: generate_graph.H:558
Definition: generate_graph.H:357
Definition: generate_graph.H:418

Leandro Rabindranath León