datasketches 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +310 -0
  4. data/NOTICE +11 -0
  5. data/README.md +126 -0
  6. data/ext/datasketches/cpc_wrapper.cpp +50 -0
  7. data/ext/datasketches/ext.cpp +12 -0
  8. data/ext/datasketches/extconf.rb +11 -0
  9. data/ext/datasketches/hll_wrapper.cpp +69 -0
  10. data/lib/datasketches.rb +9 -0
  11. data/lib/datasketches/version.rb +3 -0
  12. data/vendor/datasketches-cpp/CMakeLists.txt +126 -0
  13. data/vendor/datasketches-cpp/LICENSE +311 -0
  14. data/vendor/datasketches-cpp/MANIFEST.in +19 -0
  15. data/vendor/datasketches-cpp/NOTICE +11 -0
  16. data/vendor/datasketches-cpp/README.md +42 -0
  17. data/vendor/datasketches-cpp/common/CMakeLists.txt +45 -0
  18. data/vendor/datasketches-cpp/common/include/MurmurHash3.h +173 -0
  19. data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +458 -0
  20. data/vendor/datasketches-cpp/common/include/bounds_binomial_proportions.hpp +291 -0
  21. data/vendor/datasketches-cpp/common/include/ceiling_power_of_2.hpp +41 -0
  22. data/vendor/datasketches-cpp/common/include/common_defs.hpp +51 -0
  23. data/vendor/datasketches-cpp/common/include/conditional_back_inserter.hpp +68 -0
  24. data/vendor/datasketches-cpp/common/include/conditional_forward.hpp +70 -0
  25. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +114 -0
  26. data/vendor/datasketches-cpp/common/include/inv_pow2_table.hpp +107 -0
  27. data/vendor/datasketches-cpp/common/include/memory_operations.hpp +57 -0
  28. data/vendor/datasketches-cpp/common/include/serde.hpp +196 -0
  29. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +38 -0
  30. data/vendor/datasketches-cpp/common/test/catch.hpp +17618 -0
  31. data/vendor/datasketches-cpp/common/test/catch_runner.cpp +7 -0
  32. data/vendor/datasketches-cpp/common/test/test_allocator.cpp +31 -0
  33. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +108 -0
  34. data/vendor/datasketches-cpp/common/test/test_runner.cpp +29 -0
  35. data/vendor/datasketches-cpp/common/test/test_type.hpp +137 -0
  36. data/vendor/datasketches-cpp/cpc/CMakeLists.txt +74 -0
  37. data/vendor/datasketches-cpp/cpc/include/compression_data.hpp +6022 -0
  38. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +62 -0
  39. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +147 -0
  40. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +742 -0
  41. data/vendor/datasketches-cpp/cpc/include/cpc_confidence.hpp +167 -0
  42. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +311 -0
  43. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +810 -0
  44. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +102 -0
  45. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +346 -0
  46. data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +137 -0
  47. data/vendor/datasketches-cpp/cpc/include/icon_estimator.hpp +274 -0
  48. data/vendor/datasketches-cpp/cpc/include/kxp_byte_lookup.hpp +81 -0
  49. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +84 -0
  50. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +266 -0
  51. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +44 -0
  52. data/vendor/datasketches-cpp/cpc/test/compression_test.cpp +67 -0
  53. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +381 -0
  54. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +149 -0
  55. data/vendor/datasketches-cpp/fi/CMakeLists.txt +54 -0
  56. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +319 -0
  57. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +484 -0
  58. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +114 -0
  59. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map_impl.hpp +345 -0
  60. data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +44 -0
  61. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +84 -0
  62. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +360 -0
  63. data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
  64. data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
  65. data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
  66. data/vendor/datasketches-cpp/fi/test/reverse_purge_hash_map_test.cpp +47 -0
  67. data/vendor/datasketches-cpp/hll/CMakeLists.txt +92 -0
  68. data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +303 -0
  69. data/vendor/datasketches-cpp/hll/include/AuxHashMap.hpp +83 -0
  70. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +811 -0
  71. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable.hpp +40 -0
  72. data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +291 -0
  73. data/vendor/datasketches-cpp/hll/include/CouponHashSet.hpp +59 -0
  74. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +417 -0
  75. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +91 -0
  76. data/vendor/datasketches-cpp/hll/include/CubicInterpolation-internal.hpp +233 -0
  77. data/vendor/datasketches-cpp/hll/include/CubicInterpolation.hpp +43 -0
  78. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers-internal.hpp +90 -0
  79. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers.hpp +48 -0
  80. data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +335 -0
  81. data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +69 -0
  82. data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +124 -0
  83. data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +55 -0
  84. data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +158 -0
  85. data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +56 -0
  86. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +706 -0
  87. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +136 -0
  88. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +462 -0
  89. data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +149 -0
  90. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +85 -0
  91. data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +170 -0
  92. data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +287 -0
  93. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +239 -0
  94. data/vendor/datasketches-cpp/hll/include/RelativeErrorTables-internal.hpp +112 -0
  95. data/vendor/datasketches-cpp/hll/include/RelativeErrorTables.hpp +46 -0
  96. data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +56 -0
  97. data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +43 -0
  98. data/vendor/datasketches-cpp/hll/include/hll.hpp +669 -0
  99. data/vendor/datasketches-cpp/hll/include/hll.private.hpp +32 -0
  100. data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +79 -0
  101. data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +51 -0
  102. data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +130 -0
  103. data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +181 -0
  104. data/vendor/datasketches-cpp/hll/test/CrossCountingTest.cpp +93 -0
  105. data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +191 -0
  106. data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +389 -0
  107. data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +313 -0
  108. data/vendor/datasketches-cpp/hll/test/IsomorphicTest.cpp +141 -0
  109. data/vendor/datasketches-cpp/hll/test/TablesTest.cpp +44 -0
  110. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +168 -0
  111. data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
  112. data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
  113. data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
  114. data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
  115. data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
  116. data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
  117. data/vendor/datasketches-cpp/kll/CMakeLists.txt +58 -0
  118. data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +150 -0
  119. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +319 -0
  120. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +67 -0
  121. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +169 -0
  122. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +559 -0
  123. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +1131 -0
  124. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +44 -0
  125. data/vendor/datasketches-cpp/kll/test/kll_sketch_custom_type_test.cpp +154 -0
  126. data/vendor/datasketches-cpp/kll/test/kll_sketch_float_one_item_v1.sk +0 -0
  127. data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
  128. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +685 -0
  129. data/vendor/datasketches-cpp/kll/test/kll_sketch_validation.cpp +229 -0
  130. data/vendor/datasketches-cpp/pyproject.toml +17 -0
  131. data/vendor/datasketches-cpp/python/CMakeLists.txt +61 -0
  132. data/vendor/datasketches-cpp/python/README.md +78 -0
  133. data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +345 -0
  134. data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +354 -0
  135. data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +346 -0
  136. data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +463 -0
  137. data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +396 -0
  138. data/vendor/datasketches-cpp/python/src/__init__.py +2 -0
  139. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +90 -0
  140. data/vendor/datasketches-cpp/python/src/datasketches.cpp +40 -0
  141. data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +123 -0
  142. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +136 -0
  143. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +209 -0
  144. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +162 -0
  145. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +488 -0
  146. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +140 -0
  147. data/vendor/datasketches-cpp/python/tests/__init__.py +0 -0
  148. data/vendor/datasketches-cpp/python/tests/cpc_test.py +64 -0
  149. data/vendor/datasketches-cpp/python/tests/fi_test.py +110 -0
  150. data/vendor/datasketches-cpp/python/tests/hll_test.py +131 -0
  151. data/vendor/datasketches-cpp/python/tests/kll_test.py +119 -0
  152. data/vendor/datasketches-cpp/python/tests/theta_test.py +121 -0
  153. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +148 -0
  154. data/vendor/datasketches-cpp/python/tests/vo_test.py +101 -0
  155. data/vendor/datasketches-cpp/sampling/CMakeLists.txt +48 -0
  156. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +392 -0
  157. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +1752 -0
  158. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +239 -0
  159. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +645 -0
  160. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +43 -0
  161. data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +67 -0
  162. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +509 -0
  163. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +358 -0
  164. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
  165. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
  166. data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
  167. data/vendor/datasketches-cpp/setup.py +94 -0
  168. data/vendor/datasketches-cpp/theta/CMakeLists.txt +57 -0
  169. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +73 -0
  170. data/vendor/datasketches-cpp/theta/include/theta_a_not_b_impl.hpp +83 -0
  171. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +88 -0
  172. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +130 -0
  173. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +533 -0
  174. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +939 -0
  175. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +122 -0
  176. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +109 -0
  177. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +45 -0
  178. data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +244 -0
  179. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
  180. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
  181. data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
  182. data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +218 -0
  183. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +438 -0
  184. data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +97 -0
  185. data/vendor/datasketches-cpp/theta/test/theta_update_empty_from_java.sk +0 -0
  186. data/vendor/datasketches-cpp/theta/test/theta_update_estimation_from_java.sk +0 -0
  187. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +104 -0
  188. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_a_not_b.hpp +52 -0
  189. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_a_not_b_impl.hpp +32 -0
  190. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +52 -0
  191. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection_impl.hpp +31 -0
  192. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +179 -0
  193. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +238 -0
  194. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +81 -0
  195. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +43 -0
  196. data/vendor/datasketches-cpp/tuple/include/bounds_on_ratios_in_sampled_sets.hpp +135 -0
  197. data/vendor/datasketches-cpp/tuple/include/bounds_on_ratios_in_theta_sketched_sets.hpp +135 -0
  198. data/vendor/datasketches-cpp/tuple/include/jaccard_similarity.hpp +172 -0
  199. data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental.hpp +53 -0
  200. data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental_impl.hpp +33 -0
  201. data/vendor/datasketches-cpp/tuple/include/theta_comparators.hpp +48 -0
  202. data/vendor/datasketches-cpp/tuple/include/theta_constants.hpp +34 -0
  203. data/vendor/datasketches-cpp/tuple/include/theta_helpers.hpp +54 -0
  204. data/vendor/datasketches-cpp/tuple/include/theta_intersection_base.hpp +59 -0
  205. data/vendor/datasketches-cpp/tuple/include/theta_intersection_base_impl.hpp +121 -0
  206. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental.hpp +78 -0
  207. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental_impl.hpp +43 -0
  208. data/vendor/datasketches-cpp/tuple/include/theta_set_difference_base.hpp +54 -0
  209. data/vendor/datasketches-cpp/tuple/include/theta_set_difference_base_impl.hpp +80 -0
  210. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental.hpp +393 -0
  211. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental_impl.hpp +481 -0
  212. data/vendor/datasketches-cpp/tuple/include/theta_union_base.hpp +60 -0
  213. data/vendor/datasketches-cpp/tuple/include/theta_union_base_impl.hpp +84 -0
  214. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental.hpp +88 -0
  215. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental_impl.hpp +47 -0
  216. data/vendor/datasketches-cpp/tuple/include/theta_update_sketch_base.hpp +259 -0
  217. data/vendor/datasketches-cpp/tuple/include/theta_update_sketch_base_impl.hpp +389 -0
  218. data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +57 -0
  219. data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b_impl.hpp +33 -0
  220. data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +104 -0
  221. data/vendor/datasketches-cpp/tuple/include/tuple_intersection_impl.hpp +43 -0
  222. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +496 -0
  223. data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +587 -0
  224. data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +109 -0
  225. data/vendor/datasketches-cpp/tuple/include/tuple_union_impl.hpp +47 -0
  226. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +53 -0
  227. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +1 -0
  228. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
  229. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
  230. data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
  231. data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +1 -0
  232. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +298 -0
  233. data/vendor/datasketches-cpp/tuple/test/theta_a_not_b_experimental_test.cpp +250 -0
  234. data/vendor/datasketches-cpp/tuple/test/theta_compact_empty_from_java.sk +0 -0
  235. data/vendor/datasketches-cpp/tuple/test/theta_compact_estimation_from_java.sk +0 -0
  236. data/vendor/datasketches-cpp/tuple/test/theta_compact_single_item_from_java.sk +0 -0
  237. data/vendor/datasketches-cpp/tuple/test/theta_intersection_experimental_test.cpp +224 -0
  238. data/vendor/datasketches-cpp/tuple/test/theta_jaccard_similarity_test.cpp +144 -0
  239. data/vendor/datasketches-cpp/tuple/test/theta_sketch_experimental_test.cpp +247 -0
  240. data/vendor/datasketches-cpp/tuple/test/theta_union_experimental_test.cpp +44 -0
  241. data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +289 -0
  242. data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +235 -0
  243. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +98 -0
  244. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_allocation_test.cpp +102 -0
  245. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +249 -0
  246. data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +187 -0
  247. metadata +302 -0
@@ -0,0 +1,393 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ #ifndef THETA_SKETCH_EXPERIMENTAL_HPP_
21
+ #define THETA_SKETCH_EXPERIMENTAL_HPP_
22
+
23
+ #include "theta_update_sketch_base.hpp"
24
+
25
+ namespace datasketches {
26
+
27
+ // experimental theta sketch derived from the same base as tuple sketch
28
+
29
+ template<typename Allocator = std::allocator<uint64_t>>
30
+ class theta_sketch_experimental {
31
+ public:
32
+ using Entry = uint64_t;
33
+ using ExtractKey = trivial_extract_key;
34
+ using iterator = theta_iterator<Entry, ExtractKey>;
35
+ using const_iterator = theta_const_iterator<Entry, ExtractKey>;
36
+
37
+ virtual ~theta_sketch_experimental() = default;
38
+
39
+ /**
40
+ * @return allocator
41
+ */
42
+ virtual Allocator get_allocator() const = 0;
43
+
44
+ /**
45
+ * @return true if this sketch represents an empty set (not the same as no retained entries!)
46
+ */
47
+ virtual bool is_empty() const = 0;
48
+
49
+ /**
50
+ * @return estimate of the distinct count of the input stream
51
+ */
52
+ double get_estimate() const;
53
+
54
+ /**
55
+ * Returns the approximate lower error bound given a number of standard deviations.
56
+ * This parameter is similar to the number of standard deviations of the normal distribution
57
+ * and corresponds to approximately 67%, 95% and 99% confidence intervals.
58
+ * @param num_std_devs number of Standard Deviations (1, 2 or 3)
59
+ * @return the lower bound
60
+ */
61
+ double get_lower_bound(uint8_t num_std_devs) const;
62
+
63
+ /**
64
+ * Returns the approximate upper error bound given a number of standard deviations.
65
+ * This parameter is similar to the number of standard deviations of the normal distribution
66
+ * and corresponds to approximately 67%, 95% and 99% confidence intervals.
67
+ * @param num_std_devs number of Standard Deviations (1, 2 or 3)
68
+ * @return the upper bound
69
+ */
70
+ double get_upper_bound(uint8_t num_std_devs) const;
71
+
72
+ /**
73
+ * @return true if the sketch is in estimation mode (as opposed to exact mode)
74
+ */
75
+ bool is_estimation_mode() const;
76
+
77
+ /**
78
+ * @return theta as a fraction from 0 to 1 (effective sampling rate)
79
+ */
80
+ double get_theta() const;
81
+
82
+ /**
83
+ * @return theta as a positive integer between 0 and LLONG_MAX
84
+ */
85
+ virtual uint64_t get_theta64() const = 0;
86
+
87
+ /**
88
+ * @return the number of retained entries in the sketch
89
+ */
90
+ virtual uint32_t get_num_retained() const = 0;
91
+
92
+ /**
93
+ * @return hash of the seed that was used to hash the input
94
+ */
95
+ virtual uint16_t get_seed_hash() const = 0;
96
+
97
+ /**
98
+ * @return true if retained entries are ordered
99
+ */
100
+ virtual bool is_ordered() const = 0;
101
+
102
+ /**
103
+ * Provides a human-readable summary of this sketch as a string
104
+ * @param print_items if true include the list of items retained by the sketch
105
+ * @return sketch summary as a string
106
+ */
107
+ virtual string<Allocator> to_string(bool print_items = false) const;
108
+
109
+ /**
110
+ * Iterator over hash values in this sketch.
111
+ * @return begin iterator
112
+ */
113
+ virtual iterator begin() = 0;
114
+
115
+ /**
116
+ * Iterator pointing past the valid range.
117
+ * Not to be incremented or dereferenced.
118
+ * @return end iterator
119
+ */
120
+ virtual iterator end() = 0;
121
+
122
+ /**
123
+ * Const iterator over hash values in this sketch.
124
+ * @return begin iterator
125
+ */
126
+ virtual const_iterator begin() const = 0;
127
+
128
+ /**
129
+ * Const iterator pointing past the valid range.
130
+ * Not to be incremented or dereferenced.
131
+ * @return end iterator
132
+ */
133
+ virtual const_iterator end() const = 0;
134
+
135
+ protected:
136
+ virtual void print_specifics(std::ostringstream& os) const = 0;
137
+ };
138
+
139
+ // forward declaration
140
+ template<typename A> class compact_theta_sketch_experimental;
141
+
142
+ template<typename Allocator = std::allocator<uint64_t>>
143
+ class update_theta_sketch_experimental: public theta_sketch_experimental<Allocator> {
144
+ public:
145
+ using Base = theta_sketch_experimental<Allocator>;
146
+ using Entry = typename Base::Entry;
147
+ using ExtractKey = typename Base::ExtractKey;
148
+ using iterator = typename Base::iterator;
149
+ using const_iterator = typename Base::const_iterator;
150
+ using theta_table = theta_update_sketch_base<Entry, ExtractKey, Allocator>;
151
+ using resize_factor = typename theta_table::resize_factor;
152
+
153
+ // No constructor here. Use builder instead.
154
+ class builder;
155
+
156
+ update_theta_sketch_experimental(const update_theta_sketch_experimental&) = default;
157
+ update_theta_sketch_experimental(update_theta_sketch_experimental&&) noexcept = default;
158
+ virtual ~update_theta_sketch_experimental() = default;
159
+ update_theta_sketch_experimental& operator=(const update_theta_sketch_experimental&) = default;
160
+ update_theta_sketch_experimental& operator=(update_theta_sketch_experimental&&) = default;
161
+
162
+ virtual Allocator get_allocator() const;
163
+ virtual bool is_empty() const;
164
+ virtual bool is_ordered() const;
165
+ virtual uint16_t get_seed_hash() const;
166
+ virtual uint64_t get_theta64() const;
167
+ virtual uint32_t get_num_retained() const;
168
+
169
+ /**
170
+ * @return configured nominal number of entries in the sketch
171
+ */
172
+ uint8_t get_lg_k() const;
173
+
174
+ /**
175
+ * @return configured resize factor of the sketch
176
+ */
177
+ resize_factor get_rf() const;
178
+
179
+ /**
180
+ * Update this sketch with a given string.
181
+ * @param value string to update the sketch with
182
+ */
183
+ void update(const std::string& value);
184
+
185
+ /**
186
+ * Update this sketch with a given unsigned 64-bit integer.
187
+ * @param value uint64_t to update the sketch with
188
+ */
189
+ void update(uint64_t value);
190
+
191
+ /**
192
+ * Update this sketch with a given signed 64-bit integer.
193
+ * @param value int64_t to update the sketch with
194
+ */
195
+ void update(int64_t value);
196
+
197
+ /**
198
+ * Update this sketch with a given unsigned 32-bit integer.
199
+ * For compatibility with Java implementation.
200
+ * @param value uint32_t to update the sketch with
201
+ */
202
+ void update(uint32_t value);
203
+
204
+ /**
205
+ * Update this sketch with a given signed 32-bit integer.
206
+ * For compatibility with Java implementation.
207
+ * @param value int32_t to update the sketch with
208
+ */
209
+ void update(int32_t value);
210
+
211
+ /**
212
+ * Update this sketch with a given unsigned 16-bit integer.
213
+ * For compatibility with Java implementation.
214
+ * @param value uint16_t to update the sketch with
215
+ */
216
+ void update(uint16_t value);
217
+
218
+ /**
219
+ * Update this sketch with a given signed 16-bit integer.
220
+ * For compatibility with Java implementation.
221
+ * @param value int16_t to update the sketch with
222
+ */
223
+ void update(int16_t value);
224
+
225
+ /**
226
+ * Update this sketch with a given unsigned 8-bit integer.
227
+ * For compatibility with Java implementation.
228
+ * @param value uint8_t to update the sketch with
229
+ */
230
+ void update(uint8_t value);
231
+
232
+ /**
233
+ * Update this sketch with a given signed 8-bit integer.
234
+ * For compatibility with Java implementation.
235
+ * @param value int8_t to update the sketch with
236
+ */
237
+ void update(int8_t value);
238
+
239
+ /**
240
+ * Update this sketch with a given double-precision floating point value.
241
+ * For compatibility with Java implementation.
242
+ * @param value double to update the sketch with
243
+ */
244
+ void update(double value);
245
+
246
+ /**
247
+ * Update this sketch with a given floating point value.
248
+ * For compatibility with Java implementation.
249
+ * @param value float to update the sketch with
250
+ */
251
+ void update(float value);
252
+
253
+ /**
254
+ * Update this sketch with given data of any type.
255
+ * This is a "universal" update that covers all cases above,
256
+ * but may produce different hashes.
257
+ * Be very careful to hash input values consistently using the same approach
258
+ * both over time and on different platforms
259
+ * and while passing sketches between C++ environment and Java environment.
260
+ * Otherwise two sketches that should represent overlapping sets will be disjoint
261
+ * For instance, for signed 32-bit values call update(int32_t) method above,
262
+ * which does widening conversion to int64_t, if compatibility with Java is expected
263
+ * @param data pointer to the data
264
+ * @param length of the data in bytes
265
+ */
266
+ void update(const void* data, size_t length);
267
+
268
+ /**
269
+ * Remove retained entries in excess of the nominal size k (if any)
270
+ */
271
+ void trim();
272
+
273
+ /**
274
+ * Converts this sketch to a compact sketch (ordered or unordered).
275
+ * @param ordered optional flag to specify if ordered sketch should be produced
276
+ * @return compact sketch
277
+ */
278
+ compact_theta_sketch_experimental<Allocator> compact(bool ordered = true) const;
279
+
280
+ virtual iterator begin();
281
+ virtual iterator end();
282
+ virtual const_iterator begin() const;
283
+ virtual const_iterator end() const;
284
+
285
+ private:
286
+ theta_table table_;
287
+
288
+ // for builder
289
+ update_theta_sketch_experimental(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, uint64_t theta,
290
+ uint64_t seed, const Allocator& allocator);
291
+
292
+ virtual void print_specifics(std::ostringstream& os) const;
293
+ };
294
+
295
+ // compact sketch
296
+
297
+ template<typename Allocator = std::allocator<uint64_t>>
298
+ class compact_theta_sketch_experimental: public theta_sketch_experimental<Allocator> {
299
+ public:
300
+ using Base = theta_sketch_experimental<Allocator>;
301
+ using iterator = typename Base::iterator;
302
+ using const_iterator = typename Base::const_iterator;
303
+ using AllocBytes = typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>;
304
+ using vector_bytes = std::vector<uint8_t, AllocBytes>;
305
+
306
+ static const uint8_t SERIAL_VERSION = 3;
307
+ static const uint8_t SKETCH_TYPE = 3;
308
+
309
+ // Instances of this type can be obtained:
310
+ // - by compacting an update_theta_sketch
311
+ // - as a result of a set operation
312
+ // - by deserializing a previously serialized compact sketch
313
+
314
+ compact_theta_sketch_experimental(const Base& other, bool ordered);
315
+ compact_theta_sketch_experimental(const compact_theta_sketch_experimental&) = default;
316
+ compact_theta_sketch_experimental(compact_theta_sketch_experimental&&) noexcept = default;
317
+ virtual ~compact_theta_sketch_experimental() = default;
318
+ compact_theta_sketch_experimental& operator=(const compact_theta_sketch_experimental&) = default;
319
+ compact_theta_sketch_experimental& operator=(compact_theta_sketch_experimental&&) = default;
320
+
321
+ virtual Allocator get_allocator() const;
322
+ virtual bool is_empty() const;
323
+ virtual bool is_ordered() const;
324
+ virtual uint64_t get_theta64() const;
325
+ virtual uint32_t get_num_retained() const;
326
+ virtual uint16_t get_seed_hash() const;
327
+
328
+ /**
329
+ * This method serializes the sketch into a given stream in a binary form
330
+ * @param os output stream
331
+ */
332
+ void serialize(std::ostream& os) const;
333
+
334
+ /**
335
+ * This method serializes the sketch as a vector of bytes.
336
+ * An optional header can be reserved in front of the sketch.
337
+ * It is an uninitialized space of a given size.
338
+ * This header is used in Datasketches PostgreSQL extension.
339
+ * @param header_size_bytes space to reserve in front of the sketch
340
+ */
341
+ vector_bytes serialize(unsigned header_size_bytes = 0) const;
342
+
343
+ virtual iterator begin();
344
+ virtual iterator end();
345
+ virtual const_iterator begin() const;
346
+ virtual const_iterator end() const;
347
+
348
+ /**
349
+ * This method deserializes a sketch from a given stream.
350
+ * @param is input stream
351
+ * @param seed the seed for the hash function that was used to create the sketch
352
+ * @return an instance of the sketch
353
+ */
354
+ static compact_theta_sketch_experimental deserialize(std::istream& is,
355
+ uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
356
+
357
+ /**
358
+ * This method deserializes a sketch from a given array of bytes.
359
+ * @param bytes pointer to the array of bytes
360
+ * @param size the size of the array
361
+ * @param seed the seed for the hash function that was used to create the sketch
362
+ * @return an instance of the sketch
363
+ */
364
+ static compact_theta_sketch_experimental deserialize(const void* bytes, size_t size,
365
+ uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
366
+
367
+ // for internal use
368
+ compact_theta_sketch_experimental(bool is_empty, bool is_ordered, uint16_t seed_hash, uint64_t theta, std::vector<uint64_t, Allocator>&& entries);
369
+
370
+ private:
371
+ enum flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED };
372
+
373
+ bool is_empty_;
374
+ bool is_ordered_;
375
+ uint16_t seed_hash_;
376
+ uint64_t theta_;
377
+ std::vector<uint64_t, Allocator> entries_;
378
+
379
+ virtual void print_specifics(std::ostringstream& os) const;
380
+ };
381
+
382
+ template<typename Allocator>
383
+ class update_theta_sketch_experimental<Allocator>::builder: public theta_base_builder<builder, Allocator> {
384
+ public:
385
+ builder(const Allocator& allocator = Allocator());
386
+ update_theta_sketch_experimental build() const;
387
+ };
388
+
389
+ } /* namespace datasketches */
390
+
391
+ #include "theta_sketch_experimental_impl.hpp"
392
+
393
+ #endif
@@ -0,0 +1,481 @@
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 <sstream>
21
+
22
+ #include "serde.hpp"
23
+ #include "binomial_bounds.hpp"
24
+ #include "theta_helpers.hpp"
25
+
26
+ namespace datasketches {
27
+
28
+ template<typename A>
29
+ bool theta_sketch_experimental<A>::is_estimation_mode() const {
30
+ return get_theta64() < theta_constants::MAX_THETA && !is_empty();
31
+ }
32
+
33
+ template<typename A>
34
+ double theta_sketch_experimental<A>::get_theta() const {
35
+ return static_cast<double>(get_theta64()) / theta_constants::MAX_THETA;
36
+ }
37
+
38
+ template<typename A>
39
+ double theta_sketch_experimental<A>::get_estimate() const {
40
+ return get_num_retained() / get_theta();
41
+ }
42
+
43
+ template<typename A>
44
+ double theta_sketch_experimental<A>::get_lower_bound(uint8_t num_std_devs) const {
45
+ if (!is_estimation_mode()) return get_num_retained();
46
+ return binomial_bounds::get_lower_bound(get_num_retained(), get_theta(), num_std_devs);
47
+ }
48
+
49
+ template<typename A>
50
+ double theta_sketch_experimental<A>::get_upper_bound(uint8_t num_std_devs) const {
51
+ if (!is_estimation_mode()) return get_num_retained();
52
+ return binomial_bounds::get_upper_bound(get_num_retained(), get_theta(), num_std_devs);
53
+ }
54
+
55
+ template<typename A>
56
+ string<A> theta_sketch_experimental<A>::to_string(bool detail) const {
57
+ std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
58
+ os << "### Theta sketch summary:" << std::endl;
59
+ os << " num retained entries : " << get_num_retained() << std::endl;
60
+ os << " seed hash : " << get_seed_hash() << std::endl;
61
+ os << " empty? : " << (is_empty() ? "true" : "false") << std::endl;
62
+ os << " ordered? : " << (is_ordered() ? "true" : "false") << std::endl;
63
+ os << " estimation mode? : " << (is_estimation_mode() ? "true" : "false") << std::endl;
64
+ os << " theta (fraction) : " << get_theta() << std::endl;
65
+ os << " theta (raw 64-bit) : " << get_theta64() << std::endl;
66
+ os << " estimate : " << this->get_estimate() << std::endl;
67
+ os << " lower bound 95% conf : " << this->get_lower_bound(2) << std::endl;
68
+ os << " upper bound 95% conf : " << this->get_upper_bound(2) << std::endl;
69
+ print_specifics(os);
70
+ os << "### End sketch summary" << std::endl;
71
+ if (detail) {
72
+ os << "### Retained entries" << std::endl;
73
+ for (const auto& hash: *this) {
74
+ os << hash << std::endl;
75
+ }
76
+ os << "### End retained entries" << std::endl;
77
+ }
78
+ return os.str();
79
+ }
80
+
81
+ // update sketch
82
+
83
+ template<typename A>
84
+ update_theta_sketch_experimental<A>::update_theta_sketch_experimental(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf,
85
+ uint64_t theta, uint64_t seed, const A& allocator):
86
+ table_(lg_cur_size, lg_nom_size, rf, theta, seed, allocator)
87
+ {}
88
+
89
+ template<typename A>
90
+ A update_theta_sketch_experimental<A>::get_allocator() const {
91
+ return table_.allocator_;
92
+ }
93
+
94
+ template<typename A>
95
+ bool update_theta_sketch_experimental<A>::is_empty() const {
96
+ return table_.is_empty_;
97
+ }
98
+
99
+ template<typename A>
100
+ bool update_theta_sketch_experimental<A>::is_ordered() const {
101
+ return false;
102
+ }
103
+
104
+ template<typename A>
105
+ uint64_t update_theta_sketch_experimental<A>::get_theta64() const {
106
+ return table_.theta_;
107
+ }
108
+
109
+ template<typename A>
110
+ uint32_t update_theta_sketch_experimental<A>::get_num_retained() const {
111
+ return table_.num_entries_;
112
+ }
113
+
114
+ template<typename A>
115
+ uint16_t update_theta_sketch_experimental<A>::get_seed_hash() const {
116
+ return compute_seed_hash(table_.seed_);
117
+ }
118
+
119
+ template<typename A>
120
+ uint8_t update_theta_sketch_experimental<A>::get_lg_k() const {
121
+ return table_.lg_nom_size_;
122
+ }
123
+
124
+ template<typename A>
125
+ auto update_theta_sketch_experimental<A>::get_rf() const -> resize_factor {
126
+ return table_.rf_;
127
+ }
128
+
129
+ template<typename A>
130
+ void update_theta_sketch_experimental<A>::update(uint64_t value) {
131
+ update(&value, sizeof(value));
132
+ }
133
+
134
+ template<typename A>
135
+ void update_theta_sketch_experimental<A>::update(int64_t value) {
136
+ update(&value, sizeof(value));
137
+ }
138
+
139
+ template<typename A>
140
+ void update_theta_sketch_experimental<A>::update(uint32_t value) {
141
+ update(static_cast<int32_t>(value));
142
+ }
143
+
144
+ template<typename A>
145
+ void update_theta_sketch_experimental<A>::update(int32_t value) {
146
+ update(static_cast<int64_t>(value));
147
+ }
148
+
149
+ template<typename A>
150
+ void update_theta_sketch_experimental<A>::update(uint16_t value) {
151
+ update(static_cast<int16_t>(value));
152
+ }
153
+
154
+ template<typename A>
155
+ void update_theta_sketch_experimental<A>::update(int16_t value) {
156
+ update(static_cast<int64_t>(value));
157
+ }
158
+
159
+ template<typename A>
160
+ void update_theta_sketch_experimental<A>::update(uint8_t value) {
161
+ update(static_cast<int8_t>(value));
162
+ }
163
+
164
+ template<typename A>
165
+ void update_theta_sketch_experimental<A>::update(int8_t value) {
166
+ update(static_cast<int64_t>(value));
167
+ }
168
+
169
+ template<typename A>
170
+ void update_theta_sketch_experimental<A>::update(double value) {
171
+ update(canonical_double(value));
172
+ }
173
+
174
+ template<typename A>
175
+ void update_theta_sketch_experimental<A>::update(float value) {
176
+ update(static_cast<double>(value));
177
+ }
178
+
179
+ template<typename A>
180
+ void update_theta_sketch_experimental<A>::update(const std::string& value) {
181
+ if (value.empty()) return;
182
+ update(value.c_str(), value.length());
183
+ }
184
+
185
+ template<typename A>
186
+ void update_theta_sketch_experimental<A>::update(const void* data, size_t length) {
187
+ const uint64_t hash = table_.hash_and_screen(data, length);
188
+ if (hash == 0) return;
189
+ auto result = table_.find(hash);
190
+ if (!result.second) {
191
+ table_.insert(result.first, hash);
192
+ }
193
+ }
194
+
195
+ template<typename A>
196
+ void update_theta_sketch_experimental<A>::trim() {
197
+ table_.trim();
198
+ }
199
+
200
+ template<typename A>
201
+ auto update_theta_sketch_experimental<A>::begin() -> iterator {
202
+ return iterator(table_.entries_, 1 << table_.lg_cur_size_, 0);
203
+ }
204
+
205
+ template<typename A>
206
+ auto update_theta_sketch_experimental<A>::end() -> iterator {
207
+ return iterator(nullptr, 0, 1 << table_.lg_cur_size_);
208
+ }
209
+
210
+ template<typename A>
211
+ auto update_theta_sketch_experimental<A>::begin() const -> const_iterator {
212
+ return const_iterator(table_.entries_, 1 << table_.lg_cur_size_, 0);
213
+ }
214
+
215
+ template<typename A>
216
+ auto update_theta_sketch_experimental<A>::end() const -> const_iterator {
217
+ return const_iterator(nullptr, 0, 1 << table_.lg_cur_size_);
218
+ }
219
+ template<typename A>
220
+ compact_theta_sketch_experimental<A> update_theta_sketch_experimental<A>::compact(bool ordered) const {
221
+ return compact_theta_sketch_experimental<A>(*this, ordered);
222
+ }
223
+
224
+ template<typename A>
225
+ void update_theta_sketch_experimental<A>::print_specifics(std::ostringstream& os) const {
226
+ os << " lg nominal size : " << static_cast<int>(table_.lg_nom_size_) << std::endl;
227
+ os << " lg current size : " << static_cast<int>(table_.lg_cur_size_) << std::endl;
228
+ os << " resize factor : " << (1 << table_.rf_) << std::endl;
229
+ }
230
+
231
+ // builder
232
+
233
+ template<typename A>
234
+ update_theta_sketch_experimental<A>::builder::builder(const A& allocator): theta_base_builder<builder, A>(allocator) {}
235
+
236
+ template<typename A>
237
+ update_theta_sketch_experimental<A> update_theta_sketch_experimental<A>::builder::build() const {
238
+ return update_theta_sketch_experimental(this->starting_lg_size(), this->lg_k_, this->rf_, this->starting_theta(), this->seed_, this->allocator_);
239
+ }
240
+
241
+ // experimental compact theta sketch
242
+
243
+ template<typename A>
244
+ compact_theta_sketch_experimental<A>::compact_theta_sketch_experimental(const Base& other, bool ordered):
245
+ is_empty_(other.is_empty()),
246
+ is_ordered_(other.is_ordered() || ordered),
247
+ seed_hash_(other.get_seed_hash()),
248
+ theta_(other.get_theta64()),
249
+ entries_(other.get_allocator())
250
+ {
251
+ entries_.reserve(other.get_num_retained());
252
+ std::copy(other.begin(), other.end(), std::back_inserter(entries_));
253
+ if (ordered && !other.is_ordered()) std::sort(entries_.begin(), entries_.end());
254
+ }
255
+
256
+ template<typename A>
257
+ compact_theta_sketch_experimental<A>::compact_theta_sketch_experimental(bool is_empty, bool is_ordered, uint16_t seed_hash, uint64_t theta,
258
+ std::vector<uint64_t, A>&& entries):
259
+ is_empty_(is_empty),
260
+ is_ordered_(is_ordered),
261
+ seed_hash_(seed_hash),
262
+ theta_(theta),
263
+ entries_(std::move(entries))
264
+ {}
265
+
266
+ template<typename A>
267
+ A compact_theta_sketch_experimental<A>::get_allocator() const {
268
+ return entries_.get_allocator();
269
+ }
270
+
271
+ template<typename A>
272
+ bool compact_theta_sketch_experimental<A>::is_empty() const {
273
+ return is_empty_;
274
+ }
275
+
276
+ template<typename A>
277
+ bool compact_theta_sketch_experimental<A>::is_ordered() const {
278
+ return is_ordered_;
279
+ }
280
+
281
+ template<typename A>
282
+ uint64_t compact_theta_sketch_experimental<A>::get_theta64() const {
283
+ return theta_;
284
+ }
285
+
286
+ template<typename A>
287
+ uint32_t compact_theta_sketch_experimental<A>::get_num_retained() const {
288
+ return entries_.size();
289
+ }
290
+
291
+ template<typename A>
292
+ uint16_t compact_theta_sketch_experimental<A>::get_seed_hash() const {
293
+ return seed_hash_;
294
+ }
295
+
296
+ template<typename A>
297
+ auto compact_theta_sketch_experimental<A>::begin() -> iterator {
298
+ return iterator(entries_.data(), entries_.size(), 0);
299
+ }
300
+
301
+ template<typename A>
302
+ auto compact_theta_sketch_experimental<A>::end() -> iterator {
303
+ return iterator(nullptr, 0, entries_.size());
304
+ }
305
+
306
+ template<typename A>
307
+ auto compact_theta_sketch_experimental<A>::begin() const -> const_iterator {
308
+ return const_iterator(entries_.data(), entries_.size(), 0);
309
+ }
310
+
311
+ template<typename A>
312
+ auto compact_theta_sketch_experimental<A>::end() const -> const_iterator {
313
+ return const_iterator(nullptr, 0, entries_.size());
314
+ }
315
+
316
+ template<typename A>
317
+ void compact_theta_sketch_experimental<A>::print_specifics(std::ostringstream&) const {}
318
+
319
+ template<typename A>
320
+ void compact_theta_sketch_experimental<A>::serialize(std::ostream& os) const {
321
+ const bool is_single_item = entries_.size() == 1 && !this->is_estimation_mode();
322
+ const uint8_t preamble_longs = this->is_empty() || is_single_item ? 1 : this->is_estimation_mode() ? 3 : 2;
323
+ os.write(reinterpret_cast<const char*>(&preamble_longs), sizeof(preamble_longs));
324
+ const uint8_t serial_version = SERIAL_VERSION;
325
+ os.write(reinterpret_cast<const char*>(&serial_version), sizeof(serial_version));
326
+ const uint8_t type = SKETCH_TYPE;
327
+ os.write(reinterpret_cast<const char*>(&type), sizeof(type));
328
+ const uint16_t unused16 = 0;
329
+ os.write(reinterpret_cast<const char*>(&unused16), sizeof(unused16));
330
+ const uint8_t flags_byte(
331
+ (1 << flags::IS_COMPACT) |
332
+ (1 << flags::IS_READ_ONLY) |
333
+ (this->is_empty() ? 1 << flags::IS_EMPTY : 0) |
334
+ (this->is_ordered() ? 1 << flags::IS_ORDERED : 0)
335
+ );
336
+ os.write(reinterpret_cast<const char*>(&flags_byte), sizeof(flags_byte));
337
+ const uint16_t seed_hash = get_seed_hash();
338
+ os.write(reinterpret_cast<const char*>(&seed_hash), sizeof(seed_hash));
339
+ if (!this->is_empty()) {
340
+ if (!is_single_item) {
341
+ const uint32_t num_entries = entries_.size();
342
+ os.write(reinterpret_cast<const char*>(&num_entries), sizeof(num_entries));
343
+ const uint32_t unused32 = 0;
344
+ os.write(reinterpret_cast<const char*>(&unused32), sizeof(unused32));
345
+ if (this->is_estimation_mode()) {
346
+ os.write(reinterpret_cast<const char*>(&(this->theta_)), sizeof(uint64_t));
347
+ }
348
+ }
349
+ os.write(reinterpret_cast<const char*>(entries_.data()), entries_.size() * sizeof(uint64_t));
350
+ }
351
+ }
352
+
353
+ template<typename A>
354
+ auto compact_theta_sketch_experimental<A>::serialize(unsigned header_size_bytes) const -> vector_bytes {
355
+ const bool is_single_item = entries_.size() == 1 && !this->is_estimation_mode();
356
+ const uint8_t preamble_longs = this->is_empty() || is_single_item ? 1 : this->is_estimation_mode() ? 3 : 2;
357
+ const size_t size = header_size_bytes + sizeof(uint64_t) * preamble_longs
358
+ + sizeof(uint64_t) * entries_.size();
359
+ vector_bytes bytes(size, 0, entries_.get_allocator());
360
+ uint8_t* ptr = bytes.data() + header_size_bytes;
361
+
362
+ ptr += copy_to_mem(&preamble_longs, ptr, sizeof(preamble_longs));
363
+ const uint8_t serial_version = SERIAL_VERSION;
364
+ ptr += copy_to_mem(&serial_version, ptr, sizeof(serial_version));
365
+ const uint8_t type = SKETCH_TYPE;
366
+ ptr += copy_to_mem(&type, ptr, sizeof(type));
367
+ const uint16_t unused16 = 0;
368
+ ptr += copy_to_mem(&unused16, ptr, sizeof(unused16));
369
+ const uint8_t flags_byte(
370
+ (1 << flags::IS_COMPACT) |
371
+ (1 << flags::IS_READ_ONLY) |
372
+ (this->is_empty() ? 1 << flags::IS_EMPTY : 0) |
373
+ (this->is_ordered() ? 1 << flags::IS_ORDERED : 0)
374
+ );
375
+ ptr += copy_to_mem(&flags_byte, ptr, sizeof(flags_byte));
376
+ const uint16_t seed_hash = get_seed_hash();
377
+ ptr += copy_to_mem(&seed_hash, ptr, sizeof(seed_hash));
378
+ if (!this->is_empty()) {
379
+ if (!is_single_item) {
380
+ const uint32_t num_entries = entries_.size();
381
+ ptr += copy_to_mem(&num_entries, ptr, sizeof(num_entries));
382
+ const uint32_t unused32 = 0;
383
+ ptr += copy_to_mem(&unused32, ptr, sizeof(unused32));
384
+ if (this->is_estimation_mode()) {
385
+ ptr += copy_to_mem(&theta_, ptr, sizeof(uint64_t));
386
+ }
387
+ }
388
+ ptr += copy_to_mem(entries_.data(), ptr, entries_.size() * sizeof(uint64_t));
389
+ }
390
+ return bytes;
391
+ }
392
+
393
+ template<typename A>
394
+ compact_theta_sketch_experimental<A> compact_theta_sketch_experimental<A>::deserialize(std::istream& is, uint64_t seed, const A& allocator) {
395
+ uint8_t preamble_longs;
396
+ is.read(reinterpret_cast<char*>(&preamble_longs), sizeof(preamble_longs));
397
+ uint8_t serial_version;
398
+ is.read(reinterpret_cast<char*>(&serial_version), sizeof(serial_version));
399
+ uint8_t type;
400
+ is.read(reinterpret_cast<char*>(&type), sizeof(type));
401
+ uint16_t unused16;
402
+ is.read(reinterpret_cast<char*>(&unused16), sizeof(unused16));
403
+ uint8_t flags_byte;
404
+ is.read(reinterpret_cast<char*>(&flags_byte), sizeof(flags_byte));
405
+ uint16_t seed_hash;
406
+ is.read(reinterpret_cast<char*>(&seed_hash), sizeof(seed_hash));
407
+ checker<true>::check_sketch_type(type, SKETCH_TYPE);
408
+ checker<true>::check_serial_version(serial_version, SERIAL_VERSION);
409
+ const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
410
+ if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
411
+
412
+ uint64_t theta = theta_constants::MAX_THETA;
413
+ uint32_t num_entries = 0;
414
+ if (!is_empty) {
415
+ if (preamble_longs == 1) {
416
+ num_entries = 1;
417
+ } else {
418
+ is.read(reinterpret_cast<char*>(&num_entries), sizeof(num_entries));
419
+ uint32_t unused32;
420
+ is.read(reinterpret_cast<char*>(&unused32), sizeof(unused32));
421
+ if (preamble_longs > 2) {
422
+ is.read(reinterpret_cast<char*>(&theta), sizeof(theta));
423
+ }
424
+ }
425
+ }
426
+ std::vector<uint64_t, A> entries(num_entries, 0, allocator);
427
+ if (!is_empty) is.read(reinterpret_cast<char*>(entries.data()), sizeof(uint64_t) * entries.size());
428
+
429
+ const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
430
+ if (!is.good()) throw std::runtime_error("error reading from std::istream");
431
+ return compact_theta_sketch_experimental(is_empty, is_ordered, seed_hash, theta, std::move(entries));
432
+ }
433
+
434
+ template<typename A>
435
+ compact_theta_sketch_experimental<A> compact_theta_sketch_experimental<A>::deserialize(const void* bytes, size_t size, uint64_t seed, const A& allocator) {
436
+ ensure_minimum_memory(size, 8);
437
+ const char* ptr = static_cast<const char*>(bytes);
438
+ const char* base = ptr;
439
+ uint8_t preamble_longs;
440
+ ptr += copy_from_mem(ptr, &preamble_longs, sizeof(preamble_longs));
441
+ uint8_t serial_version;
442
+ ptr += copy_from_mem(ptr, &serial_version, sizeof(serial_version));
443
+ uint8_t type;
444
+ ptr += copy_from_mem(ptr, &type, sizeof(type));
445
+ uint16_t unused16;
446
+ ptr += copy_from_mem(ptr, &unused16, sizeof(unused16));
447
+ uint8_t flags_byte;
448
+ ptr += copy_from_mem(ptr, &flags_byte, sizeof(flags_byte));
449
+ uint16_t seed_hash;
450
+ ptr += copy_from_mem(ptr, &seed_hash, sizeof(seed_hash));
451
+ checker<true>::check_sketch_type(type, SKETCH_TYPE);
452
+ checker<true>::check_serial_version(serial_version, SERIAL_VERSION);
453
+ const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
454
+ if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
455
+
456
+ uint64_t theta = theta_constants::MAX_THETA;
457
+ uint32_t num_entries = 0;
458
+ if (!is_empty) {
459
+ if (preamble_longs == 1) {
460
+ num_entries = 1;
461
+ } else {
462
+ ensure_minimum_memory(size, 8); // read the first prelong before this method
463
+ ptr += copy_from_mem(ptr, &num_entries, sizeof(num_entries));
464
+ uint32_t unused32;
465
+ ptr += copy_from_mem(ptr, &unused32, sizeof(unused32));
466
+ if (preamble_longs > 2) {
467
+ ensure_minimum_memory(size, (preamble_longs - 1) << 3);
468
+ ptr += copy_from_mem(ptr, &theta, sizeof(theta));
469
+ }
470
+ }
471
+ }
472
+ const size_t entries_size_bytes = sizeof(uint64_t) * num_entries;
473
+ check_memory_size(ptr - base + entries_size_bytes, size);
474
+ std::vector<uint64_t, A> entries(num_entries, 0, allocator);
475
+ if (!is_empty) ptr += copy_from_mem(ptr, entries.data(), entries_size_bytes);
476
+
477
+ const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
478
+ return compact_theta_sketch_experimental(is_empty, is_ordered, seed_hash, theta, std::move(entries));
479
+ }
480
+
481
+ } /* namespace datasketches */