datasketches-cpp
Loading...
Searching...
No Matches
quantiles_sorted_view.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_HPP_
21#define QUANTILES_SORTED_VIEW_HPP_
22
23#include <functional>
24#include <cmath>
25
26#include "common_defs.hpp"
27
28namespace datasketches {
29
35template<
36 typename T,
37 typename Comparator, // strict weak ordering function (see C++ named requirements: Compare)
38 typename Allocator
39>
40class quantiles_sorted_view {
41public:
43 using Entry = typename std::conditional<std::is_arithmetic<T>::value, std::pair<T, uint64_t>, std::pair<const T*, uint64_t>>::type;
44 using AllocEntry = typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
45 using Container = std::vector<Entry, AllocEntry>;
46
48 quantiles_sorted_view(uint32_t num, const Comparator& comparator, const Allocator& allocator);
49
51 template<typename Iterator>
52 void add(Iterator begin, Iterator end, uint64_t weight);
53
55 void convert_to_cummulative();
56
58 void add_min(const T& min_item);
59
61 void convert_to_cummulative(const T& max_item);
62
63 class const_iterator;
64
70 const_iterator begin() const;
71
78 const_iterator end() const;
79
81 size_t size() const;
82
95 double get_rank(const T& item, bool inclusive = true) const;
96
101 using quantile_return_type = typename std::conditional<std::is_arithmetic<T>::value, T, const T&>::type;
102
114 quantile_return_type get_quantile(double rank, bool inclusive = true) const;
115
116 using vector_double = std::vector<double, typename std::allocator_traits<Allocator>::template rebind_alloc<double>>;
117
140 vector_double get_CDF(const T* split_points, uint32_t size, bool inclusive = true) const;
141
161 vector_double get_PMF(const T* split_points, uint32_t size, bool inclusive = true) const;
162
163private:
164 Comparator comparator_;
165 uint64_t total_weight_;
166 bool min_added_;
167 Container entries_;
168
169 static inline const T& deref_helper(const T* t) { return *t; }
170 static inline T deref_helper(T t) { return t; }
171
172 struct compare_pairs_by_first {
173 explicit compare_pairs_by_first(const Comparator& comparator): comparator_(comparator) {}
174 bool operator()(const Entry& a, const Entry& b) const {
175 return comparator_(deref_helper(a.first), deref_helper(b.first));
176 }
177 Comparator comparator_;
178 };
179
180 struct compare_pairs_by_second {
181 bool operator()(const Entry& a, const Entry& b) const {
182 return a.second < b.second;
183 }
184 };
185
186 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
187 static inline T ref_helper(const T& t) { return t; }
188
189 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
190 static inline const T* ref_helper(const T& t) { return std::addressof(t); }
191
192 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
193 static inline Entry make_dummy_entry(uint64_t weight) { return Entry(0, weight); }
194
195 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
196 static inline Entry make_dummy_entry(uint64_t weight) { return Entry(nullptr, weight); }
197
198 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
199 static inline void check_split_points(const T* items, uint32_t size) {
200 for (uint32_t i = 0; i < size ; i++) {
201 if (std::isnan(items[i])) {
202 throw std::invalid_argument("Values must not be NaN");
203 }
204 if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
205 throw std::invalid_argument("Values must be unique and monotonically increasing");
206 }
207 }
208 }
209
210 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
211 static inline void check_split_points(const T* items, uint32_t size) {
212 for (uint32_t i = 0; i < size ; i++) {
213 if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
214 throw std::invalid_argument("Items must be unique and monotonically increasing");
215 }
216 }
217 }
218};
219
220template<typename T, typename C, typename A>
221class quantiles_sorted_view<T, C, A>::const_iterator: public quantiles_sorted_view<T, C, A>::Container::const_iterator {
222public:
223 using Base = typename quantiles_sorted_view<T, C, A>::Container::const_iterator;
224 using value_type = typename std::conditional<std::is_arithmetic<T>::value, typename Base::value_type, std::pair<const T&, const uint64_t>>::type;
225
226 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
227 const value_type operator*() const { return Base::operator*(); }
228
229 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
230 const value_type operator*() const { return value_type(*(Base::operator*().first), Base::operator*().second); }
231
232 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
233 const value_type* operator->() const { return Base::operator->(); }
234
235 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
236 const return_value_holder<value_type> operator->() const { return **this; }
237
238 uint64_t get_weight() const {
239 if (*this == begin) return Base::operator*().second;
240 return Base::operator*().second - (*this - 1).operator*().second;
241 }
242
243 uint64_t get_cumulative_weight(bool inclusive = true) const {
244 return inclusive ? Base::operator*().second : Base::operator*().second - get_weight();
245 }
246
247private:
248 Base begin;
249
250 friend class quantiles_sorted_view<T, C, A>;
251 const_iterator(const Base& it, const Base& begin): Base(it), begin(begin) {}
252};
253
254} /* namespace datasketches */
255
256#include "quantiles_sorted_view_impl.hpp"
257
258#endif
Sorted view for quantiles sketches (REQ, KLL and Quantiles).
Definition quantiles_sorted_view.hpp:40
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