PageRenderTime 37ms CodeModel.GetById 14ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

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

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