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);
45 static thread_local std::uniform_int_distribution<uint64_t> next_uint64(0, UINT64_MAX);
48 static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
49 random_bit(
static_cast<uint32_t
>(std::chrono::system_clock::now().time_since_epoch().count()
50 + std::hash<std::thread::id>{}(std::this_thread::get_id())));
52 inline void override_seed(uint64_t s) {
59 template<
typename T>
void unused(T&&...) {}
64 constexpr uint8_t log2(uint32_t n) {
65 return (n > 1) ? 1 + log2(n >> 1) : 0;
68 constexpr uint8_t lg_size_from_count(uint32_t n,
double load_factor) {
69 return log2(n) + ((n >
static_cast<uint32_t
>((1 << (log2(n) + 1)) * load_factor)) ? 2 : 1);
74 static inline T read(std::istream& is) {
76 is.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
81 static inline void read(std::istream& is, T* ptr,
size_t size_bytes) {
82 is.read(
reinterpret_cast<char*
>(ptr), size_bytes);
86 static inline void write(std::ostream& os, T value) {
87 os.write(
reinterpret_cast<const char*
>(&value),
sizeof(T));
91 static inline void write(std::ostream& os,
const T* ptr,
size_t size_bytes) {
92 os.write(
reinterpret_cast<const char*
>(ptr), size_bytes);
97 char* ptr =
static_cast<char*
>(
static_cast<void*
>(&value));
98 const int len =
sizeof(T);
99 for (
size_t i = 0; i < len / 2; ++i) {
100 std::swap(ptr[i], ptr[len - i - 1]);
106 static inline T read_big_endian(std::istream& is) {
108 is.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
109 return byteswap(value);
114 class return_value_holder {
116 return_value_holder(T value): value_(value) {}
117 const T* operator->()
const {
return std::addressof(value_); }
DataSketches namespace.
Definition: binomial_bounds.hpp:38