Turi Create  4.0
unordered_set.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_UNORDERED_SET_HPP
7 #define TURI_SERIALIZE_UNORDERED_SET_HPP
8 
9 #include <boost/unordered_set.hpp>
10 #include <core/storage/serialization/iarchive.hpp>
11 #include <core/storage/serialization/oarchive.hpp>
12 #include <core/storage/serialization/iterator.hpp>
13 
14 namespace turi {
15 namespace archive_detail {
16  /** serializes a set */
17  template <typename OutArcType, typename T>
18  struct serialize_impl<OutArcType, boost::unordered_set<T>, false > {
19  static void exec(OutArcType& oarc, const boost::unordered_set<T>& vec){
20  serialize_iterator(oarc,
21  vec.begin(), vec.end(), vec.size());
22  }
23  };
24 
25  /** deserializes a set */
26  template <typename InArcType, typename T>
27  struct deserialize_impl<InArcType, boost::unordered_set<T>, false > {
28  static void exec(InArcType& iarc, boost::unordered_set<T>& vec){
29  vec.clear();
30  // get the number of elements to deserialize
31  size_t length = 0;
32  iarc >> length;
33  // iterate through and send to the output iterator
34  for (size_t x = 0; x < length ; ++x){
35  T v;
36  iarc >> v;
37  vec.insert(v);
38  }
39  }
40  };
41 
42 } // archive_detail
43 } // turicreate
44 
45 
46 
47 
48 // check if C++11 exists. If it does, also support the std::unordered_set
49 #if defined(__cplusplus) && __cplusplus >= 201103L
50 #include <unordered_set>
51 
52 namespace turi {
53 namespace archive_detail {
54  /** serializes a set */
55  template <typename OutArcType, typename T>
56  struct serialize_impl<OutArcType, std::unordered_set<T>, false > {
57  static void exec(OutArcType& oarc, const std::unordered_set<T>& vec){
58  serialize_iterator(oarc,
59  vec.begin(), vec.end(), vec.size());
60  }
61  };
62 
63  /** deserializes a set */
64  template <typename InArcType, typename T>
65  struct deserialize_impl<InArcType, std::unordered_set<T>, false > {
66  static void exec(InArcType& iarc, std::unordered_set<T>& vec){
67  vec.clear();
68  // get the number of elements to deserialize
69  size_t length = 0;
70  iarc >> length;
71  // iterate through and send to the output iterator
72  for (size_t x = 0; x < length ; ++x){
73  T v;
74  iarc >> v;
75  vec.insert(v);
76  }
77  }
78  };
79 
80 } // archive_detail
81 } // turicreate
82 
83 
84 #endif
85 
86 
87 
88 
89 #endif
STL namespace.
void serialize_iterator(OutArcType &oarc, RandomAccessIterator begin, RandomAccessIterator end)
Serializes the contents between the iterators begin and end.
Definition: iterator.hpp:36