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-graph-concepts.H>
138#include <ah-errors.H>
139
140namespace Aleph
141{
142
151template <class GT>
153{
159 void operator()(std::ofstream & output, GT & g, typename GT::Node * p)
160 {
161 (void)g;
162 output.write(reinterpret_cast<const char*>(&p->get_info()),
163 sizeof(typename GT::Node_Type));
164 }
165
171 void operator()(std::ostream & output, GT & g, typename GT::Node * p)
172 {
173 (void)g;
174 output << p->get_info() << std::endl;
175 }
176};
177
186template <class GT>
188{
194 void operator()(std::ofstream & output, GT & g, typename GT::Arc * a)
195 {
196 (void)g;
197 output.write(reinterpret_cast<const char*>(&a->get_info()),
198 sizeof(typename GT::Arc_Type));
199 }
200
206 void operator()(std::ostream & output, GT & g, typename GT::Arc * a)
207 {
208 (void)g;
209 output << a->get_info() << std::endl;
210 }
211};
212
221template <class GT>
223{
229 void operator()(std::ifstream & input, GT & g, typename GT::Node * p)
230 {
231 (void)g;
232 input.read(reinterpret_cast<char*>(&p->get_info()),
233 sizeof(typename GT::Node_Type));
234 }
235
241 void operator()(std::istream & input, GT & g, typename GT::Node * p)
242 {
243 (void)g;
244 input >> p->get_info();
245 }
246};
247
256template <class GT>
258{
264 void operator()(std::ifstream & input, GT & g, typename GT::Arc * a)
265 {
266 (void)g;
267 input.read(reinterpret_cast<char*>(&a->get_info()),
268 sizeof(typename GT::Arc_Type));
269 }
270
276 void operator()(std::istream & input, GT & g, typename GT::Arc * a)
277 {
278 (void)g;
279 input >> a->get_info();
280 }
281};
282
312template <AlephGraph GT,
320{
321 GT & g;
322
327
330
331 bool verbose_mode = false;
332
333public:
334
342 void set_verbose(bool v) noexcept { verbose_mode = v; }
343
348
352 void set_load_node(const Load_Node & ln) { load_node = ln; }
353
357 void set_store_node(const Store_Node & sn) { store_node = sn; }
358
362 void set_load_arc(const Load_Arc & la) { load_arc = la; }
363
367 void set_store_arc(const Store_Arc & sa) { store_arc = sa; }
368
372 void set_node_filter(const NF & nf) { node_filter = nf; }
373
377 void set_arc_filter(const AF & af) { arc_filter = af; }
378
382 explicit IO_Graph(GT & __g) noexcept : g(__g) {}
383
387 explicit IO_Graph(GT * gptr) noexcept : g(*gptr) {}
388
402 void save(std::ofstream & output)
403 {
404 const size_t num_nodes = g.get_num_nodes();
405
406 if (verbose_mode)
407 std::cout << "Storing " << num_nodes << " nodes ... ";
408
409 output.write(reinterpret_cast<const char*>(&num_nodes), sizeof(num_nodes));
410
411 int i = 0;
413
414 for (Node_Iterator<GT, NF> it(g, node_filter); it.has_curr();
415 it.next_ne(), ++i)
416 {
417 auto p = it.get_curr();
418
419 if (verbose_mode)
420 std::cout << i << " ";
421
422 store_node(output, g, p);
423 nodes_table.insert(p, i);
424 }
425
426 const size_t num_arcs = g.get_num_arcs();
427
428 if (verbose_mode)
429 std::cout << " done " << std::endl
430 << "Storing " << num_arcs << " arcs ... " << std::endl;
431
432 output.write(reinterpret_cast<const char*>(&num_arcs), sizeof(num_arcs));
433
434 for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
435 {
436 auto a = it.get_curr();
437
438 auto src = g.get_src_node(a);
439 auto tgt = g.get_tgt_node(a);
440
441 const int src_idx = nodes_table.find(src);
442 const int tgt_idx = nodes_table.find(tgt);
443
444 output.write(reinterpret_cast<const char*>(&src_idx), sizeof(int));
445 output.write(reinterpret_cast<const char*>(&tgt_idx), sizeof(int));
446
447 if (verbose_mode)
448 std::cout << " " << src_idx << "--" << tgt_idx << " ";
449
450 store_arc(output, g, a);
451
452 if (verbose_mode)
453 std::cout << std::endl;
454 }
455
456 if (verbose_mode)
457 std::cout << " done " << std::endl << std::endl;
458 }
459
470 void load(std::ifstream & input)
471 {
472 size_t num_nodes;
473 input.read(reinterpret_cast<char*>(&num_nodes), sizeof(num_nodes));
475 << "Failed to read node count from binary stream";
476
477 if (verbose_mode)
478 std::cout << "Loading " << num_nodes << " nodes ...";
479
481 if (num_nodes > 0)
482 nodes_table.reserve(0, num_nodes - 1);
483
484 for (size_t i = 0; i < num_nodes; ++i)
485 {
486 std::unique_ptr<typename GT::Node> p(new typename GT::Node);
487
488 if (verbose_mode)
489 std::cout << " " << i;
490
491 load_node(input, g, p.get());
493 << "Failed to load node " << i << " from binary stream";
494
495 typename GT::Node * inserted = g.insert_node(p.release());
496 nodes_table.access(i) = inserted;
497 }
498
499 size_t num_arcs;
500 input.read(reinterpret_cast<char*>(&num_arcs), sizeof(num_arcs));
502 << "Failed to read arc count from binary stream";
503
504 if (verbose_mode)
505 std::cout << " done " << std::endl
506 << "Loading " << num_arcs << " arcs ... " << std::endl;
507
508 for (size_t i = 0; i < num_arcs; ++i)
509 {
510 int src_idx;
511 input.read(reinterpret_cast<char*>(&src_idx), sizeof(int));
513 << "Failed to read source index for arc " << i;
514
515 auto src = nodes_table.access(src_idx);
516
517 int tgt_idx;
518 input.read(reinterpret_cast<char*>(&tgt_idx), sizeof(int));
520 << "Failed to read target index for arc " << i;
521
522 auto tgt = nodes_table.access(tgt_idx);
523 auto a = g.insert_arc(src, tgt);
524
525 if (verbose_mode)
526 std::cout << " " << src_idx << "--" << tgt_idx << " ";
527
528 load_arc(input, g, a);
530 << "Failed to load arc " << i << " data";
531
532 if (verbose_mode)
533 std::cout << std::endl;
534 }
535
536 if (verbose_mode)
537 std::cout << " done " << std::endl << std::endl;
538 }
539
559 void save_in_text_mode(std::ostream & output)
560 {
561 const size_t num_nodes = g.get_num_nodes();
562 const size_t num_arcs = g.get_num_arcs();
563
564 output << num_nodes << std::endl
565 << num_arcs << std::endl;
566
567 if (verbose_mode)
568 std::cout << "Storing " << num_nodes << " nodes ... ";
569
570 int i = 0;
572
573 for (Node_Iterator<GT, NF> it(g, node_filter); it.has_curr();
574 it.next_ne(), ++i)
575 {
576 typename GT::Node * p = it.get_curr();
577
578 if (verbose_mode)
579 std::cout << i << " ";
580
581 store_node(output, g, p);
582 nodes_table.insert(p, i);
583 }
584
585 if (verbose_mode)
586 std::cout << " done " << std::endl
587 << "Storing " << num_arcs << " arcs ... " << std::endl;
588
589 for (Arc_Iterator<GT, AF> it(g, arc_filter); it.has_curr(); it.next_ne())
590 {
591 auto a = it.get_curr();
592
593 auto src = g.get_src_node(a);
594 auto tgt = g.get_tgt_node(a);
595
596 const int src_idx = nodes_table.find(src);
597 const int tgt_idx = nodes_table.find(tgt);
598
599 output << src_idx << " " << tgt_idx << " ";
600
601 if (verbose_mode)
602 std::cout << " " << src_idx << "--" << tgt_idx << " ";
603
604 store_arc(output, g, a);
605
606 if (verbose_mode)
607 std::cout << std::endl;
608 }
609
610 if (verbose_mode)
611 std::cout << " done " << std::endl << std::endl;
612 }
613
624 void load_in_text_mode(std::istream & input)
625 {
626 size_t num_nodes;
627 size_t num_arcs;
628
629 input >> num_nodes >> num_arcs;
631 << "Failed to read node/arc count from text stream";
632
633 input.ignore();
634
635 if (verbose_mode)
636 std::cout << "Loading " << num_nodes << " nodes ...";
637
639 if (num_nodes > 0)
640 nodes_table.reserve(0, num_nodes - 1);
641
642 for (size_t i = 0; i < num_nodes; ++i)
643 {
644 std::unique_ptr<typename GT::Node> p(new typename GT::Node);
645
646 if (verbose_mode)
647 std::cout << " " << i;
648
649 load_node(input, g, p.get());
651 << "Failed to load node " << i << " from text stream";
652
653 typename GT::Node * inserted = g.insert_node(p.release());
654 nodes_table.access(i) = inserted;
655 }
656
657 if (verbose_mode)
658 std::cout << " done " << std::endl
659 << "Loading " << num_arcs << " arcs ... " << std::endl;
660
661 for (size_t i = 0; i < num_arcs; ++i)
662 {
663 int src_idx;
664 int tgt_idx;
665
666 input >> src_idx >> tgt_idx;
668 << "Failed to read arc " << i << " indices from text stream";
669
670 auto src = nodes_table.access(src_idx);
671 auto tgt = nodes_table.access(tgt_idx);
672 auto a = g.insert_arc(src, tgt);
673
674 if (verbose_mode)
675 std::cout << " " << src_idx << "--" << tgt_idx << " ";
676
677 load_arc(input, g, a);
679 << "Failed to load arc " << i << " data from text stream";
680
681 if (verbose_mode)
682 std::cout << std::endl;
683 }
684
685 if (verbose_mode)
686 std::cout << " done " << std::endl << std::endl;
687 }
688};
689
690} // namespace Aleph
691
692// Global namespace compatibility
693using Aleph::IO_Graph;
698
699#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:271
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
int num_nodes
Definition btreepic.C:410
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:320
void save(std::ofstream &output)
Save graph to binary stream.
Definition io_graph.H:402
Store_Arc store_arc
Definition io_graph.H:326
void set_store_arc(const Store_Arc &sa)
Set the arc storage functor.
Definition io_graph.H:367
IO_Graph(GT &__g) noexcept
Construct from graph reference.
Definition io_graph.H:382
void save_in_text_mode(std::ostream &output)
Save graph to text stream.
Definition io_graph.H:559
void load_in_text_mode(std::istream &input)
Load graph from text stream.
Definition io_graph.H:624
void load(std::ifstream &input)
Load graph from binary stream.
Definition io_graph.H:470
void set_arc_filter(const AF &af)
Set the arc filter for save operations.
Definition io_graph.H:377
bool is_verbose() const noexcept
Check if verbose mode is enabled.
Definition io_graph.H:347
void set_store_node(const Store_Node &sn)
Set the node storage functor.
Definition io_graph.H:357
void set_load_arc(const Load_Arc &la)
Set the arc loading functor.
Definition io_graph.H:362
IO_Graph(GT *gptr) noexcept
Construct from graph pointer.
Definition io_graph.H:387
Load_Node load_node
Definition io_graph.H:323
Store_Node store_node
Definition io_graph.H:324
void set_verbose(bool v) noexcept
Enable or disable verbose mode.
Definition io_graph.H:342
void set_node_filter(const NF &nf)
Set the node filter for save operations.
Definition io_graph.H:372
void set_load_node(const Load_Node &ln)
Set the node loading functor.
Definition io_graph.H:352
Load_Arc load_arc
Definition io_graph.H:325
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:437
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:440
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1207
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1165
Default arc loading functor for binary and text modes.
Definition io_graph.H:258
void operator()(std::ifstream &input, GT &g, typename GT::Arc *a)
Load arc from binary stream.
Definition io_graph.H:264
void operator()(std::istream &input, GT &g, typename GT::Arc *a)
Load arc from text stream.
Definition io_graph.H:276
Default node loading functor for binary and text modes.
Definition io_graph.H:223
void operator()(std::istream &input, GT &g, typename GT::Node *p)
Load node from text stream.
Definition io_graph.H:241
void operator()(std::ifstream &input, GT &g, typename GT::Node *p)
Load node from binary stream.
Definition io_graph.H:229
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1001
Default filter for the graph nodes.
Definition tpl_graph.H:1193
Default arc storage functor for binary and text modes.
Definition io_graph.H:188
void operator()(std::ostream &output, GT &g, typename GT::Arc *a)
Store arc to text stream.
Definition io_graph.H:206
void operator()(std::ofstream &output, GT &g, typename GT::Arc *a)
Store arc to binary stream.
Definition io_graph.H:194
Default node storage functor for binary and text modes.
Definition io_graph.H:153
void operator()(std::ofstream &output, GT &g, typename GT::Node *p)
Store node to binary stream.
Definition io_graph.H:159
void operator()(std::ostream &output, GT &g, typename GT::Node *p)
Store node to text stream.
Definition io_graph.H:171
Generic graph and digraph implementations.
ofstream output
Definition writeHeap.C:215