datasketches 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (245) 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 +3 -3
  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 +23 -20
  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/README.md +1 -3
  15. data/vendor/datasketches-cpp/common/CMakeLists.txt +12 -11
  16. data/vendor/datasketches-cpp/common/include/MurmurHash3.h +25 -27
  17. data/vendor/datasketches-cpp/common/include/common_defs.hpp +15 -10
  18. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +11 -2
  19. data/vendor/datasketches-cpp/common/include/kolmogorov_smirnov.hpp +9 -6
  20. data/vendor/datasketches-cpp/common/include/memory_operations.hpp +5 -4
  21. data/vendor/datasketches-cpp/common/include/optional.hpp +148 -0
  22. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view.hpp +95 -2
  23. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view_impl.hpp +1 -1
  24. data/vendor/datasketches-cpp/common/include/serde.hpp +69 -20
  25. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +2 -2
  26. data/vendor/datasketches-cpp/common/test/integration_test.cpp +6 -0
  27. data/vendor/datasketches-cpp/common/test/optional_test.cpp +85 -0
  28. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +14 -14
  29. data/vendor/datasketches-cpp/{python/src/__init__.py → count/CMakeLists.txt} +25 -1
  30. data/vendor/datasketches-cpp/count/include/count_min.hpp +405 -0
  31. data/vendor/datasketches-cpp/count/include/count_min_impl.hpp +497 -0
  32. data/vendor/datasketches-cpp/{MANIFEST.in → count/test/CMakeLists.txt} +23 -20
  33. data/vendor/datasketches-cpp/count/test/count_min_allocation_test.cpp +155 -0
  34. data/vendor/datasketches-cpp/count/test/count_min_test.cpp +303 -0
  35. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +14 -20
  36. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +7 -4
  37. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +17 -17
  38. data/vendor/datasketches-cpp/cpc/include/cpc_confidence.hpp +3 -3
  39. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +40 -40
  40. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +14 -11
  41. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +35 -11
  42. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +8 -8
  43. data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +16 -8
  44. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +3 -2
  45. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +5 -5
  46. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +20 -7
  47. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_deserialize_from_java_test.cpp +60 -0
  48. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_serialize_for_java.cpp +38 -0
  49. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +4 -29
  50. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +4 -4
  51. data/vendor/datasketches-cpp/{tox.ini → density/CMakeLists.txt} +24 -8
  52. data/vendor/datasketches-cpp/density/include/density_sketch.hpp +256 -0
  53. data/vendor/datasketches-cpp/density/include/density_sketch_impl.hpp +543 -0
  54. data/vendor/datasketches-cpp/{python/datasketches/__init__.py → density/test/CMakeLists.txt} +15 -3
  55. data/vendor/datasketches-cpp/density/test/density_sketch_test.cpp +244 -0
  56. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +21 -9
  57. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +6 -4
  58. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +9 -3
  59. data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +14 -1
  60. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_deserialize_from_java_test.cpp +95 -0
  61. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_serialize_for_java.cpp +83 -0
  62. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +3 -42
  63. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +2 -2
  64. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +3 -1
  65. data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +19 -11
  66. data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +2 -5
  67. data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +19 -7
  68. data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +1 -1
  69. data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +98 -42
  70. data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +2 -0
  71. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +94 -61
  72. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +20 -8
  73. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +4 -4
  74. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +3 -1
  75. data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +3 -21
  76. data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +8 -0
  77. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +14 -18
  78. data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +1 -1
  79. data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +8 -2
  80. data/vendor/datasketches-cpp/hll/include/hll.hpp +79 -65
  81. data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +14 -1
  82. data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +7 -1
  83. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +0 -68
  84. data/vendor/datasketches-cpp/hll/test/hll_sketch_deserialize_from_java_test.cpp +69 -0
  85. data/vendor/datasketches-cpp/hll/test/hll_sketch_serialize_for_java.cpp +52 -0
  86. data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +0 -1
  87. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +2 -2
  88. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +79 -53
  89. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +61 -132
  90. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +14 -1
  91. data/vendor/datasketches-cpp/kll/test/kll_sketch_deserialize_from_java_test.cpp +103 -0
  92. data/vendor/datasketches-cpp/kll/test/kll_sketch_serialize_for_java.cpp +62 -0
  93. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +5 -40
  94. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch.hpp +76 -54
  95. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch_impl.hpp +66 -136
  96. data/vendor/datasketches-cpp/quantiles/test/CMakeLists.txt +14 -1
  97. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_deserialize_from_java_test.cpp +84 -0
  98. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_serialize_for_java.cpp +52 -0
  99. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_test.cpp +15 -39
  100. data/vendor/datasketches-cpp/req/include/req_common.hpp +7 -3
  101. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +2 -4
  102. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +105 -26
  103. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +50 -111
  104. data/vendor/datasketches-cpp/req/test/CMakeLists.txt +14 -1
  105. data/vendor/datasketches-cpp/req/test/req_sketch_deserialize_from_java_test.cpp +55 -0
  106. data/vendor/datasketches-cpp/{tuple/include/array_of_doubles_intersection_impl.hpp → req/test/req_sketch_serialize_for_java.cpp} +12 -7
  107. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +3 -89
  108. data/vendor/datasketches-cpp/sampling/CMakeLists.txt +4 -0
  109. data/vendor/datasketches-cpp/sampling/include/ebpps_sample.hpp +210 -0
  110. data/vendor/datasketches-cpp/sampling/include/ebpps_sample_impl.hpp +535 -0
  111. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch.hpp +281 -0
  112. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch_impl.hpp +531 -0
  113. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +89 -32
  114. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +33 -19
  115. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +13 -10
  116. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +23 -19
  117. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +55 -8
  118. data/vendor/datasketches-cpp/sampling/test/ebpps_allocation_test.cpp +96 -0
  119. data/vendor/datasketches-cpp/sampling/test/ebpps_sample_test.cpp +137 -0
  120. data/vendor/datasketches-cpp/sampling/test/ebpps_sketch_test.cpp +266 -0
  121. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_deserialize_from_java_test.cpp +81 -0
  122. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_serialize_for_java.cpp +54 -0
  123. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +33 -51
  124. data/vendor/datasketches-cpp/sampling/test/var_opt_union_deserialize_from_java_test.cpp +50 -0
  125. data/vendor/datasketches-cpp/sampling/test/var_opt_union_serialize_for_java.cpp +56 -0
  126. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +0 -20
  127. data/vendor/datasketches-cpp/theta/CMakeLists.txt +1 -0
  128. data/vendor/datasketches-cpp/theta/include/bit_packing.hpp +6279 -0
  129. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_sampled_sets.hpp +1 -0
  130. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_theta_sketched_sets.hpp +7 -6
  131. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser.hpp +14 -8
  132. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +60 -46
  133. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +20 -5
  134. data/vendor/datasketches-cpp/theta/include/theta_constants.hpp +10 -4
  135. data/vendor/datasketches-cpp/theta/include/theta_helpers.hpp +3 -1
  136. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +13 -5
  137. data/vendor/datasketches-cpp/theta/include/theta_intersection_base_impl.hpp +5 -5
  138. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +3 -3
  139. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity.hpp +2 -1
  140. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity_base.hpp +1 -0
  141. data/vendor/datasketches-cpp/theta/include/theta_set_difference_base_impl.hpp +1 -1
  142. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +180 -33
  143. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +430 -130
  144. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +17 -10
  145. data/vendor/datasketches-cpp/theta/include/theta_union_base_impl.hpp +10 -10
  146. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +3 -3
  147. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base.hpp +21 -6
  148. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base_impl.hpp +13 -3
  149. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +15 -1
  150. data/vendor/datasketches-cpp/theta/test/bit_packing_test.cpp +80 -0
  151. data/vendor/datasketches-cpp/theta/test/theta_sketch_deserialize_from_java_test.cpp +57 -0
  152. data/vendor/datasketches-cpp/theta/test/theta_sketch_serialize_for_java.cpp +61 -0
  153. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +39 -188
  154. data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +25 -0
  155. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +8 -7
  156. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +19 -144
  157. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b.hpp → array_tuple_a_not_b.hpp} +24 -16
  158. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b_impl.hpp → array_tuple_a_not_b_impl.hpp} +4 -4
  159. data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection.hpp +65 -0
  160. data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection_impl.hpp +31 -0
  161. data/vendor/datasketches-cpp/tuple/include/array_tuple_sketch.hpp +237 -0
  162. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_sketch_impl.hpp → array_tuple_sketch_impl.hpp} +40 -41
  163. data/vendor/datasketches-cpp/tuple/include/array_tuple_union.hpp +81 -0
  164. data/vendor/datasketches-cpp/tuple/include/array_tuple_union_impl.hpp +43 -0
  165. data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +11 -2
  166. data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +17 -10
  167. data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +2 -1
  168. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +95 -32
  169. data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +2 -1
  170. data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +19 -11
  171. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +16 -1
  172. data/vendor/datasketches-cpp/tuple/test/aod_sketch_deserialize_from_java_test.cpp +76 -0
  173. data/vendor/datasketches-cpp/tuple/test/aod_sketch_serialize_for_java.cpp +62 -0
  174. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +5 -129
  175. data/vendor/datasketches-cpp/tuple/test/engagement_test.cpp +85 -89
  176. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +3 -1
  177. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_deserialize_from_java_test.cpp +47 -0
  178. data/vendor/datasketches-cpp/{python/src/datasketches.cpp → tuple/test/tuple_sketch_serialize_for_java.cpp} +16 -30
  179. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +1 -1
  180. data/vendor/datasketches-cpp/version.cfg.in +1 -1
  181. metadata +61 -79
  182. data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
  183. data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
  184. data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
  185. data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
  186. data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
  187. data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
  188. data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
  189. data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
  190. data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
  191. data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
  192. data/vendor/datasketches-cpp/pyproject.toml +0 -23
  193. data/vendor/datasketches-cpp/python/CMakeLists.txt +0 -81
  194. data/vendor/datasketches-cpp/python/README.md +0 -85
  195. data/vendor/datasketches-cpp/python/datasketches/PySerDe.py +0 -104
  196. data/vendor/datasketches-cpp/python/include/py_serde.hpp +0 -113
  197. data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +0 -345
  198. data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +0 -354
  199. data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +0 -346
  200. data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +0 -463
  201. data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +0 -403
  202. data/vendor/datasketches-cpp/python/pybind11Path.cmd +0 -21
  203. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +0 -90
  204. data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +0 -128
  205. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +0 -134
  206. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +0 -210
  207. data/vendor/datasketches-cpp/python/src/ks_wrapper.cpp +0 -68
  208. data/vendor/datasketches-cpp/python/src/py_serde.cpp +0 -111
  209. data/vendor/datasketches-cpp/python/src/quantiles_wrapper.cpp +0 -204
  210. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +0 -215
  211. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +0 -172
  212. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +0 -490
  213. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +0 -173
  214. data/vendor/datasketches-cpp/python/tests/__init__.py +0 -16
  215. data/vendor/datasketches-cpp/python/tests/cpc_test.py +0 -64
  216. data/vendor/datasketches-cpp/python/tests/fi_test.py +0 -110
  217. data/vendor/datasketches-cpp/python/tests/hll_test.py +0 -130
  218. data/vendor/datasketches-cpp/python/tests/kll_test.py +0 -125
  219. data/vendor/datasketches-cpp/python/tests/quantiles_test.py +0 -126
  220. data/vendor/datasketches-cpp/python/tests/req_test.py +0 -126
  221. data/vendor/datasketches-cpp/python/tests/theta_test.py +0 -146
  222. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +0 -148
  223. data/vendor/datasketches-cpp/python/tests/vo_test.py +0 -125
  224. data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
  225. data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
  226. data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
  227. data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
  228. data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
  229. data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +0 -67
  230. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
  231. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
  232. data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
  233. data/vendor/datasketches-cpp/setup.py +0 -110
  234. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
  235. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
  236. data/vendor/datasketches-cpp/theta/test/theta_compact_exact_from_java.sk +0 -0
  237. data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
  238. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +0 -52
  239. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +0 -81
  240. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +0 -43
  241. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +0 -1
  242. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
  243. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
  244. data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
  245. data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +0 -1
@@ -0,0 +1,244 @@
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
+ #include <cmath>
21
+ #include <catch2/catch.hpp>
22
+
23
+ #include <density_sketch.hpp>
24
+
25
+ namespace datasketches {
26
+
27
+ TEST_CASE("density sketch: empty", "[density_sketch]") {
28
+ density_sketch<float> sketch(10, 3);
29
+ REQUIRE(sketch.is_empty());
30
+ REQUIRE_THROWS_AS(sketch.get_estimate({0, 0, 0}), std::runtime_error);
31
+ }
32
+
33
+ TEST_CASE("density sketch: one item", "[density_sketch]") {
34
+ density_sketch<float> sketch(10, 3);
35
+
36
+ // dimension mismatch
37
+ REQUIRE_THROWS_AS(sketch.update(std::vector<float>({0, 0})), std::invalid_argument);
38
+
39
+ sketch.update(std::vector<float>({0, 0, 0}));
40
+ REQUIRE_FALSE(sketch.is_empty());
41
+ REQUIRE_FALSE(sketch.is_estimation_mode());
42
+ REQUIRE(sketch.get_estimate({0, 0, 0}) == 1);
43
+ REQUIRE(sketch.get_estimate({0.01, 0.01, 0.01}) > 0.95);
44
+ REQUIRE(sketch.get_estimate({1, 1, 1}) < 0.05);
45
+ }
46
+
47
+ TEST_CASE("density sketch: merge", "[density_sketch]") {
48
+ density_sketch<float> sketch1(10, 4);
49
+ sketch1.update(std::vector<float>({0, 0, 0, 0}));
50
+ sketch1.update(std::vector<float>({1, 2, 3, 4}));
51
+
52
+ density_sketch<float> sketch2(10, 4);
53
+ sketch2.update(std::vector<float>({5, 6, 7, 8}));
54
+
55
+ sketch1.merge(sketch2);
56
+
57
+ REQUIRE(sketch1.get_n() == 3);
58
+ REQUIRE(sketch1.get_num_retained() == 3);
59
+ }
60
+
61
+ TEST_CASE("density sketch: iterator", "[density_sketch]") {
62
+ density_sketch<float> sketch(10, 3);
63
+ unsigned n = 1000;
64
+ for (unsigned i = 1; i <= n; ++i) sketch.update(std::vector<float>(3, i));
65
+ REQUIRE(sketch.get_n() == n);
66
+ REQUIRE(sketch.is_estimation_mode());
67
+ //std::cout << sketch.to_string(true, true);
68
+ unsigned count = 0;
69
+ for (auto pair: sketch) {
70
+ ++count;
71
+ // just to assert something about the output
72
+ REQUIRE(pair.first.size() == sketch.get_dim());
73
+ }
74
+ REQUIRE(count == sketch.get_num_retained());
75
+ }
76
+
77
+ // spherical kernel for testing, returns 1 for vectors within radius and 0 otherwise
78
+ template<typename T>
79
+ struct spherical_kernel {
80
+ spherical_kernel(T radius = 1.0) : _radius_squared(radius * radius) {}
81
+ T operator()(const std::vector<T>& v1, const std::vector<T>& v2) const {
82
+ return std::inner_product(v1.begin(), v1.end(), v2.begin(), 0.0, std::plus<T>(), [](T a, T b){return (a-b)*(a-b);}) <= _radius_squared ? 1.0 : 0.0;
83
+ }
84
+ private:
85
+ T _radius_squared;
86
+ };
87
+
88
+ TEST_CASE("custom kernel", "[density_sketch]") {
89
+ density_sketch<float, spherical_kernel<float>> sketch(10, 3, spherical_kernel<float>(0.5));
90
+
91
+ // update with (1,1,1) and test points inside and outside the kernel
92
+ sketch.update(std::vector<float>(3, 1.0));
93
+ REQUIRE(sketch.get_estimate(std::vector<float>(3, 1.001)) == 1.0);
94
+ REQUIRE(sketch.get_estimate(std::vector<float>(3, 2.0)) == 0.0);
95
+
96
+ // rest of test follows iterator test above
97
+ unsigned n = 1000;
98
+ for (unsigned i = 2; i <= n; ++i) sketch.update(std::vector<float>(3, i));
99
+ REQUIRE(sketch.get_n() == n);
100
+ REQUIRE(sketch.is_estimation_mode());
101
+ unsigned count = 0;
102
+ for (auto pair: sketch) {
103
+ ++count;
104
+ // just to assert something about the output
105
+ REQUIRE(pair.first.size() == sketch.get_dim());
106
+ }
107
+ REQUIRE(count == sketch.get_num_retained());
108
+ }
109
+
110
+ TEST_CASE("serialize empty", "[density_sketch]") {
111
+ density_sketch<double> sk(10, 2);
112
+ auto bytes = sk.serialize();
113
+ auto sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
114
+ REQUIRE(sk2.is_empty());
115
+ REQUIRE(!sk2.is_estimation_mode());
116
+ REQUIRE(sk.get_k() == sk2.get_k());
117
+ REQUIRE(sk.get_dim() == sk2.get_dim());
118
+ REQUIRE(sk.get_n() == sk2.get_n());
119
+ REQUIRE(sk.get_num_retained() == sk2.get_num_retained());
120
+
121
+ std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
122
+ sk.serialize(s);
123
+ auto sk3 = density_sketch<double>::deserialize(s);
124
+ REQUIRE(sk3.is_empty());
125
+ REQUIRE(!sk3.is_estimation_mode());
126
+ REQUIRE(sk.get_k() == sk3.get_k());
127
+ REQUIRE(sk.get_dim() == sk3.get_dim());
128
+ REQUIRE(sk.get_n() == sk3.get_n());
129
+ REQUIRE(sk.get_num_retained() == sk3.get_num_retained());
130
+ }
131
+
132
+ TEST_CASE("serialize bytes", "[density_sketch]") {
133
+ uint16_t k = 10;
134
+ uint32_t dim = 3;
135
+ density_sketch<double> sk(k, dim);
136
+
137
+ for (uint16_t i = 0; i < k; ++i) {
138
+ double val = static_cast<double>(i);
139
+ sk.update(std::vector<double>({val, std::sqrt(val), -val}));
140
+ }
141
+ REQUIRE(!sk.is_estimation_mode());
142
+
143
+ // exact mode
144
+ auto bytes = sk.serialize();
145
+ auto sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
146
+ REQUIRE(!sk2.is_empty());
147
+ REQUIRE(!sk2.is_estimation_mode());
148
+ REQUIRE(sk.get_k() == sk2.get_k());
149
+ REQUIRE(sk.get_dim() == sk2.get_dim());
150
+ REQUIRE(sk.get_n() == sk2.get_n());
151
+ REQUIRE(sk.get_num_retained() == sk2.get_num_retained());
152
+ auto it1 = sk.begin();
153
+ auto it2 = sk2.begin();
154
+ while (it1 != sk.end()) {
155
+ REQUIRE(it1->first[0] == it2->first[0]);
156
+ REQUIRE(it1->second == it2->second);
157
+ ++it1;
158
+ ++it2;
159
+ }
160
+
161
+ // estimation mode
162
+ size_t n = 1031;
163
+ for (uint32_t i = k; i < n; ++i) {
164
+ double val = static_cast<double>(i);
165
+ sk.update(std::vector<double>({val, std::sqrt(val), -val}));
166
+ }
167
+ REQUIRE(sk.is_estimation_mode());
168
+
169
+ bytes = sk.serialize();
170
+ sk2 = density_sketch<double>::deserialize(bytes.data(), bytes.size());
171
+ REQUIRE(!sk2.is_empty());
172
+ REQUIRE(sk2.is_estimation_mode());
173
+ REQUIRE(sk.get_k() == sk2.get_k());
174
+ REQUIRE(sk.get_dim() == sk2.get_dim());
175
+ REQUIRE(sk.get_n() == sk2.get_n());
176
+ REQUIRE(sk.get_num_retained() == sk2.get_num_retained());
177
+ it1 = sk.begin();
178
+ it2 = sk2.begin();
179
+ while (it1 != sk.end()) {
180
+ REQUIRE(it1->first[0] == it2->first[0]);
181
+ REQUIRE(it1->second == it2->second);
182
+ ++it1;
183
+ ++it2;
184
+ }
185
+ }
186
+
187
+ TEST_CASE("serialize stream", "[density_sketch]") {
188
+ uint16_t k = 10;
189
+ uint32_t dim = 3;
190
+ density_sketch<float> sk(k, dim);
191
+
192
+ for (uint16_t i = 0; i < k; ++i) {
193
+ float val = static_cast<float>(i);
194
+ sk.update(std::vector<float>({val, std::sin(val), std::cos(val)}));
195
+ }
196
+ REQUIRE(!sk.is_estimation_mode());
197
+
198
+ // exact mode
199
+ std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
200
+ sk.serialize(s);
201
+ auto sk2 = density_sketch<float>::deserialize(s);
202
+ REQUIRE(!sk2.is_empty());
203
+ REQUIRE(!sk2.is_estimation_mode());
204
+ REQUIRE(sk.get_k() == sk2.get_k());
205
+ REQUIRE(sk.get_dim() == sk2.get_dim());
206
+ REQUIRE(sk.get_n() == sk2.get_n());
207
+ REQUIRE(sk.get_num_retained() == sk2.get_num_retained());
208
+ auto it1 = sk.begin();
209
+ auto it2 = sk2.begin();
210
+ while (it1 != sk.end()) {
211
+ REQUIRE(it1->first[0] == it2->first[0]);
212
+ REQUIRE(it1->second == it2->second);
213
+ ++it1;
214
+ ++it2;
215
+ }
216
+
217
+ // estimation mode
218
+ size_t n = 1031;
219
+ for (uint32_t i = k; i < n; ++i) {
220
+ float val = static_cast<float>(i);
221
+ sk.update(std::vector<float>({val, std::sqrt(val), -val}));
222
+ }
223
+ REQUIRE(sk.is_estimation_mode());
224
+
225
+ std::stringstream s2(std::ios::in | std::ios::out | std::ios::binary);
226
+ sk.serialize(s2);
227
+ sk2 = density_sketch<float>::deserialize(s2);
228
+ REQUIRE(!sk2.is_empty());
229
+ REQUIRE(sk2.is_estimation_mode());
230
+ REQUIRE(sk.get_k() == sk2.get_k());
231
+ REQUIRE(sk.get_dim() == sk2.get_dim());
232
+ REQUIRE(sk.get_n() == sk2.get_n());
233
+ REQUIRE(sk.get_num_retained() == sk2.get_num_retained());
234
+ it1 = sk.begin();
235
+ it2 = sk2.begin();
236
+ while (it1 != sk.end()) {
237
+ REQUIRE(it1->first[0] == it2->first[0]);
238
+ REQUIRE(it1->second == it2->second);
239
+ ++it1;
240
+ ++it2;
241
+ }
242
+ }
243
+
244
+ } /* namespace datasketches */
@@ -32,15 +32,19 @@
32
32
 
33
33
  namespace datasketches {
34
34
 
35
- /*
35
+ /// Frequent items error type
36
+ enum frequent_items_error_type {
37
+ NO_FALSE_POSITIVES, ///< include an item in the result list if get_lower_bound(item) &gt; threshold
38
+ NO_FALSE_NEGATIVES ///< include an item in the result list if get_upper_bound(item) &gt; threshold
39
+ };
40
+
41
+ /**
42
+ * Frequent Items sketch.
43
+ *
36
44
  * Based on Java implementation here:
37
45
  * https://github.com/apache/datasketches-java/blob/master/src/main/java/org/apache/datasketches/frequencies/ItemsSketch.java
38
- * author Alexander Saydakov
46
+ * @author Alexander Saydakov
39
47
  */
40
-
41
- enum frequent_items_error_type { NO_FALSE_POSITIVES, NO_FALSE_NEGATIVES };
42
-
43
- // type W for weight must be an arithmetic type (integral or floating point)
44
48
  template<
45
49
  typename T,
46
50
  typename W = uint64_t,
@@ -49,6 +53,7 @@ template<
49
53
  typename A = std::allocator<T>
50
54
  >
51
55
  class frequent_items_sketch {
56
+ static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
52
57
  public:
53
58
 
54
59
  static const uint8_t LG_MIN_MAP_SIZE = 3;
@@ -194,7 +199,7 @@ public:
194
199
  * There may be items omitted from the set with true frequencies greater than the
195
200
  * threshold (false negatives).</p>
196
201
  *
197
- * @param error_type determines whether no false positives or no false negatives are desired.
202
+ * @param err_type determines whether no false positives or no false negatives are desired.
198
203
  * @return an array of frequent items
199
204
  */
200
205
  vector_row get_frequent_items(frequent_items_error_type err_type) const;
@@ -217,7 +222,7 @@ public:
217
222
  * There may be items omitted from the set with true frequencies greater than the
218
223
  * threshold (false negatives).</p>
219
224
  *
220
- * @param error_type determines whether no false positives or no false negatives are desired.
225
+ * @param err_type determines whether no false positives or no false negatives are desired.
221
226
  * @param threshold to include items in the result list
222
227
  * @return an array of frequent items
223
228
  */
@@ -293,7 +298,9 @@ private:
293
298
  static const uint8_t PREAMBLE_LONGS_EMPTY = 1;
294
299
  static const uint8_t PREAMBLE_LONGS_NONEMPTY = 4;
295
300
  static constexpr double EPSILON_FACTOR = 3.5;
296
- enum flags { IS_EMPTY };
301
+ // due to a mistake different bits were used in C++ and Java to indicate empty sketch
302
+ // therefore both are set and checked for compatibility with historical binary format
303
+ enum flags { IS_EMPTY_1 = 0, IS_EMPTY_2 = 2 };
297
304
  W total_weight;
298
305
  W offset;
299
306
  reverse_purge_hash_map<T, W, H, E, A> map;
@@ -318,14 +325,19 @@ private:
318
325
  class items_deleter;
319
326
  };
320
327
 
328
+ /// Row in the output from #get_frequent_items
321
329
  template<typename T, typename W, typename H, typename E, typename A>
322
330
  class frequent_items_sketch<T, W, H, E, A>::row {
323
331
  public:
324
332
  row(const T* item, W weight, W offset):
325
333
  item(item), weight(weight), offset(offset) {}
334
+ /// @return item
326
335
  const T& get_item() const { return *item; }
336
+ /// @return frequency (weight) estimate
327
337
  W get_estimate() const { return weight + offset; }
338
+ /// @return estimate lower bound
328
339
  W get_lower_bound() const { return weight; }
340
+ /// @return estimate upper bound
329
341
  W get_upper_bound() const { return weight + offset; }
330
342
  private:
331
343
  const T* item;
@@ -174,7 +174,8 @@ void frequent_items_sketch<T, W, H, E, A>::serialize(std::ostream& os, const Ser
174
174
  const uint8_t lg_cur_size = map.get_lg_cur_size();
175
175
  write(os, lg_cur_size);
176
176
  const uint8_t flags_byte(
177
- (is_empty() ? 1 << flags::IS_EMPTY : 0)
177
+ (is_empty() ? 1 << flags::IS_EMPTY_1 : 0)
178
+ | (is_empty() ? 1 << flags::IS_EMPTY_2 : 0)
178
179
  );
179
180
  write(os, flags_byte);
180
181
  const uint16_t unused16 = 0;
@@ -234,7 +235,8 @@ auto frequent_items_sketch<T, W, H, E, A>::serialize(unsigned header_size_bytes,
234
235
  const uint8_t lg_cur_size = map.get_lg_cur_size();
235
236
  ptr += copy_to_mem(lg_cur_size, ptr);
236
237
  const uint8_t flags_byte(
237
- (is_empty() ? 1 << flags::IS_EMPTY : 0)
238
+ (is_empty() ? 1 << flags::IS_EMPTY_1 : 0)
239
+ | (is_empty() ? 1 << flags::IS_EMPTY_2 : 0)
238
240
  );
239
241
  ptr += copy_to_mem(flags_byte, ptr);
240
242
  ptr += sizeof(uint16_t); // unused
@@ -298,7 +300,7 @@ frequent_items_sketch<T, W, H, E, A> frequent_items_sketch<T, W, H, E, A>::deser
298
300
  const auto flags_byte = read<uint8_t>(is);
299
301
  read<uint16_t>(is); // unused
300
302
 
301
- const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
303
+ const bool is_empty = (flags_byte & (1 << flags::IS_EMPTY_1)) | (flags_byte & (1 << flags::IS_EMPTY_2));
302
304
 
303
305
  check_preamble_longs(preamble_longs, is_empty);
304
306
  check_serial_version(serial_version);
@@ -352,7 +354,7 @@ frequent_items_sketch<T, W, H, E, A> frequent_items_sketch<T, W, H, E, A>::deser
352
354
  ptr += copy_from_mem(ptr, flags_byte);
353
355
  ptr += sizeof(uint16_t); // unused
354
356
 
355
- const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
357
+ const bool is_empty = (flags_byte & (1 << flags::IS_EMPTY_1)) | (flags_byte & (1 << flags::IS_EMPTY_2));
356
358
 
357
359
  check_preamble_longs(preamble_longs, is_empty);
358
360
  check_serial_version(serial_version);
@@ -91,8 +91,14 @@ private:
91
91
 
92
92
  // This iterator uses strides based on golden ratio to avoid clustering during merge
93
93
  template<typename K, typename V, typename H, typename E, typename A>
94
- class reverse_purge_hash_map<K, V, H, E, A>::iterator: public std::iterator<std::input_iterator_tag, K> {
94
+ class reverse_purge_hash_map<K, V, H, E, A>::iterator {
95
95
  public:
96
+ using iterator_category = std::input_iterator_tag;
97
+ using value_type = std::pair<K&, V>;
98
+ using difference_type = void;
99
+ using pointer = void;
100
+ using reference = const value_type;
101
+
96
102
  friend class reverse_purge_hash_map<K, V, H, E, A>;
97
103
  iterator& operator++() {
98
104
  ++count;
@@ -107,8 +113,8 @@ public:
107
113
  iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
108
114
  bool operator==(const iterator& rhs) const { return count == rhs.count; }
109
115
  bool operator!=(const iterator& rhs) const { return count != rhs.count; }
110
- const std::pair<K&, V> operator*() const {
111
- return std::pair<K&, V>(map->keys_[index], map->values_[index]);
116
+ reference operator*() const {
117
+ return value_type(map->keys_[index], map->values_[index]);
112
118
  }
113
119
  private:
114
120
  static constexpr double GOLDEN_RATIO_RECIPROCAL = 0.6180339887498949; // = (sqrt(5) - 1) / 2
@@ -20,7 +20,6 @@ add_executable(fi_test)
20
20
  target_link_libraries(fi_test fi common_test_lib)
21
21
 
22
22
  set_target_properties(fi_test PROPERTIES
23
- CXX_STANDARD 11
24
23
  CXX_STANDARD_REQUIRED YES
25
24
  )
26
25
 
@@ -42,3 +41,17 @@ target_sources(fi_test
42
41
  frequent_items_sketch_test.cpp
43
42
  frequent_items_sketch_custom_type_test.cpp
44
43
  )
44
+
45
+ if (SERDE_COMPAT)
46
+ target_sources(fi_test
47
+ PRIVATE
48
+ frequent_items_sketch_deserialize_from_java_test.cpp
49
+ )
50
+ endif()
51
+
52
+ if (GENERATE)
53
+ target_sources(fi_test
54
+ PRIVATE
55
+ frequent_items_sketch_serialize_for_java.cpp
56
+ )
57
+ endif()
@@ -0,0 +1,95 @@
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
+ #include <catch2/catch.hpp>
21
+ #include <fstream>
22
+ #include <frequent_items_sketch.hpp>
23
+
24
+ namespace datasketches {
25
+
26
+ // assume the binary sketches for this test have been generated by datasketches-java code
27
+ // in the subdirectory called "java" in the root directory of this project
28
+ static std::string testBinaryInputPath = std::string(TEST_BINARY_INPUT_PATH) + "../../java/";
29
+
30
+ TEST_CASE("frequent longs", "[serde_compat]") {
31
+ const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
32
+ for (const unsigned n: n_arr) {
33
+ std::ifstream is;
34
+ is.exceptions(std::ios::failbit | std::ios::badbit);
35
+ is.open(testBinaryInputPath + "frequent_long_n" + std::to_string(n) + "_java.sk", std::ios::binary);
36
+ const auto sketch = frequent_items_sketch<int64_t>::deserialize(is);
37
+ REQUIRE(sketch.is_empty() == (n == 0));
38
+ if (n > 10) {
39
+ REQUIRE(sketch.get_maximum_error() > 0);
40
+ } else {
41
+ REQUIRE(sketch.get_maximum_error() == 0);
42
+ }
43
+ REQUIRE(sketch.get_total_weight() == n);
44
+ }
45
+ }
46
+
47
+ TEST_CASE("frequent strings", "[serde_compat]") {
48
+ const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
49
+ for (const unsigned n: n_arr) {
50
+ std::ifstream is;
51
+ is.exceptions(std::ios::failbit | std::ios::badbit);
52
+ is.open(testBinaryInputPath + "frequent_string_n" + std::to_string(n) + "_java.sk", std::ios::binary);
53
+ const auto sketch = frequent_items_sketch<std::string>::deserialize(is);
54
+ REQUIRE(sketch.is_empty() == (n == 0));
55
+ if (n > 10) {
56
+ REQUIRE(sketch.get_maximum_error() > 0);
57
+ } else {
58
+ REQUIRE(sketch.get_maximum_error() == 0);
59
+ }
60
+ REQUIRE(sketch.get_total_weight() == n);
61
+ }
62
+ }
63
+
64
+ TEST_CASE("frequent strings ascii", "[serde_compat]") {
65
+ std::ifstream is;
66
+ is.exceptions(std::ios::failbit | std::ios::badbit);
67
+ is.open(testBinaryInputPath + "frequent_string_ascii_java.sk", std::ios::binary);
68
+ const auto sketch = frequent_items_sketch<std::string>::deserialize(is);
69
+ REQUIRE_FALSE(sketch.is_empty());
70
+ REQUIRE(sketch.get_maximum_error() == 0);
71
+ REQUIRE(sketch.get_total_weight() == 10);
72
+ REQUIRE(sketch.get_estimate("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa") == 1);
73
+ REQUIRE(sketch.get_estimate("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb") == 2);
74
+ REQUIRE(sketch.get_estimate("ccccccccccccccccccccccccccccc") == 3);
75
+ REQUIRE(sketch.get_estimate("ddddddddddddddddddddddddddddd") == 4);
76
+ }
77
+
78
+ TEST_CASE("frequent strings utf8", "[serde_compat]") {
79
+ std::ifstream is;
80
+ is.exceptions(std::ios::failbit | std::ios::badbit);
81
+ is.open(testBinaryInputPath + "frequent_string_utf8_java.sk", std::ios::binary);
82
+ const auto sketch = frequent_items_sketch<std::string>::deserialize(is);
83
+ REQUIRE_FALSE(sketch.is_empty());
84
+ REQUIRE(sketch.get_maximum_error() == 0);
85
+ REQUIRE(sketch.get_total_weight() == 28);
86
+ REQUIRE(sketch.get_estimate("абвгд") == 1);
87
+ REQUIRE(sketch.get_estimate("еёжзи") == 2);
88
+ REQUIRE(sketch.get_estimate("йклмн") == 3);
89
+ REQUIRE(sketch.get_estimate("опрст") == 4);
90
+ REQUIRE(sketch.get_estimate("уфхцч") == 5);
91
+ REQUIRE(sketch.get_estimate("шщъыь") == 6);
92
+ REQUIRE(sketch.get_estimate("эюя") == 7);
93
+ }
94
+
95
+ } /* namespace datasketches */
@@ -0,0 +1,83 @@
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
+ #include <catch2/catch.hpp>
21
+ #include <fstream>
22
+ #include <frequent_items_sketch.hpp>
23
+
24
+ namespace datasketches {
25
+
26
+ TEST_CASE("frequent longs sketch generate", "[serialize_for_java]") {
27
+ const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
28
+ for (const unsigned n: n_arr) {
29
+ frequent_items_sketch<long> sketch(6);
30
+ for (unsigned i = 1; i <= n; ++i) sketch.update(i);
31
+ REQUIRE(sketch.is_empty() == (n == 0));
32
+ if (n > 10) {
33
+ REQUIRE(sketch.get_maximum_error() > 0);
34
+ } else {
35
+ REQUIRE(sketch.get_maximum_error() == 0);
36
+ }
37
+ REQUIRE(sketch.get_total_weight() == n);
38
+ std::ofstream os("frequent_long_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
39
+ sketch.serialize(os);
40
+ }
41
+ }
42
+
43
+ TEST_CASE("frequent strings sketch generate", "[serialize_for_java]") {
44
+ const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
45
+ for (const unsigned n: n_arr) {
46
+ frequent_items_sketch<std::string> sketch(6);
47
+ for (unsigned i = 1; i <= n; ++i) sketch.update(std::to_string(i));
48
+ REQUIRE(sketch.is_empty() == (n == 0));
49
+ if (n > 10) {
50
+ REQUIRE(sketch.get_maximum_error() > 0);
51
+ } else {
52
+ REQUIRE(sketch.get_maximum_error() == 0);
53
+ }
54
+ REQUIRE(sketch.get_total_weight() == n);
55
+ std::ofstream os("frequent_string_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
56
+ sketch.serialize(os);
57
+ }
58
+ }
59
+
60
+ TEST_CASE("frequent strings sketch ascii", "[serialize_for_java]") {
61
+ frequent_items_sketch<std::string> sketch(6);
62
+ sketch.update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1);
63
+ sketch.update("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 2);
64
+ sketch.update("ccccccccccccccccccccccccccccc", 3);
65
+ sketch.update("ddddddddddddddddddddddddddddd", 4);
66
+ std::ofstream os("frequent_string_ascii_cpp.sk", std::ios::binary);
67
+ sketch.serialize(os);
68
+ }
69
+
70
+ TEST_CASE("frequent strings sketch utf8", "[serialize_for_java]") {
71
+ frequent_items_sketch<std::string> sketch(6);
72
+ sketch.update("абвгд", 1);
73
+ sketch.update("еёжзи", 2);
74
+ sketch.update("йклмн", 3);
75
+ sketch.update("опрст", 4);
76
+ sketch.update("уфхцч", 5);
77
+ sketch.update("шщъыь", 6);
78
+ sketch.update("эюя", 7);
79
+ std::ofstream os("frequent_string_utf8_cpp.sk", std::ios::binary);
80
+ sketch.serialize(os);
81
+ }
82
+
83
+ } /* namespace datasketches */
@@ -70,6 +70,7 @@ TEST_CASE("frequent items: several items, no resize, no purge", "[frequent_items
70
70
  REQUIRE(sketch.get_estimate("b") == 3);
71
71
  REQUIRE(sketch.get_estimate("c") == 2);
72
72
  REQUIRE(sketch.get_estimate("d") == 1);
73
+ REQUIRE(sketch.get_maximum_error() == 0);
73
74
  }
74
75
 
75
76
  TEST_CASE("frequent items: several items, with resize, no purge", "[frequent_items_sketch]") {
@@ -96,6 +97,7 @@ TEST_CASE("frequent items: several items, with resize, no purge", "[frequent_ite
96
97
  REQUIRE(sketch.get_estimate("b") == 3);
97
98
  REQUIRE(sketch.get_estimate("c") == 2);
98
99
  REQUIRE(sketch.get_estimate("d") == 1);
100
+ REQUIRE(sketch.get_maximum_error() == 0);
99
101
  }
100
102
 
101
103
  TEST_CASE("frequent items: estimation mode", "[frequent_items_sketch]") {
@@ -149,6 +151,7 @@ TEST_CASE("frequent items: merge exact mode", "[frequent_items_sketch]") {
149
151
  REQUIRE(sketch1.get_estimate(2) == 3);
150
152
  REQUIRE(sketch1.get_estimate(3) == 2);
151
153
  REQUIRE(sketch1.get_estimate(4) == 1);
154
+ REQUIRE(sketch1.get_maximum_error() == 0);
152
155
  }
153
156
 
154
157
  TEST_CASE("frequent items: merge estimation mode", "[frequent_items_sketch]") {
@@ -199,48 +202,6 @@ TEST_CASE("frequent items: merge estimation mode", "[frequent_items_sketch]") {
199
202
  REQUIRE(9 <= items[1].get_estimate()); // always overestimated
200
203
  }
201
204
 
202
- TEST_CASE("frequent items: deserialize from java long", "[frequent_items_sketch]") {
203
- std::ifstream is;
204
- is.exceptions(std::ios::failbit | std::ios::badbit);
205
- is.open(testBinaryInputPath + "longs_sketch_from_java.sk", std::ios::binary);
206
- auto sketch = frequent_items_sketch<long long>::deserialize(is);
207
- REQUIRE_FALSE(sketch.is_empty());
208
- REQUIRE(sketch.get_total_weight() == 4);
209
- REQUIRE(sketch.get_num_active_items() == 4);
210
- REQUIRE(sketch.get_estimate(1) == 1);
211
- REQUIRE(sketch.get_estimate(2) == 1);
212
- REQUIRE(sketch.get_estimate(3) == 1);
213
- REQUIRE(sketch.get_estimate(4) == 1);
214
- }
215
-
216
- TEST_CASE("frequent items: deserialize from java string", "[frequent_items_sketch]") {
217
- std::ifstream is;
218
- is.exceptions(std::ios::failbit | std::ios::badbit);
219
- is.open(testBinaryInputPath + "items_sketch_string_from_java.sk", std::ios::binary);
220
- auto sketch = frequent_items_sketch<std::string>::deserialize(is);
221
- REQUIRE_FALSE(sketch.is_empty());
222
- REQUIRE(sketch.get_total_weight() == 4);
223
- REQUIRE(sketch.get_num_active_items() == 4);
224
- REQUIRE(sketch.get_estimate("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa") == 1);
225
- REQUIRE(sketch.get_estimate("bbbbbbbbbbbbbbbbbbbbbbbbbbbbb") == 1);
226
- REQUIRE(sketch.get_estimate("ccccccccccccccccccccccccccccc") == 1);
227
- REQUIRE(sketch.get_estimate("ddddddddddddddddddddddddddddd") == 1);
228
- }
229
-
230
- TEST_CASE("frequent items: deserialize from java string, utf-8", "[frequent_items_sketch]") {
231
- std::ifstream is;
232
- is.exceptions(std::ios::failbit | std::ios::badbit);
233
- is.open(testBinaryInputPath + "items_sketch_string_utf8_from_java.sk", std::ios::binary);
234
- auto sketch = frequent_items_sketch<std::string>::deserialize(is);
235
- REQUIRE_FALSE(sketch.is_empty());
236
- REQUIRE(sketch.get_total_weight() == 10);
237
- REQUIRE(sketch.get_num_active_items() == 4);
238
- REQUIRE(sketch.get_estimate("абвгд") == 1);
239
- REQUIRE(sketch.get_estimate("еёжзи") == 2);
240
- REQUIRE(sketch.get_estimate("йклмн") == 3);
241
- REQUIRE(sketch.get_estimate("опрст") == 4);
242
- }
243
-
244
205
  TEST_CASE("frequent items: deserialize long64 stream", "[frequent_items_sketch]") {
245
206
  frequent_items_sketch<long long> sketch1(3);
246
207
  sketch1.update(1, 1);
@@ -169,9 +169,9 @@ CouponList<A>* CouponList<A>::newList(std::istream& is, const A& allocator) {
169
169
  }
170
170
 
171
171
  template<typename A>
172
- vector_u8<A> CouponList<A>::serialize(bool compact, unsigned header_size_bytes) const {
172
+ auto CouponList<A>::serialize(bool compact, unsigned header_size_bytes) const -> vector_bytes {
173
173
  const size_t sketchSizeBytes = (compact ? getCompactSerializationBytes() : getUpdatableSerializationBytes()) + header_size_bytes;
174
- vector_u8<A> byteArr(sketchSizeBytes, 0, getAllocator());
174
+ vector_bytes byteArr(sketchSizeBytes, 0, getAllocator());
175
175
  uint8_t* bytes = byteArr.data() + header_size_bytes;
176
176
 
177
177
  bytes[hll_constants::PREAMBLE_INTS_BYTE] = static_cast<uint8_t>(getPreInts());