Aleph-w  1.9
General library for algorithms and data structures
io_graph.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 IO_GRAPH_H
28 # define IO_GRAPH_H
29 
30 # include <fstream>
31 # include <iostream>
32 # include <tpl_graph.H>
33 
34 extern bool verbose; // comunmente definida en main
35 
36 namespace Aleph {
37 
38 
39  template <class GT>
40 struct Dft_Store_Node
41 {
42  void operator () (ofstream & output, GT &, typename GT::Node * p)
43  {
44  output.write((char*) &p->get_info(), sizeof(typename GT::Node_Type));
45  }
46 
47  void operator () (ostream & output, GT &, typename GT::Node * p)
48  {
49  output << p->get_info() << endl;
50  }
51 };
52 
53  template <class GT>
54 struct Dft_Store_Arc
55 {
56  void operator () (ofstream & output, GT &, typename GT::Arc * a)
57  {
58  output.write((char*) &a->get_info(), sizeof(typename GT::Arc_Type));
59  }
60 
61  void operator () (ostream & output, GT &, typename GT::Arc * a)
62  {
63  output << a->get_info() << endl;
64  }
65 };
66 
67  template <class GT>
68 struct Dft_Load_Node
69 {
70  void operator () (ifstream & input, GT &, typename GT::Node * p)
71  {
72  input.read((char*) &p->get_info(), sizeof(typename GT::Node_Type));
73  }
74 
75  void operator () (istream & input, GT &, typename GT::Node * p)
76  {
77  input >> p->get_info();
78  }
79 };
80 
81  template <class GT>
82 struct Dft_Load_Arc
83 {
84  void operator () (ifstream & input, GT &, typename GT::Arc * a)
85  {
86  input.read((char*) &a->get_info(), sizeof(typename GT::Arc_Type));
87  }
88 
89  void operator () (istream & input, GT &, typename GT::Arc * a)
90  {
91  input >> a->get_info();
92  }
93 };
94 
144 template <class GT,
145  class Load_Node = Dft_Load_Node<GT>,
146  class Store_Node = Dft_Store_Node<GT>,
147  class Load_Arc = Dft_Load_Arc<GT>,
148  class Store_Arc = Dft_Store_Arc<GT>,
149  class NF = Aleph::Dft_Show_Node<GT>, // Filtro de nodos
150  class AF = Aleph::Dft_Show_Arc<GT> > // Filtro de arcos
151 class IO_Graph
152 {
153  GT & g;
154 
155  Load_Node load_node = Load_Node();
156  Store_Node store_node = Store_Node();
157  Load_Arc load_arc = Load_Arc();
158  Store_Arc store_arc = Store_Arc();
159 
160  NF node_filter = NF();
161  AF arc_filter = AF();
162 
163 public:
164 
165  void set_load_node(const Load_Node & ln) { load_node = ln; }
166 
167  void set_store_node(const Store_Node & sn) { store_node = sn; }
168 
169  void set_load_arc(const Load_Arc & la) { load_arc = la; }
170 
171  void set_store_arc(const Store_Arc & sa) { store_arc = sa; }
172 
173  void set_node_filter(const NF & nf) { node_filter = nf; }
174 
175  void set_arc_filter(const AF & af) { arc_filter = af; }
176 
178  IO_Graph(GT & __g) noexcept : g(__g) { /* empty */ }
179 
181  IO_Graph(GT * gptr) noexcept : g(*gptr) { /* empty */ }
182 
184  void save(ofstream & output)
185  {
186  // guarda cantidad de nodos
187  const size_t num_nodes = g.get_num_nodes();
188 
189  if (verbose)
190  cout << "Storing " << num_nodes << " nodes ... ";
191 
192  output.write((const char*) &num_nodes, sizeof(num_nodes));
193 
194  int i = 0; // contador de nodos. Lleva los índices en la tabla
195 
196  // tabla de pares <ptr nodo, orden de isnerción>
198 
199  for (Node_Iterator<GT,NF> it(g, node_filter); it.has_curr();
200  it.next_ne(), ++i)
201  {
202  auto p = it.get_curr_ne();
203 
204  if (verbose)
205  cout << i << " ";
206 
207  store_node(output, g, p);
208 
209  nodes_table.insert(p, i);
210  }
211 
212  const size_t num_arcs = g.get_num_arcs();
213 
214  if (verbose)
215  cout << " done " << endl
216  << "Storing " << num_arcs << " arcs ... " << endl;
217 
218  output.write((const char*) &num_arcs, sizeof(num_arcs));
219 
220  for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
221  {
222  auto a = it.get_curr_ne();
223 
224  auto src = g.get_src_node(a);
225  auto tgt = g.get_tgt_node(a);
226 
227  const auto & src_idx = nodes_table.find(src);
228  const auto & tgt_idx = nodes_table.find(tgt);
229 
230  // guarda índices de nodos origen y destino respectivamente
231  output.write((const char*) &src_idx, sizeof(int));
232  output.write((const char*) &tgt_idx, sizeof(int));
233 
234  if (verbose)
235  cout << " " << src_idx << "--" << tgt_idx << " ";
236 
237  store_arc(output, g, a);
238 
239  if (verbose)
240  cout << endl;
241  }
242 
243  if (verbose)
244  cout << " done " << endl << endl;
245  }
246 
248  void load(ifstream & input)
249  {
250  // lee cantidad de nodos
251  size_t num_nodes;
252  input.read((char *) &num_nodes, sizeof(num_nodes));
253 
254  if (verbose)
255  cout << "Loading " << num_nodes << " nodes ...";
256 
257  DynArray<typename GT::Node*> nodes_table(num_nodes);
258  nodes_table.reserve(0, num_nodes - 1);
259 
260  for (size_t i = 0; i < num_nodes; ++i)
261  {
262  typename GT::Node * p = new typename GT::Node;
263 
264  if (verbose)
265  cout << " " << i;
266 
267  load_node(input, g, p);
268 
269  p = g.insert_node(p);
270  nodes_table.access(i) = p;
271  }
272 
273  size_t num_arcs;
274  input.read((char *) &num_arcs, sizeof(num_arcs));
275 
276  if (verbose)
277  cout << " done " << endl
278  << "Loading " << num_arcs << " arcs ... " << endl;
279 
280  for (int i = 0; i < num_arcs; ++i)
281  {
282  int src_idx;
283  input.read((char*) &src_idx, sizeof(int));
284  auto src = nodes_table.access(src_idx);
285 
286  int tgt_idx;
287  input.read((char*) &tgt_idx, sizeof(int));
288  auto tgt = nodes_table.access(tgt_idx);
289 
290  auto a = g.insert_arc(src, tgt);
291 
292  if (verbose)
293  cout << " " << src_idx << "--" << tgt_idx << " ";
294 
295  load_arc(input, g, a);
296 
297  if (verbose)
298  cout << endl;
299  }
300 
301  if (verbose)
302  cout << " done " << endl << endl;
303  }
304 
306  void save_in_text_mode(ostream & output)
307  { // guarda cantidad de nodos
308  const size_t num_nodes = g.get_num_nodes();
309  const size_t num_arcs = g.get_num_arcs();
310  output << num_nodes << endl
311  << num_arcs << endl;
312 
313  if (verbose)
314  cout << "Storing " << num_nodes << " nodes ... ";
315 
316  int i = 0; // contador de nodos. Lleva los índices en la tabla
317 
318  // tabla de pares <ptr nodo, orden de inserción>
320 
321  for (Node_Iterator<GT,NF> it(g, node_filter); it.has_curr();
322  it.next_ne(), ++i)
323  {
324  typename GT::Node * p = it.get_curr_ne();
325 
326  if (verbose)
327  cout << i << " ";
328 
329  store_node(output, g, p);
330 
331  nodes_table.insert(p, i);
332  }
333 
334  if (verbose)
335  cout << " done " << endl
336  << "Storing " << num_arcs << " arcs ... " << endl;
337 
338  for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
339  {
340  auto a = it.get_curr_ne();
341 
342  auto src = g.get_src_node(a);
343  auto tgt = g.get_tgt_node(a);
344 
345  const auto & src_idx = nodes_table.find(src);
346  const auto & tgt_idx = nodes_table.find(tgt);
347 
348  // guarda índices de nodos origen y destino respectivamente
349  output << src_idx << " " << tgt_idx << " ";
350 
351  if (verbose)
352  cout << " " << src_idx << "--" << tgt_idx << " ";
353 
354  store_arc(output, g, a);
355 
356  if (verbose)
357  cout << endl;
358  }
359 
360  if (verbose)
361  cout << " done " << endl << endl;
362  }
363 
365  void load_in_text_mode(istream & input)
366  {
367  // lee cantidad de nodos
368  size_t num_nodes;
369  size_t num_arcs;
370 
371  input >> num_nodes >> num_arcs;
372  input.ignore();
373 
374  if (verbose)
375  cout << "Loading " << num_nodes << " nodes ...";
376 
377  DynArray<typename GT::Node*> nodes_table(num_nodes);
378  nodes_table.reserve(0, num_nodes - size_t(1));
379 
380  for (size_t i = 0; i < num_nodes; ++i)
381  {
382  auto p = new typename GT::Node;
383 
384  if (verbose)
385  cout << " " << i;
386 
387  load_node(input, g, p);
388 
389  p = g.insert_node(p);
390  nodes_table.access(i) = p;
391  }
392 
393  if (verbose)
394  cout << " done " << endl
395  << "Loading " << num_arcs << " arcs ... " << endl;
396 
397  for (int i = 0; i < num_arcs; ++i)
398  {
399  int src_idx;
400  int tgt_idx;
401 
402  input >> src_idx >> tgt_idx;
403 
404  auto src = nodes_table.access(src_idx);
405  auto tgt = nodes_table.access(tgt_idx);
406  auto a = g.insert_arc(src, tgt);
407 
408  if (verbose)
409  cout << " " << src_idx << "--" << tgt_idx << " ";
410 
411  load_arc(input, g, a);
412 
413  if (verbose)
414  cout << " " << endl;
415  }
416 
417  if (verbose)
418  cout << " done " << endl << endl;
419  }
420 };
421 
422 } // end namespace Aleph
423 
424 # endif // IO_GRAPH_H
Definition: io_graph.H:151
void reserve(const size_t l, const size_t r)
Definition: tpl_dynArray.H:998
Definition: tpl_graph.H:1225
IO_Graph(GT &__g) noexcept
Constructor con referencia a grafo.
Definition: io_graph.H:178
Pair * insert(const Key &key, const Type &data)
Definition: tpl_dynMapTree.H:112
T & access(const size_t i) const noexcept
Definition: tpl_dynArray.H:874
Definition: tpl_graph.H:1257
IO_Graph(GT *gptr) noexcept
Constructor con puntero a grafo.
Definition: io_graph.H:181
void save_in_text_mode(ostream &output)
Guarda el grafo en el stream output.
Definition: io_graph.H:306
Definition: ah-comb.H:35
Definition: tpl_graph.H:1063
void load(ifstream &input)
Carga el grafo desde el stream output.
Definition: io_graph.H:248
void save(ofstream &output)
Guarda el grafo en el stream output.
Definition: io_graph.H:184
Definition: tpl_dynMapTree.H:357
Definition: tpl_graph.H:1270
Definition: tpl_dynArray.H:159
void load_in_text_mode(istream &input)
Carga el grafo desde el stream intput.
Definition: io_graph.H:365
Type & find(const Key &key)
Definition: tpl_dynMapTree.H:242

Leandro Rabindranath León