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/pending/property_serialize.hpp>
14 #include <boost/config.hpp>
15 #include <boost/detail/workaround.hpp>
17 #include <boost/serialization/collections_save_imp.hpp>
18 #include <boost/serialization/collections_load_imp.hpp>
19 #include <boost/serialization/split_free.hpp>
23 namespace serialization
{
25 // Turn off tracking for adjacency_list. It's not polymorphic, and we
26 // need to do this to enable saving of non-const adjacency lists.
27 template<class OEL
, class VL
, class D
, class VP
, class EP
, class GP
, class EL
>
28 struct tracking_level
<boost::adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> > {
29 typedef mpl::integral_c_tag tag
;
30 typedef mpl::int_
<track_never
> type
;
31 BOOST_STATIC_CONSTANT(int, value
= tracking_level::type::value
);
34 template<class Archive
, class OEL
, class VL
, class D
,
35 class VP
, class EP
, class GP
, class EL
>
38 const boost::adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> &graph
,
39 const unsigned int /* file_version */
41 typedef adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> Graph
;
42 typedef typename graph_traits
<Graph
>::vertex_descriptor Vertex
;
44 int V
= num_vertices(graph
);
45 int E
= num_edges(graph
);
46 ar
<< BOOST_SERIALIZATION_NVP(V
);
47 ar
<< BOOST_SERIALIZATION_NVP(E
);
49 // assign indices to vertices
50 std::map
<Vertex
,int> indices
;
52 typename graph_traits
<Graph
>::vertex_iterator vi
;
53 for (vi
= vertices(graph
).first
; vi
!= vertices(graph
).second
; ++vi
) {
55 ar
<< serialization::make_nvp("vertex_property", get(vertex_all_t(), graph
, *vi
) );
59 typename graph_traits
<Graph
>::edge_iterator ei
;
60 for (ei
= edges(graph
).first
; ei
!= edges(graph
).second
; ++ei
){
61 ar
<< serialization::make_nvp("u" , indices
[source(*ei
,graph
)]);
62 ar
<< serialization::make_nvp("v" , indices
[target(*ei
,graph
)]);
63 ar
<< serialization::make_nvp("edge_property", get(edge_all_t(), graph
, *ei
) );
68 template<class Archive
, class OEL
, class VL
, class D
,
69 class VP
, class EP
, class GP
, class EL
>
72 boost::adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> &graph
,
73 const unsigned int /* file_version */
75 typedef adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> Graph
;
76 typedef typename graph_traits
<Graph
>::vertex_descriptor Vertex
;
77 typedef typename graph_traits
<Graph
>::edge_descriptor Edge
;
80 ar
>> BOOST_SERIALIZATION_NVP(V
);
82 ar
>> BOOST_SERIALIZATION_NVP(E
);
84 std::vector
<Vertex
> verts(V
);
87 Vertex v
= add_vertex(graph
);
89 ar
>> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph
, v
) );
93 ar
>> BOOST_SERIALIZATION_NVP(u
);
94 ar
>> BOOST_SERIALIZATION_NVP(v
);
95 Edge e
; bool inserted
;
96 tie(e
,inserted
) = add_edge(verts
[u
], verts
[v
], graph
);
97 ar
>> serialization::make_nvp("edge_property", get(edge_all_t(), graph
, e
) );
101 template<class Archive
, class OEL
, class VL
, class D
, class VP
, class EP
, class GP
, class EL
>
102 inline void serialize(
104 boost::adjacency_list
<OEL
,VL
,D
,VP
,EP
,GP
,EL
> &graph
,
105 const unsigned int file_version
107 boost::serialization::split_free(ar
, graph
, file_version
);
114 #endif // ADJ_LIST_SERIALIZE_HPP