GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vector.hpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 #ifndef GRAPHLAB_SERIALIZE_VECTOR_HPP
25 #define GRAPHLAB_SERIALIZE_VECTOR_HPP
26 #include <vector>
27 #include <graphlab/serialization/iarchive.hpp>
28 #include <graphlab/serialization/oarchive.hpp>
29 #include <graphlab/serialization/iterator.hpp>
30 
31 
32 namespace graphlab {
33  namespace archive_detail {
34  /**
35  * We re-dispatch vectors because based on the contained type,
36  * it is actually possible to serialize them like a POD
37  */
38  template <typename OutArcType, typename ValueType, bool IsPOD>
39  struct vector_serialize_impl {
40  static void exec(OutArcType& oarc, const ValueType& vec) {
41  // really this is an assert false. But the static assert
42  // must depend on a template parameter
43  BOOST_STATIC_ASSERT(sizeof(OutArcType) == 0);
44  assert(false);
45  };
46  };
47  /**
48  * We re-dispatch vectors because based on the contained type,
49  * it is actually possible to deserialize them like iarc POD
50  */
51  template <typename InArcType, typename ValueType, bool IsPOD>
52  struct vector_deserialize_impl {
53  static void exec(InArcType& iarc, ValueType& vec) {
54  // really this is an assert false. But the static assert
55  // must depend on a template parameter
56  BOOST_STATIC_ASSERT(sizeof(InArcType) == 0);
57  assert(false);
58  };
59  };
60 
61  /// If contained type is not a POD use the standard serializer
62  template <typename OutArcType, typename ValueType>
63  struct vector_serialize_impl<OutArcType, ValueType, false > {
64  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
65  oarc << size_t(vec.size());
66  serialize_iterator(oarc,vec.begin(), vec.end());
67  }
68  };
69 
70  /// Fast vector serialization if contained type is a POD
71  template <typename OutArcType, typename ValueType>
72  struct vector_serialize_impl<OutArcType, ValueType, true > {
73  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
74  oarc << size_t(vec.size());
75  serialize(oarc, &(vec[0]),sizeof(ValueType)*vec.size());
76  }
77  };
78 
79  /// If contained type is not a POD use the standard deserializer
80  template <typename InArcType, typename ValueType>
81  struct vector_deserialize_impl<InArcType, ValueType, false > {
82  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
83  size_t len;
84  iarc >> len;
85  vec.clear(); vec.reserve(len);
86  deserialize_iterator<InArcType, ValueType>(iarc, std::inserter(vec, vec.end()));
87  }
88  };
89 
90  /// Fast vector deserialization if contained type is a POD
91  template <typename InArcType, typename ValueType>
92  struct vector_deserialize_impl<InArcType, ValueType, true > {
93  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
94  size_t len;
95  iarc >> len;
96  vec.clear(); vec.resize(len);
97  deserialize(iarc, &(vec[0]), sizeof(ValueType)*vec.size());
98  }
99  };
100 
101 
102 
103  /**
104  Serializes a vector */
105  template <typename OutArcType, typename ValueType>
106  struct serialize_impl<OutArcType, std::vector<ValueType>, false > {
107  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
108  vector_serialize_impl<OutArcType, ValueType,
109  gl_is_pod_or_scaler<ValueType>::value >::exec(oarc, vec);
110  }
111  };
112  /**
113  deserializes a vector */
114  template <typename InArcType, typename ValueType>
115  struct deserialize_impl<InArcType, std::vector<ValueType>, false > {
116  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
117  vector_deserialize_impl<InArcType, ValueType,
118  gl_is_pod_or_scaler<ValueType>::value >::exec(iarc, vec);
119  }
120  };
121  } // archive_detail
122 } // namespace graphlab
123 
124 #endif
125