datasketches 0.3.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -0
  3. data/NOTICE +1 -1
  4. data/README.md +0 -2
  5. data/ext/datasketches/cpc_wrapper.cpp +2 -2
  6. data/ext/datasketches/kll_wrapper.cpp +0 -10
  7. data/lib/datasketches/version.rb +1 -1
  8. data/lib/datasketches.rb +1 -1
  9. data/vendor/datasketches-cpp/CMakeLists.txt +1 -0
  10. data/vendor/datasketches-cpp/CODE_OF_CONDUCT.md +3 -0
  11. data/vendor/datasketches-cpp/CONTRIBUTING.md +50 -0
  12. data/vendor/datasketches-cpp/Doxyfile +2827 -0
  13. data/vendor/datasketches-cpp/LICENSE +0 -76
  14. data/vendor/datasketches-cpp/NOTICE +1 -1
  15. data/vendor/datasketches-cpp/README.md +1 -3
  16. data/vendor/datasketches-cpp/common/CMakeLists.txt +12 -11
  17. data/vendor/datasketches-cpp/common/include/common_defs.hpp +11 -8
  18. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +0 -2
  19. data/vendor/datasketches-cpp/common/include/kolmogorov_smirnov.hpp +9 -6
  20. data/vendor/datasketches-cpp/common/include/optional.hpp +148 -0
  21. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view.hpp +95 -2
  22. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view_impl.hpp +1 -1
  23. data/vendor/datasketches-cpp/common/include/serde.hpp +69 -20
  24. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +1 -1
  25. data/vendor/datasketches-cpp/common/test/optional_test.cpp +85 -0
  26. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +14 -14
  27. data/vendor/datasketches-cpp/count/include/count_min.hpp +132 -78
  28. data/vendor/datasketches-cpp/count/include/count_min_impl.hpp +132 -152
  29. data/vendor/datasketches-cpp/count/test/CMakeLists.txt +11 -12
  30. data/vendor/datasketches-cpp/count/test/count_min_allocation_test.cpp +61 -61
  31. data/vendor/datasketches-cpp/count/test/count_min_test.cpp +175 -178
  32. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +14 -20
  33. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +7 -4
  34. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +17 -17
  35. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +40 -40
  36. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +13 -10
  37. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +35 -11
  38. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +8 -8
  39. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +3 -2
  40. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +5 -5
  41. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +20 -7
  42. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_deserialize_from_java_test.cpp +60 -0
  43. data/vendor/datasketches-cpp/{python/include/py_object_lt.hpp → cpc/test/cpc_sketch_serialize_for_java.cpp} +15 -14
  44. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +4 -29
  45. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +4 -4
  46. data/vendor/datasketches-cpp/density/include/density_sketch.hpp +29 -9
  47. data/vendor/datasketches-cpp/density/include/density_sketch_impl.hpp +1 -1
  48. data/vendor/datasketches-cpp/density/test/CMakeLists.txt +0 -1
  49. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +21 -9
  50. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +6 -4
  51. data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +14 -1
  52. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_deserialize_from_java_test.cpp +95 -0
  53. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_serialize_for_java.cpp +83 -0
  54. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +3 -42
  55. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +2 -2
  56. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +3 -1
  57. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +3 -3
  58. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +5 -3
  59. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +4 -4
  60. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +3 -1
  61. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +0 -12
  62. data/vendor/datasketches-cpp/hll/include/hll.hpp +70 -57
  63. data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +14 -1
  64. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +0 -68
  65. data/vendor/datasketches-cpp/hll/test/hll_sketch_deserialize_from_java_test.cpp +69 -0
  66. data/vendor/datasketches-cpp/hll/test/hll_sketch_serialize_for_java.cpp +52 -0
  67. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +6 -4
  68. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +71 -50
  69. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +59 -130
  70. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +14 -1
  71. data/vendor/datasketches-cpp/kll/test/kll_sketch_deserialize_from_java_test.cpp +103 -0
  72. data/vendor/datasketches-cpp/kll/test/kll_sketch_serialize_for_java.cpp +62 -0
  73. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +3 -38
  74. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch.hpp +68 -51
  75. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch_impl.hpp +62 -132
  76. data/vendor/datasketches-cpp/quantiles/test/CMakeLists.txt +14 -1
  77. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_deserialize_from_java_test.cpp +84 -0
  78. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_serialize_for_java.cpp +52 -0
  79. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_test.cpp +14 -38
  80. data/vendor/datasketches-cpp/req/include/req_common.hpp +7 -3
  81. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +2 -2
  82. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +97 -23
  83. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +48 -109
  84. data/vendor/datasketches-cpp/req/test/CMakeLists.txt +14 -1
  85. data/vendor/datasketches-cpp/req/test/req_sketch_deserialize_from_java_test.cpp +55 -0
  86. data/vendor/datasketches-cpp/{tuple/include/array_of_doubles_intersection_impl.hpp → req/test/req_sketch_serialize_for_java.cpp} +12 -7
  87. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +3 -89
  88. data/vendor/datasketches-cpp/sampling/CMakeLists.txt +4 -0
  89. data/vendor/datasketches-cpp/sampling/include/ebpps_sample.hpp +210 -0
  90. data/vendor/datasketches-cpp/sampling/include/ebpps_sample_impl.hpp +539 -0
  91. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch.hpp +281 -0
  92. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch_impl.hpp +531 -0
  93. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +69 -26
  94. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +3 -3
  95. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +10 -11
  96. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +4 -4
  97. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +55 -8
  98. data/vendor/datasketches-cpp/sampling/test/ebpps_allocation_test.cpp +96 -0
  99. data/vendor/datasketches-cpp/sampling/test/ebpps_sample_test.cpp +137 -0
  100. data/vendor/datasketches-cpp/sampling/test/ebpps_sketch_test.cpp +266 -0
  101. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_deserialize_from_java_test.cpp +81 -0
  102. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_serialize_for_java.cpp +54 -0
  103. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +0 -37
  104. data/vendor/datasketches-cpp/sampling/test/var_opt_union_deserialize_from_java_test.cpp +50 -0
  105. data/vendor/datasketches-cpp/sampling/test/var_opt_union_serialize_for_java.cpp +56 -0
  106. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +0 -18
  107. data/vendor/datasketches-cpp/theta/include/bit_packing.hpp +2608 -2608
  108. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_sampled_sets.hpp +1 -0
  109. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_theta_sketched_sets.hpp +7 -6
  110. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +20 -5
  111. data/vendor/datasketches-cpp/theta/include/theta_constants.hpp +10 -4
  112. data/vendor/datasketches-cpp/theta/include/theta_helpers.hpp +1 -1
  113. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +13 -5
  114. data/vendor/datasketches-cpp/theta/include/theta_intersection_base_impl.hpp +5 -5
  115. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +3 -3
  116. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity.hpp +2 -1
  117. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity_base.hpp +1 -0
  118. data/vendor/datasketches-cpp/theta/include/theta_set_difference_base_impl.hpp +1 -1
  119. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +126 -27
  120. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +8 -8
  121. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +17 -10
  122. data/vendor/datasketches-cpp/theta/include/theta_union_base_impl.hpp +1 -1
  123. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +3 -3
  124. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base.hpp +5 -2
  125. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base_impl.hpp +11 -1
  126. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +14 -1
  127. data/vendor/datasketches-cpp/theta/test/theta_sketch_deserialize_from_java_test.cpp +57 -0
  128. data/vendor/datasketches-cpp/theta/test/theta_sketch_serialize_for_java.cpp +61 -0
  129. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +0 -188
  130. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +8 -7
  131. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +19 -144
  132. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b.hpp → array_tuple_a_not_b.hpp} +24 -16
  133. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b_impl.hpp → array_tuple_a_not_b_impl.hpp} +4 -4
  134. data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection.hpp +65 -0
  135. data/vendor/datasketches-cpp/{python/include/py_object_ostream.hpp → tuple/include/array_tuple_intersection_impl.hpp} +7 -24
  136. data/vendor/datasketches-cpp/tuple/include/array_tuple_sketch.hpp +237 -0
  137. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_sketch_impl.hpp → array_tuple_sketch_impl.hpp} +40 -41
  138. data/vendor/datasketches-cpp/tuple/include/array_tuple_union.hpp +81 -0
  139. data/vendor/datasketches-cpp/tuple/include/array_tuple_union_impl.hpp +43 -0
  140. data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +11 -2
  141. data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +17 -10
  142. data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +2 -1
  143. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +95 -32
  144. data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +19 -11
  145. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +16 -1
  146. data/vendor/datasketches-cpp/tuple/test/aod_sketch_deserialize_from_java_test.cpp +76 -0
  147. data/vendor/datasketches-cpp/tuple/test/aod_sketch_serialize_for_java.cpp +62 -0
  148. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +5 -129
  149. data/vendor/datasketches-cpp/tuple/test/engagement_test.cpp +85 -89
  150. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +3 -1
  151. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_deserialize_from_java_test.cpp +47 -0
  152. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_serialize_for_java.cpp +38 -0
  153. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +1 -1
  154. data/vendor/datasketches-cpp/version.cfg.in +1 -1
  155. metadata +47 -93
  156. data/vendor/datasketches-cpp/MANIFEST.in +0 -39
  157. data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
  158. data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
  159. data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
  160. data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
  161. data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
  162. data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
  163. data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
  164. data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
  165. data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
  166. data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
  167. data/vendor/datasketches-cpp/pyproject.toml +0 -23
  168. data/vendor/datasketches-cpp/python/CMakeLists.txt +0 -87
  169. data/vendor/datasketches-cpp/python/README.md +0 -85
  170. data/vendor/datasketches-cpp/python/datasketches/DensityWrapper.py +0 -87
  171. data/vendor/datasketches-cpp/python/datasketches/KernelFunction.py +0 -35
  172. data/vendor/datasketches-cpp/python/datasketches/PySerDe.py +0 -110
  173. data/vendor/datasketches-cpp/python/datasketches/TuplePolicy.py +0 -77
  174. data/vendor/datasketches-cpp/python/datasketches/TupleWrapper.py +0 -205
  175. data/vendor/datasketches-cpp/python/datasketches/__init__.py +0 -38
  176. data/vendor/datasketches-cpp/python/include/kernel_function.hpp +0 -98
  177. data/vendor/datasketches-cpp/python/include/py_serde.hpp +0 -113
  178. data/vendor/datasketches-cpp/python/include/quantile_conditional.hpp +0 -104
  179. data/vendor/datasketches-cpp/python/include/tuple_policy.hpp +0 -136
  180. data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +0 -345
  181. data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +0 -354
  182. data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +0 -346
  183. data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +0 -463
  184. data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +0 -403
  185. data/vendor/datasketches-cpp/python/pybind11Path.cmd +0 -21
  186. data/vendor/datasketches-cpp/python/src/__init__.py +0 -18
  187. data/vendor/datasketches-cpp/python/src/count_wrapper.cpp +0 -101
  188. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +0 -76
  189. data/vendor/datasketches-cpp/python/src/datasketches.cpp +0 -58
  190. data/vendor/datasketches-cpp/python/src/density_wrapper.cpp +0 -95
  191. data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +0 -182
  192. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +0 -126
  193. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +0 -158
  194. data/vendor/datasketches-cpp/python/src/ks_wrapper.cpp +0 -68
  195. data/vendor/datasketches-cpp/python/src/py_serde.cpp +0 -112
  196. data/vendor/datasketches-cpp/python/src/quantiles_wrapper.cpp +0 -155
  197. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +0 -154
  198. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +0 -166
  199. data/vendor/datasketches-cpp/python/src/tuple_wrapper.cpp +0 -215
  200. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +0 -490
  201. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +0 -173
  202. data/vendor/datasketches-cpp/python/tests/__init__.py +0 -16
  203. data/vendor/datasketches-cpp/python/tests/count_min_test.py +0 -86
  204. data/vendor/datasketches-cpp/python/tests/cpc_test.py +0 -64
  205. data/vendor/datasketches-cpp/python/tests/density_test.py +0 -93
  206. data/vendor/datasketches-cpp/python/tests/fi_test.py +0 -149
  207. data/vendor/datasketches-cpp/python/tests/hll_test.py +0 -129
  208. data/vendor/datasketches-cpp/python/tests/kll_test.py +0 -159
  209. data/vendor/datasketches-cpp/python/tests/quantiles_test.py +0 -160
  210. data/vendor/datasketches-cpp/python/tests/req_test.py +0 -159
  211. data/vendor/datasketches-cpp/python/tests/theta_test.py +0 -148
  212. data/vendor/datasketches-cpp/python/tests/tuple_test.py +0 -206
  213. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +0 -148
  214. data/vendor/datasketches-cpp/python/tests/vo_test.py +0 -132
  215. data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
  216. data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
  217. data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
  218. data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
  219. data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
  220. data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +0 -67
  221. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
  222. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
  223. data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
  224. data/vendor/datasketches-cpp/setup.py +0 -110
  225. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
  226. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
  227. data/vendor/datasketches-cpp/theta/test/theta_compact_exact_from_java.sk +0 -0
  228. data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
  229. data/vendor/datasketches-cpp/tox.ini +0 -26
  230. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +0 -52
  231. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +0 -81
  232. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +0 -43
  233. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +0 -1
  234. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
  235. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
  236. data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
  237. data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +0 -1
@@ -0,0 +1,539 @@
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 _EBPPS_SAMPLE_IMPL_HPP_
21
+ #define _EBPPS_SAMPLE_IMPL_HPP_
22
+
23
+ #include "common_defs.hpp"
24
+ #include "conditional_forward.hpp"
25
+ #include "ebpps_sample.hpp"
26
+ #include "serde.hpp"
27
+
28
+ #include <cmath>
29
+ #include <string>
30
+ #include <sstream>
31
+
32
+ namespace datasketches {
33
+
34
+ template<typename T, typename A>
35
+ ebpps_sample<T,A>::ebpps_sample(uint32_t reserved_size, const A& allocator) :
36
+ allocator_(allocator),
37
+ c_(0.0),
38
+ partial_item_(),
39
+ data_(allocator)
40
+ {
41
+ data_.reserve(reserved_size);
42
+ }
43
+
44
+ template<typename T, typename A>
45
+ template<typename TT>
46
+ ebpps_sample<T,A>::ebpps_sample(TT&& item, double theta, const A& allocator) :
47
+ allocator_(allocator),
48
+ c_(theta),
49
+ partial_item_(),
50
+ data_(allocator)
51
+ {
52
+ if (theta == 1.0) {
53
+ data_.reserve(1);
54
+ data_.emplace_back(std::forward<TT>(item));
55
+ } else {
56
+ partial_item_.emplace(std::forward<TT>(item));
57
+ }
58
+ }
59
+
60
+ template<typename T, typename A>
61
+ ebpps_sample<T,A>::ebpps_sample(std::vector<T, A>&& data, optional<T>&& partial_item, double c, const A& allocator) :
62
+ allocator_(allocator),
63
+ c_(c),
64
+ partial_item_(partial_item),
65
+ data_(data, allocator)
66
+ {}
67
+
68
+ template<typename T, typename A>
69
+ auto ebpps_sample<T,A>::get_sample() const -> result_type {
70
+ double unused;
71
+ const double c_frac = std::modf(c_, &unused);
72
+ const bool include_partial = next_double() < c_frac;
73
+ const uint32_t result_size = static_cast<uint32_t>(data_.size()) + (include_partial ? 1 : 0);
74
+
75
+ result_type result;
76
+ result.reserve(result_size);
77
+ std::copy(data_.begin(), data_.end(), std::back_inserter(result));
78
+ if (include_partial)
79
+ result.emplace_back(static_cast<const T&>(*partial_item_));
80
+
81
+ return result;
82
+ }
83
+
84
+ template<typename T, typename A>
85
+ void ebpps_sample<T,A>::downsample(double theta) {
86
+ if (theta >= 1.0) return;
87
+
88
+ const double new_c = theta * c_;
89
+ double new_c_int;
90
+ const double new_c_frac = std::modf(new_c, &new_c_int);
91
+ double c_int;
92
+ const double c_frac = std::modf(c_, &c_int);
93
+
94
+ if (new_c_int == 0.0) {
95
+ // no full items retained
96
+ if (next_double() > (c_frac / c_)) {
97
+ swap_with_partial();
98
+ }
99
+ data_.clear();
100
+ } else if (new_c_int == c_int) {
101
+ // no items deleted
102
+ if (next_double() > (1 - theta * c_frac)/(1 - new_c_frac)) {
103
+ swap_with_partial();
104
+ }
105
+ } else {
106
+ if (next_double() < theta * c_frac) {
107
+ // subsample data in random order; last item is partial
108
+ // create sample size new_c_int then swap_with_partial()
109
+ subsample(static_cast<uint32_t>(new_c_int));
110
+ swap_with_partial();
111
+ } else {
112
+ // create sample size new_c_int + 1 then move_one_to_partial)
113
+ subsample(static_cast<uint32_t>(new_c_int) + 1);
114
+ move_one_to_partial();
115
+ }
116
+ }
117
+
118
+ if (new_c == new_c_int)
119
+ partial_item_.reset();
120
+
121
+ c_ = new_c;
122
+ }
123
+
124
+ template<typename T, typename A>
125
+ template<typename FwdSample>
126
+ void ebpps_sample<T,A>::merge(FwdSample&& other) {
127
+ double c_int;
128
+ const double c_frac = std::modf(c_, &c_int);
129
+
130
+ double unused;
131
+ const double other_c_frac = std::modf(other.c_, &unused);
132
+
133
+ // update c_ here but do NOT recompute fractional part yet
134
+ c_ += other.c_;
135
+
136
+ for (uint32_t i = 0; i < other.data_.size(); ++i)
137
+ data_.emplace_back(conditional_forward<FwdSample>(other.data_[i]));
138
+
139
+ // This modifies the original algorithm slightly due to numeric
140
+ // precision issues. Specifically, the test if c_frac + other_c_frac == 1.0
141
+ // happens before tests for < 1.0 or > 1.0 and can also be triggered
142
+ // if c_ == floor(c_) (the updated value of c_, not the input).
143
+ //
144
+ // We can still run into issues where c_frac + other_c_frac == epsilon
145
+ // and the first case would have ideally triggered. As a result, we must
146
+ // check if the partial item exists before adding to the data_ vector.
147
+
148
+ if (c_frac == 0.0 && other_c_frac == 0.0) {
149
+ partial_item_.reset();
150
+ } else if (c_frac + other_c_frac == 1.0 || c_ == std::floor(c_)) {
151
+ if (next_double() <= c_frac) {
152
+ if (partial_item_)
153
+ data_.emplace_back(std::move(*partial_item_));
154
+ } else {
155
+ if (other.partial_item_)
156
+ data_.emplace_back(conditional_forward<FwdSample>(*other.partial_item_));
157
+ }
158
+ partial_item_.reset();
159
+ } else if (c_frac + other_c_frac < 1.0) {
160
+ if (next_double() > c_frac / (c_frac + other_c_frac)) {
161
+ set_partial(conditional_forward<FwdSample>(*other.partial_item_));
162
+ }
163
+ } else { // c_frac + other_c_frac > 1
164
+ if (next_double() <= (1 - c_frac) / ((1 - c_frac) + (1 - other_c_frac))) {
165
+ data_.emplace_back(conditional_forward<FwdSample>(*other.partial_item_));
166
+ } else {
167
+ data_.emplace_back(std::move(*partial_item_));
168
+ partial_item_.reset();
169
+ set_partial(conditional_forward<FwdSample>(*other.partial_item_));
170
+ }
171
+ }
172
+ }
173
+
174
+ template<typename T, typename A>
175
+ string<A> ebpps_sample<T ,A>::to_string() const {
176
+ std::ostringstream oss;
177
+ oss << " sample:" << std::endl;
178
+ uint32_t idx = 0;
179
+ for (const T& item : data_)
180
+ oss << "\t" << idx++ << ":\t" << item << std::endl;
181
+ oss << " partial: ";
182
+ if (bool(partial_item_))
183
+ oss << (*partial_item_) << std::endl;
184
+ else
185
+ oss << "NULL" << std::endl;
186
+
187
+ return oss.str();
188
+ }
189
+
190
+ template<typename T, typename A>
191
+ void ebpps_sample<T,A>::subsample(uint32_t num_samples) {
192
+ // we can perform a Fisher-Yates style shuffle, stopping after
193
+ // num_samples points since subsequent swaps would only be
194
+ // between items after num_samples. This is valid since a
195
+ // point from anywhere in the initial array would be eligible
196
+ // to end up in the final subsample.
197
+
198
+ if (num_samples == data_.size()) return;
199
+
200
+ auto erase_start = data_.begin();
201
+ uint32_t data_len = static_cast<uint32_t>(data_.size());
202
+ for (uint32_t i = 0; i < num_samples; ++i, ++erase_start) {
203
+ uint32_t j = i + random_idx(data_len - i);
204
+ std::swap(data_[i], data_[j]);
205
+ }
206
+
207
+ data_.erase(erase_start, data_.end());
208
+ }
209
+
210
+ template<typename T, typename A>
211
+ template<typename FwdItem>
212
+ void ebpps_sample<T,A>::set_partial(FwdItem&& item) {
213
+ if (partial_item_)
214
+ *partial_item_ = conditional_forward<FwdItem>(item);
215
+ else
216
+ partial_item_.emplace(conditional_forward<FwdItem>(item));
217
+ }
218
+
219
+ template<typename T, typename A>
220
+ void ebpps_sample<T,A>::move_one_to_partial() {
221
+ const size_t idx = random_idx(static_cast<uint32_t>(data_.size()));
222
+ // swap selected item to end so we can delete it easily
223
+ const size_t last_idx = data_.size() - 1;
224
+ if (idx != last_idx) {
225
+ std::swap(data_[idx], data_[last_idx]);
226
+ }
227
+
228
+ set_partial(std::move(data_[last_idx]));
229
+
230
+ data_.pop_back();
231
+ }
232
+
233
+ template<typename T, typename A>
234
+ void ebpps_sample<T,A>::swap_with_partial() {
235
+ if (partial_item_) {
236
+ const size_t idx = random_idx(static_cast<uint32_t>(data_.size()));
237
+ std::swap(data_[idx], *partial_item_);
238
+ } else {
239
+ move_one_to_partial();
240
+ }
241
+ }
242
+
243
+ template<typename T, typename A>
244
+ void ebpps_sample<T,A>::reset() {
245
+ c_ = 0.0;
246
+ partial_item_.reset();
247
+ data_.clear();
248
+ }
249
+
250
+ template<typename T, typename A>
251
+ double ebpps_sample<T,A>::get_c() const {
252
+ return c_;
253
+ }
254
+
255
+ template<typename T, typename A>
256
+ auto ebpps_sample<T,A>::get_full_items() const -> result_type {
257
+ return result_type(data_);
258
+ }
259
+
260
+ template<typename T, typename A>
261
+ bool ebpps_sample<T,A>::has_partial_item() const {
262
+ return bool(partial_item_);
263
+ }
264
+
265
+ template<typename T, typename A>
266
+ T ebpps_sample<T,A>::get_partial_item() const {
267
+ if (!partial_item_) throw std::runtime_error("Call to get_partial_item() when no partial item exists");
268
+ return *partial_item_;
269
+ }
270
+
271
+ template<typename T, typename A>
272
+ uint32_t ebpps_sample<T,A>::random_idx(uint32_t max) {
273
+ static std::uniform_int_distribution<uint32_t> dist;
274
+ return dist(random_utils::rand, std::uniform_int_distribution<uint32_t>::param_type(0, max - 1));
275
+ }
276
+
277
+ template<typename T, typename A>
278
+ double ebpps_sample<T,A>::next_double() {
279
+ return random_utils::next_double(random_utils::rand);
280
+ }
281
+
282
+ template<typename T, typename A>
283
+ uint32_t ebpps_sample<T,A>::get_num_retained_items() const {
284
+ return static_cast<uint32_t>(data_.size() + (partial_item_ ? 1 : 0));
285
+ }
286
+
287
+ // implementation for fixed-size arithmetic types (integral and floating point)
288
+ template<typename T, typename A>
289
+ template<typename TT, typename SerDe, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type>
290
+ size_t ebpps_sample<T, A>::get_serialized_size_bytes(const SerDe&) const {
291
+ if (c_ == 0.0)
292
+ return 0;
293
+ else
294
+ return sizeof(c_) + (data_.size() + (partial_item_ ? 1 : 0)) * sizeof(T);
295
+ }
296
+
297
+ // implementation for all other types
298
+ template<typename T, typename A>
299
+ template<typename TT, typename SerDe, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type>
300
+ size_t ebpps_sample<T, A>::get_serialized_size_bytes(const SerDe& sd) const {
301
+ if (c_ == 0.0) return 0;
302
+
303
+ size_t num_bytes = sizeof(c_);
304
+ for (auto it : data_)
305
+ num_bytes += sd.size_of_item(it);
306
+
307
+ if (partial_item_)
308
+ num_bytes += sd.size_of_item(*partial_item_);
309
+
310
+ return num_bytes;
311
+ }
312
+
313
+ template<typename T, typename A>
314
+ template<typename SerDe>
315
+ size_t ebpps_sample<T,A>::serialize(uint8_t* ptr, const uint8_t* end_ptr, const SerDe& sd) const {
316
+ uint8_t* st_ptr = ptr;
317
+
318
+ ensure_minimum_memory(end_ptr - ptr, sizeof(c_));
319
+ ptr += copy_to_mem(c_, ptr);
320
+
321
+ ptr += sd.serialize(ptr, end_ptr - ptr, data_.data(), static_cast<unsigned>(data_.size()));
322
+
323
+ if (partial_item_) {
324
+ ptr += sd.serialize(ptr, end_ptr - ptr, &*partial_item_, 1);
325
+ }
326
+
327
+ return ptr - st_ptr;
328
+ }
329
+
330
+ template<typename T, typename A>
331
+ template<typename SerDe>
332
+ void ebpps_sample<T,A>::serialize(std::ostream& os, const SerDe& sd) const {
333
+ write(os, c_);
334
+
335
+ sd.serialize(os, data_.data(), static_cast<unsigned>(data_.size()));
336
+
337
+ if (partial_item_)
338
+ sd.serialize(os, &*partial_item_, 1);
339
+
340
+ if (!os.good()) throw std::runtime_error("error writing to std::ostream");
341
+ }
342
+
343
+ template<typename T, typename A>
344
+ template<typename SerDe>
345
+ std::pair<ebpps_sample<T, A>, size_t> ebpps_sample<T, A>::deserialize(const uint8_t* ptr, size_t size, const SerDe& sd, const A& allocator) {
346
+ const uint8_t* st_ptr = ptr;
347
+ const uint8_t* end_ptr = ptr + size;
348
+
349
+ ensure_minimum_memory(size, sizeof(double));
350
+ double c;
351
+ ptr += copy_from_mem(ptr, c);
352
+ if (c < 0.0)
353
+ throw std::runtime_error("sketch image has C < 0.0 during deserializaiton");
354
+
355
+ double c_int;
356
+ const double c_frac = std::modf(c, &c_int);
357
+ const bool has_partial = c_frac != 0.0;
358
+
359
+ const uint32_t num_full_items = static_cast<uint32_t>(c_int);
360
+ A alloc(allocator);
361
+ std::unique_ptr<T, items_deleter> items(alloc.allocate(num_full_items), items_deleter(allocator, false, num_full_items));
362
+ ptr += sd.deserialize(ptr, end_ptr - ptr, items.get(), num_full_items);
363
+ // serde did not throw, enable destructors
364
+ items.get_deleter().set_destroy(true);
365
+ std::vector<T, A> data(std::make_move_iterator(items.get()),
366
+ std::make_move_iterator(items.get() + num_full_items),
367
+ allocator);
368
+
369
+ optional<T> partial_item;
370
+ if (has_partial) {
371
+ optional<T> tmp; // space to deserialize
372
+ ptr += sd.deserialize(ptr, end_ptr - ptr, &*tmp, 1);
373
+ // serde did not throw so place item and clean up
374
+ partial_item.emplace(*tmp);
375
+ (*tmp).~T();
376
+ }
377
+
378
+ return std::pair<ebpps_sample<T,A>, size_t>(
379
+ ebpps_sample<T,A>(std::move(data), std::move(partial_item), c, allocator),
380
+ ptr - st_ptr);
381
+ }
382
+
383
+ template<typename T, typename A>
384
+ template<typename SerDe>
385
+ ebpps_sample<T, A> ebpps_sample<T, A>::deserialize(std::istream& is, const SerDe& sd, const A& allocator) {
386
+ const double c = read<double>(is);
387
+ if (c < 0.0)
388
+ throw std::runtime_error("sketch image has C < 0.0 during deserializaiton");
389
+
390
+ double c_int;
391
+ const double c_frac = std::modf(c, &c_int);
392
+ const bool has_partial = c_frac != 0.0;
393
+
394
+ const uint32_t num_full_items = static_cast<uint32_t>(c_int);
395
+ A alloc(allocator);
396
+ std::unique_ptr<T, items_deleter> items(alloc.allocate(num_full_items), items_deleter(allocator, false, num_full_items));
397
+ sd.deserialize(is, items.get(), num_full_items);
398
+ // serde did not throw, enable destructors
399
+ items.get_deleter().set_destroy(true);
400
+ std::vector<T, A> data(std::make_move_iterator(items.get()),
401
+ std::make_move_iterator(items.get() + num_full_items),
402
+ allocator);
403
+
404
+ optional<T> partial_item;
405
+ if (has_partial) {
406
+ optional<T> tmp; // space to deserialize
407
+ sd.deserialize(is, &*tmp, 1);
408
+ // serde did not throw so place item and clean up
409
+ partial_item.emplace(*tmp);
410
+ (*tmp).~T();
411
+ }
412
+
413
+ if (!is.good()) throw std::runtime_error("error reading from std::istream");
414
+
415
+ return ebpps_sample<T,A>(std::move(data), std::move(partial_item), c, allocator);
416
+ }
417
+
418
+
419
+ template<typename T, typename A>
420
+ typename ebpps_sample<T, A>::const_iterator ebpps_sample<T, A>::begin() const {
421
+ return const_iterator(this);
422
+ }
423
+
424
+ template<typename T, typename A>
425
+ typename ebpps_sample<T, A>::const_iterator ebpps_sample<T, A>::end() const {
426
+ return const_iterator(nullptr);
427
+ }
428
+
429
+
430
+ // -------- ebpps_sketch::const_iterator implementation ---------
431
+
432
+ template<typename T, typename A>
433
+ ebpps_sample<T, A>::const_iterator::const_iterator(const ebpps_sample* sample) :
434
+ sample_(sample),
435
+ idx_(0),
436
+ use_partial_(false)
437
+ {
438
+ if (sample == nullptr)
439
+ return;
440
+
441
+ // determine in advance if we use the partial item
442
+ double c_int;
443
+ const double c_frac = std::modf(sample_->get_c(), &c_int);
444
+ use_partial_ = sample->next_double() < c_frac;
445
+
446
+ // sample with no items
447
+ if (sample_->data_.size() == 0 && use_partial_) {
448
+ idx_ = PARTIAL_IDX;
449
+ }
450
+
451
+ if (sample_->c_== 0.0 || (sample_->data_.size() == 0 && !sample_->has_partial_item())) { sample_ = nullptr; }
452
+ }
453
+
454
+ template<typename T, typename A>
455
+ ebpps_sample<T, A>::const_iterator::const_iterator(const const_iterator& other) :
456
+ sample_(other.sample_),
457
+ idx_(other.idx_),
458
+ use_partial_(other.use_partial_)
459
+ {}
460
+
461
+ template<typename T, typename A>
462
+ typename ebpps_sample<T, A>::const_iterator& ebpps_sample<T, A>::const_iterator::operator++() {
463
+ if (sample_ == nullptr)
464
+ return *this;
465
+ else if (idx_ == PARTIAL_IDX) {
466
+ idx_ = sample_->data_.size();
467
+ sample_ = nullptr;
468
+ return * this;
469
+ }
470
+
471
+ ++idx_;
472
+
473
+ if (idx_ == sample_->data_.size()) {
474
+ if (use_partial_)
475
+ idx_ = PARTIAL_IDX;
476
+ else
477
+ sample_ = nullptr;
478
+ }
479
+
480
+ return *this;
481
+ }
482
+
483
+ template<typename T, typename A>
484
+ typename ebpps_sample<T, A>::const_iterator& ebpps_sample<T, A>::const_iterator::operator++(int) {
485
+ const_iterator tmp(*this);
486
+ operator++();
487
+ return tmp;
488
+ }
489
+
490
+ template<typename T, typename A>
491
+ bool ebpps_sample<T, A>::const_iterator::operator==(const const_iterator& other) const {
492
+ if (sample_ != other.sample_) return false;
493
+ if (sample_ == nullptr) return true; // end (and we know other.sample_ is also null)
494
+ return idx_ == other.idx_;
495
+ }
496
+
497
+ template<typename T, typename A>
498
+ bool ebpps_sample<T, A>::const_iterator::operator!=(const const_iterator& other) const {
499
+ return !operator==(other);
500
+ }
501
+
502
+ template<typename T, typename A>
503
+ auto ebpps_sample<T, A>::const_iterator::operator*() const -> reference {
504
+ if (idx_ == PARTIAL_IDX)
505
+ return *(sample_->partial_item_);
506
+ else
507
+ return sample_->data_[idx_];
508
+ }
509
+
510
+ template<typename T, typename A>
511
+ auto ebpps_sample<T, A>::const_iterator::operator->() const -> pointer {
512
+ return **this;
513
+ }
514
+
515
+ template<typename T, typename A>
516
+ class ebpps_sample<T, A>::items_deleter {
517
+ public:
518
+ items_deleter(const A& allocator, bool destroy, size_t num): allocator_(allocator), destroy_(destroy), num_(num) {}
519
+ void operator() (T* ptr) {
520
+ if (ptr != nullptr) {
521
+ if (destroy_) {
522
+ for (size_t i = 0; i < num_; ++i) {
523
+ ptr[i].~T();
524
+ }
525
+ }
526
+ allocator_.deallocate(ptr, num_);
527
+ }
528
+ }
529
+ void set_destroy(bool destroy) { destroy_ = destroy; }
530
+ private:
531
+ A allocator_;
532
+ bool destroy_;
533
+ size_t num_;
534
+ };
535
+
536
+
537
+ } // namespace datasketches
538
+
539
+ #endif // _EBPPS_SAMPLE_IMPL_HPP_