Skip to content

Commit 9bc7a42

Browse files
moved random_bit into random_utils namespace
1 parent acb806d commit 9bc7a42

5 files changed

Lines changed: 12 additions & 13 deletions

File tree

common/include/common_defs.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ static const uint64_t DEFAULT_SEED = 9001;
3535

3636
enum resize_factor { X1 = 0, X2, X4, X8 };
3737

38-
template<typename A> using AllocChar = typename std::allocator_traits<A>::template rebind_alloc<char>;
39-
template<typename A> using string = std::basic_string<char, std::char_traits<char>, AllocChar<A>>;
40-
41-
// thread-safe random bit
42-
static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
43-
random_bit(static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count()
44-
+ std::hash<std::thread::id>{}(std::this_thread::get_id())));
38+
template<typename A> using string = std::basic_string<char, std::char_traits<char>, typename std::allocator_traits<A>::template rebind_alloc<char>>;
4539

4640
// common random declarations
4741
namespace random_utils {
4842
static std::random_device rd; // possibly unsafe in MinGW with GCC < 9.2
4943
static thread_local std::mt19937_64 rand(rd());
5044
static thread_local std::uniform_real_distribution<> next_double(0.0, 1.0);
5145

46+
// thread-safe random bit
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())));
50+
5251
inline void override_seed(uint64_t s) {
5352
rand.seed(s);
5453
}

density/include/density_sketch_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ template<typename T, typename K, typename A>
143143
void density_sketch<T, K, A>::compact_level(unsigned height) {
144144
auto& level = levels_[height];
145145
std::vector<bool> bits(level.size());
146-
bits[0] = random_bit();
146+
bits[0] = random_utils::random_bit();
147147
std::random_shuffle(level.begin(), level.end());
148148
for (unsigned i = 1; i < level.size(); ++i) {
149149
T delta = 0;

kll/include/kll_helper_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void kll_helper::randomly_halve_down(T* buf, uint32_t start, uint32_t length) {
9999
#ifdef KLL_VALIDATION
100100
const uint32_t offset = deterministic_offset();
101101
#else
102-
const uint32_t offset = random_bit();
102+
const uint32_t offset = random_utils::random_bit();
103103
#endif
104104
uint32_t j = start + offset;
105105
for (uint32_t i = start; i < (start + half_length); i++) {
@@ -115,7 +115,7 @@ void kll_helper::randomly_halve_up(T* buf, uint32_t start, uint32_t length) {
115115
#ifdef KLL_VALIDATION
116116
const uint32_t offset = deterministic_offset();
117117
#else
118-
const uint32_t offset = random_bit();
118+
const uint32_t offset = random_utils::random_bit();
119119
#endif
120120
uint32_t j = (start + length) - 1 - offset;
121121
for (uint32_t i = (start + length) - 1; i >= (start + half_length); i--) {

quantiles/include/quantiles_sketch_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ void quantiles_sketch<T, C, A>::zip_buffer(Level& buf_in, Level& buf_out) {
958958
uint32_t rand_offset = next_offset;
959959
next_offset = 1 - next_offset;
960960
#else
961-
uint32_t rand_offset = random_bit();
961+
uint32_t rand_offset = random_utils::random_bit();
962962
#endif
963963
if ((buf_in.size() != 2 * buf_out.capacity())
964964
|| (buf_out.size() > 0)) {

req/include/req_compactor_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex
277277
if (compaction_range.second - compaction_range.first < 2) throw std::logic_error("compaction range error");
278278

279279
if ((state_ & 1) == 1) { coin_ = !coin_; } // for odd flip coin;
280-
else { coin_ = random_bit(); } // random coin flip
280+
else { coin_ = random_utils::random_bit(); } // random coin flip
281281

282282
const auto num = (compaction_range.second - compaction_range.first) / 2;
283283
next.ensure_space(num);
@@ -493,7 +493,7 @@ comparator_(comparator),
493493
allocator_(allocator),
494494
lg_weight_(lg_weight),
495495
hra_(hra),
496-
coin_(random_bit()),
496+
coin_(random_utils::random_bit()),
497497
sorted_(sorted),
498498
section_size_raw_(section_size_raw),
499499
section_size_(nearest_even(section_size_raw)),

0 commit comments

Comments
 (0)