20 #ifndef _COMMON_DEFS_HPP_
21 #define _COMMON_DEFS_HPP_
34 static const uint64_t DEFAULT_SEED = 9001;
36 enum resize_factor { X1 = 0, X2, X4, X8 };
38 template<
typename A>
using string = std::basic_string<char, std::char_traits<char>,
typename std::allocator_traits<A>::template rebind_alloc<char>>;
41 namespace random_utils {
42 static std::random_device rd;
43 static thread_local std::mt19937_64 rand(rd());
44 static thread_local std::uniform_real_distribution<> next_double(0.0, 1.0);
47 static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
48 random_bit(
static_cast<uint32_t
>(std::chrono::system_clock::now().time_since_epoch().count()
49 + std::hash<std::thread::id>{}(std::this_thread::get_id())));
51 inline void override_seed(uint64_t s) {
58 template<
typename T>
void unused(T&&...) {}
63 constexpr uint8_t log2(uint32_t n) {
64 return (n > 1) ? 1 + log2(n >> 1) : 0;
67 constexpr uint8_t lg_size_from_count(uint32_t n,
double load_factor) {
68 return log2(n) + ((n >
static_cast<uint32_t
>((1 << (log2(n) + 1)) * load_factor)) ? 2 : 1);
73 static inline T read(std::istream& is) {
75 is.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
80 static inline void read(std::istream& is, T* ptr,
size_t size_bytes) {
81 is.read(
reinterpret_cast<char*
>(ptr), size_bytes);
85 static inline void write(std::ostream& os, T value) {
86 os.write(
reinterpret_cast<const char*
>(&value),
sizeof(T));
90 static inline void write(std::ostream& os,
const T* ptr,
size_t size_bytes) {
91 os.write(
reinterpret_cast<const char*
>(ptr), size_bytes);
96 char* ptr =
static_cast<char*
>(
static_cast<void*
>(&value));
97 const int len =
sizeof(T);
98 for (
size_t i = 0; i < len / 2; ++i) {
99 std::swap(ptr[i], ptr[len - i - 1]);
105 static inline T read_big_endian(std::istream& is) {
107 is.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
108 return byteswap(value);
116 const T* operator->()
const {
return std::addressof(value_); }
Definition: common_defs.hpp:113
DataSketches namespace.
Definition: common_defs.hpp:32