Aleph-w  1.9
General library for algorithms and data structures
tpl_indexNode.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_INDEXNODE_H
28 # define TPL_INDEXNODE_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
42  operator () (typename GT::Node * p1, typename GT::Node * p2) const
43  {
44  return p1->get_info() < p2->get_info();
45  }
46  };
47 
82  template <class GT, class Compare = Dft_Node_Cmp<GT>,
83  template <class /* Key */, class /* Compare */> class Tree = Treap,
84  class SN = Dft_Show_Node<GT> >
85  class IndexNode
86  {
87  private:
88 
89  typedef typename GT::Node GT_Node;
90  typedef typename GT::Node_Type GT_Node_Type;
91 
93 
94  GT & g;
95  SN & sn;
96 
97  public:
98 
104  GT_Node * insert(GT_Node * p)
105  {
106  index.put(p);
107  return p;
108  }
109 
117  GT_Node * insert_in_graph(const GT_Node_Type & info)
118  {
119  GT_Node * ret_val = g.insert_node(info);
120 
121  try
122  {
123  insert(ret_val);
124 
125  return ret_val;
126  }
127  catch (...)
128  {
129  g.remove_node(ret_val);
130  throw;
131  }
132  }
133 
135  GT_Node * insert_in_graph(GT_Node_Type && info = GT_Node_Type())
136  {
137  GT_Node * ret_val = g.insert_node(std::forward<GT_Node_Type>(info));
138 
139  try
140  {
141  insert(ret_val);
142 
143  return ret_val;
144  }
145  catch (...)
146  {
147  g.remove_node(ret_val);
148  throw;
149  }
150  }
151 
161  GT_Node * search(GT_Node * p)
162  {
163  GT_Node ** ret_val = index.search(p);
164 
165  if (ret_val == nullptr)
166  return nullptr;
167 
168  return *ret_val;
169  }
170 
182  GT_Node * search(const GT_Node_Type & info)
183  {
184  GT_Node dummy_node(info);
185  GT_Node * dummy_node_ptr = &dummy_node;
186 
187  return search(dummy_node_ptr);
188  }
189 
191  void remove(GT_Node * p)
192  {
193  index.remove(p);
194  }
195 
202  void remove_from_graph(GT_Node * p)
203  {
204  index.find(p); // dispara excepción si p no está en el índice
205  index.remove(p);
206  g.remove_node(p);
207  }
208 
210  void clear_index()
211  {
212  index.empty();
213  }
214 
216  void build_index()
217  {
218  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
219  {
220  GT_Node * p = it.get_curr_ne();
221  if (search(p) != p)
222  insert(p);
223  }
224  }
225 
227  void clear_graph()
228  {
229  clear_index();
230  g.clear_graph();
231  }
232 
233  private:
234 
235  void init()
236  {
237  for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
238  insert(it.get_curr_ne());
239  }
240 
241  public:
242 
250  IndexNode(GT & __g, SN && __sn = SN()) : g(__g), sn(__sn)
251  {
252  init();
253  }
254 
255  IndexNode(GT & __g, SN & __sn) : g(__g), sn(__sn)
256  {
257  init();
258  }
259 
261  size_t size() const { return index.size(); }
262  };
263 
264 }
265 
266 # endif
267 
GT_Node * search(const GT_Node_Type &info)
Definition: tpl_indexNode.H:182
void remove_from_graph(GT_Node *p)
Definition: tpl_indexNode.H:202
size_t remove(const Key &key)
Definition: tpl_dynSetTree.H:327
void clear_index()
Borra el índice; todos los nodos son eliminados.
Definition: tpl_indexNode.H:210
Definition: tpl_graph.H:1257
void clear_graph()
Elimina todos los nodos del grafo y del índice.
Definition: tpl_indexNode.H:227
Definition: ah-comb.H:35
Definition: tpl_treap.H:535
GT_Node * insert(GT_Node *p)
Definition: tpl_indexNode.H:104
const size_t & size() const
Retorna la cardinalidad del conjunto.
Definition: tpl_dynSetTree.H:502
Definition: tpl_indexNode.H:85
Definition: tpl_graph.H:1270
void build_index()
Inserta todos los nodos del grafo en el índice.
Definition: tpl_indexNode.H:216
GT_Node * search(GT_Node *p)
Definition: tpl_indexNode.H:161
Key & find(const Key &key) const
Definition: tpl_dynSetTree.H:414
GT_Node * insert_in_graph(GT_Node_Type &&info=GT_Node_Type())
Definition: tpl_indexNode.H:135
IndexNode(GT &__g, SN &&__sn=SN())
Definition: tpl_indexNode.H:250
void empty()
Elimina todos los elementos del conjunto.
Definition: tpl_dynSetTree.H:133
size_t size() const
Retorna la cantidad de arcos que contiene el índice.
Definition: tpl_indexNode.H:261
Key * search(const Key &key) const
Definition: tpl_dynSetTree.H:462
GT_Node * insert_in_graph(const GT_Node_Type &info)
Definition: tpl_indexNode.H:117

Leandro Rabindranath León