-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
110 lines (95 loc) · 2.99 KB
/
util.h
File metadata and controls
110 lines (95 loc) · 2.99 KB
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
#ifndef SFSS_UTIL_HEADER
#define SFSS_UTIL_HEADER
#include <stdlib.h>
#include <cstdint> // for uint32_t
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <cassert>
#include <cstring> // for memcpy
#include <vector>
#include <array>
#include <emp-tool/emp-tool.h>
#include <gmpxx.h>
#include "twokeyprp.h"
#include <thread>
using namespace emp;
typedef uint64_t index_type;
inline std::ostream &operator<<(std::ostream &os, __uint128_t value)
{
if (value == 0)
{
os << "0";
return os;
}
char buf[40] = {0};
int i = 39;
while (value > 0 && i)
{
buf[--i] = "0123456789"[value % 10];
value /= 10;
}
os << (buf + i);
return os;
}
// Helper for element size calculation
template <typename T>
size_t get_serialized_size_helper(const T &v) {
if constexpr (std::is_arithmetic<T>::value) {
return sizeof(T);
} else {
return v.get_serialized_size();
}
}
// Helper for element serialization
template <typename T>
size_t serialize_helper(const T &v, char *buf) {
if constexpr (std::is_arithmetic<T>::value) {
std::memcpy(buf, &v, sizeof(T));
return sizeof(T);
} else {
v.serialize(buf);
return v.get_serialized_size();
}
}
// Helper for element deserialization
template <typename T>
size_t deserialize_helper(T &v, const char *buf)
{
if constexpr (std::is_arithmetic<T>::value) {
std::memcpy(&v, buf, sizeof(T));
return sizeof(T);
} else {
v.deserialize(buf);
return v.get_serialized_size();
}
}
#define my_assert(expr, msg) \
if (!(expr)) \
{ \
std::cerr << "Assertion failed: " << msg << "\n"; \
}
// get the `i`-th bit of `idx`.
// NOTE: `N` <= 32.
template <int N = 32>
bool get_bit(index_type idx, uint64_t i)
{
// i \in [1,2...,len]
my_assert(N <= 64, "N should <= 64!");
uint64_t mask = 1L << (N - i);
uint64_t result = idx & mask;
return (result == 0) ? false : true;
}
enum TimeitLevel { TIMEIT_NONE = 0, TIMEIT_LOG = 1, TIMEIT_WARNING = 2, TIMEIT_DEBUG = 3 };
extern TimeitLevel GLOBAL_TIMEIT_LEVEL;
TimeitLevel GLOBAL_TIMEIT_LEVEL = TIMEIT_DEBUG;
#define TIMEIT_START(name) auto start_##name = std::chrono::high_resolution_clock::now();
#define TIMEIT_END_LEVEL(name, level) \
auto end_##name = std::chrono::high_resolution_clock::now(); \
if (level <= GLOBAL_TIMEIT_LEVEL) { \
std::chrono::duration<double, std::milli> duration_##name = end_##name - start_##name; \
std::cout << "Time taken for " << #name << ": " \
<< "\033[1;32m" << duration_##name.count() << " ms\033[0m\n" << std::endl; \
}
#define TIMEIT_END(name) TIMEIT_END_LEVEL(name, TIMEIT_DEBUG)
#endif