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 2020/07/10 19:41:17 UTC

[incubator-datasketches-cpp] branch tuple_sketch updated: removed duplication

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

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


The following commit(s) were added to refs/heads/tuple_sketch by this push:
     new b1124b7  removed duplication
b1124b7 is described below

commit b1124b7aa4165c8e855f3987df47ad9b7300ba41
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Jul 10 12:40:59 2020 -0700

    removed duplication
---
 tuple/include/theta_union_base.hpp      |  5 ++---
 tuple/include/theta_union_base_impl.hpp | 34 +++++++++++----------------------
 2 files changed, 13 insertions(+), 26 deletions(-)

diff --git a/tuple/include/theta_union_base.hpp b/tuple/include/theta_union_base.hpp
index fbe4cf7..6e3823f 100644
--- a/tuple/include/theta_union_base.hpp
+++ b/tuple/include/theta_union_base.hpp
@@ -40,9 +40,8 @@ public:
 
   theta_union_base(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t seed, const Policy& policy);
 
-  // TODO: try to avoid duplication
-  void update(const Sketch& sketch);
-  void update(Sketch&& sketch);
+  template<typename FwdSketch>
+  void update(FwdSketch&& sketch);
 
   CompactSketch get_result(bool ordered = true) const;
 
diff --git a/tuple/include/theta_union_base_impl.hpp b/tuple/include/theta_union_base_impl.hpp
index 63c0f0f..4a62af2 100644
--- a/tuple/include/theta_union_base_impl.hpp
+++ b/tuple/include/theta_union_base_impl.hpp
@@ -28,30 +28,18 @@ table_(lg_cur_size, lg_nom_size, rf, p, seed),
 union_theta_(table_.theta_)
 {}
 
-template<typename EN, typename EK, typename P, typename S, typename CS, typename A>
-void theta_union_base<EN, EK, P, S, CS, A>::update(const S& sketch) {
-  if (sketch.is_empty()) return;
-  if (sketch.get_seed_hash() != compute_seed_hash(table_.seed_)) throw std::invalid_argument("seed hash mismatch");
-  table_.is_empty_ = false;
-  if (sketch.get_theta64() < union_theta_) union_theta_ = sketch.get_theta64();
-  for (const auto& entry: sketch) {
-    const uint64_t hash = EK()(entry);
-    if (hash < union_theta_) {
-      auto result = table_.find(hash);
-      if (!result.second) {
-        table_.insert(result.first, entry);
-      } else {
-        policy_(*result.first, entry);
-      }
-    } else {
-      if (sketch.is_ordered()) break; // early stop
-    }
-  }
-  if (table_.theta_ < union_theta_) union_theta_ = table_.theta_;
+template<typename Container, typename Element>
+using fwd_type = typename std::conditional<std::is_lvalue_reference<Container>::value,
+    Element, typename std::remove_reference<Element>::type&&>::type;
+
+template<typename Container, typename Element>
+fwd_type<Container, Element> forward_element(Element&& element) {
+  return std::forward<fwd_type<Container, Element>>(std::forward<Element>(element));
 }
 
 template<typename EN, typename EK, typename P, typename S, typename CS, typename A>
-void theta_union_base<EN, EK, P, S, CS, A>::update(S&& sketch) {
+template<typename SS>
+void theta_union_base<EN, EK, P, S, CS, A>::update(SS&& sketch) {
   if (sketch.is_empty()) return;
   if (sketch.get_seed_hash() != compute_seed_hash(table_.seed_)) throw std::invalid_argument("seed hash mismatch");
   table_.is_empty_ = false;
@@ -61,9 +49,9 @@ void theta_union_base<EN, EK, P, S, CS, A>::update(S&& sketch) {
     if (hash < union_theta_) {
       auto result = table_.find(hash);
       if (!result.second) {
-        table_.insert(result.first, std::move(entry));
+        table_.insert(result.first, forward_element<SS>(entry));
       } else {
-        policy_(*result.first, std::move(entry));
+        policy_(*result.first, forward_element<SS>(entry));
       }
     } else {
       if (sketch.is_ordered()) break; // early stop


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