Aleph-w  1.5a.2
Biblioteca general de algoritmos y estructuras de datos
 Todo Clases Archivos Funciones Variables 'typedefs' Enumeraciones Amigas Grupos Páginas
tpl_graph_indexs.H
1 # ifndef TPL_GRAPH_INDEXS_H
2 # define TPL_GRAPH_INDEXS_H
3 
4 # include <tpl_dynSetTree.H>
5 # include <tpl_graph.H>
6 
7 using namespace Aleph;
8 
9 namespace Aleph
10 {
11 
12 template <class GT>
13 struct Dft_Node_Cmp
14 {
15  bool operator () (typename GT::Node * p1, typename GT::Node * p2) const
16  {
17  return p1->get_info() < p2->get_info();
18  }
19 };
20 
21 template <class GT>
22 struct Dft_Arc_Cmp
23 {
24  bool operator () (typename GT::Arc * a1, typename GT::Arc * a2) const
25  {
26  if (a1->src_node < a2->src_node)
27  return true;
28 
29  return a1->tgt_node < a2->tgt_node;
30  }
31 };
32 
66 template <
67  class GT,
68  class Compare = Dft_Node_Cmp<GT>,
69  template <typename /* Key */, class /* Compare */> class Tree = Treap,
70  class SN = Dft_Show_Node<GT>
71  >
72 class Nodes_Index : public DynSetTree <Tree, typename GT::Node *, Compare>
73 {
74  typedef typename GT::Node GT_Node;
75  typedef typename GT::Node_Type GT_Node_Info;
76  typedef DynSetTree <Tree, GT_Node *, Compare> Tree_Type;
77 
78  GT & g;
79  SN & sn;
80 
81  void init()
82  {
83  for (Node_Iterator<GT, SN> it(g, sn); it.has_current(); it.next())
84  insert(it.get_current());
85  }
86 
87 public:
88  Nodes_Index(GT & _g, Compare & cmp, SN & _sn)
89  : Tree_Type(cmp), g(_g), sn(_sn)
90  {
91  init();
92  }
93 
94  Nodes_Index(GT & _g, Compare && cmp = Compare(), SN && _sn = SN())
95  : Tree_Type(cmp), g(_g), sn(_sn)
96  {
97  init();
98  }
99 
105  GT_Node * insert_in_graph(GT_Node * p)
106  {
107  g.insert_node(p);
108 
109  if (insert(p) == NULL)
110  {
111  g.remove_node(p);
112  return NULL;
113  }
114 
115  return p;
116  }
117 
125  GT_Node * insert_in_graph(const GT_Node_Info & info)
126  {
127  GT_Node * p = g.insert_node(info);
128 
129  if (insert(p) == NULL)
130  {
131  g.remove_node(p);
132  return NULL;
133  }
134 
135  return p;
136  }
137 
139  GT_Node * insert_in_graph(const GT_Node_Info && info = GT_Node_Info())
140  {
141  return insert_in_graph(info);
142  }
143 
153  GT_Node * search(GT_Node * p)
154  {
155  GT_Node ** ret_val = Tree_Type::search(p);
156 
157  if (ret_val == NULL)
158  return NULL;
159 
160  return *ret_val;
161  }
162 
163  GT_Node * search(const GT_Node_Info & info)
164  {
165  GT_Node dummy_node(info);
166  GT_Node * dummy_node_ptr = &dummy_node;
167 
168  return search(dummy_node_ptr);
169  }
170 
177  void remove_from_graph(GT_Node * p) throw(std::exception, std::domain_error)
178  {
179  find(p); // dispara excepción si p no está en el índice
180  remove(p);
181  g.remove_node(p);
182  }
183 };
184 
209 template <
210  class GT,
211  class Compare = Dft_Arc_Cmp<GT>,
212  template <typename /* Key */, class /* Compare */> class Tree = Treap,
213  class SA = Dft_Show_Arc<GT>
214  >
215 class Arcs_Index : public DynSetTree <Tree, typename GT::Arc *, Compare>
216 {
217  typedef typename GT::Node GT_Node;
218  typedef typename GT::Node_Type GT_Node_Info;
219  typedef typename GT::Arc GT_Arc;
220  typedef typename GT::Arc_Type GT_Arc_Info;
222 
223  GT & g;
224  SA & sa;
225 
226  void init()
227  {
228  for (Arc_Iterator<GT, SA> it(g, sa); it.has_current(); it.next())
229  insert(it.get_current());
230  }
231 
232 public:
233  Arcs_Index(GT & _g, Compare & cmp, SA & _sa)
234  : Tree_Type(cmp), g(_g), sa(_sa)
235  {
236  init();
237  }
238 
239  Arcs_Index(GT & _g, Compare && cmp = Compare(), SA && _sa = SA())
240  : Tree_Type(cmp), g(_g), sa(_sa)
241  {
242  init();
243  }
244 
256  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
257  const GT_Arc_Info & info)
258  {
259  GT_Arc * a = g.insert_arc(src, tgt, info);
260 
261  if (insert(a) == NULL)
262  {
263  g.remove_arc(a);
264  return NULL;
265  }
266 
267  return a;
268  }
269 
271  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
272  const GT_Arc_Info && info = GT_Arc_Info())
273  {
274  return insert_in_graph(src, tgt, info);
275  }
276 
284  GT_Arc * search(GT_Node * src, GT_Node * tgt, const GT_Arc_Info & info)
285  {
286  GT_Arc arc(info);
287  arc.src_node = src; arc.tgt_node = tgt;
288  GT_Arc * ptr_arc = &arc;
289 
290  GT_Arc ** ret_val = Tree_Type::search(ptr_arc);
291 
292  if (ret_val != NULL)
293  return *ret_val;
294 
295  if (g.is_digraph())
296  return NULL;
297 
298  std::swap(arc.src_node, arc.tgt_node);
299  ret_val = Tree_Type::search(ptr_arc);
300  if (ret_val == NULL)
301  return NULL;
302 
303  I(((src == (*ret_val)->src_node) and (tgt == (*ret_val)->tgt_node)) or
304  ((tgt == (*ret_val)->src_node) and (src == (*ret_val)->tgt_node)));
305 
306  return *ret_val;
307  }
308 
310  GT_Arc * search(GT_Node * src, GT_Node * tgt,
311  const GT_Arc_Info && info = GT_Arc_Info())
312  {
313  return search(src, tgt, info);
314  }
315 
322  void remove_from_graph(GT_Arc * a) throw(std::exception, std::domain_error)
323  {
324  find(a);
325  remove(a);
326  g.remove_arc(a);
327  }
328 
329 };
330 
331 } // End namespace Aleph
332 
333 # endif
334 
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexs.H:256
Definition: tpl_graph_indexes.H:255
GT_Node * insert_in_graph(const GT_Node_Info &info)
Definition: tpl_graph_indexs.H:125
Definition: tpl_graph.H:751
Definition: tpl_treap.H:352
Definition: tpl_graph_indexes.H:72
GT_Node ** search(const GT_Node *&key) const
Definition: tpl_dynSetTree.H:364
Definition: tpl_graph.H:794
GT_Node * insert_in_graph(const GT_Node_Info &&info=GT_Node_Info())
Definition: tpl_graph_indexs.H:139
GT_Node * search(GT_Node *p)
Definition: tpl_graph_indexs.H:153
GT_Node * insert_in_graph(GT_Node *p)
Definition: tpl_graph_indexs.H:105
void remove_from_graph(GT_Node *p)
Definition: tpl_graph_indexs.H:177
GT::Node *& find(const GT::Node *&key)
Definition: tpl_dynSetTree.H:316
Definition: tpl_graph.H:634
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Definition: tpl_dynSetTree.H:34
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Definition: tpl_graph_indexs.H:310
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexs.H:284
void remove_from_graph(GT_Arc *a)
Definition: tpl_graph_indexs.H:322
Definition: tpl_graph.H:814
Definition: tpl_graph_indexes.H:22
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Definition: tpl_graph_indexs.H:271
GT::Node ** insert(const GT::Node *&key)
Definition: tpl_dynSetTree.H:177
Definition: tpl_graph_indexes.H:13

Leandro Rabindranath León