-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstl_serialization.h
532 lines (465 loc) · 19.5 KB
/
stl_serialization.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* ===============================================================
* Description: Serialization for STL data structures.
*
* Author: Ayush Dubey, [email protected]
*
* Copyright (C) 2015, Cornell University, see the LICENSE file
* for licensing agreement
* ===============================================================
*/
#ifndef weaver_common_stl_serialization_h_
#define weaver_common_stl_serialization_h_
#include <stdint.h>
#include <memory>
#include <vector>
#include <deque>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <google/sparse_hash_set>
#include <google/sparse_hash_map>
#include <google/dense_hash_map>
#include <queue>
#include <string>
#include <e/serialization.h>
#include "common/weaver_serialization.h"
namespace message
{
uint64_t size(void*, const bool&);
uint64_t size(void*, const uint8_t&);
uint64_t size(void*, const uint16_t&);
uint64_t size(void*, const uint32_t&);
uint64_t size(void*, const uint64_t&);
uint64_t size(void*, const int64_t&);
uint64_t size(void*, const int&);
uint64_t size(void*, const double&);
uint64_t size(void*, const std::string &t);
uint64_t size(void*, const std::vector<bool> &t);
template <typename T1, typename T2, typename T3> inline uint64_t size(void*, const std::set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> inline uint64_t size(void*, const std::unordered_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> inline uint64_t size(void*, const google::sparse_hash_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3, typename T4> inline uint64_t size(void*, const std::unordered_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> inline uint64_t size(void*, const google::sparse_hash_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> inline uint64_t size(void*, const google::dense_hash_map<T1, T2, T3, T4>& t);
template <typename T> inline uint64_t size(void*, const std::vector<T>& t);
template <typename T> inline uint64_t size(void*, const std::deque<T>& t);
template <typename T1, typename T2, typename T3> uint64_t size(void*, std::priority_queue<T1, T2, T3>);
template <typename T1, typename T2> inline uint64_t size(void*, const std::pair<T1, T2>& t);
template <typename T1, typename T2, typename T3> inline uint64_t size(void*, const std::tuple<T1, T2, T3>& t);
template <typename T> inline uint64_t size(void*, const std::shared_ptr<T> &ptr_t);
template <typename T> inline uint64_t size(void*, const std::unique_ptr<T> &ptr_t);
void pack_buffer(e::packer &packer, void*, const bool &t);
void pack_buffer(e::packer &packer, void*, const uint8_t &t);
void pack_buffer(e::packer &packer, void*, const uint16_t &t);
void pack_buffer(e::packer &packer, void*, const uint32_t &t);
void pack_buffer(e::packer &packer, void*, const uint64_t &t);
void pack_buffer(e::packer &packer, void*, const int64_t &t);
void pack_buffer(e::packer &packer, void*, const int &t);
void pack_buffer(e::packer &packer, void*, const double &t);
void pack_string(e::packer &packer, const std::string &t, const uint32_t sz);
void pack_buffer(e::packer &packer, void*, const std::string &t);
void pack_buffer(e::packer &packer, void*, const std::vector<bool> &t);
template <typename T1, typename T2, typename T3> inline void pack_buffer(e::packer& packer, void*, const std::set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> inline void pack_buffer(e::packer& packer, void*, const std::unordered_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> inline void pack_buffer(e::packer&, void*, const google::sparse_hash_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3, typename T4> void pack_buffer(e::packer& packer, void*, const std::unordered_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> inline void pack_buffer(e::packer&, void*, const google::sparse_hash_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> inline void pack_buffer(e::packer&, void*, const google::dense_hash_map<T1, T2, T3, T4>& t);
template <typename T> inline void pack_buffer(e::packer& packer, void*, const std::vector<T>& t);
template <typename T> inline void pack_buffer(e::packer& packer, void*, const std::deque<T>& t);
template <typename T1, typename T2, typename T3> void pack_buffer(e::packer&, void*, std::priority_queue<T1, T2, T3>);
template <typename T1, typename T2> inline void pack_buffer(e::packer &packer, void*, const std::pair<T1, T2>& t);
template <typename T1, typename T2, typename T3> inline void pack_buffer(e::packer &packer, void*, const std::tuple<T1, T2, T3>& t);
template <typename T> inline void pack_buffer(e::packer& packer, void*, const std::shared_ptr<T> &ptr_t);
template <typename T> inline void pack_buffer(e::packer& packer, void*, const std::unique_ptr<T> &ptr_t);
void unpack_buffer(e::unpacker &unpacker, void*, bool &t);
void unpack_buffer(e::unpacker &unpacker, void*, uint8_t &t);
void unpack_buffer(e::unpacker &unpacker, void*, uint16_t &t);
void unpack_buffer(e::unpacker &unpacker, void*, uint32_t &t);
void unpack_buffer(e::unpacker &unpacker, void*, uint64_t &t);
void unpack_buffer(e::unpacker &unpacker, void*, int64_t &t);
void unpack_buffer(e::unpacker &unpacker, void*, int &t);
void unpack_buffer(e::unpacker &unpacker, void*, double &t);
void unpack_string(e::unpacker &unpacker, std::string &t, const uint32_t sz);
void unpack_buffer(e::unpacker &unpacker, void*, std::string &t);
void unpack_buffer(e::unpacker &unpacker, void*, std::vector<bool> &t);
template <typename T1, typename T2, typename T3> void unpack_buffer(e::unpacker &unpacker, void*, std::set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> void unpack_buffer(e::unpacker &unpacker, void*, std::unordered_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3> void unpack_buffer(e::unpacker &unpacker, void*, google::sparse_hash_set<T1, T2, T3>& t);
template <typename T1, typename T2, typename T3, typename T4> void unpack_buffer(e::unpacker &unpacker, void*, std::unordered_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> void unpack_buffer(e::unpacker &unpacker, void*, google::sparse_hash_map<T1, T2, T3, T4>& t);
template <typename T1, typename T2, typename T3, typename T4> void unpack_buffer(e::unpacker &unpacker, void*, google::dense_hash_map<T1, T2, T3, T4>& t);
template <typename T> void unpack_buffer(e::unpacker &unpacker, void*, std::vector<T>& t);
template <typename T> void unpack_buffer(e::unpacker &unpacker, void*, std::deque<T>& t);
template <typename T1, typename T2, typename T3> void unpack_buffer(e::unpacker &unpacker, void*, std::priority_queue<T1, T2, T3>&);
template <typename T1, typename T2> void unpack_buffer(e::unpacker &unpacker, void*, std::pair<T1, T2>& t);
template <typename T1, typename T2, typename T3> void unpack_buffer(e::unpacker &unpacker, void*, std::tuple<T1, T2, T3>& t);
template <typename T> void unpack_buffer(e::unpacker &unpacker, void*, std::shared_ptr<T> &ptr_t);
template <typename T> void unpack_buffer(e::unpacker &unpacker, void*, std::unique_ptr<T> &ptr_t);
// size templates
template <typename T1, typename T2>
inline uint64_t size(void *aux_args, const std::pair<T1, T2> &t)
{
return size(aux_args, t.first) + size(aux_args, t.second);
}
template <typename T1, typename T2, typename T3>
inline uint64_t size(void *aux_args, const std::tuple<T1, T2, T3> &t){
return size(aux_args, std::get<0>(t))
+ size(aux_args, std::get<1>(t))
+ size(aux_args, std::get<2>(t));
}
template <typename T>
inline uint64_t size(void *aux_args, const std::shared_ptr<T> &ptr_t)
{
bool dummy;
uint64_t sz = size(aux_args, dummy);
if (ptr_t) {
sz += size(aux_args, *ptr_t);
}
return sz;
}
template <typename T>
inline uint64_t size(void *aux_args, const std::unique_ptr<T> &ptr_t)
{
bool dummy;
uint64_t sz = size(aux_args, dummy);
if (ptr_t) {
sz += size(aux_args, *ptr_t);
}
return sz;
}
#define SET_SZ \
uint64_t total_size = sizeof(uint32_t); \
for (const T1 &elem : t) { \
total_size += size(aux_args, elem); \
} \
return total_size;
template <typename T1, typename T2, typename T3>
inline uint64_t size(void *aux_args, const std::set<T1,T2,T3> &t)
{
SET_SZ;
}
template <typename T1, typename T2, typename T3>
inline uint64_t size(void *aux_args, const std::unordered_set<T1,T2,T3> &t)
{
SET_SZ;
}
template <typename T1, typename T2, typename T3>
inline uint64_t size(void *aux_args, const google::sparse_hash_set<T1,T2,T3> &t)
{
SET_SZ;
}
#undef SET_SZ
#define MAP_SZ \
uint64_t total_size = sizeof(uint32_t); \
for (const std::pair<const T1, T2> &pair : t) { \
total_size += size(aux_args, pair.first) + size(aux_args, pair.second); \
} \
return total_size;
template <typename T1, typename T2, typename T3, typename T4>
inline uint64_t size(void *aux_args, const std::unordered_map<T1, T2, T3, T4> &t)
{
MAP_SZ;
}
template <typename T1, typename T2, typename T3, typename T4>
inline uint64_t size(void *aux_args, const google::sparse_hash_map<T1,T2,T3,T4> &t)
{
MAP_SZ;
}
template <typename T1, typename T2, typename T3, typename T4>
inline uint64_t size(void *aux_args, const google::dense_hash_map<T1,T2,T3,T4> &t)
{
MAP_SZ;
}
#undef MAP_SZ
template <typename T>
inline uint64_t size(void *aux_args, const std::vector<T> &t)
{
uint64_t tot_size = sizeof(uint32_t);
for (const T &elem: t) {
tot_size += size(aux_args, elem);
}
return tot_size;
}
template <typename T>
inline uint64_t size(void *aux_args, const std::deque<T> &t)
{
uint64_t tot_size = sizeof(uint32_t);
for (const T &elem: t) {
tot_size += size(aux_args, elem);
}
return tot_size;
}
template <typename T1, typename T2, typename T3>
inline uint64_t size(void *aux_args, std::priority_queue<T1, T2, T3> t)
{
// cannot iterate pqueue so create a copy, no reference
uint64_t sz = sizeof(uint32_t);
while (!t.empty()) {
sz += size(aux_args, t.top());
t.pop();
}
return sz;
}
// packing templates
template <typename T1, typename T2>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::pair<T1, T2> &t)
{
// assumes constant size
pack_buffer(packer, aux_args, t.first);
pack_buffer(packer, aux_args, t.second);
}
template <typename T1, typename T2, typename T3>
inline void pack_buffer(e::packer &packer, void *aux_args, const std::tuple<T1, T2, T3> &t){
pack_buffer(packer, aux_args, std::get<0>(t));
pack_buffer(packer, aux_args, std::get<1>(t));
pack_buffer(packer, aux_args, std::get<2>(t));
}
template <typename T> inline void pack_buffer(e::packer& packer, void *aux_args, const std::shared_ptr<T> &ptr_t)
{
bool exists;
if (ptr_t) {
exists = true;
pack_buffer(packer, aux_args, exists);
pack_buffer(packer, aux_args, *ptr_t);
} else {
exists = false;
pack_buffer(packer, aux_args, exists);
}
}
template <typename T> inline void pack_buffer(e::packer& packer, void *aux_args, const std::unique_ptr<T> &ptr_t)
{
bool exists;
if (ptr_t) {
exists = true;
pack_buffer(packer, aux_args, exists);
pack_buffer(packer, aux_args, *ptr_t);
} else {
exists = false;
pack_buffer(packer, aux_args, exists);
}
}
template <typename T>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::vector<T> &t)
{
// !assumes constant element size
assert(t.size() <= UINT32_MAX);
uint32_t num_elems = t.size();
pack_buffer(packer, aux_args, num_elems);
for (const T &elem: t) {
pack_buffer(packer, aux_args, elem);
}
}
template <typename T>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::deque<T> &t)
{
// !assumes constant element size
assert(t.size() <= UINT32_MAX);
uint32_t num_elems = t.size();
pack_buffer(packer, aux_args, num_elems);
for (const T &elem : t) {
pack_buffer(packer, aux_args, elem);
}
}
template <typename T1, typename T2, typename T3>
inline void
pack_buffer(e::packer &packer, void *aux_args, std::priority_queue<T1, T2, T3> t)
{
assert(t.size() <= UINT32_MAX);
uint32_t num_elems = t.size();
packer = packer << num_elems;
while (!t.empty()) {
pack_buffer(packer, aux_args, t.top());
t.pop();
}
}
#define SET_PACK \
assert(t.size() <= UINT32_MAX); \
uint32_t num_keys = t.size(); \
pack_buffer(packer, aux_args, num_keys); \
for (const T1 &elem : t) { \
pack_buffer(packer, aux_args, elem); \
}
template <typename T1, typename T2, typename T3>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::set<T1,T2,T3> &t)
{
SET_PACK;
}
template <typename T1, typename T2, typename T3>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::unordered_set<T1,T2,T3> &t)
{
SET_PACK;
}
template <typename T1, typename T2, typename T3>
inline void
pack_buffer(e::packer &packer, void *aux_args, const google::sparse_hash_set<T1, T2, T3> &t)
{
SET_PACK;
}
#undef SET_PACK
#define MAP_PACK \
assert(t.size() <= UINT32_MAX); \
uint32_t num_keys = t.size(); \
pack_buffer(packer, aux_args, num_keys); \
for (const std::pair<const T1, T2> &pair : t) { \
pack_buffer(packer, aux_args, pair.first); \
pack_buffer(packer, aux_args, pair.second); \
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
pack_buffer(e::packer &packer, void *aux_args, const std::unordered_map<T1, T2, T3, T4> &t)
{
MAP_PACK;
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
pack_buffer(e::packer &packer, void *aux_args, const google::sparse_hash_map<T1, T2, T3, T4> &t)
{
MAP_PACK;
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
pack_buffer(e::packer &packer, void *aux_args, const google::dense_hash_map<T1, T2, T3, T4> &t)
{
MAP_PACK;
}
#undef MAP_PACK
// unpacking templates
template <typename T1, typename T2>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::pair<T1, T2> &t)
{
//assumes constant size
unpack_buffer(unpacker, aux_args, t.first);
unpack_buffer(unpacker, aux_args, t.second);
}
template <typename T1, typename T2, typename T3>
inline void unpack_buffer(e::unpacker &unpacker, void *aux_args, std::tuple<T1, T2, T3>& t){
unpack_buffer(unpacker, aux_args, std::get<0>(t));
unpack_buffer(unpacker, aux_args, std::get<1>(t));
unpack_buffer(unpacker, aux_args, std::get<2>(t));
}
template <typename T> inline void unpack_buffer(e::unpacker &unpacker, void *aux_args, std::shared_ptr<T> &ptr_t)
{
bool exists;
unpack_buffer(unpacker, aux_args, exists);
if (exists) {
ptr_t.reset(new T());
unpack_buffer(unpacker, aux_args, *ptr_t);
}
}
template <typename T> inline void unpack_buffer(e::unpacker &unpacker, void *aux_args, std::unique_ptr<T> &ptr_t)
{
bool exists;
unpack_buffer(unpacker, aux_args, exists);
if (exists) {
ptr_t.reset(new T());
unpack_buffer(unpacker, aux_args, *ptr_t);
}
}
template <typename T>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::vector<T> &t)
{
assert(t.size() == 0);
uint32_t elements_left;
unpack_buffer(unpacker, aux_args, elements_left);
t.resize(elements_left);
for (uint32_t i = 0; i < elements_left; i++) {
unpack_buffer(unpacker, aux_args, t[i]);
}
}
template <typename T>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::deque<T> &t)
{
assert(t.size() == 0);
uint32_t elements_left;
unpack_buffer(unpacker, aux_args, elements_left);
t.resize(elements_left);
for (uint32_t i = 0; i < elements_left; i++) {
unpack_buffer(unpacker, aux_args, t[i]);
}
}
template <typename T1, typename T2, typename T3>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::priority_queue<T1, T2, T3> &t)
{
assert(t.size() == 0);
uint32_t elements_left = 0;
unpack_buffer(unpacker, aux_args, elements_left);
while (elements_left > 0) {
T1 to_add;
unpack_buffer(unpacker, aux_args, to_add);
t.push(std::move(to_add));
elements_left--;
}
}
#define SET_UNPACK \
assert(t.size() == 0); \
uint32_t elements_left; \
unpack_buffer(unpacker, aux_args, elements_left); \
while (elements_left > 0) { \
T1 new_elem; \
unpack_buffer(unpacker, aux_args, new_elem); \
t.insert(new_elem); \
elements_left--; \
}
template <typename T1, typename T2, typename T3>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::set<T1,T2,T3> &t)
{
SET_UNPACK;
}
template <typename T1, typename T2, typename T3>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::unordered_set<T1,T2,T3> &t)
{
SET_UNPACK;
}
template <typename T1, typename T2, typename T3>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, google::sparse_hash_set<T1,T2,T3> &t)
{
SET_UNPACK;
}
#undef SET_UNPACK
#define MAP_UNPACK \
assert(t.size() == 0); \
uint32_t elements_left; \
unpack_buffer(unpacker, aux_args, elements_left); \
while (elements_left > 0) { \
T1 key_to_add; \
T2 val_to_add; \
unpack_buffer(unpacker, aux_args, key_to_add); \
unpack_buffer(unpacker, aux_args, val_to_add); \
t[key_to_add] = val_to_add; \
elements_left--; \
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, std::unordered_map<T1, T2, T3, T4> &t)
{
MAP_UNPACK;
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, google::sparse_hash_map<T1,T2,T3,T4> &t)
{
MAP_UNPACK;
}
template <typename T1, typename T2, typename T3, typename T4>
inline void
unpack_buffer(e::unpacker &unpacker, void *aux_args, google::dense_hash_map<T1,T2,T3,T4> &t)
{
MAP_UNPACK;
}
#undef MAP_UNPACK
}
#endif