20 #ifndef QUANTILES_SORTED_VIEW_IMPL_HPP_
21 #define QUANTILES_SORTED_VIEW_IMPL_HPP_
29 template<
typename T,
typename C,
typename A>
30 quantiles_sorted_view<T, C, A>::quantiles_sorted_view(uint32_t num,
const C& comparator,
const A& allocator):
31 comparator_(comparator),
35 entries_.reserve(num);
38 template<
typename T,
typename C,
typename A>
39 template<
typename Iterator>
40 void quantiles_sorted_view<T, C, A>::add(Iterator first, Iterator last, uint64_t weight) {
41 const size_t size_before = entries_.size();
42 for (
auto it = first; it != last; ++it) entries_.push_back(Entry(ref_helper(*it), weight));
43 if (size_before > 0) {
44 Container tmp(entries_.get_allocator());
45 tmp.reserve(entries_.capacity());
47 entries_.begin(), entries_.begin() + size_before,
48 entries_.begin() + size_before, entries_.end(),
49 std::back_inserter(tmp), compare_pairs_by_first(comparator_)
51 std::swap(tmp, entries_);
55 template<
typename T,
typename C,
typename A>
56 void quantiles_sorted_view<T, C, A>::convert_to_cummulative() {
57 for (
auto& entry: entries_) {
58 total_weight_ += entry.second;
59 entry.second = total_weight_;
63 template<
typename T,
typename C,
typename A>
65 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
67 std::upper_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_))
68 : std::lower_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_));
70 if (it == entries_.begin())
return 0;
72 return static_cast<double>(it->second) / total_weight_;
75 template<
typename T,
typename C,
typename A>
77 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
78 uint64_t weight =
static_cast<uint64_t
>(inclusive ? std::ceil(rank * total_weight_) : rank * total_weight_);
80 std::lower_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second())
81 : std::upper_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second());
82 if (it == entries_.end())
return deref_helper(entries_[entries_.size() - 1].first);
83 return deref_helper(it->first);
86 template<
typename T,
typename C,
typename A>
88 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
89 check_split_points(split_points, size);
90 vector_double ranks(entries_.get_allocator());
91 ranks.reserve(size + 1);
92 for (uint32_t i = 0; i < size; ++i) ranks.push_back(get_rank(split_points[i], inclusive));
97 template<
typename T,
typename C,
typename A>
99 auto buckets = get_CDF(split_points, size, inclusive);
100 for (uint32_t i = size; i > 0; --i) {
101 buckets[i] -= buckets[i - 1];
106 template<
typename T,
typename C,
typename A>
108 return const_iterator(entries_.begin(), entries_.begin());
111 template<
typename T,
typename C,
typename A>
113 return const_iterator(entries_.end(), entries_.begin());
116 template<
typename T,
typename C,
typename A>
118 return entries_.
size();
Sorted view for quantiles sketches (REQ, KLL and Quantiles)
Definition: quantiles_sorted_view.hpp:38
size_t size() const
Definition: quantiles_sorted_view_impl.hpp:117
typename std::conditional< std::is_arithmetic< T >::value, T, const T & >::type quantile_return_type
Quantile return type.
Definition: quantiles_sorted_view.hpp:93
typename std::conditional< std::is_arithmetic< T >::value, std::pair< T, uint64_t >, std::pair< const T *, uint64_t > >::type Entry
Entry type.
Definition: quantiles_sorted_view.hpp:41
DataSketches namespace.
Definition: binomial_bounds.hpp:38