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 2022/02/23 00:41:23 UTC

[datasketches-cpp] 01/01: reduced code duplication

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

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

commit d4b11984a3a2ef063ed4c5e1893c2dab74920c04
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Tue Feb 22 16:41:08 2022 -0800

    reduced code duplication
---
 kll/include/kll_sketch.hpp      | 20 ++++---------------
 kll/include/kll_sketch_impl.hpp | 43 +++++++----------------------------------
 2 files changed, 11 insertions(+), 52 deletions(-)

diff --git a/kll/include/kll_sketch.hpp b/kll/include/kll_sketch.hpp
index 7f81fba..3f5291d 100644
--- a/kll/include/kll_sketch.hpp
+++ b/kll/include/kll_sketch.hpp
@@ -187,28 +187,16 @@ class kll_sketch {
      * This method takes lvalue.
      * @param value an item from a stream of items
      */
-    void update(const T& value);
-
-    /**
-     * Updates this sketch with the given data item.
-     * This method takes rvalue.
-     * @param value an item from a stream of items
-     */
-    void update(T&& value);
+    template<typename FwdT>
+    void update(FwdT&& value);
 
     /**
      * Merges another sketch into this one.
      * This method takes lvalue.
      * @param other sketch to merge into this one
      */
-    void merge(const kll_sketch& other);
-
-    /**
-     * Merges another sketch into this one.
-     * This method takes rvalue.
-     * @param other sketch to merge into this one
-     */
-    void merge(kll_sketch&& other);
+    template<typename FwdSk>
+    void merge(FwdSk&& other);
 
     /**
      * Returns true if this sketch is empty.
diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp
index a5c3406..cd520f0 100644
--- a/kll/include/kll_sketch_impl.hpp
+++ b/kll/include/kll_sketch_impl.hpp
@@ -24,6 +24,7 @@
 #include <iomanip>
 #include <sstream>
 
+#include "conditional_forward.hpp"
 #include "memory_operations.hpp"
 #include "kll_helper.hpp"
 
@@ -146,19 +147,12 @@ kll_sketch<T, C, S, A>::~kll_sketch() {
 }
 
 template<typename T, typename C, typename S, typename A>
-void kll_sketch<T, C, S, A>::update(const T& value) {
+template<typename FwdT>
+void kll_sketch<T, C, S, A>::update(FwdT&& value) {
   if (!check_update_value(value)) { return; }
   update_min_max(value);
   const uint32_t index = internal_update();
-  new (&items_[index]) T(value);
-}
-
-template<typename T, typename C, typename S, typename A>
-void kll_sketch<T, C, S, A>::update(T&& value) {
-  if (!check_update_value(value)) { return; }
-  update_min_max(value);
-  const uint32_t index = internal_update();
-  new (&items_[index]) T(std::move(value));
+  new (&items_[index]) T(std::forward<FwdT>(value));
 }
 
 template<typename T, typename C, typename S, typename A>
@@ -181,7 +175,8 @@ uint32_t kll_sketch<T, C, S, A>::internal_update() {
 }
 
 template<typename T, typename C, typename S, typename A>
-void kll_sketch<T, C, S, A>::merge(const kll_sketch& other) {
+template<typename FwdSk>
+void kll_sketch<T, C, S, A>::merge(FwdSk&& other) {
   if (other.is_empty()) return;
   if (m_ != other.m_) {
     throw std::invalid_argument("incompatible M: " + std::to_string(m_) + " and " + std::to_string(other.m_));
@@ -196,7 +191,7 @@ void kll_sketch<T, C, S, A>::merge(const kll_sketch& other) {
   const uint64_t final_n = n_ + other.n_;
   for (uint32_t i = other.levels_[0]; i < other.levels_[1]; i++) {
     const uint32_t index = internal_update();
-    new (&items_[index]) T(other.items_[i]);
+    new (&items_[index]) T(conditional_forward<FwdSk>(other.items_[i]));
   }
   if (other.num_levels_ >= 2) merge_higher_levels(other, final_n);
   n_ = final_n;
@@ -205,30 +200,6 @@ void kll_sketch<T, C, S, A>::merge(const kll_sketch& other) {
 }
 
 template<typename T, typename C, typename S, typename A>
-void kll_sketch<T, C, S, A>::merge(kll_sketch&& other) {
-  if (other.is_empty()) return;
-  if (m_ != other.m_) {
-    throw std::invalid_argument("incompatible M: " + std::to_string(m_) + " and " + std::to_string(other.m_));
-  }
-  if (is_empty()) {
-    min_value_ = new (allocator_.allocate(1)) T(std::move(*other.min_value_));
-    max_value_ = new (allocator_.allocate(1)) T(std::move(*other.max_value_));
-  } else {
-    if (C()(*other.min_value_, *min_value_)) *min_value_ = std::move(*other.min_value_);
-    if (C()(*max_value_, *other.max_value_)) *max_value_ = std::move(*other.max_value_);
-  }
-  const uint64_t final_n = n_ + other.n_;
-  for (uint32_t i = other.levels_[0]; i < other.levels_[1]; i++) {
-    const uint32_t index = internal_update();
-    new (&items_[index]) T(std::move(other.items_[i]));
-  }
-  if (other.num_levels_ >= 2) merge_higher_levels(std::forward<kll_sketch>(other), final_n);
-  n_ = final_n;
-  if (other.is_estimation_mode()) min_k_ = std::min(min_k_, other.min_k_);
-  assert_correct_total_weight();
-}
-
-template<typename T, typename C, typename S, typename A>
 bool kll_sketch<T, C, S, A>::is_empty() const {
   return n_ == 0;
 }

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