|
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/pending/property_serialize.hpp> |
|
14 #include <boost/config.hpp> |
|
15 #include <boost/detail/workaround.hpp> |
|
16 |
|
17 #include <boost/serialization/collections_save_imp.hpp> |
|
18 #include <boost/serialization/collections_load_imp.hpp> |
|
19 #include <boost/serialization/split_free.hpp> |
|
20 |
|
21 namespace boost { |
|
22 |
|
23 namespace serialization { |
|
24 |
|
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); |
|
32 }; |
|
33 |
|
34 template<class Archive, class OEL, class VL, class D, |
|
35 class VP, class EP, class GP, class EL> |
|
36 inline void save( |
|
37 Archive & ar, |
|
38 const boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph, |
|
39 const unsigned int /* file_version */ |
|
40 ){ |
|
41 typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph; |
|
42 typedef typename graph_traits<Graph>::vertex_descriptor Vertex; |
|
43 |
|
44 int V = num_vertices(graph); |
|
45 int E = num_edges(graph); |
|
46 ar << BOOST_SERIALIZATION_NVP(V); |
|
47 ar << BOOST_SERIALIZATION_NVP(E); |
|
48 |
|
49 // assign indices to vertices |
|
50 std::map<Vertex,int> indices; |
|
51 int num = 0; |
|
52 typename graph_traits<Graph>::vertex_iterator vi; |
|
53 for (vi = vertices(graph).first; vi != vertices(graph).second; ++vi) { |
|
54 indices[*vi] = num++; |
|
55 ar << serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, *vi) ); |
|
56 } |
|
57 |
|
58 // write edges |
|
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) ); |
|
64 } |
|
65 } |
|
66 |
|
67 |
|
68 template<class Archive, class OEL, class VL, class D, |
|
69 class VP, class EP, class GP, class EL> |
|
70 inline void load( |
|
71 Archive & ar, |
|
72 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph, |
|
73 const unsigned int /* file_version */ |
|
74 ){ |
|
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; |
|
78 |
|
79 unsigned int V; |
|
80 ar >> BOOST_SERIALIZATION_NVP(V); |
|
81 unsigned int E; |
|
82 ar >> BOOST_SERIALIZATION_NVP(E); |
|
83 |
|
84 std::vector<Vertex> verts(V); |
|
85 int i = 0; |
|
86 while(V-- > 0){ |
|
87 Vertex v = add_vertex(graph); |
|
88 verts[i++] = v; |
|
89 ar >> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) ); |
|
90 } |
|
91 while(E-- > 0){ |
|
92 int u; int 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) ); |
|
98 } |
|
99 } |
|
100 |
|
101 template<class Archive, class OEL, class VL, class D, class VP, class EP, class GP, class EL> |
|
102 inline void serialize( |
|
103 Archive & ar, |
|
104 boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph, |
|
105 const unsigned int file_version |
|
106 ){ |
|
107 boost::serialization::split_free(ar, graph, file_version); |
|
108 } |
|
109 |
|
110 }//serialization |
|
111 }//boost |
|
112 |
|
113 |
|
114 #endif // ADJ_LIST_SERIALIZE_HPP |