Turi Create  4.0
iterator.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SERIALIZE_ITERATOR_HPP
7 #define TURI_SERIALIZE_ITERATOR_HPP
8 
9 #include <iterator>
10 #include <core/storage/serialization/oarchive.hpp>
11 #include <core/storage/serialization/iarchive.hpp>
12 
13 namespace turi {
14 
15  /**
16  * \ingroup group_serialization
17  \brief Serializes the contents between the iterators begin and end.
18 
19  This function prefers random access iterators since it needs
20  a distance between the begin and end iterator.
21  This function as implemented will work for other input iterators
22  but is extremely inefficient.
23 
24  \tparam OutArcType The output archive type. This should not need to be
25  specified. The compiler will typically infer this
26  correctly.
27  \tparam RandomAccessIterator The iterator type. This should not need to be
28  specified. The compiler will typically infer this
29  correctly.
30 
31  \param oarc A reference to the output archive to write to.
32  \param begin The start of the iterator range to write.
33  \param end The end of the iterator range to write.
34  */
35  template <typename OutArcType, typename RandomAccessIterator>
36  void serialize_iterator(OutArcType& oarc, RandomAccessIterator begin,
37  RandomAccessIterator end){
38  const size_t vsize = std::distance(begin, end);
39  oarc << vsize;
40  // store each element
41  for(; begin != end; ++begin) oarc << *begin;
42  }
43 
44 
45  /**
46  \ingroup group_serialization
47  \brief Serializes the contents between the iterators begin and end.
48 
49  This functions takes all iterator types, but takes a "count" for
50  efficiency. This count is checked and will return failure if the number
51  of elements serialized does not match the count
52 
53  \tparam OutArcType The output archive type. This should not need to be
54  specified. The compiler will typically infer this
55  correctly.
56  \tparam InputIterator The iterator type. This should not need to be
57  specified. The compiler will typically infer this
58  correctly.
59 
60  \param oarc A reference to the output archive to write to.
61  \param begin The start of the iterator range to write.
62  \param end The end of the iterator range to write.
63  \param vsize The distance between the iterators begin and end. Must match
64  std::distance(begin, end);
65  */
66  template <typename OutArcType, typename InputIterator>
67  void serialize_iterator(OutArcType& oarc, InputIterator begin,
68  InputIterator end, size_t vsize){
69  oarc << vsize;
70  //store each element
71  size_t count = 0;
72  for(; begin != end; ++begin) { oarc << *begin; ++count; }
73  // fail if count does not match
74  ASSERT_EQ(count, vsize);
75  }
76 
77  /**
78  \ingroup group_serialization
79  \brief The accompanying function to serialize_iterator()
80  Reads elements from the stream and writes it to the output iterator.
81 
82  Note that this requires an additional template parameter T which is the
83  "type of object to deserialize"
84  This is necessary for instance for the map type. The
85  <code>map<T,U>::value_type</code>
86  is <code>pair<const T,U></code>which is not useful since I cannot assign to
87  it. In this case, <code>T=pair<T,U></code>
88 
89  \tparam OutArcType The output archive type.
90  \tparam T The type of values to deserialize
91  \tparam OutputIterator The type of the output iterator to be written to.
92  This should not need to be specified. The compiler
93  will typically infer this correctly.
94 
95  \param iarc A reference to the input archive
96  \param result The output iterator to write to
97 
98  */
99  template <typename InArcType, typename T, typename OutputIterator>
100  void deserialize_iterator(InArcType& iarc, OutputIterator result) {
101  // get the number of elements to deserialize
102  size_t length = 0;
103  iarc >> length;
104 
105  // iterate through and send to the output iterator
106  for (size_t x = 0; x < length ; ++x){
107  /**
108  * A compiler error on this line means that one of the user
109  * defined types currently trying to be serialized (e.g.,
110  * vertex_data, edge_data, messages, gather_type, or
111  * vertex_programs) does not have a default constructor.
112  */
113  T v;
114  iarc >> v;
115  (*result) = v;
116  result++;
117  }
118  }
119 
120 
121 }
122 #endif
void serialize_iterator(OutArcType &oarc, RandomAccessIterator begin, RandomAccessIterator end)
Serializes the contents between the iterators begin and end.
Definition: iterator.hpp:36
void deserialize_iterator(InArcType &iarc, OutputIterator result)
The accompanying function to serialize_iterator() Reads elements from the stream and writes it to the...
Definition: iterator.hpp:100