Skip to content
Permalink
Browse files
return a value from get_quantile() for arithmetic types
  • Loading branch information
AlexanderSaydakov committed Feb 5, 2022
1 parent 54f3e51 commit 70bfb6167c53aee65461deaf8d7b77c40d5b8993
Showing 4 changed files with 7 additions and 5 deletions.
@@ -50,11 +50,12 @@ class quantile_sketch_sorted_view {
size_t size() const;

// makes sense only with cumulative weight
const T& get_quantile(double rank) const;
using quantile_return_type = typename std::conditional<std::is_arithmetic<T>::value, T, const T&>::type;
quantile_return_type get_quantile(double rank) const;

private:
static inline const T& deref_helper(const T* t) { return *t; }
static inline const T& deref_helper(const T& t) { return t; }
static inline T deref_helper(T t) { return t; }

struct compare_pairs_by_first_ptr {
bool operator()(const Entry& a, const Entry& b) {
@@ -52,7 +52,7 @@ void quantile_sketch_sorted_view<T, C, A>::convert_to_cummulative() {
}

template<typename T, typename C, typename A>
const T& quantile_sketch_sorted_view<T, C, A>::get_quantile(double rank) const {
auto quantile_sketch_sorted_view<T, C, A>::get_quantile(double rank) const -> quantile_return_type {
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(), make_dummy_entry<T>(weight), compare_pairs_by_second());
@@ -276,8 +276,9 @@ class kll_sketch {
*
* @return the approximation to the value at the given fraction
*/
using quantile_return_type = typename std::conditional<std::is_arithmetic<T>::value, T, const T&>::type;
template<bool inclusive = false>
const T& get_quantile(double fraction) const;
quantile_return_type get_quantile(double fraction) const;

/**
* This is a more efficient multiple-query version of get_quantile().
@@ -267,7 +267,7 @@ T kll_sketch<T, C, S, A>::get_max_value() const {

template<typename T, typename C, typename S, typename A>
template<bool inclusive>
const T& kll_sketch<T, C, S, A>::get_quantile(double rank) const {
auto kll_sketch<T, C, S, A>::get_quantile(double rank) const -> quantile_return_type {
if (is_empty()) return get_invalid_value();
if (rank == 0.0) return *min_value_;
if (rank == 1.0) return *max_value_;

0 comments on commit 70bfb61

Please sign in to comment.