datasketches 0.1.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/LICENSE +40 -3
  4. data/NOTICE +1 -1
  5. data/ext/datasketches/cpc_wrapper.cpp +12 -13
  6. data/ext/datasketches/ext.cpp +1 -1
  7. data/ext/datasketches/ext.h +4 -0
  8. data/ext/datasketches/extconf.rb +1 -1
  9. data/ext/datasketches/fi_wrapper.cpp +6 -8
  10. data/ext/datasketches/hll_wrapper.cpp +13 -14
  11. data/ext/datasketches/kll_wrapper.cpp +28 -76
  12. data/ext/datasketches/theta_wrapper.cpp +27 -41
  13. data/ext/datasketches/vo_wrapper.cpp +4 -6
  14. data/lib/datasketches/version.rb +1 -1
  15. data/vendor/datasketches-cpp/CMakeLists.txt +10 -0
  16. data/vendor/datasketches-cpp/LICENSE +40 -3
  17. data/vendor/datasketches-cpp/NOTICE +1 -1
  18. data/vendor/datasketches-cpp/README.md +4 -4
  19. data/vendor/datasketches-cpp/common/include/MurmurHash3.h +18 -7
  20. data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +8 -8
  21. data/vendor/datasketches-cpp/common/include/bounds_binomial_proportions.hpp +12 -15
  22. data/vendor/datasketches-cpp/common/include/common_defs.hpp +26 -0
  23. data/vendor/datasketches-cpp/common/include/conditional_forward.hpp +20 -8
  24. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +2 -2
  25. data/vendor/datasketches-cpp/common/include/memory_operations.hpp +12 -0
  26. data/vendor/datasketches-cpp/common/include/serde.hpp +7 -7
  27. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +24 -0
  28. data/vendor/datasketches-cpp/common/test/integration_test.cpp +77 -0
  29. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +9 -1
  30. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +13 -3
  31. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +20 -20
  32. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +116 -105
  33. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +22 -6
  34. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +140 -101
  35. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +2 -2
  36. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +20 -20
  37. data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +10 -16
  38. data/vendor/datasketches-cpp/cpc/include/icon_estimator.hpp +6 -6
  39. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +10 -10
  40. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +21 -21
  41. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +1 -0
  42. data/vendor/datasketches-cpp/cpc/test/compression_test.cpp +10 -10
  43. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_allocation_test.cpp +237 -0
  44. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +25 -0
  45. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +1 -1
  46. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +15 -10
  47. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +102 -105
  48. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +19 -13
  49. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map_impl.hpp +141 -125
  50. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +15 -12
  51. data/vendor/datasketches-cpp/fi/test/reverse_purge_hash_map_test.cpp +5 -5
  52. data/vendor/datasketches-cpp/hll/CMakeLists.txt +3 -0
  53. data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +81 -109
  54. data/vendor/datasketches-cpp/hll/include/AuxHashMap.hpp +25 -24
  55. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +15 -15
  56. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable.hpp +5 -5
  57. data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +89 -105
  58. data/vendor/datasketches-cpp/hll/include/CouponHashSet.hpp +13 -13
  59. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +130 -165
  60. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +21 -22
  61. data/vendor/datasketches-cpp/hll/include/CubicInterpolation-internal.hpp +2 -4
  62. data/vendor/datasketches-cpp/hll/include/CubicInterpolation.hpp +2 -2
  63. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers-internal.hpp +1 -1
  64. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers.hpp +2 -2
  65. data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +88 -83
  66. data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +9 -9
  67. data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +34 -45
  68. data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +7 -8
  69. data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +41 -52
  70. data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +7 -8
  71. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +220 -251
  72. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +42 -42
  73. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +36 -38
  74. data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +22 -22
  75. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +15 -14
  76. data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +47 -44
  77. data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +62 -87
  78. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +121 -128
  79. data/vendor/datasketches-cpp/hll/include/RelativeErrorTables.hpp +1 -1
  80. data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +9 -9
  81. data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +5 -5
  82. data/vendor/datasketches-cpp/hll/include/hll.hpp +25 -53
  83. data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +8 -8
  84. data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +36 -36
  85. data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +28 -28
  86. data/vendor/datasketches-cpp/hll/test/CrossCountingTest.cpp +2 -2
  87. data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +37 -37
  88. data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +57 -61
  89. data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +10 -14
  90. data/vendor/datasketches-cpp/hll/test/IsomorphicTest.cpp +3 -3
  91. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +4 -4
  92. data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +5 -4
  93. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +6 -6
  94. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +14 -6
  95. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +40 -25
  96. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +50 -6
  97. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +164 -136
  98. data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov.hpp +67 -0
  99. data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov_impl.hpp +78 -0
  100. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +1 -0
  101. data/vendor/datasketches-cpp/kll/test/kll_sketch_custom_type_test.cpp +11 -10
  102. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +178 -88
  103. data/vendor/datasketches-cpp/kll/test/kolmogorov_smirnov_test.cpp +111 -0
  104. data/vendor/datasketches-cpp/pyproject.toml +4 -2
  105. data/vendor/datasketches-cpp/python/CMakeLists.txt +12 -6
  106. data/vendor/datasketches-cpp/python/README.md +52 -49
  107. data/vendor/datasketches-cpp/python/pybind11Path.cmd +3 -0
  108. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +1 -1
  109. data/vendor/datasketches-cpp/python/src/datasketches.cpp +2 -0
  110. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +4 -6
  111. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +4 -2
  112. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +246 -0
  113. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +38 -28
  114. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +11 -5
  115. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +2 -2
  116. data/vendor/datasketches-cpp/python/tests/hll_test.py +1 -2
  117. data/vendor/datasketches-cpp/python/tests/kll_test.py +5 -5
  118. data/vendor/datasketches-cpp/python/tests/req_test.py +126 -0
  119. data/vendor/datasketches-cpp/python/tests/theta_test.py +28 -3
  120. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +4 -4
  121. data/vendor/datasketches-cpp/python/tests/vo_test.py +3 -3
  122. data/vendor/datasketches-cpp/req/CMakeLists.txt +60 -0
  123. data/vendor/datasketches-cpp/{tuple/include/theta_a_not_b_experimental_impl.hpp → req/include/req_common.hpp} +18 -8
  124. data/vendor/datasketches-cpp/req/include/req_compactor.hpp +137 -0
  125. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +488 -0
  126. data/vendor/datasketches-cpp/req/include/req_quantile_calculator.hpp +69 -0
  127. data/vendor/datasketches-cpp/req/include/req_quantile_calculator_impl.hpp +60 -0
  128. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +395 -0
  129. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +810 -0
  130. data/vendor/datasketches-cpp/req/test/CMakeLists.txt +43 -0
  131. data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
  132. data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
  133. data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
  134. data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
  135. data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
  136. data/vendor/datasketches-cpp/req/test/req_sketch_custom_type_test.cpp +128 -0
  137. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +494 -0
  138. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +19 -13
  139. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +130 -127
  140. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +5 -5
  141. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +41 -49
  142. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +1 -0
  143. data/vendor/datasketches-cpp/sampling/test/var_opt_allocation_test.cpp +96 -0
  144. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +6 -6
  145. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +13 -44
  146. data/vendor/datasketches-cpp/setup.py +11 -6
  147. data/vendor/datasketches-cpp/theta/CMakeLists.txt +30 -3
  148. data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_sampled_sets.hpp +3 -2
  149. data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_theta_sketched_sets.hpp +1 -1
  150. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser.hpp +67 -0
  151. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +70 -0
  152. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +12 -29
  153. data/vendor/datasketches-cpp/theta/include/theta_a_not_b_impl.hpp +5 -46
  154. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_comparators.hpp +0 -0
  155. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_constants.hpp +11 -4
  156. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_helpers.hpp +0 -0
  157. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +26 -28
  158. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base.hpp +0 -0
  159. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base_impl.hpp +0 -0
  160. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +8 -90
  161. data/vendor/datasketches-cpp/{tuple/test/theta_union_experimental_test.cpp → theta/include/theta_jaccard_similarity.hpp} +11 -18
  162. data/vendor/datasketches-cpp/{tuple/include/jaccard_similarity.hpp → theta/include/theta_jaccard_similarity_base.hpp} +24 -36
  163. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base.hpp +0 -0
  164. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base_impl.hpp +5 -0
  165. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +163 -256
  166. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +250 -651
  167. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +27 -60
  168. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base.hpp +1 -1
  169. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base_impl.hpp +6 -1
  170. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +13 -69
  171. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base.hpp +10 -21
  172. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base_impl.hpp +44 -30
  173. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +1 -0
  174. data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +23 -1
  175. data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +21 -1
  176. data/vendor/datasketches-cpp/{tuple → theta}/test/theta_jaccard_similarity_test.cpp +60 -5
  177. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +74 -235
  178. data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +22 -2
  179. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +3 -35
  180. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +47 -60
  181. data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +38 -0
  182. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +28 -13
  183. data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +57 -70
  184. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +1 -6
  185. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +1 -1
  186. data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +18 -21
  187. data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +13 -16
  188. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +7 -6
  189. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_allocation_test.cpp +3 -3
  190. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +20 -20
  191. data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +13 -16
  192. metadata +51 -36
  193. data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental.hpp +0 -53
  194. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental.hpp +0 -78
  195. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental_impl.hpp +0 -43
  196. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental.hpp +0 -393
  197. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental_impl.hpp +0 -481
  198. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental.hpp +0 -88
  199. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental_impl.hpp +0 -47
  200. data/vendor/datasketches-cpp/tuple/test/theta_a_not_b_experimental_test.cpp +0 -250
  201. data/vendor/datasketches-cpp/tuple/test/theta_compact_empty_from_java.sk +0 -0
  202. data/vendor/datasketches-cpp/tuple/test/theta_compact_estimation_from_java.sk +0 -0
  203. data/vendor/datasketches-cpp/tuple/test/theta_compact_single_item_from_java.sk +0 -0
  204. data/vendor/datasketches-cpp/tuple/test/theta_intersection_experimental_test.cpp +0 -224
  205. data/vendor/datasketches-cpp/tuple/test/theta_sketch_experimental_test.cpp +0 -247
@@ -28,24 +28,38 @@
28
28
 
29
29
  namespace datasketches {
30
30
 
31
- template <typename T, typename C, typename A>
32
- kll_quantile_calculator<T, C, A>::kll_quantile_calculator(const T* items, const uint32_t* levels, uint8_t num_levels, uint64_t n):
33
- n_(n), levels_(num_levels + 1)
31
+ template<typename T, typename C, typename A>
32
+ template<typename S>
33
+ kll_quantile_calculator<T, C, A>::kll_quantile_calculator(const kll_sketch<T, C, S, A>& sketch):
34
+ n_(sketch.n_), levels_(sketch.num_levels_ + 1, 0, sketch.allocator_), entries_(sketch.allocator_)
34
35
  {
35
- const uint32_t num_items = levels[num_levels] - levels[0];
36
- entries_.reserve(num_items);
37
- populate_from_sketch(items, levels, num_levels);
38
- merge_sorted_blocks(entries_, levels_.data(), levels_.size() - 1, num_items);
39
- if (!is_sorted(entries_.begin(), entries_.end(), compare_pair_by_first<C>())) throw std::logic_error("entries must be sorted");
40
- convert_to_preceding_cummulative();
36
+ const uint32_t num_items = sketch.levels_[sketch.num_levels_] - sketch.levels_[0];
37
+ if (num_items > 0) {
38
+ entries_.reserve(num_items);
39
+ populate_from_sketch(sketch.items_, sketch.levels_.data(), sketch.num_levels_);
40
+ if (!sketch.is_level_zero_sorted_) std::sort(entries_.begin(), entries_.begin() + levels_[1], compare_pair_by_first<C>());
41
+ merge_sorted_blocks(entries_, levels_.data(), static_cast<uint8_t>(levels_.size()) - 1, num_items);
42
+ if (!is_sorted(entries_.begin(), entries_.end(), compare_pair_by_first<C>())) throw std::logic_error("entries must be sorted");
43
+ convert_to_preceding_cummulative();
44
+ }
41
45
  }
42
46
 
43
- template <typename T, typename C, typename A>
47
+ template<typename T, typename C, typename A>
44
48
  T kll_quantile_calculator<T, C, A>::get_quantile(double fraction) const {
45
49
  return approximately_answer_positional_query(pos_of_phi(fraction, n_));
46
50
  }
47
51
 
48
- template <typename T, typename C, typename A>
52
+ template<typename T, typename C, typename A>
53
+ auto kll_quantile_calculator<T, C, A>::begin() const -> const_iterator {
54
+ return entries_.begin();
55
+ }
56
+
57
+ template<typename T, typename C, typename A>
58
+ auto kll_quantile_calculator<T, C, A>::end() const -> const_iterator {
59
+ return entries_.end();
60
+ }
61
+
62
+ template<typename T, typename C, typename A>
49
63
  void kll_quantile_calculator<T, C, A>::populate_from_sketch(const T* items, const uint32_t* levels, uint8_t num_levels) {
50
64
  size_t src_level = 0;
51
65
  size_t dst_level = 0;
@@ -68,7 +82,7 @@ void kll_quantile_calculator<T, C, A>::populate_from_sketch(const T* items, cons
68
82
  if (levels_.size() > static_cast<size_t>(dst_level + 1)) levels_.resize(dst_level + 1);
69
83
  }
70
84
 
71
- template <typename T, typename C, typename A>
85
+ template<typename T, typename C, typename A>
72
86
  T kll_quantile_calculator<T, C, A>::approximately_answer_positional_query(uint64_t pos) const {
73
87
  if (pos >= n_) throw std::logic_error("position out of range");
74
88
  const uint32_t num_items = levels_[levels_.size() - 1];
@@ -77,7 +91,7 @@ T kll_quantile_calculator<T, C, A>::approximately_answer_positional_query(uint64
77
91
  return entries_[index].first;
78
92
  }
79
93
 
80
- template <typename T, typename C, typename A>
94
+ template<typename T, typename C, typename A>
81
95
  void kll_quantile_calculator<T, C, A>::convert_to_preceding_cummulative() {
82
96
  uint64_t subtotal = 0;
83
97
  for (auto& entry: entries_) {
@@ -87,13 +101,13 @@ void kll_quantile_calculator<T, C, A>::convert_to_preceding_cummulative() {
87
101
  }
88
102
  }
89
103
 
90
- template <typename T, typename C, typename A>
104
+ template<typename T, typename C, typename A>
91
105
  uint64_t kll_quantile_calculator<T, C, A>::pos_of_phi(double phi, uint64_t n) {
92
- const uint64_t pos = std::floor(phi * n);
106
+ const uint64_t pos = static_cast<uint64_t>(std::floor(phi * n));
93
107
  return (pos == n) ? n - 1 : pos;
94
108
  }
95
109
 
96
- template <typename T, typename C, typename A>
110
+ template<typename T, typename C, typename A>
97
111
  uint32_t kll_quantile_calculator<T, C, A>::chunk_containing_pos(uint64_t pos) const {
98
112
  if (entries_.size() < 1) throw std::logic_error("array too short");
99
113
  if (pos < entries_[0].second) throw std::logic_error("position too small");
@@ -101,27 +115,27 @@ uint32_t kll_quantile_calculator<T, C, A>::chunk_containing_pos(uint64_t pos) co
101
115
  return search_for_chunk_containing_pos(pos, 0, entries_.size());
102
116
  }
103
117
 
104
- template <typename T, typename C, typename A>
105
- uint32_t kll_quantile_calculator<T, C, A>::search_for_chunk_containing_pos(uint64_t pos, uint32_t l, uint32_t r) const {
118
+ template<typename T, typename C, typename A>
119
+ uint32_t kll_quantile_calculator<T, C, A>::search_for_chunk_containing_pos(uint64_t pos, uint64_t l, uint64_t r) const {
106
120
  if (l + 1 == r) {
107
- return l;
121
+ return static_cast<uint32_t>(l);
108
122
  }
109
- const uint32_t m(l + (r - l) / 2);
123
+ const uint64_t m = l + (r - l) / 2;
110
124
  if (entries_[m].second <= pos) {
111
125
  return search_for_chunk_containing_pos(pos, m, r);
112
126
  }
113
127
  return search_for_chunk_containing_pos(pos, l, m);
114
128
  }
115
129
 
116
- template <typename T, typename C, typename A>
130
+ template<typename T, typename C, typename A>
117
131
  void kll_quantile_calculator<T, C, A>::merge_sorted_blocks(Container& entries, const uint32_t* levels, uint8_t num_levels, uint32_t num_items) {
118
132
  if (num_levels == 1) return;
119
- Container temporary;
133
+ Container temporary(entries.get_allocator());
120
134
  temporary.reserve(num_items);
121
135
  merge_sorted_blocks_direct(entries, temporary, levels, 0, num_levels);
122
136
  }
123
137
 
124
- template <typename T, typename C, typename A>
138
+ template<typename T, typename C, typename A>
125
139
  void kll_quantile_calculator<T, C, A>::merge_sorted_blocks_direct(Container& orig, Container& temp, const uint32_t* levels,
126
140
  uint8_t starting_level, uint8_t num_levels) {
127
141
  if (num_levels == 1) return;
@@ -129,10 +143,11 @@ void kll_quantile_calculator<T, C, A>::merge_sorted_blocks_direct(Container& ori
129
143
  const uint8_t num_levels_2 = num_levels - num_levels_1;
130
144
  const uint8_t starting_level_1 = starting_level;
131
145
  const uint8_t starting_level_2 = starting_level + num_levels_1;
132
- const auto chunk_begin = temp.begin() + temp.size();
146
+ const auto initial_size = temp.size();
133
147
  merge_sorted_blocks_reversed(orig, temp, levels, starting_level_1, num_levels_1);
134
148
  merge_sorted_blocks_reversed(orig, temp, levels, starting_level_2, num_levels_2);
135
149
  const uint32_t num_items_1 = levels[starting_level_1 + num_levels_1] - levels[starting_level_1];
150
+ const auto chunk_begin = temp.begin() + initial_size;
136
151
  std::merge(
137
152
  std::make_move_iterator(chunk_begin), std::make_move_iterator(chunk_begin + num_items_1),
138
153
  std::make_move_iterator(chunk_begin + num_items_1), std::make_move_iterator(temp.end()),
@@ -141,7 +156,7 @@ void kll_quantile_calculator<T, C, A>::merge_sorted_blocks_direct(Container& ori
141
156
  temp.erase(chunk_begin, temp.end());
142
157
  }
143
158
 
144
- template <typename T, typename C, typename A>
159
+ template<typename T, typename C, typename A>
145
160
  void kll_quantile_calculator<T, C, A>::merge_sorted_blocks_reversed(Container& orig, Container& temp, const uint32_t* levels,
146
161
  uint8_t starting_level, uint8_t num_levels) {
147
162
  if (num_levels == 1) {
@@ -153,15 +153,23 @@ template<typename A> using vector_u32 = std::vector<uint32_t, AllocU32<A>>;
153
153
  template<typename A> using AllocD = typename std::allocator_traits<A>::template rebind_alloc<double>;
154
154
  template<typename A> using vector_d = std::vector<double, AllocD<A>>;
155
155
 
156
+ namespace kll_constants {
157
+ const uint16_t DEFAULT_K = 200;
158
+ }
159
+
156
160
  template <typename T, typename C = std::less<T>, typename S = serde<T>, typename A = std::allocator<T>>
157
161
  class kll_sketch {
158
162
  public:
163
+ using value_type = T;
164
+ using comparator = C;
165
+
159
166
  static const uint8_t DEFAULT_M = 8;
160
- static const uint16_t DEFAULT_K = 200;
167
+ // TODO: Redundant and deprecated. Will be remove din next major version.
168
+ static const uint16_t DEFAULT_K = kll_constants::DEFAULT_K;
161
169
  static const uint16_t MIN_K = DEFAULT_M;
162
170
  static const uint16_t MAX_K = (1 << 16) - 1;
163
171
 
164
- explicit kll_sketch(uint16_t k = DEFAULT_K);
172
+ explicit kll_sketch(uint16_t k = kll_constants::DEFAULT_K, const A& allocator = A());
165
173
  kll_sketch(const kll_sketch& other);
166
174
  kll_sketch(kll_sketch&& other) noexcept;
167
175
  ~kll_sketch();
@@ -202,6 +210,12 @@ class kll_sketch {
202
210
  */
203
211
  bool is_empty() const;
204
212
 
213
+ /**
214
+ * Returns configured parameter k
215
+ * @return parameter k
216
+ */
217
+ uint16_t get_k() const;
218
+
205
219
  /**
206
220
  * Returns the length of the input stream.
207
221
  * @return stream length
@@ -290,7 +304,7 @@ class kll_sketch {
290
304
  *
291
305
  * @return array of approximations to the given number of evenly-spaced fractional ranks.
292
306
  */
293
- std::vector<T, A> get_quantiles(size_t num) const;
307
+ std::vector<T, A> get_quantiles(uint32_t num) const;
294
308
 
295
309
  /**
296
310
  * Returns an approximation to the normalized (fractional) rank of the given value from 0 to 1,
@@ -377,6 +391,33 @@ class kll_sketch {
377
391
  template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
378
392
  size_t get_serialized_size_bytes() const;
379
393
 
394
+ /**
395
+ * Returns upper bound on the serialized size of a sketch given a parameter <em>k</em> and stream
396
+ * length. The resulting size is an overestimate to make sure actual sketches don't exceed it.
397
+ * This method can be used if allocation of storage is necessary beforehand, but it is not
398
+ * optimal.
399
+ * This method is for arithmetic types (integral and floating point)
400
+ * @param k parameter that controls size of the sketch and accuracy of estimates
401
+ * @param n stream length
402
+ * @return upper bound on the serialized size
403
+ */
404
+ template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
405
+ static size_t get_max_serialized_size_bytes(uint16_t k, uint64_t n);
406
+
407
+ /**
408
+ * Returns upper bound on the serialized size of a sketch given a parameter <em>k</em> and stream
409
+ * length. The resulting size is an overestimate to make sure actual sketches don't exceed it.
410
+ * This method can be used if allocation of storage is necessary beforehand, but it is not
411
+ * optimal.
412
+ * This method is for all other non-arithmetic types, and it takes a max size of an item as input.
413
+ * @param k parameter that controls size of the sketch and accuracy of estimates
414
+ * @param n stream length
415
+ * @param max_item_size_bytes maximum size of an item in bytes
416
+ * @return upper bound on the serialized size
417
+ */
418
+ template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
419
+ static size_t get_max_serialized_size_bytes(uint16_t k, uint64_t n, size_t max_item_size_bytes);
420
+
380
421
  /**
381
422
  * This method serializes the sketch into a given stream in a binary form
382
423
  * @param os output stream
@@ -385,7 +426,7 @@ class kll_sketch {
385
426
 
386
427
  // This is a convenience alias for users
387
428
  // The type returned by the following serialize method
388
- typedef vector_u8<A> vector_bytes;
429
+ using vector_bytes = vector_u8<A>;
389
430
 
390
431
  /**
391
432
  * This method serializes the sketch as a vector of bytes.
@@ -401,7 +442,7 @@ class kll_sketch {
401
442
  * @param is input stream
402
443
  * @return an instance of a sketch
403
444
  */
404
- static kll_sketch deserialize(std::istream& is);
445
+ static kll_sketch<T, C, S, A> deserialize(std::istream& is, const A& allocator = A());
405
446
 
406
447
  /**
407
448
  * This method deserializes a sketch from a given array of bytes.
@@ -409,7 +450,7 @@ class kll_sketch {
409
450
  * @param size the size of the array
410
451
  * @return an instance of a sketch
411
452
  */
412
- static kll_sketch deserialize(const void* bytes, size_t size);
453
+ static kll_sketch<T, C, S, A> deserialize(const void* bytes, size_t size, const A& allocator = A());
413
454
 
414
455
  /*
415
456
  * Gets the normalized rank error given k and pmf.
@@ -461,6 +502,7 @@ class kll_sketch {
461
502
  static const uint8_t PREAMBLE_INTS_SHORT = 2; // for empty and single item
462
503
  static const uint8_t PREAMBLE_INTS_FULL = 5;
463
504
 
505
+ A allocator_;
464
506
  uint16_t k_;
465
507
  uint8_t m_; // minimum buffer "width"
466
508
  uint16_t min_k_; // for error estimation after merging with different k
@@ -473,6 +515,8 @@ class kll_sketch {
473
515
  T* max_value_;
474
516
  bool is_level_zero_sorted_;
475
517
 
518
+ friend class kll_quantile_calculator<T, C, A>;
519
+
476
520
  // for deserialization
477
521
  class item_deleter;
478
522
  class items_deleter;