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