GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
iterator.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_ITERATOR_HPP
25 #define GRAPHLAB_SERIALIZE_ITERATOR_HPP
26 
27 #include <iterator>
28 #include <graphlab/serialization/oarchive.hpp>
29 #include <graphlab/serialization/iarchive.hpp>
30 
31 namespace graphlab {
32 
33  /**
34  * \ingroup group_serialization
35  \brief Serializes the contents between the iterators begin and end.
36 
37  This function prefers random access iterators since it needs
38  a distance between the begin and end iterator.
39  This function as implemented will work for other input iterators
40  but is extremely inefficient.
41 
42  \tparam OutArcType The output archive type. This should not need to be
43  specified. The compiler will typically infer this
44  correctly.
45  \tparam RandomAccessIterator The iterator type. This should not need to be
46  specified. The compiler will typically infer this
47  correctly.
48 
49  \param oarc A reference to the output archive to write to.
50  \param begin The start of the iterator range to write.
51  \param end The end of the iterator range to write.
52  */
53  template <typename OutArcType, typename RandomAccessIterator>
54  void serialize_iterator(OutArcType& oarc, RandomAccessIterator begin,
55  RandomAccessIterator end){
56  const size_t vsize = std::distance(begin, end);
57  oarc << vsize;
58  // store each element
59  for(; begin != end; ++begin) oarc << *begin;
60  }
61 
62 
63  /**
64  \ingroup group_serialization
65  \brief Serializes the contents between the iterators begin and end.
66 
67  This functions takes all iterator types, but takes a "count" for
68  efficiency. This count is checked and will return failure if the number
69  of elements serialized does not match the count
70 
71  \tparam OutArcType The output archive type. This should not need to be
72  specified. The compiler will typically infer this
73  correctly.
74  \tparam InputIterator The iterator type. This should not need to be
75  specified. The compiler will typically infer this
76  correctly.
77 
78  \param oarc A reference to the output archive to write to.
79  \param begin The start of the iterator range to write.
80  \param end The end of the iterator range to write.
81  \param vsize The distance between the iterators begin and end. Must match
82  std::distance(begin, end);
83  */
84  template <typename OutArcType, typename InputIterator>
85  void serialize_iterator(OutArcType& oarc, InputIterator begin,
86  InputIterator end, size_t vsize){
87  oarc << vsize;
88  //store each element
89  size_t count = 0;
90  for(; begin != end; ++begin) { oarc << *begin; ++count; }
91  // fail if count does not match
92  ASSERT_EQ(count, vsize);
93  }
94 
95  /**
96  \ingroup group_serialization
97  \brief The accompanying function to serialize_iterator()
98  Reads elements from the stream and writes it to the output iterator.
99 
100  Note that this requires an additional template parameter T which is the
101  "type of object to deserialize"
102  This is necessary for instance for the map type. The
103  <code>map<T,U>::value_type</code>
104  is <code>pair<const T,U></code>which is not useful since I cannot assign to
105  it. In this case, <code>T=pair<T,U></code>
106 
107  \tparam OutArcType The output archive type.
108  \tparam T The type of values to deserialize
109  \tparam OutputIterator The type of the output iterator to be written to.
110  This should not need to be specified. The compiler
111  will typically infer this correctly.
112 
113  \param iarc A reference to the input archive
114  \param result The output iterator to write to
115 
116  */
117  template <typename InArcType, typename T, typename OutputIterator>
118  void deserialize_iterator(InArcType& iarc, OutputIterator result) {
119  // get the number of elements to deserialize
120  size_t length = 0;
121  iarc >> length;
122 
123  // iterate through and send to the output iterator
124  for (size_t x = 0; x < length ; ++x){
125  /**
126  * A compiler error on this line means that one of the user
127  * defined types currently trying to be serialized (e.g.,
128  * vertex_data, edge_data, messages, gather_type, or
129  * vertex_programs) does not have a default constructor.
130  */
131  T v;
132  iarc >> v;
133  (*result) = v;
134  result++;
135  }
136  }
137 
138 
139 }
140 #endif
141