Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
io_graph.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
130#ifndef IO_GRAPH_H
131#define IO_GRAPH_H
132
133#include <fstream>
134#include <iostream>
135#include <memory>
136#include <tpl_graph.H>
137#include <ah-errors.H>
138
139namespace Aleph
140{
141
150template <class GT>
152{
158 void operator()(std::ofstream & output, GT & g, typename GT::Node * p)
159 {
160 (void)g;
161 output.write(reinterpret_cast<const char*>(&p->get_info()),
162 sizeof(typename GT::Node_Type));
163 }
164
170 void operator()(std::ostream & output, GT & g, typename GT::Node * p)
171 {
172 (void)g;
173 output << p->get_info() << std::endl;
174 }
175};
176
185template <class GT>
187{
193 void operator()(std::ofstream & output, GT & g, typename GT::Arc * a)
194 {
195 (void)g;
196 output.write(reinterpret_cast<const char*>(&a->get_info()),
197 sizeof(typename GT::Arc_Type));
198 }
199
205 void operator()(std::ostream & output, GT & g, typename GT::Arc * a)
206 {
207 (void)g;
208 output << a->get_info() << std::endl;
209 }
210};
211
220template <class GT>
222{
228 void operator()(std::ifstream & input, GT & g, typename GT::Node * p)
229 {
230 (void)g;
231 input.read(reinterpret_cast<char*>(&p->get_info()),
232 sizeof(typename GT::Node_Type));
233 }
234
240 void operator()(std::istream & input, GT & g, typename GT::Node * p)
241 {
242 (void)g;
243 input >> p->get_info();
244 }
245};
246
255template <class GT>
257{
263 void operator()(std::ifstream & input, GT & g, typename GT::Arc * a)
264 {
265 (void)g;
266 input.read(reinterpret_cast<char*>(&a->get_info()),
267 sizeof(typename GT::Arc_Type));
268 }
269
275 void operator()(std::istream & input, GT & g, typename GT::Arc * a)
276 {
277 (void)g;
278 input >> a->get_info();
279 }
280};
281
311template <class GT,
319{
320 GT & g;
321
326
329
330 bool verbose_mode = false;
331
332public:
333
341 void set_verbose(bool v) noexcept { verbose_mode = v; }
342
347
351 void set_load_node(const Load_Node & ln) { load_node = ln; }
352
356 void set_store_node(const Store_Node & sn) { store_node = sn; }
357
361 void set_load_arc(const Load_Arc & la) { load_arc = la; }
362
366 void set_store_arc(const Store_Arc & sa) { store_arc = sa; }
367
371 void set_node_filter(const NF & nf) { node_filter = nf; }
372
376 void set_arc_filter(const AF & af) { arc_filter = af; }
377
381 explicit IO_Graph(GT & __g) noexcept : g(__g) {}
382
386 explicit IO_Graph(GT * gptr) noexcept : g(*gptr) {}
387
401 void save(std::ofstream & output)
402 {
403 const size_t num_nodes = g.get_num_nodes();
404
405 if (verbose_mode)
406 std::cout << "Storing " << num_nodes << " nodes ... ";
407
408 output.write(reinterpret_cast<const char*>(&num_nodes), sizeof(num_nodes));
409
410 int i = 0;
412
413 for (Node_Iterator<GT, NF> it(g, node_filter); it.has_curr();
414 it.next_ne(), ++i)
415 {
416 auto p = it.get_curr();
417
418 if (verbose_mode)
419 std::cout << i << " ";
420
421 store_node(output, g, p);
422 nodes_table.insert(p, i);
423 }
424
425 const size_t num_arcs = g.get_num_arcs();
426
427 if (verbose_mode)
428 std::cout << " done " << std::endl
429 << "Storing " << num_arcs << " arcs ... " << std::endl;
430
431 output.write(reinterpret_cast<const char*>(&num_arcs), sizeof(num_arcs));
432
433 for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
434 {
435 auto a = it.get_curr();
436
437 auto src = g.get_src_node(a);
438 auto tgt = g.get_tgt_node(a);
439
440 const int src_idx = nodes_table.find(src);
441 const int tgt_idx = nodes_table.find(tgt);
442
443 output.write(reinterpret_cast<const char*>(&src_idx), sizeof(int));
444 output.write(reinterpret_cast<const char*>(&tgt_idx), sizeof(int));
445
446 if (verbose_mode)
447 std::cout << " " << src_idx << "--" << tgt_idx << " ";
448
449 store_arc(output, g, a);
450
451 if (verbose_mode)
452 std::cout << std::endl;
453 }
454
455 if (verbose_mode)
456 std::cout << " done " << std::endl << std::endl;
457 }
458
469 void load(std::ifstream & input)
470 {
471 size_t num_nodes;
472 input.read(reinterpret_cast<char*>(&num_nodes), sizeof(num_nodes));
474 << "Failed to read node count from binary stream";
475
476 if (verbose_mode)
477 std::cout << "Loading " << num_nodes << " nodes ...";
478
480 if (num_nodes > 0)
481 nodes_table.reserve(0, num_nodes - 1);
482
483 for (size_t i = 0; i < num_nodes; ++i)
484 {
485 std::unique_ptr<typename GT::Node> p(new typename GT::Node);
486
487 if (verbose_mode)
488 std::cout << " " << i;
489
490 load_node(input, g, p.get());
492 << "Failed to load node " << i << " from binary stream";
493
494 typename GT::Node * inserted = g.insert_node(p.release());
495 nodes_table.access(i) = inserted;
496 }
497
498 size_t num_arcs;
499 input.read(reinterpret_cast<char*>(&num_arcs), sizeof(num_arcs));
501 << "Failed to read arc count from binary stream";
502
503 if (verbose_mode)
504 std::cout << " done " << std::endl
505 << "Loading " << num_arcs << " arcs ... " << std::endl;
506
507 for (size_t i = 0; i < num_arcs; ++i)
508 {
509 int src_idx;
510 input.read(reinterpret_cast<char*>(&src_idx), sizeof(int));
512 << "Failed to read source index for arc " << i;
513
514 auto src = nodes_table.access(src_idx);
515
516 int tgt_idx;
517 input.read(reinterpret_cast<char*>(&tgt_idx), sizeof(int));
519 << "Failed to read target index for arc " << i;
520
521 auto tgt = nodes_table.access(tgt_idx);
522 auto a = g.insert_arc(src, tgt);
523
524 if (verbose_mode)
525 std::cout << " " << src_idx << "--" << tgt_idx << " ";
526
527 load_arc(input, g, a);
529 << "Failed to load arc " << i << " data";
530
531 if (verbose_mode)
532 std::cout << std::endl;
533 }
534
535 if (verbose_mode)
536 std::cout << " done " << std::endl << std::endl;
537 }
538
558 void save_in_text_mode(std::ostream & output)
559 {
560 const size_t num_nodes = g.get_num_nodes();
561 const size_t num_arcs = g.get_num_arcs();
562
563 output << num_nodes << std::endl
564 << num_arcs << std::endl;
565
566 if (verbose_mode)
567 std::cout << "Storing " << num_nodes << " nodes ... ";
568
569 int i = 0;
571
572 for (Node_Iterator<GT, NF> it(g, node_filter); it.has_curr();
573 it.next_ne(), ++i)
574 {
575 typename GT::Node * p = it.get_curr();
576
577 if (verbose_mode)
578 std::cout << i << " ";
579
580 store_node(output, g, p);
581 nodes_table.insert(p, i);
582 }
583
584 if (verbose_mode)
585 std::cout << " done " << std::endl
586 << "Storing " << num_arcs << " arcs ... " << std::endl;
587
588 for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
589 {
590 auto a = it.get_curr();
591
592 auto src = g.get_src_node(a);
593 auto tgt = g.get_tgt_node(a);
594
595 const int src_idx = nodes_table.find(src);
596 const int tgt_idx = nodes_table.find(tgt);
597
598 output << src_idx << " " << tgt_idx << " ";
599
600 if (verbose_mode)
601 std::cout << " " << src_idx << "--" << tgt_idx << " ";
602
603 store_arc(output, g, a);
604
605 if (verbose_mode)
606 std::cout << std::endl;
607 }
608
609 if (verbose_mode)
610 std::cout << " done " << std::endl << std::endl;
611 }
612
623 void load_in_text_mode(std::istream & input)
624 {
625 size_t num_nodes;
626 size_t num_arcs;
627
628 input >> num_nodes >> num_arcs;
630 << "Failed to read node/arc count from text stream";
631
632 input.ignore();
633
634 if (verbose_mode)
635 std::cout << "Loading " << num_nodes << " nodes ...";
636
638 if (num_nodes > 0)
639 nodes_table.reserve(0, num_nodes - 1);
640
641 for (size_t i = 0; i < num_nodes; ++i)
642 {
643 std::unique_ptr<typename GT::Node> p(new typename GT::Node);
644
645 if (verbose_mode)
646 std::cout << " " << i;
647
648 load_node(input, g, p.get());
650 << "Failed to load node " << i << " from text stream";
651
652 typename GT::Node * inserted = g.insert_node(p.release());
653 nodes_table.access(i) = inserted;
654 }
655
656 if (verbose_mode)
657 std::cout << " done " << std::endl
658 << "Loading " << num_arcs << " arcs ... " << std::endl;
659
660 for (size_t i = 0; i < num_arcs; ++i)
661 {
662 int src_idx;
663 int tgt_idx;
664
665 input >> src_idx >> tgt_idx;
667 << "Failed to read arc " << i << " indices from text stream";
668
669 auto src = nodes_table.access(src_idx);
670 auto tgt = nodes_table.access(tgt_idx);
671 auto a = g.insert_arc(src, tgt);
672
673 if (verbose_mode)
674 std::cout << " " << src_idx << "--" << tgt_idx << " ";
675
676 load_arc(input, g, a);
678 << "Failed to load arc " << i << " data from text stream";
679
680 if (verbose_mode)
681 std::cout << std::endl;
682 }
683
684 if (verbose_mode)
685 std::cout << " done " << std::endl << std::endl;
686 }
687};
688
689} // namespace Aleph
690
691// Global namespace compatibility
692using Aleph::IO_Graph;
697
698#endif // IO_GRAPH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
int num_nodes
Definition btreepic.C:410
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
Dynamic map implemented with a treap.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Graph serialization and deserialization class.
Definition io_graph.H:319
void save(std::ofstream &output)
Save graph to binary stream.
Definition io_graph.H:401
Store_Arc store_arc
Definition io_graph.H:325
void set_store_arc(const Store_Arc &sa)
Set the arc storage functor.
Definition io_graph.H:366
IO_Graph(GT &__g) noexcept
Construct from graph reference.
Definition io_graph.H:381
void save_in_text_mode(std::ostream &output)
Save graph to text stream.
Definition io_graph.H:558
void load_in_text_mode(std::istream &input)
Load graph from text stream.
Definition io_graph.H:623
void load(std::ifstream &input)
Load graph from binary stream.
Definition io_graph.H:469
void set_arc_filter(const AF &af)
Set the arc filter for save operations.
Definition io_graph.H:376
bool is_verbose() const noexcept
Check if verbose mode is enabled.
Definition io_graph.H:346
void set_store_node(const Store_Node &sn)
Set the node storage functor.
Definition io_graph.H:356
void set_load_arc(const Load_Arc &la)
Set the arc loading functor.
Definition io_graph.H:361
IO_Graph(GT *gptr) noexcept
Construct from graph pointer.
Definition io_graph.H:386
Load_Node load_node
Definition io_graph.H:322
Store_Node store_node
Definition io_graph.H:323
void set_verbose(bool v) noexcept
Enable or disable verbose mode.
Definition io_graph.H:341
void set_node_filter(const NF &nf)
Set the node filter for save operations.
Definition io_graph.H:371
void set_load_node(const Load_Node &ln)
Set the node loading functor.
Definition io_graph.H:351
Load_Arc load_arc
Definition io_graph.H:324
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:436
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:439
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Default arc loading functor for binary and text modes.
Definition io_graph.H:257
void operator()(std::ifstream &input, GT &g, typename GT::Arc *a)
Load arc from binary stream.
Definition io_graph.H:263
void operator()(std::istream &input, GT &g, typename GT::Arc *a)
Load arc from text stream.
Definition io_graph.H:275
Default node loading functor for binary and text modes.
Definition io_graph.H:222
void operator()(std::istream &input, GT &g, typename GT::Node *p)
Load node from text stream.
Definition io_graph.H:240
void operator()(std::ifstream &input, GT &g, typename GT::Node *p)
Load node from binary stream.
Definition io_graph.H:228
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Default filter for the graph nodes.
Definition tpl_graph.H:1192
Default arc storage functor for binary and text modes.
Definition io_graph.H:187
void operator()(std::ostream &output, GT &g, typename GT::Arc *a)
Store arc to text stream.
Definition io_graph.H:205
void operator()(std::ofstream &output, GT &g, typename GT::Arc *a)
Store arc to binary stream.
Definition io_graph.H:193
Default node storage functor for binary and text modes.
Definition io_graph.H:152
void operator()(std::ofstream &output, GT &g, typename GT::Node *p)
Store node to binary stream.
Definition io_graph.H:158
void operator()(std::ostream &output, GT &g, typename GT::Node *p)
Store node to text stream.
Definition io_graph.H:170
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
ofstream output
Definition writeHeap.C:213