|
27 | 27 | #include "ddsketch.hpp" |
28 | 28 | #include "store_factory.hpp" |
29 | 29 | namespace datasketches { |
30 | | - |
31 | 30 | template<class Store, class Mapping> |
32 | 31 | DDSketch<Store, Mapping>::DDSketch(const double& relative_accuracy): DDSketch(Mapping(relative_accuracy)) {} |
33 | 32 |
|
@@ -201,6 +200,29 @@ double DDSketch<Store, Mapping>::get_quantile(const double& rank, const double& |
201 | 200 | throw std::invalid_argument("no such element"); |
202 | 201 | } |
203 | 202 |
|
| 203 | +template<class Store, class Mapping> |
| 204 | +typename DDSketch<Store, Mapping>::vector_double DDSketch<Store, Mapping>::get_CDF(const T* split_points, uint32_t size) const { |
| 205 | + check_split_pints(split_points, size); |
| 206 | + vector_double ranks; |
| 207 | + ranks.reserve(size + 1); |
| 208 | + for (uint32_t i = 0; i < size; ++i) { |
| 209 | + ranks.push_back(get_rank(split_points[i])); |
| 210 | + } |
| 211 | + ranks.push_back(1.0); |
| 212 | + return ranks; |
| 213 | +} |
| 214 | + |
| 215 | +template<class Store, class Mapping> |
| 216 | +typename DDSketch<Store, Mapping>::vector_double DDSketch<Store, Mapping>::get_PMF(const typename DDSketch<Store, Mapping>::T* split_points, uint32_t size) const { |
| 217 | + vector_double buckets = get_CDF(split_points, size); |
| 218 | + for (uint32_t i = size; i > 0; --i) { |
| 219 | + buckets[i] -= buckets[i - 1]; |
| 220 | + } |
| 221 | + |
| 222 | + return buckets; |
| 223 | +} |
| 224 | + |
| 225 | + |
204 | 226 | template<class Store, class Mapping> |
205 | 227 | void DDSketch<Store, Mapping>::serialize(std::ostream& os) const { |
206 | 228 | index_mapping.serialize(os); |
@@ -279,6 +301,18 @@ bool DDSketch<Store, Mapping>::operator==(const DDSketch<Store, Mapping>& other) |
279 | 301 | max_indexed_value == other.max_indexed_value; |
280 | 302 | } |
281 | 303 |
|
| 304 | +template<class Store, class Mapping> |
| 305 | +void DDSketch<Store, Mapping>::check_split_pints(const T *items, uint32_t size) { |
| 306 | + for (uint32_t i = 0; i < size ; i++) { |
| 307 | + if (std::isnan(items[i])) { |
| 308 | + throw std::invalid_argument("Values must not be NaN"); |
| 309 | + } |
| 310 | + if ((i < (size - 1)) && !(items[i] < items[i + 1])) { |
| 311 | + throw std::invalid_argument("Values must be unique and monotonically increasing"); |
| 312 | + } |
| 313 | + } |
| 314 | +} |
| 315 | + |
282 | 316 | } |
283 | 317 |
|
284 | 318 | #endif |
0 commit comments