/Src/Dependencies/Boost/boost/graph/adj_list_serialize.hpp

http://hadesmem.googlecode.com/ · C++ Header · 113 lines · 84 code · 17 blank · 12 comment · 2 complexity · 6e31feeb1acbf2e96f1dc1c097ee6fdc MD5 · raw file

  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. }
  56. template<class Archive, class OEL, class VL, class D,
  57. class VP, class EP, class GP, class EL>
  58. inline void load(
  59. Archive & ar,
  60. boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
  61. const unsigned int /* file_version */
  62. ){
  63. typedef adjacency_list<OEL,VL,D,VP,EP,GP,EL> Graph;
  64. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  65. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  66. unsigned int V;
  67. ar >> BOOST_SERIALIZATION_NVP(V);
  68. unsigned int E;
  69. ar >> BOOST_SERIALIZATION_NVP(E);
  70. std::vector<Vertex> verts(V);
  71. int i = 0;
  72. while(V-- > 0){
  73. Vertex v = add_vertex(graph);
  74. verts[i++] = v;
  75. ar >> serialization::make_nvp("vertex_property", get(vertex_all_t(), graph, v) );
  76. }
  77. while(E-- > 0){
  78. int u; int v;
  79. ar >> BOOST_SERIALIZATION_NVP(u);
  80. ar >> BOOST_SERIALIZATION_NVP(v);
  81. Edge e; bool inserted;
  82. boost::tie(e,inserted) = add_edge(verts[u], verts[v], graph);
  83. ar >> serialization::make_nvp("edge_property", get(edge_all_t(), graph, e) );
  84. }
  85. }
  86. template<class Archive, class OEL, class VL, class D, class VP, class EP, class GP, class EL>
  87. inline void serialize(
  88. Archive & ar,
  89. boost::adjacency_list<OEL,VL,D,VP,EP,GP,EL> &graph,
  90. const unsigned int file_version
  91. ){
  92. boost::serialization::split_free(ar, graph, file_version);
  93. }
  94. }//serialization
  95. }//boost
  96. #endif // ADJ_LIST_SERIALIZE_HPP