Skip to content
Permalink
Browse files
copy items of integral types instead of using pointers
  • Loading branch information
AlexanderSaydakov committed Jan 31, 2022
1 parent 26c89ad commit b9f6b556fd8b28a57e8a1db429249ca8509dce77
Showing 2 changed files with 35 additions and 8 deletions.
@@ -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 @@ class quantile_sketch_sorted_view {
// 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 @@ class quantile_sketch_sorted_view {
}
};

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 @@ class quantile_sketch_sorted_view<T, C, A>::const_iterator: public quantile_sket
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; }
};

@@ -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>

0 comments on commit b9f6b55

Please sign in to comment.