Aleph-w  1.9
General library for algorithms and data structures
tpl_indexArc.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_INDEXARC_H
28 # define TPL_INDEXARC_H
29 
30 # include <tpl_dynSetTree.H>
31 # include <tpl_graph.H>
32 
33 using namespace Aleph;
34 
35 namespace Aleph
36 {
37 
38 
62  template <
63  class GT,
64  template <class /* Key */, class /* Compare */> class Tree = Rand_Tree,
65  class SA = Dft_Show_Arc<GT>
66  >
67  class IndexArc
68  {
69  private:
70 
71  typedef typename GT::Arc GT_Arc;
72  typedef typename GT::Node GT_Node;
73  typedef typename GT::Arc_Type GT_Arc_Type;
74  typedef typename GT::Node_Type GT_Node_Type;
75 
76  typedef std::pair<void*, void*> Node_Pair;
77 
78  struct Cmp_Arc
79  {
80  bool operator () (GT_Arc * a1, GT_Arc * a2) const
81  {
82  if (a1->src_node < a2->src_node)
83  return true;
84 
85  return not (a2->src_node < a1->src_node) and a1->tgt_node < a2->tgt_node;
86  }
87  };
88 
89  GT & g;
91  SA & sa;
92 
93  public:
94 
96  GT_Arc * insert(GT_Arc * e)
97  {
98  return *index.put(e);
99  }
100 
107  GT_Arc * search(void * src, void * tgt)
108  {
109  GT_Arc arc;
110  arc.src_node = src;
111  arc.tgt_node = tgt;
112 
113  GT_Arc ** ret_val = index.search(&arc);
114 
115  if (ret_val != nullptr)
116  return *ret_val;
117 
118  if (g.is_digraph())
119  return nullptr;
120 
121  std::swap(arc.src_node, arc.tgt_node);
122  ret_val = index.search(&arc);
123 
124  if (ret_val == nullptr)
125  return nullptr;
126 
127  assert(((src == (*ret_val)->src_node) and (tgt == (*ret_val)->tgt_node)) or
128  ((tgt == (*ret_val)->src_node) and (src == (*ret_val)->tgt_node)));
129 
130  return *ret_val;
131  }
132 
139  GT_Arc * search_directed(void * src, void * tgt)
140  {
141  GT_Arc arc;
142  arc.src_node = src;
143  arc.tgt_node = tgt;
144 
145  GT_Arc ** ret_val = index.search(&arc);
146 
147  return ret_val != nullptr ? *ret_val : nullptr;
148  }
149 
155  GT_Arc * search(GT_Arc * a)
156  {
157  return search(a->src_node, a->tgt_node);
158  }
159 
171  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
172  const GT_Arc_Type & info)
173  {
174  GT_Arc * a = search(src, tgt);
175 
176  if (a != nullptr)
177  throw std::domain_error("There is already in arc between these nodes");
178 
179  a = g.insert_arc(src, tgt, info);
180  insert(a);
181 
182  return a;
183  }
184 
186  GT_Arc * insert_in_graph(GT_Node * src, GT_Node * tgt,
187  GT_Arc_Type && info = GT_Arc_Type())
188  {
189  GT_Arc * a = search(src, tgt);
190 
191  if (a != nullptr)
192  throw std::domain_error("There is already in arc between these nodes");
193 
194  a = g.insert_arc(src, tgt, std::forward<GT_Arc_Type>(info));
195  insert(a);
196 
197  return a;
198  }
199 
201  void remove(GT_Arc * e)
202  {
203  index.remove(e);
204  }
205 
207  void remove_from_graph(GT_Arc * a)
208  {
209  remove(a);
210  g.remove_arc(a);
211  }
212 
214  void clear_index()
215  {
216  index.empty();
217  }
218 
220  void build_index()
221  {
222  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
223  {
224  GT_Arc * a = it.get_curr_ne();
225  if (search(a) != a)
226  insert(a);
227  }
228  }
229 
230  private:
231 
232  void init()
233  {
234  for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
235  insert(it.get_curr_ne());
236  }
237 
238  public:
239 
248  IndexArc(GT & __g, bool with_init = true, SA && __sa = SA())
249  : g(__g), sa(__sa)
250  {
251  if (with_init)
252  init();
253  }
254 
263  IndexArc(GT & __g, bool with_init, SA & __sa) : g(__g), sa(__sa)
264  {
265  if (with_init)
266  init();
267  }
268 
270  size_t size() const { return index.size(); }
271  };
272 
273 }
274 
275 # endif
276 
GT_Arc * search_directed(void *src, void *tgt)
Definition: tpl_indexArc.H:139
Definition: tpl_graph.H:1225
IndexArc(GT &__g, bool with_init, SA &__sa)
Definition: tpl_indexArc.H:263
size_t remove(const Key &key)
Definition: tpl_dynSetTree.H:327
GT_Arc * search(GT_Arc *a)
Definition: tpl_indexArc.H:155
void clear_index()
Elimina todos los arcos del índice.
Definition: tpl_indexArc.H:214
GT_Arc * insert(GT_Arc *e)
Inserta en el índice el arco a.
Definition: tpl_indexArc.H:96
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info)
Definition: tpl_indexArc.H:171
Definition: tpl_rand_tree.H:676
IndexArc(GT &__g, bool with_init=true, SA &&__sa=SA())
Definition: tpl_indexArc.H:248
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, GT_Arc_Type &&info=GT_Arc_Type())
Definition: tpl_indexArc.H:186
Definition: ah-comb.H:35
Definition: tpl_graph.H:1063
void remove_from_graph(GT_Arc *a)
Elimina del índice y del grafo el arco a.
Definition: tpl_indexArc.H:207
const size_t & size() const
Retorna la cardinalidad del conjunto.
Definition: tpl_dynSetTree.H:502
Definition: tpl_indexArc.H:67
size_t size() const
Retorna la cantidad de arcos que contiene el índice.
Definition: tpl_indexArc.H:270
GT_Arc * search(void *src, void *tgt)
Definition: tpl_indexArc.H:107
void empty()
Elimina todos los elementos del conjunto.
Definition: tpl_dynSetTree.H:133
void build_index()
Inserta todos los arcos del grafo en el índice.
Definition: tpl_indexArc.H:220
Key * search(const Key &key) const
Definition: tpl_dynSetTree.H:462

Leandro Rabindranath León