You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2023/11/03 18:09:54 UTC

(datasketches-cpp) 01/02: moved random_bit into random_utils namespace

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch cleanup-before-5.0.0
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git

commit 9bc7a42e7c2bf92a1cd449a9559ceee5f10df79e
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Nov 3 11:09:22 2023 -0700

    moved random_bit into random_utils namespace
---
 common/include/common_defs.hpp              | 13 ++++++-------
 density/include/density_sketch_impl.hpp     |  2 +-
 kll/include/kll_helper_impl.hpp             |  4 ++--
 quantiles/include/quantiles_sketch_impl.hpp |  2 +-
 req/include/req_compactor_impl.hpp          |  4 ++--
 5 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/common/include/common_defs.hpp b/common/include/common_defs.hpp
index fff3f3d..d8e3e6c 100644
--- a/common/include/common_defs.hpp
+++ b/common/include/common_defs.hpp
@@ -35,13 +35,7 @@ static const uint64_t DEFAULT_SEED = 9001;
 
 enum resize_factor { X1 = 0, X2, X4, X8 };
 
-template<typename A> using AllocChar = typename std::allocator_traits<A>::template rebind_alloc<char>;
-template<typename A> using string = std::basic_string<char, std::char_traits<char>, AllocChar<A>>;
-
-// thread-safe random bit
-static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
-  random_bit(static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count() 
-    + std::hash<std::thread::id>{}(std::this_thread::get_id())));
+template<typename A> using string = std::basic_string<char, std::char_traits<char>, typename std::allocator_traits<A>::template rebind_alloc<char>>;
 
 // common random declarations
 namespace random_utils {
@@ -49,6 +43,11 @@ namespace random_utils {
   static thread_local std::mt19937_64 rand(rd());
   static thread_local std::uniform_real_distribution<> next_double(0.0, 1.0);
 
+  // thread-safe random bit
+  static thread_local std::independent_bits_engine<std::mt19937, 1, uint32_t>
+    random_bit(static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count()
+      + std::hash<std::thread::id>{}(std::this_thread::get_id())));
+
   inline void override_seed(uint64_t s) {
     rand.seed(s);
   }
diff --git a/density/include/density_sketch_impl.hpp b/density/include/density_sketch_impl.hpp
index 763f4e7..1144814 100755
--- a/density/include/density_sketch_impl.hpp
+++ b/density/include/density_sketch_impl.hpp
@@ -143,7 +143,7 @@ template<typename T, typename K, typename A>
 void density_sketch<T, K, A>::compact_level(unsigned height) {
   auto& level = levels_[height];
   std::vector<bool> bits(level.size());
-  bits[0] = random_bit();
+  bits[0] = random_utils::random_bit();
   std::random_shuffle(level.begin(), level.end());
   for (unsigned i = 1; i < level.size(); ++i) {
     T delta = 0;
diff --git a/kll/include/kll_helper_impl.hpp b/kll/include/kll_helper_impl.hpp
index 321761b..e763c57 100644
--- a/kll/include/kll_helper_impl.hpp
+++ b/kll/include/kll_helper_impl.hpp
@@ -99,7 +99,7 @@ void kll_helper::randomly_halve_down(T* buf, uint32_t start, uint32_t length) {
 #ifdef KLL_VALIDATION
   const uint32_t offset = deterministic_offset();
 #else
-  const uint32_t offset = random_bit();
+  const uint32_t offset = random_utils::random_bit();
 #endif
   uint32_t j = start + offset;
   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) {
 #ifdef KLL_VALIDATION
   const uint32_t offset = deterministic_offset();
 #else
-  const uint32_t offset = random_bit();
+  const uint32_t offset = random_utils::random_bit();
 #endif
   uint32_t j = (start + length) - 1 - offset;
   for (uint32_t i = (start + length) - 1; i >= (start + half_length); i--) {
diff --git a/quantiles/include/quantiles_sketch_impl.hpp b/quantiles/include/quantiles_sketch_impl.hpp
index 1234e08..558c13c 100644
--- a/quantiles/include/quantiles_sketch_impl.hpp
+++ b/quantiles/include/quantiles_sketch_impl.hpp
@@ -958,7 +958,7 @@ void quantiles_sketch<T, C, A>::zip_buffer(Level& buf_in, Level& buf_out) {
   uint32_t rand_offset = next_offset;
   next_offset = 1 - next_offset;
 #else
-  uint32_t rand_offset = random_bit();
+  uint32_t rand_offset = random_utils::random_bit();
 #endif
   if ((buf_in.size() != 2 * buf_out.capacity())
     || (buf_out.size() > 0)) {
diff --git a/req/include/req_compactor_impl.hpp b/req/include/req_compactor_impl.hpp
index f650ca8..72aafc9 100755
--- a/req/include/req_compactor_impl.hpp
+++ b/req/include/req_compactor_impl.hpp
@@ -277,7 +277,7 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex
   if (compaction_range.second - compaction_range.first < 2) throw std::logic_error("compaction range error");
 
   if ((state_ & 1) == 1) { coin_ = !coin_; } // for odd flip coin;
-  else { coin_ = random_bit(); } // random coin flip
+  else { coin_ = random_utils::random_bit(); } // random coin flip
 
   const auto num = (compaction_range.second - compaction_range.first) / 2;
   next.ensure_space(num);
@@ -493,7 +493,7 @@ comparator_(comparator),
 allocator_(allocator),
 lg_weight_(lg_weight),
 hra_(hra),
-coin_(random_bit()),
+coin_(random_utils::random_bit()),
 sorted_(sorted),
 section_size_raw_(section_size_raw),
 section_size_(nearest_even(section_size_raw)),


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org