datasketches-cpp
Loading...
Searching...
No Matches
common_defs.hpp
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef _COMMON_DEFS_HPP_
21#define _COMMON_DEFS_HPP_
22
23#include <cstdint>
24#include <string>
25#include <memory>
26#include <iostream>
27#include <random>
28#include <chrono>
29#include <thread>
30
32namespace datasketches {
33
34static const uint64_t DEFAULT_SEED = 9001;
35
36enum resize_factor { X1 = 0, X2, X4, X8 };
37
38template<typename A> using string = std::basic_string<char, std::char_traits<char>, typename std::allocator_traits<A>::template rebind_alloc<char>>;
39
40// common random declarations
41namespace random_utils {
42 static std::random_device rd; // possibly unsafe in MinGW with GCC < 9.2
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);
46
47 // thread-safe random bit
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())));
51
52 inline void override_seed(uint64_t s) {
53 rand.seed(s);
54 }
55}
56
57// utility function to hide unused compiler warning
58// usually has no additional cost
59template<typename T> void unused(T&&...) {}
60
61// common helping functions
62// TODO: find a better place for them
63
64constexpr uint8_t log2(uint32_t n) {
65 return (n > 1) ? 1 + log2(n >> 1) : 0;
66}
67
68constexpr 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);
70}
71
72// stream helpers to hide casts
73template<typename T>
74static inline T read(std::istream& is) {
75 T value;
76 is.read(reinterpret_cast<char*>(&value), sizeof(T));
77 return value;
78}
79
80template<typename T>
81static inline void read(std::istream& is, T* ptr, size_t size_bytes) {
82 is.read(reinterpret_cast<char*>(ptr), size_bytes);
83}
84
85template<typename T>
86static inline void write(std::ostream& os, T value) {
87 os.write(reinterpret_cast<const char*>(&value), sizeof(T));
88}
89
90template<typename T>
91static inline void write(std::ostream& os, const T* ptr, size_t size_bytes) {
92 os.write(reinterpret_cast<const char*>(ptr), size_bytes);
93}
94
95template<typename T>
96T byteswap(T value) {
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]);
101 }
102 return value;
103}
104
105template<typename T>
106static inline T read_big_endian(std::istream& is) {
107 T value;
108 is.read(reinterpret_cast<char*>(&value), sizeof(T));
109 return byteswap(value);
110}
111
112// wrapper for iterators to implement operator-> returning temporary value
113template<typename T>
114class return_value_holder {
115public:
116 return_value_holder(T value): value_(value) {}
117 const T* operator->() const { return std::addressof(value_); }
118private:
119 T value_;
120};
121
122} // namespace
123
124#endif // _COMMON_DEFS_HPP_
DataSketches namespace.
Definition binomial_bounds.hpp:38