datasketches-cpp
Loading...
Searching...
No Matches
quantiles_sorted_view_impl.hpp
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef QUANTILES_SORTED_VIEW_IMPL_HPP_
21#define QUANTILES_SORTED_VIEW_IMPL_HPP_
22
23#include <algorithm>
24#include <stdexcept>
25#include <cmath>
26
27namespace datasketches {
28
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),
32total_weight_(0),
33min_added_(false),
34entries_(allocator)
35{
36 if (num > 0) // the if exists to optimise for empty sketches
37 entries_.reserve(num + 2); // room for the min and max
38}
39
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)) { // an added min is smaller than all items, so it needs no merge
47 Container tmp(entries_.get_allocator());
48 tmp.reserve(entries_.capacity());
49 std::merge(
50 entries_.begin(), entries_.begin() + size_before,
51 entries_.begin() + size_before, entries_.end(),
52 std::back_inserter(tmp), compare_pairs_by_first(comparator_)
53 );
54 std::swap(tmp, entries_);
55 }
56}
57
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_;
63 }
64}
65
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);
69 min_added_ = true;
71
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));
78 }
79 convert_to_cummulative();
80}
82template<typename T, typename C, typename A>
83double quantiles_sorted_view<T, C, A>::get_rank(const T& item, bool inclusive) const {
84 if (entries_.empty()) throw std::runtime_error("operation is undefined for an empty sketch");
85 auto it = inclusive ?
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_));
88 // we need item just before
89 if (it == entries_.begin()) return 0;
90 --it;
91 return static_cast<double>(it->second) / total_weight_;
92}
93
94template<typename T, typename C, typename A>
95auto quantiles_sorted_view<T, C, A>::get_quantile(double rank, bool inclusive) const -> quantile_return_type {
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_);
98 auto it = inclusive ?
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);
103}
104
105template<typename T, typename C, typename A>
106auto quantiles_sorted_view<T, C, A>::get_CDF(const T* split_points, uint32_t size, bool inclusive) const -> vector_double {
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));
112 ranks.push_back(1);
113 return ranks;
115
116template<typename T, typename C, typename A>
117auto quantiles_sorted_view<T, C, A>::get_PMF(const T* split_points, uint32_t size, bool inclusive) const -> vector_double {
118 auto buckets = get_CDF(split_points, size, inclusive);
119 for (uint32_t i = size; i > 0; --i) {
120 buckets[i] -= buckets[i - 1];
121 }
122 return buckets;
123}
124
125template<typename T, typename C, typename A>
126auto quantiles_sorted_view<T, C, A>::begin() const -> const_iterator {
127 return const_iterator(entries_.begin(), entries_.begin());
128}
129
130template<typename T, typename C, typename A>
131auto quantiles_sorted_view<T, C, A>::end() const -> const_iterator {
132 return const_iterator(entries_.end(), entries_.begin());
133}
134
135template<typename T, typename C, typename A>
137 return entries_.size();
138}
139
140} /* namespace datasketches */
141
142#endif
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