20#ifndef QUANTILES_SORTED_VIEW_IMPL_HPP_
21#define QUANTILES_SORTED_VIEW_IMPL_HPP_
29template<
typename T,
typename C,
typename A>
30quantiles_sorted_view<T, C, A>::quantiles_sorted_view(uint32_t num,
const C& comparator,
const A& allocator):
31comparator_(comparator),
35 entries_.reserve(num);
38template<
typename T,
typename C,
typename A>
39template<
typename Iterator>
40void 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_);
55template<
typename T,
typename C,
typename A>
56void quantiles_sorted_view<T, C, A>::convert_to_cummulative() {
57 for (
auto& entry: entries_) {
58 total_weight_ += entry.second;
59 entry.second = total_weight_;
63template<
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_;
75template<
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);
86template<
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));
97template<
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];
106template<
typename T,
typename C,
typename A>
108 return const_iterator(entries_.begin(), entries_.begin());
111template<
typename T,
typename C,
typename A>
113 return const_iterator(entries_.end(), entries_.begin());
116template<
typename T,
typename C,
typename A>
118 return entries_.size();
vector_double get_CDF(const T *split_points, uint32_t size, bool inclusive=true) const
Returns an approximation to the Cumulative Distribution Function (CDF), which is the cumulative analo...
Definition quantiles_sorted_view_impl.hpp:87
size_t size() const
Definition quantiles_sorted_view_impl.hpp:117
vector_double get_PMF(const T *split_points, uint32_t size, bool inclusive=true) const
Returns an approximation to the Probability Mass Function (PMF) of the input stream given a set of sp...
Definition quantiles_sorted_view_impl.hpp:98
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
double get_rank(const T &item, bool inclusive=true) const
Returns an approximation to the normalized rank of the given item.
Definition quantiles_sorted_view_impl.hpp:64
quantile_return_type get_quantile(double rank, bool inclusive=true) const
Returns an item from the sketch that is the best approximation to an item from the original stream wi...
Definition quantiles_sorted_view_impl.hpp:76
const_iterator begin() const
Iterator pointing to the first entry in the view.
Definition quantiles_sorted_view_impl.hpp:107
const_iterator end() const
Iterator pointing to the past-the-end entry in the view.
Definition quantiles_sorted_view_impl.hpp:112
DataSketches namespace.
Definition binomial_bounds.hpp:38