PaGMO  1.1.5
adj_list_serialize_bug_fix.hpp
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 // bug fix of anomalous behavior by Dario Izzo
10 //
11 //=======================================================================
12 #ifndef ADJ_LIST_SERIALIZE_HPP
13 #define ADJ_LIST_SERIALIZE_HPP
14 
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/graph/iteration_macros.hpp>
17 #include <boost/pending/property_serialize.hpp>
18 #include <boost/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 
21 #include <boost/serialization/collections_save_imp.hpp>
22 #include <boost/serialization/collections_load_imp.hpp>
23 #include <boost/serialization/split_free.hpp>
24 
25 namespace boost {
26 
27 namespace serialization {
28 
29 // Turn off tracking for adjacency_list. It's not polymorphic, and we
30 // need to do this to enable saving of non-const adjacency lists.
31 template<class OEL, class VL, class D, class VP, class EP, class GP, class EL>
32 struct tracking_level<boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> > {
33  typedef mpl::integral_c_tag tag;
34  typedef mpl::int_<track_never> type;
35  BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
36 };
37 
38 template<class Archive, class OEL, class VL, class D,
39  class VP, class EP, class GP, class EL>
40 inline void save(
41  Archive & ar,
42  const boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
43  const unsigned int /* file_version */
44 ){
45  typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
46  typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
47 
48  int V = num_vertices(graph);
49  int E = num_edges(graph);
50  ar << BOOST_SERIALIZATION_NVP(V);
51  ar << BOOST_SERIALIZATION_NVP(E);
52 
53  // assign indices to vertices
54  std::map<Vertex,int> indices;
55  int num = 0;
56  BGL_FORALL_VERTICES_T(v, graph, Graph) {
57  indices[v] = num++;
58  ar << serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
59  }
60 
61  // write edges
62  BGL_FORALL_EDGES_T(e, graph, Graph) {
63  ar << serialization::make_nvp("u" , indices[source(e,graph)]);
64  ar << serialization::make_nvp("v" , indices[target(e,graph)]);
65  ar << serialization::make_nvp("edge_property", get(edge_all_t(), graph, e) );
66  }
67 
68  ar << serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
69 }
70 
71 
72 template<class Archive, class OEL, class VL, class D,
73  class VP, class EP, class GP, class EL>
74 inline void load(
75  Archive & ar,
76  boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
77  const unsigned int /* file_version */
78 ){
79  typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
80  typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
81  typedef typename graph_traits<Graph>::edge_descriptor Edge;
82 
83  unsigned int V;
84  ar >> BOOST_SERIALIZATION_NVP(V);
85  unsigned int E;
86  ar >> BOOST_SERIALIZATION_NVP(E);
87 
88  // Clears the graph before adding vertices into it (BUGFIX of the original file)
89  Graph graph2;
90  graph = graph2;
91 
92  std::vector<Vertex> verts(V);
93  int i = 0;
94  while(V-- > 0){
95  Vertex v = add_vertex(graph);
96  verts[i++] = v;
97  ar >> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
98  }
99  while(E-- > 0){
100  int u; int v;
101  ar >> BOOST_SERIALIZATION_NVP(u);
102  ar >> BOOST_SERIALIZATION_NVP(v);
103  Edge e; bool inserted;
104  boost::tie(e,inserted) = add_edge(verts[u], verts[v], graph);
105  ar >> serialization::make_nvp("edge_property", get(edge_all_t(), graph, e) );
106  }
107  ar >> serialization::make_nvp("graph_property", get_property(graph, graph_all_t()) );
108 }
109 
110 template<class Archive, class OEL, class VL, class D, class VP, class EP, class GP, class EL>
111 inline void serialize(
112  Archive & ar,
113  boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
114  const unsigned int file_version
115 ){
116  boost::serialization::split_free(ar, graph, file_version);
117 }
118 
119 }//serialization
120 }//boost
121 
122 
123 #endif // ADJ_LIST_SERIALIZE_HPP