NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
phmap_dump.h
浏览该文件的文档.
1#if !defined(phmap_dump_h_guard_)
2#define phmap_dump_h_guard_
3
4// ---------------------------------------------------------------------------
5// Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com
6//
7// providing dump/load/mmap_load
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// https://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20// ---------------------------------------------------------------------------
21
22#include <iostream>
23#include <fstream>
24#include <sstream>
25#include "phmap.h"
26namespace phmap
27{
28
29namespace type_traits_internal {
30
31#if defined(__GLIBCXX__) && __GLIBCXX__ < 20150801
32 template<typename T> struct IsTriviallyCopyable : public std::integral_constant<bool, __has_trivial_copy(T)> {};
33#else
34 template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {};
35#endif
36
37template <class T1, class T2>
38struct IsTriviallyCopyable<std::pair<T1, T2>> {
40};
41}
42
43namespace priv {
44
45#if !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP)
46
47// ------------------------------------------------------------------------
48// dump/load for raw_hash_set
49// ------------------------------------------------------------------------
50template <class Policy, class Hash, class Eq, class Alloc>
51template<typename OutputArchive>
54 "value_type should be trivially copyable");
55
56 ar.saveBinary(&size_, sizeof(size_t));
57 ar.saveBinary(&capacity_, sizeof(size_t));
58 if (size_ == 0)
59 return true;
60 ar.saveBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1));
61 ar.saveBinary(slots_, sizeof(slot_type) * capacity_);
62 return true;
63}
64
65template <class Policy, class Hash, class Eq, class Alloc>
66template<typename InputArchive>
69 "value_type should be trivially copyable");
70 raw_hash_set<Policy, Hash, Eq, Alloc>().swap(*this); // clear any existing content
71 ar.loadBinary(&size_, sizeof(size_t));
72 ar.loadBinary(&capacity_, sizeof(size_t));
73
74 if (capacity_) {
75 // allocate memory for ctrl_ and slots_
76 initialize_slots(capacity_);
77 }
78 if (size_ == 0)
79 return true;
80 ar.loadBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1));
81 ar.loadBinary(slots_, sizeof(slot_type) * capacity_);
82 return true;
83}
84
85// ------------------------------------------------------------------------
86// dump/load for parallel_hash_set
87// ------------------------------------------------------------------------
88template <size_t N,
89 template <class, class, class, class> class RefSet,
90 class Mtx_,
91 class Policy, class Hash, class Eq, class Alloc>
92template<typename OutputArchive>
95 "value_type should be trivially copyable");
96
97 size_t submap_count = subcnt();
98 ar.saveBinary(&submap_count, sizeof(size_t));
99 for (size_t i = 0; i < sets_.size(); ++i) {
100 auto& inner = sets_[i];
101 typename Lockable::UniqueLock m(const_cast<Inner&>(inner));
102 if (!inner.set_.phmap_dump(ar)) {
103 std::cerr << "Failed to dump submap " << i << std::endl;
104 return false;
105 }
106 }
107 return true;
108}
109
110template <size_t N,
111 template <class, class, class, class> class RefSet,
112 class Mtx_,
113 class Policy, class Hash, class Eq, class Alloc>
114template<typename InputArchive>
117 "value_type should be trivially copyable");
118
119 size_t submap_count = 0;
120 ar.loadBinary(&submap_count, sizeof(size_t));
121 if (submap_count != subcnt()) {
122 std::cerr << "submap count(" << submap_count << ") != N(" << N << ")" << std::endl;
123 return false;
124 }
125
126 for (size_t i = 0; i < submap_count; ++i) {
127 auto& inner = sets_[i];
128 typename Lockable::UniqueLock m(const_cast<Inner&>(inner));
129 if (!inner.set_.phmap_load(ar)) {
130 std::cerr << "Failed to load submap " << i << std::endl;
131 return false;
132 }
133 }
134 return true;
135}
136
137#endif // !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP)
138
139} // namespace priv
140
141
142
143// ------------------------------------------------------------------------
144// BinaryArchive
145// File is closed when archive object is destroyed
146// ------------------------------------------------------------------------
147
148// ------------------------------------------------------------------------
149// ------------------------------------------------------------------------
151public:
152 BinaryOutputArchive(const char *file_path) {
153 ofs_.open(file_path, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
154 }
155
156 bool saveBinary(const void *p, size_t sz) {
157 ofs_.write(reinterpret_cast<const char*>(p), sz);
158 return true;
159 }
160
161 template<typename V>
162 typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
163 saveBinary(const V& v) {
164 ofs_.write(reinterpret_cast<const char *>(&v), sizeof(V));
165 return true;
166 }
167
168 template<typename Map>
169 auto saveBinary(const Map& v) -> decltype(v.phmap_dump(*this), bool())
170 {
171 return v.phmap_dump(*this);
172 }
173
174private:
175 std::ofstream ofs_;
176};
177
178
180public:
181 BinaryInputArchive(const char * file_path) {
182 ifs_.open(file_path, std::ofstream::in | std::ofstream::binary);
183 }
184
185 bool loadBinary(void* p, size_t sz) {
186 ifs_.read(reinterpret_cast<char*>(p), sz);
187 return true;
188 }
189
190 template<typename V>
191 typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
193 ifs_.read(reinterpret_cast<char *>(v), sizeof(V));
194 return true;
195 }
196
197 template<typename Map>
198 auto loadBinary(Map* v) -> decltype(v->phmap_load(*this), bool())
199 {
200 return v->phmap_load(*this);
201 }
202
203private:
204 std::ifstream ifs_;
205};
206
207} // namespace phmap
208
209
210#ifdef CEREAL_SIZE_TYPE
211
212template <class T>
213using PhmapTrivCopyable = typename phmap::type_traits_internal::IsTriviallyCopyable<T>;
214
215namespace cereal
216{
217 // Overload Cereal serialization code for phmap::flat_hash_map
218 // -----------------------------------------------------------
219 template <class K, class V, class Hash, class Eq, class A>
220 void save(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryOutputArchive>::type &ar,
222 {
223 hmap.phmap_dump(ar);
224 }
225
226 template <class K, class V, class Hash, class Eq, class A>
227 void load(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryInputArchive>::type &ar,
229 {
230 hmap.phmap_load(ar);
231 }
232
233
234 // Overload Cereal serialization code for phmap::parallel_flat_hash_map
235 // --------------------------------------------------------------------
236 template <class K, class V, class Hash, class Eq, class A, size_t N, class Mtx_>
237 void save(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryOutputArchive>::type &ar,
239 {
240 hmap.phmap_dump(ar);
241 }
242
243 template <class K, class V, class Hash, class Eq, class A, size_t N, class Mtx_>
244 void load(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryInputArchive>::type &ar,
246 {
247 hmap.phmap_load(ar);
248 }
249
250 // Overload Cereal serialization code for phmap::flat_hash_set
251 // -----------------------------------------------------------
252 template <class K, class Hash, class Eq, class A>
253 void save(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryOutputArchive>::type &ar,
255 {
256 hset.phmap_dump(ar);
257 }
258
259 template <class K, class Hash, class Eq, class A>
260 void load(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryInputArchive>::type &ar,
262 {
263 hset.phmap_load(ar);
264 }
265
266 // Overload Cereal serialization code for phmap::parallel_flat_hash_set
267 // --------------------------------------------------------------------
268 template <class K, class Hash, class Eq, class A, size_t N, class Mtx_>
269 void save(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryOutputArchive>::type &ar,
271 {
272 hset.phmap_dump(ar);
273 }
274
275 template <class K, class Hash, class Eq, class A, size_t N, class Mtx_>
276 void load(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryInputArchive>::type &ar,
278 {
279 hset.phmap_load(ar);
280 }
281}
282
283#endif
284
285
286
287
288#endif // phmap_dump_h_guard_
Definition: phmap_dump.h:179
std::enable_if< type_traits_internal::IsTriviallyCopyable< V >::value, bool >::type loadBinary(V *v)
Definition: phmap_dump.h:192
BinaryInputArchive(const char *file_path)
Definition: phmap_dump.h:181
std::ifstream ifs_
Definition: phmap_dump.h:204
auto loadBinary(Map *v) -> decltype(v->phmap_load(*this), bool())
Definition: phmap_dump.h:198
bool loadBinary(void *p, size_t sz)
Definition: phmap_dump.h:185
Definition: phmap_dump.h:150
auto saveBinary(const Map &v) -> decltype(v.phmap_dump(*this), bool())
Definition: phmap_dump.h:169
std::ofstream ofs_
Definition: phmap_dump.h:175
std::enable_if< type_traits_internal::IsTriviallyCopyable< V >::value, bool >::type saveBinary(const V &v)
Definition: phmap_dump.h:163
bool saveBinary(const void *p, size_t sz)
Definition: phmap_dump.h:156
BinaryOutputArchive(const char *file_path)
Definition: phmap_dump.h:152
typename Base::WriteLock UniqueLock
Definition: phmap_base.h:5043
Definition: phmap.h:4640
Definition: phmap.h:4578
Definition: phmap.h:4879
Definition: phmap.h:4826
bool phmap_load(InputArchive &ar)
Definition: phmap_dump.h:115
bool phmap_dump(OutputArchive &ar) const
Definition: phmap_dump.h:93
Definition: phmap.h:839
bool phmap_load(InputArchive &)
Definition: phmap_dump.h:67
bool phmap_dump(OutputArchive &) const
Definition: phmap_dump.h:52
void swap(raw_hash_set &that) noexcept(IsNoThrowSwappable< hasher >() &&IsNoThrowSwappable< key_equal >() &&(!AllocTraits::propagate_on_container_swap::value||IsNoThrowSwappable< allocator_type >()))
Definition: phmap.h:1647
typename PolicyTraits::slot_type slot_type
Definition: phmap.h:849
signed char ctrl_t
Definition: phmap.h:284
Definition: btree.h:77
STL namespace
unsigned char bool
Definition: stdbool.h:25
Definition: phmap_utils.h:139
@ kWidth
Definition: phmap.h:471