1//=======================================================================
2// Copyright 2005 Jeremy G. Siek
3// Authors: Jeremy G. Siek
4//
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
11
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>
17
18#include <boost/serialization/collections_save_imp.hpp>
19#include <boost/serialization/collections_load_imp.hpp>
20#include <boost/serialization/split_free.hpp>
21
22namespace boost {
23
24namespace serialization {
25
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.
28template<class OEL, class VL, class D, class VP, class EP, class GP, class EL>
29struct 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);
33};
34
35template<class Archive, class OEL, class VL, class D,
36 class VP, class EP, class GP, class EL>
37inline void save(
38 Archive & ar,
39 const boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
40 const unsigned int /* file_version */
41){
42 typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
43 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
44
45 int V = num_vertices(graph);
46 int E = num_edges(graph);
47 ar << BOOST_SERIALIZATION_NVP(V);
48 ar << BOOST_SERIALIZATION_NVP(E);
49
50 // assign indices to vertices
51 std::map<Vertex,int> indices;
52 int num = 0;
53 BGL_FORALL_VERTICES_T(v, graph, Graph) {
54 indices[v] = num++;
55 ar << serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
56 }
57
58 // write edges
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) );
63 }
64
65 ar << serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
66}
67
68
69template<class Archive, class OEL, class VL, class D,
70 class VP, class EP, class GP, class EL>
71inline void load(
72 Archive & ar,
73 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
74 const unsigned int /* file_version */
75){
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;
79
80 unsigned int V;
81 ar >> BOOST_SERIALIZATION_NVP(V);
82 unsigned int E;
83 ar >> BOOST_SERIALIZATION_NVP(E);
84
85 std::vector<Vertex> verts(V);
86 int i = 0;
87 while(V-- > 0){
88 Vertex v = add_vertex(graph);
89 verts[i++] = v;
90 ar >> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
91 }
92 while(E-- > 0){
93 int u; int 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) );
99 }
100 ar >> serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
101}
102
103template<class Archive, class OEL, class VL, class D, class VP, class EP, class GP, class EL>
104inline void serialize(
105 Archive & ar,
106 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
107 const unsigned int file_version
108){
109 boost::serialization::split_free(ar, graph, file_version);
110}
111
112}//serialization
113}//boost
114
115
116#endif // ADJ_LIST_SERIALIZE_HPP
117

source code of boost/boost/graph/adj_list_serialize.hpp