Aleph-w  1.9
General library for algorithms and data structures
tpl_graph_indexes.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_GRAPH_INDEXES_H
28 # define TPL_GRAPH_INDEXES_H
29 
30 # include <tpl_dynSetTree.H>
31 # include <tpl_graph.H>
32 
33 using namespace Aleph;
34 
35 namespace Aleph
36 {
37 
38 template <class GT>
39 struct Dft_Node_Cmp
40 {
41  bool operator () (typename GT::Node * p1, typename GT::Node * p2) const
42  {
43  return p1->get_info() < p2->get_info();
44  }
45 };
46 
47 template <class GT>
48 struct Dft_Arc_Cmp
49 {
50  bool operator () (typename GT::Arc * a1, typename GT::Arc * a2) const
51  {
52  if (a1->src_node < a2->src_node)
53  return true;
54 
55  return not (a2->src_node < a1->src_node) and a1->tgt_node < a2->tgt_node;
56  }
57 };
58 
92 template <
93  class GT,
94  class Compare = Dft_Node_Cmp<GT>,
95  template <typename /* Key */, class /* Compare */> class Tree = Treap,
96  class SN = Dft_Show_Node<GT>
97  >
98 class Nodes_Index : public DynSetTree <typename GT::Node *, Tree, Compare>
99 {
100  typedef typename GT::Node GT_Node;
101  typedef typename GT::Node_Type GT_Node_Info;
103 
104  GT & g;
105  SN & sn;
106 
107  void init()
108  {
109  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
110  this->insert(it.get_curr_ne());
111  }
112 
113 public:
114 
115  Nodes_Index(GT & _g, Compare & cmp, SN & _sn)
116  : Tree_Type(cmp), g(_g), sn(_sn)
117  {
118  init();
119  }
120 
121  Nodes_Index(GT & _g, Compare && cmp = Compare(), SN && _sn = SN())
122  : Tree_Type(cmp), g(_g), sn(_sn)
123  {
124  init();
125  }
126 
133  GT_Node * insert_in_graph(GT_Node * p)
134  {
135  g.insert_node(p);
136 
137  if (insert(p) == nullptr)
138  {
139  g.remove_node(p);
140  return nullptr;
141  }
142 
143  return p;
144  }
145 
152  GT_Node * search_or_insert_in_graph(GT_Node * p)
153  {
154  g.insert_node(p);
155  GT_Node ** ptr_node = search_or_insert(p);
156  GT_Node * q = *ptr_node;
157 
158  if (p != q)
159  g.remove_node(p);
160 
161  return q;
162  }
163 
171  GT_Node * insert_in_graph(const GT_Node_Info & info)
172  {
173  GT_Node * p = g.insert_node(info);
174 
175  if (this->insert(p) == nullptr)
176  {
177  g.remove_node(p);
178  return nullptr;
179  }
180 
181  return p;
182  }
183 
192  GT_Node * search_or_insert_in_graph(const GT_Node_Info & info)
193  {
194  GT_Node * p = g.insert_node(info);
195  GT_Node ** ptr_node = search_or_insert(p);
196  GT_Node * q = *ptr_node;
197 
198  if (p != q)
199  g.remove_node(p);
200 
201  return q;
202  }
203 
205  GT_Node * insert_in_graph(const GT_Node_Info && info = GT_Node_Info())
206  {
207  return insert_in_graph(info);
208  }
209 
219  GT_Node * search(GT_Node * p)
220  {
221  GT_Node ** ret_val = Tree_Type::search(p);
222 
223  if (ret_val == nullptr)
224  return nullptr;
225 
226  return *ret_val;
227  }
228 
229  GT_Node * search(const GT_Node_Info & info)
230  {
231  GT_Node dummy_node(info);
232  GT_Node * dummy_node_ptr = &dummy_node;
233 
234  return search(dummy_node_ptr);
235  }
236 
243  void remove_from_graph(GT_Node * p) throw(std::exception, std::domain_error)
244  {
245  Tree_Type::find(p); // dispara excepción si p no está en el índice
246  Tree_Type::remove(p);
247  g.remove_node(p);
248  }
249 };
250 
275 template <
276  class GT,
277  class Compare = Dft_Arc_Cmp<GT>,
278  template <typename /* Key */, class /* Compare */> class Tree = Treap,
279  class SA = Dft_Show_Arc<GT>
280  >
281 class Arcs_Index : public DynSetTree <typename GT::Arc *, Tree, Compare>
282 {
283  typedef typename GT::Node GT_Node;
284  typedef typename GT::Node_Type GT_Node_Info;
285  typedef typename GT::Arc GT_Arc;
286  typedef typename GT::Arc_Type GT_Arc_Info;
288 
289  GT & g;
290  SA & sa;
291 
292  void init()
293  {
294  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
295  this->insert(it.get_curr_ne());
296  }
297 
298 public:
299  Arcs_Index(GT & _g, Compare & cmp, SA & _sa)
300  : Tree_Type(cmp), g(_g), sa(_sa)
301  {
302  init();
303  }
304 
305  Arcs_Index(GT & _g, Compare && cmp = Compare(), SA && _sa = SA())
306  : Tree_Type(cmp), g(_g), sa(_sa)
307  {
308  init();
309  }
310 
322  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
323  const GT_Arc_Info & info)
324  {
325  GT_Arc * a = g.insert_arc(src, tgt, info);
326 
327  if (this->insert(a) == nullptr)
328  {
329  g.remove_arc(a);
330  return nullptr;
331  }
332 
333  return a;
334  }
335 
337  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
338  const GT_Arc_Info && info = GT_Arc_Info())
339  {
340  return insert_in_graph(src, tgt, info);
341  }
342 
350  GT_Arc * search(GT_Node * src, GT_Node * tgt, const GT_Arc_Info & info)
351  {
352  GT_Arc arc(info);
353  arc.src_node = src; arc.tgt_node = tgt;
354  GT_Arc * ptr_arc = &arc;
355 
356  GT_Arc ** ret_val = Tree_Type::search(ptr_arc);
357 
358  if (ret_val != nullptr)
359  return *ret_val;
360 
361  if (g.is_digraph())
362  return nullptr;
363 
364  std::swap(arc.src_node, arc.tgt_node);
365  ret_val = Tree_Type::search(ptr_arc);
366  if (ret_val == nullptr)
367  return nullptr;
368 
369  assert(((src == (*ret_val)->src_node) and (tgt == (*ret_val)->tgt_node)) or
370  ((tgt == (*ret_val)->src_node) and (src == (*ret_val)->tgt_node)));
371 
372  return *ret_val;
373  }
374 
376  GT_Arc * search(GT_Node * src, GT_Node * tgt,
377  const GT_Arc_Info && info = GT_Arc_Info())
378  {
379  return search(src, tgt, info);
380  }
381 
388  void remove_from_graph(GT_Arc * a) throw(std::exception, std::domain_error)
389  {
390  Tree_Type::find(a);
391  Tree_Type::remove(a);
392  g.remove_arc(a);
393  }
394 
395 };
396 
397 } // End namespace Aleph
398 
399 # endif // GRAPH_INDEXES_H
400 
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexes.H:322
Definition: tpl_graph_indexes.H:281
Definition: tpl_graph.H:1225
typename Tree< GT::Node *, Compare >::Node Node
Definition: tpl_dynSetTree.H:72
GT_Node * insert_in_graph(const GT_Node_Info &info)
Definition: tpl_graph_indexes.H:171
GT_Node * search_or_insert_in_graph(const GT_Node_Info &info)
Definition: tpl_graph_indexes.H:192
Definition: tpl_graph_indexes.H:98
Definition: tpl_graph.H:1257
GT_Node * insert_in_graph(const GT_Node_Info &&info=GT_Node_Info())
Definition: tpl_graph_indexes.H:205
GT_Node * search(GT_Node *p)
Definition: tpl_graph_indexes.H:219
GT_Node * insert_in_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:133
void remove_from_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:243
Definition: ah-comb.H:35
Definition: tpl_graph.H:1063
Definition: tpl_treap.H:535
Definition: tpl_dynSetTree.H:61
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Definition: tpl_graph_indexes.H:376
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Definition: tpl_graph_indexes.H:350
void remove_from_graph(GT_Arc *a)
Definition: tpl_graph_indexes.H:388
Definition: tpl_graph.H:1270
GT_Node * search_or_insert_in_graph(GT_Node *p)
Definition: tpl_graph_indexes.H:152
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Definition: tpl_graph_indexes.H:337

Leandro Rabindranath León