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
@@ -1,134 +0,0 @@
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 "hll.hpp"
21
-
22
- #include <pybind11/pybind11.h>
23
-
24
- namespace py = pybind11;
25
-
26
- namespace datasketches {
27
- namespace python {
28
-
29
- hll_sketch hll_sketch_deserialize(py::bytes skBytes) {
30
- std::string skStr = skBytes; // implicit cast
31
- return hll_sketch::deserialize(skStr.c_str(), skStr.length());
32
- }
33
-
34
- py::object hll_sketch_serialize_compact(const hll_sketch& sk) {
35
- auto serResult = sk.serialize_compact();
36
- return py::bytes((char*)serResult.data(), serResult.size());
37
- }
38
-
39
- py::object hll_sketch_serialize_updatable(const hll_sketch& sk) {
40
- auto serResult = sk.serialize_updatable();
41
- return py::bytes((char*)serResult.data(), serResult.size());
42
- }
43
-
44
- }
45
- }
46
-
47
- namespace dspy = datasketches::python;
48
-
49
- void init_hll(py::module &m) {
50
- using namespace datasketches;
51
-
52
- py::enum_<target_hll_type>(m, "tgt_hll_type", "Target HLL flavor")
53
- .value("HLL_4", HLL_4)
54
- .value("HLL_6", HLL_6)
55
- .value("HLL_8", HLL_8)
56
- .export_values();
57
-
58
- py::class_<hll_sketch>(m, "hll_sketch")
59
- .def(py::init<uint8_t>(), py::arg("lg_k"))
60
- .def(py::init<uint8_t, target_hll_type>(), py::arg("lg_k"), py::arg("tgt_type"))
61
- .def(py::init<uint8_t, target_hll_type, bool>(), py::arg("lg_k"), py::arg("tgt_type"), py::arg("start_max_size")=false)
62
- .def_static("deserialize", &dspy::hll_sketch_deserialize,
63
- "Reads a bytes object and returns the corresponding hll_sketch")
64
- .def("serialize_compact", &dspy::hll_sketch_serialize_compact,
65
- "Serializes the sketch into a bytes object, compressiong the exception table if HLL_4")
66
- .def("serialize_updatable", &dspy::hll_sketch_serialize_updatable,
67
- "Serializes the sketch into a bytes object")
68
- .def("__str__", (std::string (hll_sketch::*)(bool,bool,bool,bool) const) &hll_sketch::to_string,
69
- py::arg("summary")=true, py::arg("detail")=false, py::arg("aux_detail")=false, py::arg("all")=false,
70
- "Produces a string summary of the sketch")
71
- .def("to_string", (std::string (hll_sketch::*)(bool,bool,bool,bool) const) &hll_sketch::to_string,
72
- py::arg("summary")=true, py::arg("detail")=false, py::arg("aux_detail")=false, py::arg("all")=false,
73
- "Produces a string summary of the sketch")
74
- .def_property_readonly("lg_config_k", &hll_sketch::get_lg_config_k, "Configured lg_k value for the sketch")
75
- .def_property_readonly("tgt_type", &hll_sketch::get_target_type, "Returns the HLL type (4, 6, or 8) when in estimation mode")
76
- .def("get_estimate", &hll_sketch::get_estimate,
77
- "Estimate of the distinct count of the input stream")
78
- .def("get_lower_bound", &hll_sketch::get_lower_bound, py::arg("num_std_devs"),
79
- "Returns the approximate lower error bound given the specified number of standard deviations in {1, 2, 3}")
80
- .def("get_upper_bound", &hll_sketch::get_upper_bound, py::arg("num_std_devs"),
81
- "Returns the approximate upper error bound given the specified number of standard deviations in {1, 2, 3}")
82
- .def("is_compact", &hll_sketch::is_compact,
83
- "True if the sketch is compact, otherwise False")
84
- .def("is_empty", &hll_sketch::is_empty,
85
- "True if the sketch is empty, otherwise False")
86
- .def("get_updatable_serialization_bytes", &hll_sketch::get_updatable_serialization_bytes,
87
- "Returns the size of the serialized sketch")
88
- .def("get_compact_serialization_bytes", &hll_sketch::get_compact_serialization_bytes,
89
- "Returns the size of the serialized sketch when compressing the exception table if HLL_4")
90
- .def("reset", &hll_sketch::reset,
91
- "Resets the sketch to the empty state in coupon colleciton mode")
92
- .def("update", (void (hll_sketch::*)(int64_t)) &hll_sketch::update, py::arg("datum"),
93
- "Updates the sketch with the given integral value")
94
- .def("update", (void (hll_sketch::*)(double)) &hll_sketch::update, py::arg("datum"),
95
- "Updates the sketch with the given floating point value")
96
- .def("update", (void (hll_sketch::*)(const std::string&)) &hll_sketch::update, py::arg("datum"),
97
- "Updates the sketch with the given string value")
98
- .def_static("get_max_updatable_serialization_bytes", &hll_sketch::get_max_updatable_serialization_bytes,
99
- py::arg("lg_k"), py::arg("tgt_type"),
100
- "Provides a likely upper bound on serialization size for the given paramters")
101
- .def_static("get_rel_err", &hll_sketch::get_rel_err,
102
- py::arg("upper_bound"), py::arg("unioned"), py::arg("lg_k"), py::arg("num_std_devs"),
103
- "Retuns the a priori relative error bound for the given parameters")
104
- ;
105
-
106
- py::class_<hll_union>(m, "hll_union")
107
- .def(py::init<uint8_t>(), py::arg("lg_max_k"))
108
- .def_property_readonly("lg_config_k", &hll_union::get_lg_config_k, "Configured lg_k value for the union")
109
- .def_property_readonly("tgt_type", &hll_union::get_target_type, "Returns the HLL type (4, 6, or 8) when in estimation mode")
110
- .def("get_estimate", &hll_union::get_estimate,
111
- "Estimate of the distinct count of the input stream")
112
- .def("get_lower_bound", &hll_union::get_lower_bound, py::arg("num_std_devs"),
113
- "Returns the approximate lower error bound given the specified number of standard deviations in {1, 2, 3}")
114
- .def("get_upper_bound", &hll_union::get_upper_bound, py::arg("num_std_devs"),
115
- "Returns the approximate upper error bound given the specified number of standard deviations in {1, 2, 3}")
116
- .def("is_empty", &hll_union::is_empty,
117
- "True if the union is empty, otherwise False")
118
- .def("reset", &hll_union::reset,
119
- "Resets the union to the empty state")
120
- .def("get_result", &hll_union::get_result, py::arg("tgt_type")=HLL_4,
121
- "Returns a sketch of the target type representing the current union state")
122
- .def<void (hll_union::*)(const hll_sketch&)>("update", &hll_union::update, py::arg("sketch"),
123
- "Updates the union with the given HLL sketch")
124
- .def<void (hll_union::*)(int64_t)>("update", &hll_union::update, py::arg("datum"),
125
- "Updates the union with the given integral value")
126
- .def<void (hll_union::*)(double)>("update", &hll_union::update, py::arg("datum"),
127
- "Updates the union with the given floating point value")
128
- .def<void (hll_union::*)(const std::string&)>("update", &hll_union::update, py::arg("datum"),
129
- "Updates the union with the given string value")
130
- .def_static("get_rel_err", &hll_union::get_rel_err,
131
- py::arg("upper_bound"), py::arg("unioned"), py::arg("lg_k"), py::arg("num_std_devs"),
132
- "Retuns the a priori relative error bound for the given parameters")
133
- ;
134
- }
@@ -1,210 +0,0 @@
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 "kll_sketch.hpp"
21
-
22
- #include <pybind11/pybind11.h>
23
- #include <pybind11/stl.h>
24
- #include <pybind11/numpy.h>
25
- #include <sstream>
26
- #include <vector>
27
- #include <stdexcept>
28
-
29
- namespace py = pybind11;
30
-
31
- namespace datasketches {
32
-
33
- namespace python {
34
-
35
- template<typename T>
36
- kll_sketch<T> kll_sketch_deserialize(py::bytes skBytes) {
37
- std::string skStr = skBytes; // implicit cast
38
- return kll_sketch<T>::deserialize(skStr.c_str(), skStr.length());
39
- }
40
-
41
- template<typename T>
42
- py::object kll_sketch_serialize(const kll_sketch<T>& sk) {
43
- auto serResult = sk.serialize();
44
- return py::bytes((char*)serResult.data(), serResult.size());
45
- }
46
-
47
- // maybe possible to disambiguate the static vs method rank error calls, but
48
- // this is easier for now
49
- template<typename T>
50
- double kll_sketch_generic_normalized_rank_error(uint16_t k, bool pmf) {
51
- return kll_sketch<T>::get_normalized_rank_error(k, pmf);
52
- }
53
-
54
- template<typename T>
55
- py::list kll_sketch_get_quantiles(const kll_sketch<T>& sk,
56
- std::vector<double>& ranks,
57
- bool inclusive) {
58
- size_t nQuantiles = ranks.size();
59
- auto result = sk.get_quantiles(ranks.data(), nQuantiles, inclusive);
60
- // returning as std::vector<> would copy values to a list anyway
61
- py::list list(nQuantiles);
62
- for (size_t i = 0; i < nQuantiles; ++i) {
63
- list[i] = result[i];
64
- }
65
- return list;
66
- }
67
-
68
- template<typename T>
69
- py::list kll_sketch_get_pmf(const kll_sketch<T>& sk,
70
- std::vector<T>& split_points,
71
- bool inclusive) {
72
- size_t nPoints = split_points.size();
73
- auto result = sk.get_PMF(split_points.data(), nPoints, inclusive);
74
- py::list list(nPoints + 1);
75
- for (size_t i = 0; i <= nPoints; ++i) {
76
- list[i] = result[i];
77
- }
78
- return list;
79
- }
80
-
81
- template<typename T>
82
- py::list kll_sketch_get_cdf(const kll_sketch<T>& sk,
83
- std::vector<T>& split_points,
84
- bool inclusive) {
85
- size_t nPoints = split_points.size();
86
- auto result = sk.get_CDF(split_points.data(), nPoints, inclusive);
87
- py::list list(nPoints + 1);
88
- for (size_t i = 0; i <= nPoints; ++i) {
89
- list[i] = result[i];
90
- }
91
- return list;
92
- }
93
-
94
- template<typename T>
95
- void kll_sketch_update(kll_sketch<T>& sk, py::array_t<T, py::array::c_style | py::array::forcecast> items) {
96
- if (items.ndim() != 1) {
97
- throw std::invalid_argument("input data must have only one dimension. Found: "
98
- + std::to_string(items.ndim()));
99
- }
100
-
101
- auto data = items.template unchecked<1>();
102
- for (uint32_t i = 0; i < data.size(); ++i) {
103
- sk.update(data(i));
104
- }
105
- }
106
-
107
- }
108
- }
109
-
110
- namespace dspy = datasketches::python;
111
-
112
- template<typename T>
113
- void bind_kll_sketch(py::module &m, const char* name) {
114
- using namespace datasketches;
115
-
116
- py::class_<kll_sketch<T>>(m, name)
117
- .def(py::init<uint16_t>(), py::arg("k")=kll_constants::DEFAULT_K)
118
- .def(py::init<const kll_sketch<T>&>())
119
- .def("update", (void (kll_sketch<T>::*)(const T&)) &kll_sketch<T>::update, py::arg("item"),
120
- "Updates the sketch with the given value")
121
- .def("update", &dspy::kll_sketch_update<T>, py::arg("array"),
122
- "Updates the sketch with the values in the given array")
123
- .def("merge", (void (kll_sketch<T>::*)(const kll_sketch<T>&)) &kll_sketch<T>::merge, py::arg("sketch"),
124
- "Merges the provided sketch into the this one")
125
- .def("__str__", &kll_sketch<T>::to_string, py::arg("print_levels")=false, py::arg("print_items")=false,
126
- "Produces a string summary of the sketch")
127
- .def("to_string", &kll_sketch<T>::to_string, py::arg("print_levels")=false, py::arg("print_items")=false,
128
- "Produces a string summary of the sketch")
129
- .def("is_empty", &kll_sketch<T>::is_empty,
130
- "Returns True if the sketch is empty, otherwise False")
131
- .def("get_k", &kll_sketch<T>::get_k,
132
- "Returns the configured parameter k")
133
- .def("get_n", &kll_sketch<T>::get_n,
134
- "Returns the length of the input stream")
135
- .def("get_num_retained", &kll_sketch<T>::get_num_retained,
136
- "Returns the number of retained items (samples) in the sketch")
137
- .def("is_estimation_mode", &kll_sketch<T>::is_estimation_mode,
138
- "Returns True if the sketch is in estimation mode, otherwise False")
139
- .def("get_min_value", &kll_sketch<T>::get_min_item,
140
- "Returns the minimum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
141
- .def("get_max_value", &kll_sketch<T>::get_max_item,
142
- "Returns the maximum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
143
- .def("get_quantile", &kll_sketch<T>::get_quantile, py::arg("rank"), py::arg("inclusive")=false,
144
- "Returns an approximation to the data value "
145
- "associated with the given normalized rank in a hypothetical sorted "
146
- "version of the input stream so far.\n"
147
- "For kll_floats_sketch: if the sketch is empty this returns nan. "
148
- "For kll_ints_sketch: if the sketch is empty this throws a RuntimeError.")
149
- .def("get_quantiles", &dspy::kll_sketch_get_quantiles<T>, py::arg("ranks"), py::arg("inclusive")=false,
150
- "This returns an array that could have been generated by using get_quantile() for each "
151
- "normalized rank separately.\n"
152
- "If the sketch is empty this returns an empty vector.\n"
153
- "Deprecated. Will be removed in the next major version. Use get_quantile() instead.")
154
- .def("get_rank", &kll_sketch<T>::get_rank, py::arg("value"), py::arg("inclusive")=false,
155
- "Returns an approximation to the normalized rank of the given value from 0 to 1, inclusive.\n"
156
- "The resulting approximation has a probabilistic guarantee that can be obtained from the "
157
- "get_normalized_rank_error(False) function.\n"
158
- "With the parameter inclusive=true the weight of the given value is included into the rank."
159
- "Otherwise the rank equals the sum of the weights of values less than the given value.\n"
160
- "If the sketch is empty this returns nan.")
161
- .def("get_pmf", &dspy::kll_sketch_get_pmf<T>, py::arg("split_points"), py::arg("inclusive")=false,
162
- "Returns an approximation to the Probability Mass Function (PMF) of the input stream "
163
- "given a set of split points (values).\n"
164
- "The resulting approximations have a probabilistic guarantee that can be obtained from the "
165
- "get_normalized_rank_error(True) function.\n"
166
- "If the sketch is empty this returns an empty vector.\n"
167
- "split_points is an array of m unique, monotonically increasing float values "
168
- "that divide the real number line into m+1 consecutive disjoint intervals.\n"
169
- "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
170
- "exclusive of the right split point, with the exception that the last interval will include "
171
- "the maximum value.\n"
172
- "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
173
- "inclusive of the right split point.\n"
174
- "It is not necessary to include either the min or max values in these split points.")
175
- .def("get_cdf", &dspy::kll_sketch_get_cdf<T>, py::arg("split_points"), py::arg("inclusive")=false,
176
- "Returns an approximation to the Cumulative Distribution Function (CDF), which is the "
177
- "cumulative analog of the PMF, of the input stream given a set of split points (values).\n"
178
- "The resulting approximations have a probabilistic guarantee that can be obtained from the "
179
- "get_normalized_rank_error(True) function.\n"
180
- "If the sketch is empty this returns an empty vector.\n"
181
- "split_points is an array of m unique, monotonically increasing float values "
182
- "that divide the real number line into m+1 consecutive disjoint intervals.\n"
183
- "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
184
- "exclusive of the right split point, with the exception that the last interval will include "
185
- "the maximum value.\n"
186
- "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
187
- "inclusive of the right split point.\n"
188
- "It is not necessary to include either the min or max values in these split points.")
189
- .def("normalized_rank_error", (double (kll_sketch<T>::*)(bool) const) &kll_sketch<T>::get_normalized_rank_error,
190
- py::arg("as_pmf"),
191
- "Gets the normalized rank error for this sketch.\n"
192
- "If pmf is True, returns the 'double-sided' normalized rank error for the get_PMF() function.\n"
193
- "Otherwise, it is the 'single-sided' normalized rank error for all the other queries.\n"
194
- "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials")
195
- .def_static("get_normalized_rank_error", &dspy::kll_sketch_generic_normalized_rank_error<T>,
196
- py::arg("k"), py::arg("as_pmf"),
197
- "Gets the normalized rank error given parameters k and the pmf flag.\n"
198
- "If pmf is True, returns the 'double-sided' normalized rank error for the get_PMF() function.\n"
199
- "Otherwise, it is the 'single-sided' normalized rank error for all the other queries.\n"
200
- "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials")
201
- .def("serialize", &dspy::kll_sketch_serialize<T>, "Serializes the sketch into a bytes object")
202
- .def_static("deserialize", &dspy::kll_sketch_deserialize<T>, "Deserializes the sketch from a bytes object")
203
- ;
204
- }
205
-
206
- void init_kll(py::module &m) {
207
- bind_kll_sketch<int>(m, "kll_ints_sketch");
208
- bind_kll_sketch<float>(m, "kll_floats_sketch");
209
- bind_kll_sketch<double>(m, "kll_doubles_sketch");
210
- }
@@ -1,68 +0,0 @@
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 "kolmogorov_smirnov.hpp"
21
- #include "kll_sketch.hpp"
22
- #include "quantiles_sketch.hpp"
23
-
24
- #include <pybind11/pybind11.h>
25
-
26
- namespace py = pybind11;
27
-
28
- void init_kolmogorov_smirnov(py::module &m) {
29
- using namespace datasketches;
30
-
31
- m.def("ks_test", &kolmogorov_smirnov::test<kll_sketch<int>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
32
- "Performs the Kolmogorov-Smirnov Test between kll_ints_sketches.\n"
33
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
34
- "this will return false.\n"
35
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
36
- "distribution) using the provided p-value, otherwise False.");
37
- m.def("ks_test", &kolmogorov_smirnov::test<kll_sketch<float>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
38
- "Performs the Kolmogorov-Smirnov Test between kll_floats_sketches.\n"
39
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
40
- "this will return false.\n"
41
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
42
- "distribution) using the provided p-value, otherwise False.");
43
- m.def("ks_test", &kolmogorov_smirnov::test<kll_sketch<double>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
44
- "Performs the Kolmogorov-Smirnov Test between kll_doubles_sketches.\n"
45
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
46
- "this will return false.\n"
47
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
48
- "distribution) using the provided p-value, otherwise False.");
49
-
50
- m.def("ks_test", &kolmogorov_smirnov::test<quantiles_sketch<int>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
51
- "Performs the Kolmogorov-Smirnov Test between quantiles_ints_sketches.\n"
52
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
53
- "this will return false.\n"
54
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
55
- "distribution) using the provided p-value, otherwise False.");
56
- m.def("ks_test", &kolmogorov_smirnov::test<quantiles_sketch<float>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
57
- "Performs the Kolmogorov-Smirnov Test between quantiles_floats_sketches.\n"
58
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
59
- "this will return false.\n"
60
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
61
- "distribution) using the provided p-value, otherwise False.");
62
- m.def("ks_test", &kolmogorov_smirnov::test<quantiles_sketch<double>>, py::arg("sk_1"), py::arg("sk_2"), py::arg("p"),
63
- "Performs the Kolmogorov-Smirnov Test between quantiles_doubles_sketches.\n"
64
- "Note: if the given sketches have insufficient data or if the sketch sizes are too small, "
65
- "this will return false.\n"
66
- "Returns True if we can reject the null hypothesis (that the sketches reflect the same underlying "
67
- "distribution) using the provided p-value, otherwise False.");
68
- }
@@ -1,111 +0,0 @@
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 <cstring>
21
- #include "memory_operations.hpp"
22
-
23
- #include "py_serde.hpp"
24
-
25
- #include <pybind11/pybind11.h>
26
-
27
- namespace py = pybind11;
28
-
29
- void init_serde(py::module& m) {
30
- py::class_<datasketches::py_object_serde, datasketches::PyObjectSerDe /* <--- trampoline*/>(m, "PyObjectSerDe")
31
- .def(py::init<>())
32
- .def("get_size", &datasketches::py_object_serde::get_size, py::arg("item"),
33
- "Returns the size in bytes of an item")
34
- .def("to_bytes", &datasketches::py_object_serde::to_bytes, py::arg("item"),
35
- "Retuns a bytes object with a serialized version of an item")
36
- .def("from_bytes", &datasketches::py_object_serde::from_bytes, py::arg("data"), py::arg("offset"),
37
- "Reads a bytes object starting from the given offest and returns a tuple of the reconstructed "
38
- "object and the number of additional bytes read")
39
- ;
40
- }
41
-
42
- namespace datasketches {
43
- size_t py_object_serde::size_of_item(const py::object& item) const {
44
- return get_size(item);
45
- }
46
-
47
- size_t py_object_serde::serialize(void* ptr, size_t capacity, const py::object* items, unsigned num) const {
48
- size_t bytes_written = 0;
49
- py::gil_scoped_acquire acquire;
50
- for (unsigned i = 0; i < num; ++i) {
51
- std::string bytes = to_bytes(items[i]); // implicit cast from py::bytes
52
- check_memory_size(bytes_written + bytes.size(), capacity);
53
- memcpy(ptr, bytes.c_str(), bytes.size());
54
- ptr = static_cast<char*>(ptr) + bytes.size();
55
- bytes_written += bytes.size();
56
- }
57
- py::gil_scoped_release release;
58
- return bytes_written;
59
- }
60
-
61
- size_t py_object_serde::deserialize(const void* ptr, size_t capacity, py::object* items, unsigned num) const {
62
- size_t bytes_read = 0;
63
- unsigned i = 0;
64
- bool failure = false;
65
- bool error_from_python = false;
66
- py::gil_scoped_acquire acquire;
67
-
68
- // copy data into bytes only once
69
- py::bytes bytes(static_cast<const char*>(ptr), capacity);
70
- for (; i < num && !failure; ++i) {
71
- py::tuple bytes_and_len;
72
- try {
73
- bytes_and_len = from_bytes(bytes, bytes_read);
74
- } catch (py::error_already_set &e) {
75
- failure = true;
76
- error_from_python = true;
77
- break;
78
- }
79
-
80
- size_t length = py::cast<size_t>(bytes_and_len[1]);
81
- if (bytes_read + length > capacity) {
82
- bytes_read += length; // use this value to report the error
83
- failure = true;
84
- break;
85
- }
86
-
87
- new (&items[i]) py::object(py::cast<py::object>(bytes_and_len[0]));
88
- ptr = static_cast<const char*>(ptr) + length;
89
- bytes_read += length;
90
- }
91
-
92
- if (failure) {
93
- // clean up what we've allocated
94
- for (unsigned j = 0; j < i; ++j) {
95
- items[j].dec_ref();
96
- }
97
-
98
- if (error_from_python) {
99
- throw py::value_error("Error reading value in from_bytes");
100
- } else {
101
- // this next call will throw
102
- check_memory_size(bytes_read, capacity);
103
- }
104
- }
105
-
106
- py::gil_scoped_release release;
107
- return bytes_read;
108
- }
109
-
110
-
111
- } // namespace datasketches