147#include <type_traits>
174 static_assert(std::is_trivially_copyable_v<typename GT::Node_Type>,
175 "Dft_Store_Node writes the bytes of the info: for an info that is "
176 "not trivially copyable, as std::string, give IO_Graph a functor of your own");
212 static_assert(std::is_trivially_copyable_v<typename GT::Arc_Type>,
213 "Dft_Store_Arc writes the bytes of the info: for an info that is "
214 "not trivially copyable, as std::string, give IO_Graph a functor of your own");
250 static_assert(std::is_trivially_copyable_v<typename GT::Node_Type>,
251 "Dft_Load_Node reads the bytes of the info: for an info that is "
252 "not trivially copyable, as std::string, give IO_Graph a functor of your own");
253 input.read(
reinterpret_cast<char*
>(&p->
get_info()),
288 static_assert(std::is_trivially_copyable_v<typename GT::Arc_Type>,
289 "Dft_Load_Arc reads the bytes of the info: for an info that is "
290 "not trivially copyable, as std::string, give IO_Graph a functor of your own");
291 input.read(
reinterpret_cast<char*
>(&a->
get_info()),
336template <AlephGraph
GT,
417 explicit IO_Graph(
GT & __g)
noexcept requires std::is_default_constructible_v<AF>
426 explicit IO_Graph(
GT * gptr)
noexcept requires std::is_default_constructible_v<AF>
449 nodes_table.
insert(it.get_curr(), i);
459 auto a = it.get_curr();
491 std::cout <<
"Storing " <<
num_nodes <<
" nodes ... ";
500 std::cout << i <<
" ";
506 std::cout <<
" done " << std::endl
507 <<
"Storing " << num_arcs <<
" arcs ... " << std::endl;
509 output.write(
reinterpret_cast<const char*
>(&num_arcs),
sizeof(num_arcs));
513 auto a = it.get_curr();
517 if (nodes_table.
search(src) ==
nullptr or nodes_table.
search(tgt) ==
nullptr)
520 const int src_idx = nodes_table.
find(src);
521 const int tgt_idx = nodes_table.
find(tgt);
523 output.write(
reinterpret_cast<const char*
>(&src_idx),
sizeof(
int));
524 output.write(
reinterpret_cast<const char*
>(&tgt_idx),
sizeof(
int));
527 std::cout <<
" " << src_idx <<
"--" << tgt_idx <<
" ";
532 std::cout << std::endl;
536 std::cout <<
" done " << std::endl << std::endl;
552 void load(std::ifstream & input)
557 <<
"Failed to read node count from binary stream";
560 std::cout <<
"Loading " <<
num_nodes <<
" nodes ...";
567 std::unique_ptr<typename GT::Node> p(
new typename GT::Node);
570 std::cout <<
" " << i;
574 <<
"Failed to load node " << i <<
" from binary stream";
576 nodes_table.
add(
g, std::move(p));
580 input.read(
reinterpret_cast<char*
>(&num_arcs),
sizeof(num_arcs));
582 <<
"Failed to read arc count from binary stream";
585 std::cout <<
" done " << std::endl
586 <<
"Loading " << num_arcs <<
" arcs ... " << std::endl;
588 for (
size_t i = 0; i < num_arcs; ++i)
591 input.read(
reinterpret_cast<char*
>(&src_idx),
sizeof(
int));
593 <<
"Failed to read source index for arc " << i;
596 input.read(
reinterpret_cast<char*
>(&tgt_idx),
sizeof(
int));
598 <<
"Failed to read target index for arc " << i;
600 auto src = nodes_table.
node(src_idx, i,
"source");
601 auto tgt = nodes_table.
node(tgt_idx, i,
"target");
605 std::cout <<
" " << src_idx <<
"--" << tgt_idx <<
" ";
609 <<
"Failed to load arc " << i <<
" data";
612 std::cout << std::endl;
622 std::cout <<
" done " << std::endl << std::endl;
653 << num_arcs << std::endl;
656 std::cout <<
"Storing " <<
num_nodes <<
" nodes ... ";
663 std::cout << i <<
" ";
669 std::cout <<
" done " << std::endl
670 <<
"Storing " << num_arcs <<
" arcs ... " << std::endl;
674 auto a = it.get_curr();
678 if (nodes_table.
search(src) ==
nullptr or nodes_table.
search(tgt) ==
nullptr)
681 const int src_idx = nodes_table.
find(src);
682 const int tgt_idx = nodes_table.
find(tgt);
684 output << src_idx <<
" " << tgt_idx <<
" ";
687 std::cout <<
" " << src_idx <<
"--" << tgt_idx <<
" ";
692 std::cout << std::endl;
696 std::cout <<
" done " << std::endl << std::endl;
719 <<
"Failed to read node/arc count from text stream";
724 std::cout <<
"Loading " <<
num_nodes <<
" nodes ...";
731 std::unique_ptr<typename GT::Node> p(
new typename GT::Node);
734 std::cout <<
" " << i;
738 <<
"Failed to load node " << i <<
" from text stream";
740 nodes_table.
add(
g, std::move(p));
744 std::cout <<
" done " << std::endl
745 <<
"Loading " << num_arcs <<
" arcs ... " << std::endl;
747 for (
size_t i = 0; i < num_arcs; ++i)
752 input >> src_idx >> tgt_idx;
754 <<
"Failed to read arc " << i <<
" indices from text stream";
756 auto src = nodes_table.
node(src_idx, i,
"source");
757 auto tgt = nodes_table.
node(tgt_idx, i,
"target");
761 std::cout <<
" " << src_idx <<
"--" << tgt_idx <<
" ";
765 <<
"Failed to load arc " << i <<
" data from text stream";
768 std::cout << std::endl;
778 std::cout <<
" done " << std::endl << std::endl;
809 void add(
GT &
g, std::unique_ptr<typename GT::Node> p)
820 typename GT::Node *
node(
const int idx,
const size_t i,
const char * end)
const
823 if (
static_cast<size_t>(idx) >=
num_loaded) [[unlikely]]
831 [[noreturn, gnu::noinline, gnu::cold]]
835 << i <<
": the stream has " <<
num_loaded <<
" nodes";
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
T & touch(const size_t i)
Touch the entry i.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic map implemented with a treap.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
The nodes that a load has read, by their index in the stream.
static constexpr size_t Max_Reserved
void add(GT &g, std::unique_ptr< typename GT::Node > p)
Insert the node p, loaded, into g, as the node of the next index.
void throw_invalid_index(const int idx, const size_t i, const char *end) const
DynArray< typename GT::Node * > table
void remove_from(GT &g)
Remove from g the nodes read, with their arcs: a load failed.
Loaded_Nodes(const size_t num_nodes)
Reserve for num_nodes nodes, the count of the stream, or Max_Reserved if it is greater.
GT::Node * node(const int idx, const size_t i, const char *end) const
The node of index idx of the stream, an end of the arc i.
Graph serialization and deserialization class.
void save(std::ofstream &output)
Save graph to binary stream.
size_t number_shown_nodes(DynMapTreap< typename GT::Node *, int > &nodes_table)
Gives each node that the node filter shows its number in the file, in the order of the saves,...
void set_store_arc(const Store_Arc &sa)
Set the arc storage functor.
void save_in_text_mode(std::ostream &output)
Save graph to text stream.
void load_in_text_mode(std::istream &input)
Load graph from text stream.
void load(std::ifstream &input)
Load graph from binary stream.
bool is_verbose() const noexcept
Check if verbose mode is enabled.
size_t count_shown_arcs(const DynMapTreap< typename GT::Node *, int > &nodes_table)
The arcs that the arc filter shows whose two ends are numbered.
void set_store_node(const Store_Node &sn)
Set the node storage functor.
void set_load_arc(const Load_Arc &la)
Set the arc loading functor.
void set_verbose(bool v) noexcept
Enable or disable verbose mode.
void set_arc_filter(const AF &af)
Set the arc filter for save operations.
void set_node_filter(const NF &nf)
Set the node filter for save operations.
void set_load_node(const Load_Node &ln)
Set the node loading functor.
IO_Graph(GT &__g, AF af)
Construct from graph reference, with the arc filter.
IO_Graph(GT &__g) noexcept
Construct from graph reference.
IO_Graph(GT *gptr, AF af)
Construct from graph pointer, with the arc filter.
IO_Graph(GT *gptr) noexcept
Construct from graph pointer.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
typename Node::Node_Type Node_Type
The arc class type.
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Filtered iterator on the nodes of a graph.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Filtered iterator on all the arcs of a graph.
Default arc loading functor for binary and text modes.
void operator()(std::ifstream &input, GT &g, typename GT::Arc *a)
Load arc from binary stream.
void operator()(std::istream &input, GT &g, typename GT::Arc *a)
Load arc from text stream.
Default node loading functor for binary and text modes.
void operator()(std::istream &input, GT &g, typename GT::Node *p)
Load node from text stream.
void operator()(std::ifstream &input, GT &g, typename GT::Node *p)
Load node from binary stream.
Default filter for filtered iterators on arcs.
Default filter for the graph nodes.
Default arc storage functor for binary and text modes.
void operator()(std::ostream &output, GT &g, typename GT::Arc *a)
Store arc to text stream.
void operator()(std::ofstream &output, GT &g, typename GT::Arc *a)
Store arc to binary stream.
Default node storage functor for binary and text modes.
void operator()(std::ofstream &output, GT &g, typename GT::Node *p)
Store node to binary stream.
void operator()(std::ostream &output, GT &g, typename GT::Node *p)
Store node to text stream.
Generic graph and digraph implementations.