Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
io_graph.H File Reference

Graph serialization and deserialization utilities. More...

#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <type_traits>
#include <tpl_graph.H>
#include <ah-graph-concepts.H>
#include <ah-errors.H>
Include dependency graph for io_graph.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::Dft_Store_Node< GT >
 Default node storage functor for binary and text modes. More...
 
struct  Aleph::Dft_Store_Arc< GT >
 Default arc storage functor for binary and text modes. More...
 
struct  Aleph::Dft_Load_Node< GT >
 Default node loading functor for binary and text modes. More...
 
struct  Aleph::Dft_Load_Arc< GT >
 Default arc loading functor for binary and text modes. More...
 
class  Aleph::IO_Graph< GT, Load_Node, Store_Node, Load_Arc, Store_Arc, NF, AF >
 Graph serialization and deserialization class. More...
 
class  Aleph::IO_Graph< GT, Load_Node, Store_Node, Load_Arc, Store_Arc, NF, AF >::Loaded_Nodes
 The nodes that a load has read, by their index in the stream. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Detailed Description

Graph serialization and deserialization utilities.

This file provides the IO_Graph class for reading and writing graphs to/from files in binary and text formats. The serialization format is customizable through functor template parameters.

Features

  • Binary mode: Fast, compact representation for production use
  • Text mode: Human-readable format for debugging and interchange
  • Customizable serializers: Define custom functors for node/arc data
  • Filtering: Save only nodes/arcs that pass filter predicates
  • Verbose mode: Optional progress output during I/O operations

File Format

Binary Format

[num_nodes: size_t]
[node_0 data] [node_1 data] ... [node_n-1 data]
[num_arcs: size_t]
[src_idx: int] [tgt_idx: int] [arc_0 data]
[src_idx: int] [tgt_idx: int] [arc_1 data]
...
int num_nodes
Definition btreepic.C:410

Text Format

num_arcs
node_0_data
node_1_data
...
src_idx tgt_idx arc_0_data
src_idx tgt_idx arc_1_data
...

Usage Example

#include <io_graph.H>
#include <tpl_graph.H>
using Graph = List_Graph<Graph_Node<int>, Graph_Arc<double>>;
// Save graph to binary file
// ... populate graph ...
IO_Graph<Graph> io(g);
std::ofstream out("graph.bin", std::ios::binary);
io.save(out);
// Load graph from binary file
Graph g2;
IO_Graph<Graph> io2(g2);
std::ifstream in("graph.bin", std::ios::binary);
io2.load(in);
// Text mode
std::ofstream tout("graph.txt");
io.save_in_text_mode(tout);
std::ifstream tin("graph.txt");
io2.load_in_text_mode(tin);
size_t size_t int32_t * out
Definition ca-c-api.h:120
Graph serialization and deserialization utilities.
Generic graph and digraph implementations.

Custom Serializers

The default functors of the binary mode copy the bytes of the infos, so that they take only infos that are trivially copyable: for other ones, as std::string, define custom load/store functors:

struct MyStoreNode {
void operator()(std::ofstream& out, Graph&, Graph::Node* p) {
// Custom binary serialization
const auto& info = p->get_info();
size_t len = info.name.size();
out.write(reinterpret_cast<const char*>(&len), sizeof(len));
out.write(info.name.data(), len);
}
void operator()(std::ostream& out, Graph&, Graph::Node* p) {
// Custom text serialization
out << p->get_info().name << std::endl;
}
};
IO_Graph<Graph, MyLoadNode, MyStoreNode, MyLoadArc, MyStoreArc> io(g);
Node Node
The graph type.
Definition tpl_graph.H:443

Portability of the binary format

The counts are written as size_t and the indices as int, and the default functors write the bytes of the infos, all in the order of the bytes of the machine: a binary file is read back on a machine with the same sizes and the same endianness. The text mode does not depend on them.

See also
tpl_graph.H Graph class definitions
load_digraph.H Alternative digraph loading utilities
Author
Leandro Rabindranath León

Definition in file io_graph.H.