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/01/31 22:04:33 UTC
[datasketches-cpp] branch quantile_sketch_sorted_view updated: copy items of integral types instead of using pointers
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch quantile_sketch_sorted_view
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
The following commit(s) were added to refs/heads/quantile_sketch_sorted_view by this push:
new b9f6b55 copy items of integral types instead of using pointers
b9f6b55 is described below
commit b9f6b556fd8b28a57e8a1db429249ca8509dce77
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jan 31 14:04:12 2022 -0800
copy items of integral types instead of using pointers
---
common/include/quantile_sketch_sorted_view.hpp | 35 +++++++++++++++++++---
.../include/quantile_sketch_sorted_view_impl.hpp | 8 ++---
2 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/common/include/quantile_sketch_sorted_view.hpp b/common/include/quantile_sketch_sorted_view.hpp
index 13b5d2d..f7f383d 100755
--- a/common/include/quantile_sketch_sorted_view.hpp
+++ b/common/include/quantile_sketch_sorted_view.hpp
@@ -31,7 +31,7 @@ template<
>
class quantile_sketch_sorted_view {
public:
- using Entry = std::pair<const T*, uint64_t>;
+ using Entry = typename std::conditional<std::is_integral<T>::value, std::pair<T, uint64_t>, std::pair<const T*, uint64_t>>::type;
using AllocEntry = typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
using Container = std::vector<Entry, AllocEntry>;
@@ -52,10 +52,17 @@ public:
// makes sense only with cumulative weight
const T& get_quantile(double rank) const;
+private:
+ template<typename TT = T>
+ static inline const TT& deref_helper(const TT* t) { return *t; }
+
+ template<typename TT = T>
+ static inline const TT& deref_helper(const TT& t) { return t; }
+
template<typename C>
struct compare_pairs_by_first_ptr {
bool operator()(const Entry& a, const Entry& b) {
- return C()(*a.first, *b.first);
+ return C()(deref_helper(a.first), deref_helper(b.first));
}
};
@@ -65,6 +72,18 @@ public:
}
};
+ template<typename TT = T, typename std::enable_if<std::is_integral<TT>::value, int>::type = 0>
+ static inline T ref_helper(const T& t) { return t; }
+
+ template<typename TT = T, typename std::enable_if<!std::is_integral<TT>::value, int>::type = 0>
+ static inline const T* ref_helper(const T& t) { return std::addressof(t); }
+
+ template<typename TT = T, typename std::enable_if<std::is_integral<TT>::value, int>::type = 0>
+ static inline Entry make_dummy_entry(uint64_t weight) { return Entry(0, weight); }
+
+ template<typename TT = T, typename std::enable_if<!std::is_integral<TT>::value, int>::type = 0>
+ static inline Entry make_dummy_entry(uint64_t weight) { return Entry(nullptr, weight); }
+
uint64_t total_weight_;
Container entries_;
};
@@ -73,11 +92,15 @@ template<typename T, typename C, typename A>
class quantile_sketch_sorted_view<T, C, A>::const_iterator: public quantile_sketch_sorted_view<T, C, A>::Container::const_iterator {
public:
using Base = typename quantile_sketch_sorted_view<T, C, A>::Container::const_iterator;
- using value_type = std::pair<const T&, const uint64_t>;
+ using value_type = typename std::conditional<std::is_integral<T>::value, typename Base::value_type, std::pair<const T&, const uint64_t>>::type;
const_iterator(const Base& it): Base(it) {}
- const value_type operator*() const { return value_type(*(Base::operator*().first), Base::operator*().second); }
+ template<typename TT = T, typename std::enable_if<std::is_integral<TT>::value, int>::type = 0>
+ value_type operator*() const { return Base::operator*(); }
+
+ template<typename TT = T, typename std::enable_if<!std::is_integral<TT>::value, int>::type = 0>
+ value_type operator*() const { return value_type(*(Base::operator*().first), Base::operator*().second); }
class return_value_holder {
public:
@@ -87,6 +110,10 @@ public:
value_type value_;
};
+ template<typename TT = T, typename std::enable_if<std::is_integral<TT>::value, int>::type = 0>
+ const value_type* operator->() const { return Base::operator->(); }
+
+ template<typename TT = T, typename std::enable_if<!std::is_integral<TT>::value, int>::type = 0>
return_value_holder operator->() const { return **this; }
};
diff --git a/common/include/quantile_sketch_sorted_view_impl.hpp b/common/include/quantile_sketch_sorted_view_impl.hpp
index 3331d7f..cb043d5 100755
--- a/common/include/quantile_sketch_sorted_view_impl.hpp
+++ b/common/include/quantile_sketch_sorted_view_impl.hpp
@@ -35,7 +35,7 @@ template<typename Iterator>
void quantile_sketch_sorted_view<T, C, A>::add(Iterator first, Iterator last, uint64_t weight) {
if (entries_.capacity() < entries_.size() + std::distance(first, last)) entries_.reserve(entries_.size() + std::distance(first, last));
const size_t size_before = entries_.size();
- for (auto it = first; it != last; ++it) entries_.push_back(Entry(&*it, weight));
+ for (auto it = first; it != last; ++it) entries_.push_back(Entry(ref_helper(*it), weight));
if (size_before > 0) std::inplace_merge(entries_.begin(), entries_.begin() + size_before, entries_.end(), compare_pairs_by_first_ptr<C>());
}
@@ -55,9 +55,9 @@ template<typename T, typename C, typename A>
const T& quantile_sketch_sorted_view<T, C, A>::get_quantile(double rank) const {
if (total_weight_ == 0) throw std::invalid_argument("supported for cumulative weight only");
uint64_t weight = static_cast<uint64_t>(rank * total_weight_);
- auto it = std::lower_bound(entries_.begin(), entries_.end(), Entry(nullptr, weight), compare_pairs_by_second());
- if (it == entries_.end()) return *(entries_[entries_.size() - 1].first);
- return *(it->first);
+ auto it = std::lower_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second());
+ if (it == entries_.end()) return deref_helper(entries_[entries_.size() - 1].first);
+ return deref_helper(it->first);
}
template<typename T, typename C, typename A>
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org