adj_list_serialize.hpp 4.4 KB

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