datasketches 0.2.3 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +8 -8
  4. data/ext/datasketches/kll_wrapper.cpp +7 -3
  5. data/ext/datasketches/theta_wrapper.cpp +20 -4
  6. data/lib/datasketches/version.rb +1 -1
  7. data/vendor/datasketches-cpp/CMakeLists.txt +25 -5
  8. data/vendor/datasketches-cpp/MANIFEST.in +3 -0
  9. data/vendor/datasketches-cpp/NOTICE +6 -5
  10. data/vendor/datasketches-cpp/README.md +76 -9
  11. data/vendor/datasketches-cpp/cmake/DataSketchesConfig.cmake.in +10 -0
  12. data/vendor/datasketches-cpp/common/CMakeLists.txt +18 -13
  13. data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +1 -0
  14. data/vendor/datasketches-cpp/common/include/common_defs.hpp +14 -0
  15. data/vendor/datasketches-cpp/{kll → common}/include/kolmogorov_smirnov.hpp +5 -3
  16. data/vendor/datasketches-cpp/{kll → common}/include/kolmogorov_smirnov_impl.hpp +13 -16
  17. data/vendor/datasketches-cpp/common/include/quantile_sketch_sorted_view.hpp +121 -0
  18. data/vendor/datasketches-cpp/common/include/quantile_sketch_sorted_view_impl.hpp +91 -0
  19. data/vendor/datasketches-cpp/common/test/test_type.hpp +2 -0
  20. data/vendor/datasketches-cpp/cpc/CMakeLists.txt +15 -35
  21. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +3 -1
  22. data/vendor/datasketches-cpp/cpc/include/cpc_confidence.hpp +1 -0
  23. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +5 -3
  24. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +10 -6
  25. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_allocation_test.cpp +17 -0
  26. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +1 -0
  27. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +2 -0
  28. data/vendor/datasketches-cpp/fi/CMakeLists.txt +5 -15
  29. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +37 -5
  30. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +29 -11
  31. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +2 -1
  32. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +1 -0
  33. data/vendor/datasketches-cpp/hll/CMakeLists.txt +33 -56
  34. data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +2 -0
  35. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +1 -0
  36. data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +2 -2
  37. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +1 -0
  38. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +6 -4
  39. data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +2 -0
  40. data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +2 -0
  41. data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +1 -0
  42. data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +1 -0
  43. data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +2 -0
  44. data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +1 -0
  45. data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +59 -0
  46. data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +2 -0
  47. data/vendor/datasketches-cpp/hll/test/TablesTest.cpp +1 -0
  48. data/vendor/datasketches-cpp/kll/CMakeLists.txt +5 -19
  49. data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +0 -4
  50. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +5 -2
  51. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +108 -41
  52. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +150 -132
  53. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +165 -31
  54. data/vendor/datasketches-cpp/kll/test/kolmogorov_smirnov_test.cpp +1 -1
  55. data/vendor/datasketches-cpp/pyproject.toml +1 -1
  56. data/vendor/datasketches-cpp/python/CMakeLists.txt +8 -1
  57. data/vendor/datasketches-cpp/python/README.md +13 -9
  58. data/vendor/datasketches-cpp/python/src/datasketches.cpp +4 -0
  59. data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +6 -1
  60. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +48 -13
  61. data/vendor/datasketches-cpp/python/src/ks_wrapper.cpp +68 -0
  62. data/vendor/datasketches-cpp/python/src/quantiles_wrapper.cpp +240 -0
  63. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +9 -2
  64. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +1 -0
  65. data/vendor/datasketches-cpp/python/tests/kll_test.py +10 -4
  66. data/vendor/datasketches-cpp/python/tests/quantiles_test.py +126 -0
  67. data/vendor/datasketches-cpp/quantiles/CMakeLists.txt +42 -0
  68. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch.hpp +656 -0
  69. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch_impl.hpp +1373 -0
  70. data/vendor/datasketches-cpp/quantiles/test/CMakeLists.txt +44 -0
  71. data/vendor/datasketches-cpp/quantiles/test/Qk128_n1000_v0.3.0.sk +0 -0
  72. data/vendor/datasketches-cpp/quantiles/test/Qk128_n1000_v0.6.0.sk +0 -0
  73. data/vendor/datasketches-cpp/quantiles/test/Qk128_n1000_v0.8.0.sk +0 -0
  74. data/vendor/datasketches-cpp/quantiles/test/Qk128_n1000_v0.8.3.sk +0 -0
  75. data/vendor/datasketches-cpp/quantiles/test/Qk128_n50_v0.3.0.sk +0 -0
  76. data/vendor/datasketches-cpp/quantiles/test/Qk128_n50_v0.6.0.sk +0 -0
  77. data/vendor/datasketches-cpp/quantiles/test/Qk128_n50_v0.8.0.sk +0 -0
  78. data/vendor/datasketches-cpp/quantiles/test/Qk128_n50_v0.8.3.sk +0 -0
  79. data/vendor/datasketches-cpp/quantiles/test/kolmogorov_smirnov_test.cpp +110 -0
  80. data/vendor/datasketches-cpp/quantiles/test/quantiles_compatibility_test.cpp +129 -0
  81. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_test.cpp +975 -0
  82. data/vendor/datasketches-cpp/req/CMakeLists.txt +6 -21
  83. data/vendor/datasketches-cpp/req/include/req_common.hpp +0 -5
  84. data/vendor/datasketches-cpp/req/include/req_compactor.hpp +6 -0
  85. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +30 -2
  86. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +73 -23
  87. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +95 -63
  88. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +74 -3
  89. data/vendor/datasketches-cpp/sampling/CMakeLists.txt +5 -9
  90. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +44 -7
  91. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +44 -33
  92. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +41 -6
  93. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +33 -15
  94. data/vendor/datasketches-cpp/sampling/test/var_opt_allocation_test.cpp +2 -2
  95. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +1 -0
  96. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +1 -0
  97. data/vendor/datasketches-cpp/setup.py +1 -1
  98. data/vendor/datasketches-cpp/theta/CMakeLists.txt +26 -45
  99. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_sampled_sets.hpp +1 -0
  100. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +92 -23
  101. data/vendor/datasketches-cpp/theta/include/theta_helpers.hpp +15 -0
  102. data/vendor/datasketches-cpp/theta/include/theta_intersection_base_impl.hpp +7 -6
  103. data/vendor/datasketches-cpp/theta/include/theta_set_difference_base_impl.hpp +3 -2
  104. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +32 -15
  105. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +150 -93
  106. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +6 -1
  107. data/vendor/datasketches-cpp/theta/include/theta_union_base.hpp +3 -1
  108. data/vendor/datasketches-cpp/theta/include/theta_union_base_impl.hpp +9 -2
  109. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +8 -5
  110. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base.hpp +5 -4
  111. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base_impl.hpp +34 -9
  112. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +1 -0
  113. data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +2 -0
  114. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java_v1.sk +0 -0
  115. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java_v2.sk +0 -0
  116. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java_v1.sk +0 -0
  117. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java_v2.sk +0 -0
  118. data/vendor/datasketches-cpp/theta/test/theta_compact_exact_from_java.sk +0 -0
  119. data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +2 -0
  120. data/vendor/datasketches-cpp/theta/test/theta_setop_test.cpp +446 -0
  121. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +429 -1
  122. data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +25 -11
  123. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +18 -33
  124. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +1 -1
  125. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +3 -3
  126. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +1 -1
  127. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +3 -3
  128. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +29 -9
  129. data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +34 -14
  130. data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +6 -1
  131. data/vendor/datasketches-cpp/tuple/include/tuple_union_impl.hpp +8 -3
  132. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +16 -0
  133. data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +1 -0
  134. data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +1 -0
  135. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +46 -8
  136. data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +8 -0
  137. metadata +33 -12
  138. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +0 -75
  139. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +0 -184
  140. data/vendor/datasketches-cpp/req/include/req_quantile_calculator.hpp +0 -69
  141. data/vendor/datasketches-cpp/req/include/req_quantile_calculator_impl.hpp +0 -60
  142. data/vendor/datasketches-cpp/theta/test/theta_update_empty_from_java.sk +0 -0
  143. data/vendor/datasketches-cpp/theta/test/theta_update_estimation_from_java.sk +0 -0
@@ -23,8 +23,9 @@
23
23
  #include <functional>
24
24
  #include <memory>
25
25
  #include <vector>
26
+ #include <cmath>
26
27
 
27
- #include "kll_quantile_calculator.hpp"
28
+ #include "quantile_sketch_sorted_view.hpp"
28
29
  #include "common_defs.hpp"
29
30
  #include "serde.hpp"
30
31
 
@@ -35,7 +36,7 @@ namespace datasketches {
35
36
  * and nearly optimal accuracy per retained item.
36
37
  * See <a href="https://arxiv.org/abs/1603.05346v2">Optimal Quantile Approximation in Streams</a>.
37
38
  *
38
- * <p>This is a stochastic streaming sketch that enables near-real time analysis of the
39
+ * <p>This is a stochastic streaming sketch that enables near real-time analysis of the
39
40
  * approximate distribution of values from a very large stream in a single pass, requiring only
40
41
  * that the values are comparable.
41
42
  * The analysis is obtained using <i>get_quantile()</i> or <i>get_quantiles()</i> functions or the
@@ -157,14 +158,19 @@ namespace kll_constants {
157
158
  const uint16_t DEFAULT_K = 200;
158
159
  }
159
160
 
160
- template <typename T, typename C = std::less<T>, typename S = serde<T>, typename A = std::allocator<T>>
161
+ template <
162
+ typename T,
163
+ typename C = std::less<T>, // strict weak ordering function (see C++ named requirements: Compare)
164
+ typename S = serde<T>, // deprecated, to be removed in the next major version
165
+ typename A = std::allocator<T>
166
+ >
161
167
  class kll_sketch {
162
168
  public:
163
169
  using value_type = T;
164
170
  using comparator = C;
165
171
 
166
172
  static const uint8_t DEFAULT_M = 8;
167
- // TODO: Redundant and deprecated. Will be remove din next major version.
173
+ // TODO: Redundant and deprecated. Will be removed in next major version.
168
174
  static const uint16_t DEFAULT_K = kll_constants::DEFAULT_K;
169
175
  static const uint16_t MIN_K = DEFAULT_M;
170
176
  static const uint16_t MAX_K = (1 << 16) - 1;
@@ -176,33 +182,27 @@ class kll_sketch {
176
182
  kll_sketch& operator=(const kll_sketch& other);
177
183
  kll_sketch& operator=(kll_sketch&& other);
178
184
 
179
- /**
180
- * Updates this sketch with the given data item.
181
- * This method takes lvalue.
182
- * @param value an item from a stream of items
185
+ /*
186
+ * Type converting constructor.
187
+ * @param other sketch of a different type
188
+ * @param allocator instance of an Allocator
183
189
  */
184
- void update(const T& value);
190
+ template<typename TT, typename CC, typename SS, typename AA>
191
+ explicit kll_sketch(const kll_sketch<TT, CC, SS, AA>& other, const A& allocator = A());
185
192
 
186
193
  /**
187
194
  * Updates this sketch with the given data item.
188
- * This method takes rvalue.
189
195
  * @param value an item from a stream of items
190
196
  */
191
- void update(T&& value);
192
-
193
- /**
194
- * Merges another sketch into this one.
195
- * This method takes lvalue.
196
- * @param other sketch to merge into this one
197
- */
198
- void merge(const kll_sketch& other);
197
+ template<typename FwdT>
198
+ void update(FwdT&& value);
199
199
 
200
200
  /**
201
201
  * Merges another sketch into this one.
202
- * This method takes rvalue.
203
202
  * @param other sketch to merge into this one
204
203
  */
205
- void merge(kll_sketch&& other);
204
+ template<typename FwdSk>
205
+ void merge(FwdSk&& other);
206
206
 
207
207
  /**
208
208
  * Returns true if this sketch is empty.
@@ -250,6 +250,12 @@ class kll_sketch {
250
250
  */
251
251
  T get_max_value() const;
252
252
 
253
+ /**
254
+ * Returns an instance of the comparator for this sketch.
255
+ * @return comparator
256
+ */
257
+ C get_comparator() const;
258
+
253
259
  /**
254
260
  * Returns an approximation to the value of the data item
255
261
  * that would be preceded by the given fraction of a hypothetical sorted
@@ -266,10 +272,13 @@ class kll_sketch {
266
272
  * These are also called normalized ranks or fractional ranks.
267
273
  * If fraction = 0.0, the true minimum value of the stream is returned.
268
274
  * If fraction = 1.0, the true maximum value of the stream is returned.
275
+ * If the parameter inclusive=true, the given rank is considered inclusive (includes the weight of an item)
269
276
  *
270
277
  * @return the approximation to the value at the given fraction
271
278
  */
272
- T get_quantile(double fraction) const;
279
+ using quantile_return_type = typename quantile_sketch_sorted_view<T, C, A>::quantile_return_type;
280
+ template<bool inclusive = false>
281
+ quantile_return_type get_quantile(double fraction) const;
273
282
 
274
283
  /**
275
284
  * This is a more efficient multiple-query version of get_quantile().
@@ -285,10 +294,12 @@ class kll_sketch {
285
294
  * @param fractions given array of fractional positions in the hypothetical sorted stream.
286
295
  * These are also called normalized ranks or fractional ranks.
287
296
  * These fractions must be in the interval [0.0, 1.0], inclusive.
297
+ * If the parameter inclusive=true, the given fractions are considered inclusive (include weights of items)
288
298
  *
289
299
  * @return array of approximations to the given fractions in the same order as given fractions
290
300
  * in the input array.
291
301
  */
302
+ template<bool inclusive = false>
292
303
  std::vector<T, A> get_quantiles(const double* fractions, uint32_t size) const;
293
304
 
294
305
  /**
@@ -304,11 +315,15 @@ class kll_sketch {
304
315
  *
305
316
  * @return array of approximations to the given number of evenly-spaced fractional ranks.
306
317
  */
318
+ template<bool inclusive = false>
307
319
  std::vector<T, A> get_quantiles(uint32_t num) const;
308
320
 
309
321
  /**
310
322
  * Returns an approximation to the normalized (fractional) rank of the given value from 0 to 1,
311
323
  * inclusive.
324
+ * With the template parameter inclusive=true the weight of the given value is included into the rank.
325
+ * Otherwise the rank equals the sum of the weights of all values that are less than the given value
326
+ * according to the comparator C.
312
327
  *
313
328
  * <p>The resulting approximation has a probabilistic guarantee that can be obtained from the
314
329
  * get_normalized_rank_error(false) function.
@@ -318,6 +333,7 @@ class kll_sketch {
318
333
  * @param value to be ranked
319
334
  * @return an approximate rank of the given value
320
335
  */
336
+ template<bool inclusive = false>
321
337
  double get_rank(const T& value) const;
322
338
 
323
339
  /**
@@ -338,9 +354,12 @@ class kll_sketch {
338
354
  *
339
355
  * @return an array of m+1 doubles each of which is an approximation
340
356
  * to the fraction of the input stream values (the mass) that fall into one of those intervals.
341
- * The definition of an "interval" is inclusive of the left split point and exclusive of the right
342
- * split point, with the exception that the last interval will include maximum value.
357
+ * If the template parameter inclusive=false, the definition of an "interval" is inclusive of the left split point and exclusive of the right
358
+ * split point, with the exception that the last interval will include the maximum value.
359
+ * If the template parameter inclusive=true, the definition of an "interval" is exclusive of the left split point and inclusive of the right
360
+ * split point.
343
361
  */
362
+ template<bool inclusive = false>
344
363
  vector_d<A> get_PMF(const T* split_points, uint32_t size) const;
345
364
 
346
365
  /**
@@ -364,6 +383,7 @@ class kll_sketch {
364
383
  * CDF array is the sum of the returned values in positions 0 through j of the returned PMF
365
384
  * array.
366
385
  */
386
+ template<bool inclusive = false>
367
387
  vector_d<A> get_CDF(const T* split_points, uint32_t size) const;
368
388
 
369
389
  /**
@@ -378,18 +398,20 @@ class kll_sketch {
378
398
  /**
379
399
  * Computes size needed to serialize the current state of the sketch.
380
400
  * This version is for fixed-size arithmetic types (integral and floating point).
401
+ * @param serde instance of a SerDe
381
402
  * @return size in bytes needed to serialize this sketch
382
403
  */
383
- template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
384
- size_t get_serialized_size_bytes() const;
404
+ template<typename TT = T, typename SerDe = S, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
405
+ size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
385
406
 
386
407
  /**
387
408
  * Computes size needed to serialize the current state of the sketch.
388
409
  * This version is for all other types and can be expensive since every item needs to be looked at.
410
+ * @param serde instance of a SerDe
389
411
  * @return size in bytes needed to serialize this sketch
390
412
  */
391
- template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
392
- size_t get_serialized_size_bytes() const;
413
+ template<typename TT = T, typename SerDe = S, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
414
+ size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
393
415
 
394
416
  /**
395
417
  * Returns upper bound on the serialized size of a sketch given a parameter <em>k</em> and stream
@@ -421,8 +443,10 @@ class kll_sketch {
421
443
  /**
422
444
  * This method serializes the sketch into a given stream in a binary form
423
445
  * @param os output stream
446
+ * @param instance of a SerDe
424
447
  */
425
- void serialize(std::ostream& os) const;
448
+ template<typename SerDe = S>
449
+ void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
426
450
 
427
451
  // This is a convenience alias for users
428
452
  // The type returned by the following serialize method
@@ -434,23 +458,53 @@ class kll_sketch {
434
458
  * It is a blank space of a given size.
435
459
  * This header is used in Datasketches PostgreSQL extension.
436
460
  * @param header_size_bytes space to reserve in front of the sketch
461
+ * @param instance of a SerDe
462
+ * @return serialized sketch as a vector of bytes
463
+ */
464
+ template<typename SerDe = S>
465
+ vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
466
+
467
+ /**
468
+ * This method deserializes a sketch from a given stream.
469
+ * @param is input stream
470
+ * @param allocator instance of an Allocator
471
+ * @return an instance of a sketch
472
+ *
473
+ * Deprecated, to be removed in the next major version
437
474
  */
438
- vector_bytes serialize(unsigned header_size_bytes = 0) const;
475
+ static kll_sketch deserialize(std::istream& is, const A& allocator = A());
439
476
 
440
477
  /**
441
478
  * This method deserializes a sketch from a given stream.
442
479
  * @param is input stream
480
+ * @param serde instance of a SerDe
481
+ * @param allocator instance of an Allocator
482
+ * @return an instance of a sketch
483
+ */
484
+ template<typename SerDe = S>
485
+ static kll_sketch deserialize(std::istream& is, const SerDe& sd = SerDe(), const A& allocator = A());
486
+
487
+ /**
488
+ * This method deserializes a sketch from a given array of bytes.
489
+ * @param bytes pointer to the array of bytes
490
+ * @param size the size of the array
491
+ * @param allocator instance of an Allocator
443
492
  * @return an instance of a sketch
493
+ *
494
+ * Deprecated, to be removed in the next major version
444
495
  */
445
- static kll_sketch<T, C, S, A> deserialize(std::istream& is, const A& allocator = A());
496
+ static kll_sketch deserialize(const void* bytes, size_t size, const A& allocator = A());
446
497
 
447
498
  /**
448
499
  * This method deserializes a sketch from a given array of bytes.
449
500
  * @param bytes pointer to the array of bytes
450
501
  * @param size the size of the array
502
+ * @param serde instance of a SerDe
503
+ * @param allocator instance of an Allocator
451
504
  * @return an instance of a sketch
452
505
  */
453
- static kll_sketch<T, C, S, A> deserialize(const void* bytes, size_t size, const A& allocator = A());
506
+ template<typename SerDe = S>
507
+ static kll_sketch deserialize(const void* bytes, size_t size, const SerDe& sd = SerDe(), const A& allocator = A());
454
508
 
455
509
  /*
456
510
  * Gets the normalized rank error given k and pmf.
@@ -472,6 +526,9 @@ class kll_sketch {
472
526
  const_iterator begin() const;
473
527
  const_iterator end() const;
474
528
 
529
+ template<bool inclusive = false>
530
+ quantile_sketch_sorted_view<T, C, A> get_sorted_view(bool cumulative) const;
531
+
475
532
  #ifdef KLL_VALIDATION
476
533
  uint8_t get_num_levels() { return num_levels_; }
477
534
  uint32_t* get_levels() { return levels_; }
@@ -480,7 +537,7 @@ class kll_sketch {
480
537
 
481
538
  private:
482
539
  /* Serialized sketch layout:
483
- * Adr:
540
+ * Addr:
484
541
  * || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
485
542
  * 0 || unused | M |--------K--------| Flags | FamID | SerVer | PreambleInts |
486
543
  * || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
@@ -515,8 +572,6 @@ class kll_sketch {
515
572
  T* max_value_;
516
573
  bool is_level_zero_sorted_;
517
574
 
518
- friend class kll_quantile_calculator<T, C, A>;
519
-
520
575
  // for deserialization
521
576
  class item_deleter;
522
577
  class items_deleter;
@@ -535,15 +590,21 @@ class kll_sketch {
535
590
  uint8_t find_level_to_compact() const;
536
591
  void add_empty_top_level_to_completely_full_sketch();
537
592
  void sort_level_zero();
538
- std::unique_ptr<kll_quantile_calculator<T, C, A>, std::function<void(kll_quantile_calculator<T, C, A>*)>> get_quantile_calculator();
593
+
594
+ template<bool inclusive>
539
595
  vector_d<A> get_PMF_or_CDF(const T* split_points, uint32_t size, bool is_CDF) const;
596
+ template<bool inclusive>
540
597
  void increment_buckets_unsorted_level(uint32_t from_index, uint32_t to_index, uint64_t weight,
541
598
  const T* split_points, uint32_t size, double* buckets) const;
599
+ template<bool inclusive>
542
600
  void increment_buckets_sorted_level(uint32_t from_index, uint32_t to_index, uint64_t weight,
543
601
  const T* split_points, uint32_t size, double* buckets) const;
602
+
544
603
  template<typename O> void merge_higher_levels(O&& other, uint64_t final_n);
545
- void populate_work_arrays(const kll_sketch& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels);
546
- void populate_work_arrays(kll_sketch&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels);
604
+
605
+ template<typename FwdSk>
606
+ void populate_work_arrays(FwdSk&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels);
607
+
547
608
  void assert_correct_total_weight() const;
548
609
  uint32_t safe_level_size(uint8_t level) const;
549
610
  uint32_t get_num_retained_above_level_zero() const;
@@ -553,10 +614,13 @@ class kll_sketch {
553
614
  static void check_serial_version(uint8_t serial_version);
554
615
  static void check_family_id(uint8_t family_id);
555
616
 
617
+ void check_sorting() const;
618
+
556
619
  // implementations for floating point types
557
620
  template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
558
- static TT get_invalid_value() {
559
- return std::numeric_limits<TT>::quiet_NaN();
621
+ static const TT& get_invalid_value() {
622
+ static TT value = std::numeric_limits<TT>::quiet_NaN();
623
+ return value;
560
624
  }
561
625
 
562
626
  template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
@@ -566,8 +630,8 @@ class kll_sketch {
566
630
 
567
631
  // implementations for all other types
568
632
  template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
569
- static TT get_invalid_value() {
570
- throw std::runtime_error("getting quantiles from empty sketch is not supported for this type of values");
633
+ static const TT& get_invalid_value() {
634
+ throw std::runtime_error("getting quantiles from empty sketch is not supported for this type of value");
571
635
  }
572
636
 
573
637
  template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
@@ -575,6 +639,9 @@ class kll_sketch {
575
639
  return true;
576
640
  }
577
641
 
642
+ // for type converting constructor
643
+ template<typename TT, typename CC, typename SS, typename AA>
644
+ friend class kll_sketch;
578
645
  };
579
646
 
580
647
  template<typename T, typename C, typename S, typename A>