Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
tpl_dyn_graph.H
1 # ifndef TPL_DYN_GRAPH_H
2 # define TPL_DYN_GRAPH_H
3 
4 # include <tpl_agraph.H>
5 
6 using namespace Aleph;
7 
8 namespace Aleph
9 {
10 
16  template <class GT>
17  class Dyn_Graph
18  {
19  public:
20 
21  typedef typename GT::Node Node;
22 
23  typedef typename GT::Arc Arc;
24 
25  typedef typename Node::Node_Info Node_Info;
26 
27  typedef typename Arc::Arc_Info Arc_Info;
28 
29  private:
30 
31  static Node * info_to_node(Node_Info & info)
32  {
33  Node * node_zero = 0;
34  long off_set = (long) &node_zero->get_info();
35  long ptr_info = (long) &info;
36  return (Node *) (ptr_info - off_set);
37  }
38 
39  static Arc * info_to_arc(Arc_Info & info)
40  {
41  Arc * arc_zero = 0;
42  long off_set = (long) &arc_zero->get_info();
43  long ptr_info = (long) &info;
44  return (Arc *) (ptr_info - off_set);
45  }
46 
47  GT graph;
48 
49  public:
50 
53  : graph()
54  {
55  // Empty
56  }
57 
58  Dyn_Graph(const Dyn_Graph & g)
59  : graph(g.graph)
60  {
61  // Empty
62  }
63 
64  Dyn_Graph(Dyn_Graph && g)
65  : graph(std::move(g.graph))
66  {
67  // Empty
68  }
69 
70 
76  Node_Info & insert_node(const Node_Info & info)
77  {
78  Node * node = graph.insert_node(new Node(info));
79  return node->get_info();
80  }
81 
87  Node_Info & insert_node(Node_Info && info = Node_Info())
88  {
89  Node * node = graph.insert_node(new Node(std::move(info)));
90  return node->get_info();
91  }
92 
103  Arc_Info & insert_arc(Node_Info & src_info, Node_Info & tgt_info,
104  const Arc_Info & info)
105  {
106  Node * src = info_to_node(src_info);
107  Node * tgt = info_to_node(tgt_info);
108  Arc * arc = graph.insert_arc(src, tgt);
109  arc->get_info() = info;
110  return arc->get_info();
111  }
112 
124  Arc_Info & insert_arc(Node_Info & src_info, Node_Info & tgt_info,
125  Arc_Info && info = Arc_Info())
126  {
127  Node * src = info_to_node(src_info);
128  Node * tgt = info_to_node(tgt_info);
129  Arc * arc = graph.insert_arc(src, tgt);
130  arc->get_info() = std::move(info);
131  return arc->get_info();
132  }
133 
139  Node_Info & get_src_node(Arc_Info & info)
140  {
141  Arc * arc = info_to_arc(info);
142  Node * src = graph.get_src_node(arc);
143  return src->get_info();
144  }
145 
151  Node_Info & get_tgt_node(Arc_Info & info)
152  {
153  Arc * arc = info_to_arc(info);
154  Node * tgt = graph.get_tgt_node(arc);
155  return tgt->get_info();
156  }
157 
158  Node_Info & get_connected_node(Node_Info & node_info, Arc_Info & arc_info)
159  {
160  Node * node = info_to_node(node_info);
161  Arc * arc = info_to_arc(arc_info);
162  Node * connected_node(node, arc);
163  return connected_node->get_info();
164  }
165 
170  void remove_arc(Arc_Info & info)
171  {
172  Arc * arc = info_to_arc(info);
173  graph.remove_arc(arc);
174  }
175 
180  void remove_node(Node_Info & info)
181  {
182  Node * node = info_to_node(info);
183  graph.remove_node(node);
184  }
185 
190  const size_t & get_num_nodes() const
191  {
192  return graph.get_num_nodes();
193  }
194 
199  const size_t & get_num_arcs() const
200  {
201  return graph.get_num_arcs();
202  }
203 
209  const size_t & get_num_arcs(Node_Info & info) const
210  {
211  Node * node = info_to_node(info);
212  return graph.get_num_arcs(node);
213  }
214  };
215 
216  # define GRAPH_SPECIALIZATION(G, N, A) \
217  template <typename Node_Info, typename Arc_Info> \
218  class Dyn_##G : public Dyn_Graph<G<N<Node_Info>, A<Arc_Info>>> \
219  { \
220  typedef G<N<Node_Info>, A<Arc_Info>> Graph_Type; \
221  \
222  public: \
223  Dyn_##G() \
224  : Dyn_Graph <Graph_Type>() { } \
225  \
226  Dyn_##G(const Dyn_##G & g) \
227  : Dyn_Graph <Graph_Type>(g) { } \
228  \
229  Dyn_##G(Dyn_##G && g) \
230  : Dyn_Graph <Graph_Type>(std::move(g)) { } \
231  };
232 
233  GRAPH_SPECIALIZATION(List_Graph, Graph_Node, Graph_Arc)
234 
235  GRAPH_SPECIALIZATION(List_Digraph, Graph_Node, Graph_Arc)
236 
237 } // End namespace Aleph
238 
239 # endif // TPL_DYN_GRAPH_H
240 
Node_Info & insert_node(const Node_Info &info)
Definition: tpl_dyn_graph.H:76
Definition: tpl_graph.H:29
Dyn_Graph()
Constructor por omisión.
Definition: tpl_dyn_graph.H:52
Definition: tpl_graph.H:21
Arc_Info & insert_arc(Node_Info &src_info, Node_Info &tgt_info, const Arc_Info &info)
Definition: tpl_dyn_graph.H:103
const size_t & get_num_nodes() const
Definition: tpl_dyn_graph.H:190
const size_t & get_num_arcs(Node_Info &info) const
Definition: tpl_dyn_graph.H:209
Node_Info & insert_node(Node_Info &&info=Node_Info())
Definition: tpl_dyn_graph.H:87
Node_Info & get_tgt_node(Arc_Info &info)
Definition: tpl_dyn_graph.H:151
Arc_Info & insert_arc(Node_Info &src_info, Node_Info &tgt_info, Arc_Info &&info=Arc_Info())
Definition: tpl_dyn_graph.H:124
void remove_arc(Arc_Info &info)
Definition: tpl_dyn_graph.H:170
Node_Info & get_src_node(Arc_Info &info)
Definition: tpl_dyn_graph.H:139
void remove_node(Node_Info &info)
Definition: tpl_dyn_graph.H:180
const size_t & get_num_arcs() const
Definition: tpl_dyn_graph.H:199
Definition: tpl_graph.H:19
Definition: tpl_graph.H:32
Definition: tpl_dyn_graph.H:17

Leandro Rabindranath León