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),
37 entries_.reserve(num + 2);
40template<
typename T,
typename C,
typename A>
41template<
typename Iterator>
42void quantiles_sorted_view<T, C, A>::add(Iterator first, Iterator last, uint64_t weight) {
43 if (first == last)
return;
44 const size_t size_before = entries_.size();
45 for (
auto it = first; it != last; ++it) entries_.emplace_back(ref_helper(*it), weight);
46 if (size_before > (min_added_ ? 1 : 0)) {
47 Container tmp(entries_.get_allocator());
48 tmp.reserve(entries_.capacity());
50 entries_.begin(), entries_.begin() + size_before,
51 entries_.begin() + size_before, entries_.end(),
52 std::back_inserter(tmp), compare_pairs_by_first(comparator_)
54 std::swap(tmp, entries_);
58template<
typename T,
typename C,
typename A>
59void quantiles_sorted_view<T, C, A>::convert_to_cummulative() {
60 for (
auto& entry: entries_) {
61 total_weight_ += entry.second;
62 entry.second = total_weight_;
66template<
typename T,
typename C,
typename A>
67void quantiles_sorted_view<T, C, A>::add_min(
const T& min_item) {
68 entries_.emplace_back(ref_helper(min_item), 1);
72template<
typename T,
typename C,
typename A>
73void quantiles_sorted_view<T, C, A>::convert_to_cummulative(
const T& max_item) {
74 if (min_added_) --entries_[1].second;
75 if (comparator_(deref_helper(entries_.back().first), max_item)) {
76 --entries_.back().second;
77 entries_.push_back(Entry(ref_helper(max_item), 1));
79 convert_to_cummulative();
82template<
typename T,
typename C,
typename A>
84 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
86 std::upper_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_))
87 : std::lower_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_));
89 if (it == entries_.begin())
return 0;
91 return static_cast<double>(it->second) / total_weight_;
94template<
typename T,
typename C,
typename A>
96 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
97 uint64_t weight =
static_cast<uint64_t
>(inclusive ? std::ceil(rank * total_weight_) : rank * total_weight_);
99 std::lower_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second())
100 : std::upper_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second());
101 if (it == entries_.end())
return deref_helper(entries_[entries_.size() - 1].first);
102 return deref_helper(it->first);
105template<
typename T,
typename C,
typename A>
107 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
108 check_split_points(split_points,
size);
109 vector_double ranks(entries_.get_allocator());
110 ranks.reserve(
size + 1);
111 for (uint32_t i = 0; i <
size; ++i) ranks.push_back(
get_rank(split_points[i], inclusive));
116template<
typename T,
typename C,
typename A>
118 auto buckets =
get_CDF(split_points,
size, inclusive);
119 for (uint32_t i =
size; i > 0; --i) {
120 buckets[i] -= buckets[i - 1];
125template<
typename T,
typename C,
typename A>
127 return const_iterator(entries_.begin(), entries_.begin());
130template<
typename T,
typename C,
typename A>
132 return const_iterator(entries_.end(), entries_.begin());
135template<
typename T,
typename C,
typename A>
137 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:106
size_t size() const
Definition quantiles_sorted_view_impl.hpp:136
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:117
typename std::conditional< std::is_arithmetic< T >::value, T, const T & >::type quantile_return_type
Quantile return type.
Definition quantiles_sorted_view.hpp:101
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:43
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:83
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:95
const_iterator begin() const
Iterator pointing to the first entry in the view.
Definition quantiles_sorted_view_impl.hpp:126
const_iterator end() const
Iterator pointing to the past-the-end entry in the view.
Definition quantiles_sorted_view_impl.hpp:131
DataSketches namespace.
Definition binomial_bounds.hpp:38