Aleph-w  1.9
General library for algorithms and data structures
tpl_indexGraph.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_INDEXGRAPH
28 # define TPL_INDEXGRAPH
29 
30 # include <tpl_indexNode.H>
31 # include <tpl_indexArc.H>
32 
33 namespace Aleph
34 {
35 
74 template <class GT, class Compare = Dft_Node_Cmp<GT>,
75  template <class /* Key */, class /* Compare */> class Tree = Treap>
77 {
78 private:
79 
80  typedef typename GT::Arc GT_Arc;
81  typedef typename GT::Node GT_Node;
82  typedef typename GT::Arc_Type GT_Arc_Type;
83  typedef typename GT::Node_Type GT_Node_Type;
84 
86  IndexArc<GT, Tree> idx_arc;
87 
88 public:
89 
91  Index_Graph(GT & g) : idx_node(g), idx_arc(g)
92  {
93  // empty
94  }
95 
102  GT_Node * insert_node(const GT_Node_Type & info)
103  {
104  return idx_node.insert_in_graph(info);
105  }
106 
117  GT_Arc * insert_arc(GT_Node * src, GT_Node * tgt,
118  const GT_Arc_Type & info = GT_Arc_Type())
119  {
120  if (idx_node.search(src) == nullptr)
121  throw std::domain_error("src node not in index");
122 
123  if (idx_node.search(tgt) == nullptr)
124  throw std::domain_error("tgt node not in index");
125 
126  return idx_arc.insert_in_graph(src, tgt, info);
127  }
128 
135  GT_Node * search_node(GT_Node * p)
136  {
137  return idx_node.search(p);
138  }
139 
149  GT_Node * search_node(const GT_Node_Type & info)
150  {
151  return idx_node.search(info);
152  }
153 
161  GT_Arc * search_arc(GT_Node * src, GT_Node * tgt)
162  {
163  return idx_arc.search(src, tgt);
164  }
165 
170  void remove_node(GT_Node * p)
171  {
172  // eliminar del índice los arcos emanantes del nodo (ellos serán
173  // eliminados por la eliminación de nodo del grafo)
174  for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
175  idx_arc.remove(it.get_curr_ne());
176 
177  return idx_node.remove_from_graph(p);
178  }
179 
181  void remove_arc(GT_Arc * a)
182  {
183  return idx_arc.remove_from_graph(a);
184  }
185 
187  size_t get_num_arcs() const { return idx_arc.size(); }
188 
190  size_t get_num_nodes() const { return idx_node.size(); }
191 };
192 
193  template <class GT> inline
194  bool are_equal(const GT & g1, const GT & g2)
195  {
196  if (g1.vsize() != g2.vsize() or g1.esize() != g2.esize())
197  return false;
198 
199  {
200  IndexNode<GT> t2(const_cast<GT&>(g2));
201  if (not g1.all_nodes([&t2] (auto p)
202  {
203  auto q = t2.search(p);
204  if (q == nullptr)
205  return false;
206  GT::map_nodes(p, q);
207  return true;
208  }))
209  return false;
210  }
211 
212  IndexArc<GT> t2(const_cast<GT&>(g2));
213  return g1.all_arcs([idx = &t2, &g1] (auto a)
214  {
215  auto s1 = g1.get_src_node(a);
216  auto t1 = g1.get_tgt_node(a);
217  auto s2 = mapped_node<GT>(s1);
218  auto t2 = mapped_node<GT>(t1);
219  auto a2 = idx->search(s2, t2);
220  if (a2 == nullptr)
221  return false;
222  return a2->get_info() == a->get_info();
223  });
224  }
225 
226 }
227 
228 # endif // TPL_INDEXGRAPH
229 
230 
void remove(GT_Arc *e)
Elimina del índice el arco e.
Definition: tpl_indexArc.H:201
size_t get_num_arcs() const
Retorna el número de arcos que contiene el índice.
Definition: tpl_indexGraph.H:187
void remove_from_graph(GT_Node *p)
Definition: tpl_indexNode.H:202
size_t get_num_nodes() const
Retorna el número de nodos que contiene el índice.
Definition: tpl_indexGraph.H:190
Definition: tpl_indexGraph.H:76
GT_Node * search_node(GT_Node *p)
Definition: tpl_indexGraph.H:135
GT_Node * search_node(const GT_Node_Type &info)
Definition: tpl_indexGraph.H:149
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info)
Definition: tpl_indexArc.H:171
void remove_arc(GT_Arc *a)
Elimina del grafo y del índice el arco a.
Definition: tpl_indexGraph.H:181
Definition: ah-comb.H:35
GT_Arc * search_arc(GT_Node *src, GT_Node *tgt)
Definition: tpl_indexGraph.H:161
Definition: tpl_treap.H:535
GT_Node * insert_node(const GT_Node_Type &info)
Definition: tpl_indexGraph.H:102
void remove_from_graph(GT_Arc *a)
Elimina del índice y del grafo el arco a.
Definition: tpl_indexArc.H:207
void remove_node(GT_Node *p)
Definition: tpl_indexGraph.H:170
GT_Node * search(GT_Node *p)
Definition: tpl_indexNode.H:161
Index_Graph(GT &g)
Crea un índice del grafo: los nodos y arcos son indizados.
Definition: tpl_indexGraph.H:91
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
size_t size() const
Retorna la cantidad de arcos que contiene el índice.
Definition: tpl_indexNode.H:261
GT_Node * insert_in_graph(const GT_Node_Type &info)
Definition: tpl_indexNode.H:117
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info=GT_Arc_Type())
Definition: tpl_indexGraph.H:117

Leandro Rabindranath León