Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions include/boost/graph/adj_list_serialize.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//=======================================================================
// Copyright 2005 Jeremy G. Siek
// Copyright 2026 Arnaud Becheler
// Authors: Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
Expand Down Expand Up @@ -50,14 +51,15 @@ namespace serialization
using Graph = adjacency_list< OEL, VL, D, VP, EP, GP, EL >;
using Vertex = typename graph_traits< Graph >::vertex_descriptor;

const auto V = num_vertices(graph);
const auto E = num_edges(graph);
// Fixed 4 byte integers keep binary archives readable across releases.
const auto V = static_cast< unsigned int >(num_vertices(graph));
const auto E = static_cast< unsigned int >(num_edges(graph));
ar << BOOST_NVP(V);
ar << BOOST_NVP(E);

// assign indices to vertices
std::unordered_map< Vertex, Vertex > indices(V);
Vertex num = 0;
std::unordered_map< Vertex, unsigned int > indices(V);
unsigned int num = 0;
BGL_FORALL_VERTICES_T(v, graph, Graph)
{
indices[v] = num++;
Expand Down Expand Up @@ -88,16 +90,15 @@ namespace serialization
using Graph = adjacency_list< OEL, VL, D, VP, EP, GP, EL >;
using Vertex = typename graph_traits< Graph >::vertex_descriptor;
using Edge = typename graph_traits< Graph >::edge_descriptor;
using VertexSizeType = typename graph_traits< Graph >::vertices_size_type;

VertexSizeType V;
unsigned int V;
ar >> BOOST_NVP(V);

VertexSizeType E;
unsigned int E;
ar >> BOOST_NVP(E);

std::vector< Vertex > verts(V);
size_t i = 0;
unsigned int i = 0;
while (V-- > 0)
{
const auto v = add_vertex(graph);
Expand All @@ -108,8 +109,8 @@ namespace serialization

while (E-- > 0)
{
Vertex u;
Vertex v;
unsigned int u;
unsigned int v;
ar >> BOOST_NVP(u);
ar >> BOOST_NVP(v);

Expand Down
4 changes: 3 additions & 1 deletion test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ path-constant METIS_INPUT_FILE : ./weighted_graph.gr ;

path-constant WEIGHTED_MATCHING_INPUT_FILE : ./weighted_matching.dat ;

path-constant SERIALIZE_ARCHIVES : ./serialize_archives ;

project : requirements <library>/boost/graph//boost_graph ;

alias graph_test_regular :
Expand Down Expand Up @@ -87,7 +89,7 @@ alias graph_test_regular :

[ run serialize.cpp
/boost/serialization//boost_serialization
: : : ]
: $(SERIALIZE_ARCHIVES) : : ]

[ compile reverse_graph_cc.cpp ]
[ run sequential_vertex_coloring.cpp ]
Expand Down
185 changes: 183 additions & 2 deletions test/serialize.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2006 Trustees of Indiana University
// Copyright (c) 2026 Arnaud Becheler
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -17,6 +18,20 @@
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/range/iterator_range.hpp>
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

struct vertex_properties
{
Expand Down Expand Up @@ -55,8 +70,174 @@ static_assert(boost::serialization::tracking_level< Graph >::value
typedef adjacency_list< vecS, vecS, undirectedS, vertex_properties >
Graph_no_edge_property;

int main()
using ListGraph = boost::adjacency_list< boost::listS, boost::listS, boost::bidirectionalS, vertex_properties, edge_properties >;

using PlainGraph = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS >;

using PropertyGraph = boost::adjacency_list< boost::vecS, boost::listS, boost::directedS, boost::property< boost::vertex_name_t, std::string >, boost::property< boost::edge_name_t, std::string, boost::property< boost::edge_weight_t, int > > >;

using EdgeRecord = std::tuple< std::size_t, std::size_t, std::string >;

void check_list_round_trip()
{
ListGraph g;
const auto a = boost::add_vertex(g);
const auto b = boost::add_vertex(g);
g[a].name = "A";
g[b].name = "B";
const auto ab = boost::add_edge(a, b, g).first;
g[ab].name = "ab";
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
{
boost::archive::binary_oarchive oa(ss);
oa << g;
}
ListGraph loaded;
boost::archive::binary_iarchive ia(ss);
ia >> loaded;
constexpr std::size_t expected_num_vertices = 2;
constexpr std::size_t expected_num_edges = 1;
BOOST_TEST_EQ(boost::num_vertices(loaded), expected_num_vertices);
BOOST_TEST_EQ(boost::num_edges(loaded), expected_num_edges);
const auto first_vertex = *boost::vertices(loaded).first;
const auto first_edge = *boost::edges(loaded).first;
BOOST_TEST_EQ(loaded[first_vertex].name, "A");
BOOST_TEST_EQ(loaded[first_edge].name, "ab");
}

std::size_t binary_archive_size(std::size_t num_edges)
{
PlainGraph g(2);
for (std::size_t i = 0; i < num_edges; ++i)
{
boost::add_edge(0, 1, g);
}
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
{
boost::archive::binary_oarchive oa(ss);
oa << g;
}
return ss.str().size();
}

// Each edge costs two 4 byte indices, as in archives from Boost 1.91 and earlier.
void check_binary_layout()
{
constexpr std::size_t expected_edge_size = 2 * sizeof(unsigned int);
const auto one_edge_size = binary_archive_size(1);
const auto two_edges_size = binary_archive_size(2);
BOOST_TEST_EQ(two_edges_size - one_edge_size, expected_edge_size);
}

// Gold archives hold A, B, C with edges ab1 and ab2 from A to B, bc from B to C and cc from C to C.
template < class G, class VertexTag, class EdgeTag >
void check_gold_graph(const G& g, VertexTag vertex_tag, EdgeTag edge_tag)
{
using Vertex = typename boost::graph_traits< G >::vertex_descriptor;
const std::vector< std::string > expected_vertex_names = { "A", "B", "C" };
const std::vector< EdgeRecord > expected_edges = { EdgeRecord(0, 1, "ab1"), EdgeRecord(0, 1, "ab2"), EdgeRecord(1, 2, "bc"), EdgeRecord(2, 2, "cc") };
const auto vertex_name_map = boost::get(vertex_tag, g);
const auto edge_name_map = boost::get(edge_tag, g);
std::unordered_map< Vertex, std::size_t > index_map;
std::vector< std::string > vertex_names;
for (const auto v : boost::make_iterator_range(boost::vertices(g)))
{
index_map[v] = vertex_names.size();
vertex_names.push_back(boost::get(vertex_name_map, v));
}
std::vector< EdgeRecord > edges;
for (const auto e : boost::make_iterator_range(boost::edges(g)))
{
const auto u = index_map[boost::source(e, g)];
const auto v = index_map[boost::target(e, g)];
edges.emplace_back(u, v, boost::get(edge_name_map, e));
}
std::sort(edges.begin(), edges.end());
BOOST_TEST(vertex_names == expected_vertex_names);
BOOST_TEST(edges == expected_edges);
}

void check_gold_weights(const PropertyGraph& g)
{
const std::map< std::string, int > expected_weights = { { "ab1", 1 }, { "ab2", 2 }, { "bc", 3 }, { "cc", 4 } };
const auto edge_name_map = boost::get(boost::edge_name, g);
const auto weight_map = boost::get(boost::edge_weight, g);
for (const auto e : boost::make_iterator_range(boost::edges(g)))
{
const auto& name = boost::get(edge_name_map, e);
BOOST_TEST_EQ(boost::get(weight_map, e), expected_weights.at(name));
}
}

void check_gold_plain(const PlainGraph& g)
{
using PlainEdge = std::pair< std::size_t, std::size_t >;
constexpr std::size_t expected_num_vertices = 3;
const std::vector< PlainEdge > expected_edges = { PlainEdge(0, 1), PlainEdge(0, 1), PlainEdge(1, 2), PlainEdge(2, 2) };
std::vector< PlainEdge > edges;
for (const auto e : boost::make_iterator_range(boost::edges(g)))
{
edges.emplace_back(boost::source(e, g), boost::target(e, g));
}
std::sort(edges.begin(), edges.end());
BOOST_TEST_EQ(boost::num_vertices(g), expected_num_vertices);
BOOST_TEST(edges == expected_edges);
}

template < class IArchive >
void check_gold_archive(const std::string& path, std::ios::openmode mode)
{
std::ifstream ifs(path, mode);
BOOST_TEST(ifs.good());
if (!ifs)
{
return;
}
IArchive ia(ifs);
Graph vec_vec_undirected;
ListGraph list_list_bidirectional;
PropertyGraph vec_list_directed_property;
PlainGraph vec_vec_directed_plain;
ia >> boost::make_nvp("vec_vec_undirected", vec_vec_undirected);
ia >> boost::make_nvp("list_list_bidirectional", list_list_bidirectional);
ia >> boost::make_nvp("vec_list_directed_property", vec_list_directed_property);
ia >> boost::make_nvp("vec_vec_directed_plain", vec_vec_directed_plain);
check_gold_graph(vec_vec_undirected, &vertex_properties::name, &edge_properties::name);
check_gold_graph(list_list_bidirectional, &vertex_properties::name, &edge_properties::name);
check_gold_graph(vec_list_directed_property, boost::vertex_name, boost::edge_name);
check_gold_weights(vec_list_directed_property);
check_gold_plain(vec_vec_directed_plain);
}

// Native binary archives only load where sizes and byte order match the 64 bit little endian writer.
bool is_gold_binary_platform()
{
constexpr unsigned int one = 1;
unsigned char first_byte = 0;
std::memcpy(&first_byte, &one, 1);
return sizeof(int) == 4 && sizeof(long) == 8 && sizeof(std::size_t) == 8 && first_byte == 1;
}

void check_gold_archives(const std::string& directory)
{
check_gold_archive< boost::archive::text_iarchive >(directory + "/boost_1_90.txt", std::ios::in);
check_gold_archive< boost::archive::xml_iarchive >(directory + "/boost_1_90.xml", std::ios::in);
if (is_gold_binary_platform())
{
check_gold_archive< boost::archive::binary_iarchive >(directory + "/boost_1_90.bin", std::ios::in | std::ios::binary);
}
}

int main(int argc, char* argv[])
{
check_list_round_trip();
check_binary_layout();
BOOST_TEST_GE(argc, 2);
if (argc >= 2)
{
check_gold_archives(argv[1]);
}

{
std::ofstream ofs("./kevin-bacon2.dat");
archive::xml_oarchive oa(ofs);
Expand Down Expand Up @@ -89,5 +270,5 @@ int main()
Graph_no_edge_property g_n;
ia >> BOOST_SERIALIZATION_NVP(g_n);
}
return 0;
return boost::report_errors();
}
Binary file added test/serialize_archives/boost_1_90.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/serialize_archives/boost_1_90.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22 serialization::archive 20 0 0 3 4 0 0 1 A 1 B 1 C 0 1 0 0 3 ab1 0 1 3 ab2 1 2 2 bc 2 2 2 cc 0 0 0 0 3 4 1 A 1 B 1 C 0 1 3 ab1 0 1 3 ab2 1 2 2 bc 2 2 2 cc 0 0 3 4 0 0 1 A 1 B 1 C 0 1 0 0 3 ab1 0 0 1 0 1 3 ab2 2 1 2 2 bc 3 2 2 2 cc 4 0 0 3 4 0 1 0 1 1 2 2 2
Loading
Loading