1 //=======================================================================
2 // Copyright 2005 Jeremy G. Siek
3 // Authors: Jeremy G. Siek
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 #ifndef ADJ_LIST_SERIALIZE_HPP
10 #define ADJ_LIST_SERIALIZE_HPP
12 #include <boost/graph/adjacency_list.hpp>
13 #include <boost/graph/iteration_macros.hpp>
14 #include <boost/pending/property_serialize.hpp>
15 #include <boost/config.hpp>
16 #include <boost/detail/workaround.hpp>
18 #include <boost/serialization/collections_save_imp.hpp>
19 #include <boost/serialization/collections_load_imp.hpp>
20 #include <boost/serialization/split_free.hpp>
24 namespace serialization {
26 // Turn off tracking for adjacency_list. It's not polymorphic, and we
27 // need to do this to enable saving of non-const adjacency lists.
28 template<class OEL, class VL, class D, class VP, class EP, class GP, class EL>
29 struct tracking_level<boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> > {
30 typedef mpl::integral_c_tag tag;
31 typedef mpl::int_<track_never> type;
32 BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
35 template<class Archive, class OEL, class VL, class D,
36 class VP, class EP, class GP, class EL>
39 const boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
40 const unsigned int /* file_version */
42 typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
43 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
45 int V = num_vertices(graph);
46 int E = num_edges(graph);
47 ar << BOOST_SERIALIZATION_NVP(V);
48 ar << BOOST_SERIALIZATION_NVP(E);
50 // assign indices to vertices
51 std::map<Vertex,int> indices;
53 BGL_FORALL_VERTICES_T(v, graph, Graph) {
55 ar << serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
59 BGL_FORALL_EDGES_T(e, graph, Graph) {
60 ar << serialization::make_nvp("u" , indices[source(e,graph)]);
61 ar << serialization::make_nvp("v" , indices[target(e,graph)]);
62 ar << serialization::make_nvp("edge_property", get(edge_all_t(), graph, e) );
65 ar << serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
69 template<class Archive, class OEL, class VL, class D,
70 class VP, class EP, class GP, class EL>
73 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
74 const unsigned int /* file_version */
76 typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
77 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
78 typedef typename graph_traits<Graph>::edge_descriptor Edge;
81 ar >> BOOST_SERIALIZATION_NVP(V);
83 ar >> BOOST_SERIALIZATION_NVP(E);
85 std::vector<Vertex> verts(V);
88 Vertex v = add_vertex(graph);
90 ar >> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
94 ar >> BOOST_SERIALIZATION_NVP(u);
95 ar >> BOOST_SERIALIZATION_NVP(v);
96 Edge e; bool inserted;
97 boost::tie(e,inserted) = add_edge(verts[u], verts[v], graph);
98 ar >> serialization::make_nvp("edge_property", get(edge_all_t(), graph, e) );
100 ar >> serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
103 template<class Archive, class OEL, class VL, class D, class VP, class EP, class GP, class EL>
104 inline void serialize(
106 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
107 const unsigned int file_version
109 boost::serialization::split_free(ar, graph, file_version);
116 #endif // ADJ_LIST_SERIALIZE_HPP