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_indexes.H
1 # ifndef TPL_GRAPH_INDEXES_H
2 # define TPL_GRAPH_INDEXES_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>
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>
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 not (a2->src_node < a1->src_node) and 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 <typename GT::Node *, Tree, Compare>
73 {
74  typedef typename GT::Node GT_Node;
75  typedef typename GT::Node_Type GT_Node_Info;
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  this->insert(it.get_current());
85  }
86 
87 public:
88 
89  Nodes_Index(GT & _g, Compare & cmp, SN & _sn)
90  : Tree_Type(cmp), g(_g), sn(_sn)
91  {
92  init();
93  }
94 
95  Nodes_Index(GT & _g, Compare && cmp = Compare(), SN && _sn = SN())
96  : Tree_Type(cmp), g(_g), sn(_sn)
97  {
98  init();
99  }
100 
107  GT_Node * insert_in_graph(GT_Node * p)
108  {
109  g.insert_node(p);
110 
111  if (insert(p) == NULL)
112  {
113  g.remove_node(p);
114  return NULL;
115  }
116 
117  return p;
118  }
119 
126  GT_Node * search_or_insert_in_graph(GT_Node * p)
127  {
128  g.insert_node(p);
129  GT_Node ** ptr_node = search_or_insert(p);
130  GT_Node * q = *ptr_node;
131 
132  if (p != q)
133  g.remove_node(p);
134 
135  return q;
136  }
137 
145  GT_Node * insert_in_graph(const GT_Node_Info & info)
146  {
147  GT_Node * p = g.insert_node(info);
148 
149  if (this->insert(p) == NULL)
150  {
151  g.remove_node(p);
152  return NULL;
153  }
154 
155  return p;
156  }
157 
166  GT_Node * search_or_insert_in_graph(const GT_Node_Info & info)
167  {
168  GT_Node * p = g.insert_node(info);
169  GT_Node ** ptr_node = search_or_insert(p);
170  GT_Node * q = *ptr_node;
171 
172  if (p != q)
173  g.remove_node(p);
174 
175  return q;
176  }
177 
179  GT_Node * insert_in_graph(const GT_Node_Info && info = GT_Node_Info())
180  {
181  return insert_in_graph(info);
182  }
183 
193  GT_Node * search(GT_Node * p)
194  {
195  GT_Node ** ret_val = Tree_Type::search(p);
196 
197  if (ret_val == NULL)
198  return NULL;
199 
200  return *ret_val;
201  }
202 
203  GT_Node * search(const GT_Node_Info & info)
204  {
205  GT_Node dummy_node(info);
206  GT_Node * dummy_node_ptr = &dummy_node;
207 
208  return search(dummy_node_ptr);
209  }
210 
217  void remove_from_graph(GT_Node * p) throw(std::exception, std::domain_error)
218  {
219  Tree_Type::find(p); // dispara excepción si p no está en el índice
221  g.remove_node(p);
222  }
223 };
224 
249 template <
250  class GT,
251  class Compare = Dft_Arc_Cmp<GT>,
252  template <typename /* Key */, class /* Compare */> class Tree = Treap,
253  class SA = Dft_Show_Arc<GT>
254  >
255 class Arcs_Index : public DynSetTree <typename GT::Arc *, Tree, Compare>
256 {
257  typedef typename GT::Node GT_Node;
258  typedef typename GT::Node_Type GT_Node_Info;
259  typedef typename GT::Arc GT_Arc;
260  typedef typename GT::Arc_Type GT_Arc_Info;
262 
263  GT & g;
264  SA & sa;
265 
266  void init()
267  {
268  for (Arc_Iterator<GT, SA> it(g, sa); it.has_current(); it.next())
269  this->insert(it.get_current());
270  }
271 
272 public:
273  Arcs_Index(GT & _g, Compare & cmp, SA & _sa)
274  : Tree_Type(cmp), g(_g), sa(_sa)
275  {
276  init();
277  }
278 
279  Arcs_Index(GT & _g, Compare && cmp = Compare(), SA && _sa = SA())
280  : Tree_Type(cmp), g(_g), sa(_sa)
281  {
282  init();
283  }
284 
296  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
297  const GT_Arc_Info & info)
298  {
299  GT_Arc * a = g.insert_arc(src, tgt, info);
300 
301  if (this->insert(a) == NULL)
302  {
303  g.remove_arc(a);
304  return NULL;
305  }
306 
307  return a;
308  }
309 
311  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
312  const GT_Arc_Info && info = GT_Arc_Info())
313  {
314  return insert_in_graph(src, tgt, info);
315  }
316 
324  GT_Arc * search(GT_Node * src, GT_Node * tgt, const GT_Arc_Info & info)
325  {
326  GT_Arc arc(info);
327  arc.src_node = src; arc.tgt_node = tgt;
328  GT_Arc * ptr_arc = &arc;
329 
330  GT_Arc ** ret_val = Tree_Type::search(ptr_arc);
331 
332  if (ret_val != NULL)
333  return *ret_val;
334 
335  if (g.is_digraph())
336  return NULL;
337 
338  std::swap(arc.src_node, arc.tgt_node);
339  ret_val = Tree_Type::search(ptr_arc);
340  if (ret_val == NULL)
341  return NULL;
342 
343  I(((src == (*ret_val)->src_node) and (tgt == (*ret_val)->tgt_node)) or
344  ((tgt == (*ret_val)->src_node) and (src == (*ret_val)->tgt_node)));
345 
346  return *ret_val;
347  }
348 
350  GT_Arc * search(GT_Node * src, GT_Node * tgt,
351  const GT_Arc_Info && info = GT_Arc_Info())
352  {
353  return search(src, tgt, info);
354  }
355 
362  void remove_from_graph(GT_Arc * a) throw(std::exception, std::domain_error)
363  {
364  Tree_Type::find(a);
366  g.remove_arc(a);
367  }
368 
369 };
370 
371 } // End namespace Aleph
372 
373 # endif // GRAPH_INDEXES_H
374 
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexes.H:296
Definition: tpl_graph_indexes.H:255
size_t remove(const GT_Node *&key)
Definition: tpl_dynSetTree.H:274
GT_Node * insert_in_graph(const GT_Node_Info &info)
Definition: tpl_graph_indexes.H:145
Definition: tpl_graph.H:751
Definition: tpl_treap.H:352
GT_Node * search_or_insert_in_graph(const GT_Node_Info &info)
Definition: tpl_graph_indexes.H:166
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_indexes.H:179
GT_Node * search(GT_Node *p)
Definition: tpl_graph_indexes.H:193
GT_Node * insert_in_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:107
void remove_from_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:217
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_indexes.H:350
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexes.H:324
void remove_from_graph(GT_Arc *a)
Definition: tpl_graph_indexes.H:362
Definition: tpl_graph.H:814
Definition: tpl_graph_indexes.H:22
GT_Node * search_or_insert_in_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:126
GT::Node ** search_or_insert(const GT::Node *&key)
Definition: tpl_dynSetTree.H:225
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Definition: tpl_graph_indexes.H:311
GT::Node ** insert(const GT::Node *&key)
Definition: tpl_dynSetTree.H:177
Definition: tpl_graph_indexes.H:13

Leandro Rabindranath León