adj_list_serialize.hpp 3.8 KB

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