Turi Create  4.0
unordered_map.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_MAP_HPP
7 #define TURI_SERIALIZE_UNORDERED_MAP_HPP
8 
9 #include <boost/unordered_map.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 
16 namespace archive_detail {
17  /** Serializes a map */
18  template <typename OutArcType, typename T, typename U>
19  struct serialize_impl<OutArcType, boost::unordered_map<T,U>, false > {
20  static void exec(OutArcType& oarc,
21  const boost::unordered_map<T,U>& vec){
22  serialize_iterator(oarc,
23  vec.begin(), vec.end(), vec.size());
24  }
25  };
26 
27  /** deserializes a map */
28 
29  template <typename InArcType, typename T, typename U>
30  struct deserialize_impl<InArcType, boost::unordered_map<T,U>, false > {
31  static void exec(InArcType& iarc, boost::unordered_map<T,U>& vec){
32  vec.clear();
33  // get the number of elements to deserialize
34  size_t length = 0;
35  iarc >> length;
36  // iterate through and send to the output iterator
37  for (size_t x = 0; x < length ; ++x){
38  std::pair<T, U> v;
39  iarc >> v;
40  vec[v.first] = v.second;
41  }
42  }
43  };
44 
45 } // archive_detail
46 } // turicreate
47 
48 
49 
50 #if defined(__cplusplus) && __cplusplus >= 201103L
51 #include <unordered_map>
52 namespace turi {
53 
54 namespace archive_detail {
55  /** Serializes a map */
56  template <typename OutArcType, typename T, typename U>
57  struct serialize_impl<OutArcType, std::unordered_map<T,U>, false > {
58  static void exec(OutArcType& oarc,
59  const std::unordered_map<T,U>& vec){
60  serialize_iterator(oarc,
61  vec.begin(), vec.end(), vec.size());
62  }
63  };
64 
65  /** deserializes a map */
66 
67  template <typename InArcType, typename T, typename U>
68  struct deserialize_impl<InArcType, std::unordered_map<T,U>, false > {
69  static void exec(InArcType& iarc, std::unordered_map<T,U>& vec){
70  vec.clear();
71  // get the number of elements to deserialize
72  size_t length = 0;
73  iarc >> length;
74  // iterate through and send to the output iterator
75  for (size_t x = 0; x < length ; ++x){
76  std::pair<T, U> v;
77  iarc >> v;
78  vec[v.first] = v.second;
79  }
80  }
81  };
82 
83 } // archive_detail
84 } // turicreate
85 
86 #endif
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