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,239 @@
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 _HLLUTIL_HPP_
21
+ #define _HLLUTIL_HPP_
22
+
23
+ #include "MurmurHash3.h"
24
+ #include "RelativeErrorTables.hpp"
25
+ #include "count_zeros.hpp"
26
+ #include "common_defs.hpp"
27
+ #include "ceiling_power_of_2.hpp"
28
+
29
+ #include <cmath>
30
+ #include <stdexcept>
31
+ #include <string>
32
+
33
+ namespace datasketches {
34
+
35
+ enum hll_mode { LIST = 0, SET, HLL };
36
+
37
+ // template provides internal consistency and allows static float values
38
+ // but we don't use the template parameter anywhere
39
+ template<typename A = std::allocator<char> >
40
+ class HllUtil final {
41
+ public:
42
+ // preamble stuff
43
+ static const int SER_VER = 1;
44
+ static const int FAMILY_ID = 7;
45
+
46
+ static const int EMPTY_FLAG_MASK = 4;
47
+ static const int COMPACT_FLAG_MASK = 8;
48
+ static const int OUT_OF_ORDER_FLAG_MASK = 16;
49
+ static const int FULL_SIZE_FLAG_MASK = 32;
50
+
51
+ static const int PREAMBLE_INTS_BYTE = 0;
52
+ static const int SER_VER_BYTE = 1;
53
+ static const int FAMILY_BYTE = 2;
54
+ static const int LG_K_BYTE = 3;
55
+ static const int LG_ARR_BYTE = 4;
56
+ static const int FLAGS_BYTE = 5;
57
+ static const int LIST_COUNT_BYTE = 6;
58
+ static const int HLL_CUR_MIN_BYTE = 6;
59
+ static const int MODE_BYTE = 7; // lo2bits = curMode, next 2 bits = tgtHllMode
60
+
61
+ // Coupon List
62
+ static const int LIST_INT_ARR_START = 8;
63
+ static const int LIST_PREINTS = 2;
64
+ // Coupon Hash Set
65
+ static const int HASH_SET_COUNT_INT = 8;
66
+ static const int HASH_SET_INT_ARR_START = 12;
67
+ static const int HASH_SET_PREINTS = 3;
68
+ // HLL
69
+ static const int HLL_PREINTS = 10;
70
+ static const int HLL_BYTE_ARR_START = 40;
71
+ static const int HIP_ACCUM_DOUBLE = 8;
72
+ static const int KXQ0_DOUBLE = 16;
73
+ static const int KXQ1_DOUBLE = 24;
74
+ static const int CUR_MIN_COUNT_INT = 32;
75
+ static const int AUX_COUNT_INT = 36;
76
+
77
+ static const int EMPTY_SKETCH_SIZE_BYTES = 8;
78
+
79
+ // other HllUtil stuff
80
+ static const int KEY_BITS_26 = 26;
81
+ static const int VAL_BITS_6 = 6;
82
+ static const int KEY_MASK_26 = (1 << KEY_BITS_26) - 1;
83
+ static const int VAL_MASK_6 = (1 << VAL_BITS_6) - 1;
84
+ static const int EMPTY = 0;
85
+ static const int MIN_LOG_K = 4;
86
+ static const int MAX_LOG_K = 21;
87
+
88
+ static const double HLL_HIP_RSE_FACTOR; // sqrt(log(2.0)) = 0.8325546
89
+ static const double HLL_NON_HIP_RSE_FACTOR; // sqrt((3.0 * log(2.0)) - 1.0) = 1.03896
90
+ static const double COUPON_RSE_FACTOR; // 0.409 at transition point not the asymptote
91
+ static const double COUPON_RSE; // COUPON_RSE_FACTOR / (1 << 13);
92
+
93
+ static const int LG_INIT_LIST_SIZE = 3;
94
+ static const int LG_INIT_SET_SIZE = 5;
95
+ static const int RESIZE_NUMER = 3;
96
+ static const int RESIZE_DENOM = 4;
97
+
98
+ static const int loNibbleMask = 0x0f;
99
+ static const int hiNibbleMask = 0xf0;
100
+ static const int AUX_TOKEN = 0xf;
101
+
102
+ /**
103
+ * Log2 table sizes for exceptions based on lgK from 0 to 26.
104
+ * However, only lgK from 4 to 21 are used.
105
+ */
106
+ static const int LG_AUX_ARR_INTS[];
107
+
108
+ static int coupon(const uint64_t hash[]);
109
+ static int coupon(const HashState& hashState);
110
+ static void hash(const void* key, int keyLen, uint64_t seed, HashState& result);
111
+ static int checkLgK(int lgK);
112
+ static void checkMemSize(uint64_t minBytes, uint64_t capBytes);
113
+ static inline void checkNumStdDev(int numStdDev);
114
+ static int pair(int slotNo, int value);
115
+ static int getLow26(unsigned int coupon);
116
+ static int getValue(unsigned int coupon);
117
+ static double invPow2(int e);
118
+ static unsigned int ceilingPowerOf2(unsigned int n);
119
+ static unsigned int simpleIntLog2(unsigned int n); // n must be power of 2
120
+ static int computeLgArrInts(hll_mode mode, int count, int lgConfigK);
121
+ static double getRelErr(bool upperBound, bool unioned,
122
+ int lgConfigK, int numStdDev);
123
+ };
124
+
125
+ template<typename A>
126
+ const double HllUtil<A>::HLL_HIP_RSE_FACTOR = sqrt(log(2.0)); // 0.8325546
127
+ template<typename A>
128
+ const double HllUtil<A>::HLL_NON_HIP_RSE_FACTOR = sqrt((3.0 * log(2.0)) - 1.0); // 1.03896
129
+ template<typename A>
130
+ const double HllUtil<A>::COUPON_RSE_FACTOR = 0.409;
131
+ template<typename A>
132
+ const double HllUtil<A>::COUPON_RSE = COUPON_RSE_FACTOR / (1 << 13);
133
+
134
+ template<typename A>
135
+ const int HllUtil<A>::LG_AUX_ARR_INTS[] = {
136
+ 0, 2, 2, 2, 2, 2, 2, 3, 3, 3, // 0 - 9
137
+ 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, // 10-19
138
+ 12, 13, 14, 15, 16, 17, 18 // 20-26
139
+ };
140
+
141
+ template<typename A>
142
+ inline int HllUtil<A>::coupon(const uint64_t hash[]) {
143
+ int addr26 = (int) (hash[0] & KEY_MASK_26);
144
+ int lz = count_leading_zeros_in_u64(hash[1]);
145
+ int value = ((lz > 62 ? 62 : lz) + 1);
146
+ return (value << KEY_BITS_26) | addr26;
147
+ }
148
+
149
+ template<typename A>
150
+ inline int HllUtil<A>::coupon(const HashState& hashState) {
151
+ int addr26 = (int) (hashState.h1 & KEY_MASK_26);
152
+ int lz = count_leading_zeros_in_u64(hashState.h2);
153
+ int value = ((lz > 62 ? 62 : lz) + 1);
154
+ return (value << KEY_BITS_26) | addr26;
155
+ }
156
+
157
+ template<typename A>
158
+ inline void HllUtil<A>::hash(const void* key, const int keyLen, const uint64_t seed, HashState& result) {
159
+ MurmurHash3_x64_128(key, keyLen, seed, result);
160
+ }
161
+
162
+ template<typename A>
163
+ inline double HllUtil<A>::getRelErr(const bool upperBound, const bool unioned,
164
+ const int lgConfigK, const int numStdDev) {
165
+ return RelativeErrorTables<A>::getRelErr(upperBound, unioned, lgConfigK, numStdDev);
166
+ }
167
+
168
+ template<typename A>
169
+ inline int HllUtil<A>::checkLgK(const int lgK) {
170
+ if ((lgK >= HllUtil<A>::MIN_LOG_K) && (lgK <= HllUtil<A>::MAX_LOG_K)) {
171
+ return lgK;
172
+ } else {
173
+ throw std::invalid_argument("Invalid value of k: " + std::to_string(lgK));
174
+ }
175
+ }
176
+
177
+ template<typename A>
178
+ inline void HllUtil<A>::checkMemSize(const uint64_t minBytes, const uint64_t capBytes) {
179
+ if (capBytes < minBytes) {
180
+ throw std::invalid_argument("Given destination array is not large enough: " + std::to_string(capBytes));
181
+ }
182
+ }
183
+
184
+ template<typename A>
185
+ inline void HllUtil<A>::checkNumStdDev(const int numStdDev) {
186
+ if ((numStdDev < 1) || (numStdDev > 3)) {
187
+ throw std::invalid_argument("NumStdDev may not be less than 1 or greater than 3.");
188
+ }
189
+ }
190
+
191
+ template<typename A>
192
+ inline int HllUtil<A>::pair(const int slotNo, const int value) {
193
+ return (value << HllUtil<A>::KEY_BITS_26) | (slotNo & HllUtil<A>::KEY_MASK_26);
194
+ }
195
+
196
+ template<typename A>
197
+ inline int HllUtil<A>::getLow26(const unsigned int coupon) {
198
+ return coupon & HllUtil<A>::KEY_MASK_26;
199
+ }
200
+
201
+ template<typename A>
202
+ inline int HllUtil<A>::getValue(const unsigned int coupon) {
203
+ return coupon >> HllUtil<A>::KEY_BITS_26;
204
+ }
205
+
206
+ template<typename A>
207
+ inline double HllUtil<A>::invPow2(const int e) {
208
+ union {
209
+ long long longVal;
210
+ double doubleVal;
211
+ } conv;
212
+ conv.longVal = (1023L - e) << 52;
213
+ return conv.doubleVal;
214
+ }
215
+
216
+ template<typename A>
217
+ inline uint32_t HllUtil<A>::simpleIntLog2(uint32_t n) {
218
+ if (n == 0) {
219
+ throw std::logic_error("cannot take log of 0");
220
+ }
221
+ return count_trailing_zeros_in_u32(n);
222
+ }
223
+
224
+ template<typename A>
225
+ inline int HllUtil<A>::computeLgArrInts(hll_mode mode, int count, int lgConfigK) {
226
+ // assume value missing and recompute
227
+ if (mode == LIST) { return HllUtil<A>::LG_INIT_LIST_SIZE; }
228
+ int ceilPwr2 = ceiling_power_of_2(count);
229
+ if ((HllUtil<A>::RESIZE_DENOM * count) > (HllUtil<A>::RESIZE_NUMER * ceilPwr2)) { ceilPwr2 <<= 1;}
230
+ if (mode == SET) {
231
+ return fmax(HllUtil<A>::LG_INIT_SET_SIZE, HllUtil<A>::simpleIntLog2(ceilPwr2));
232
+ }
233
+ //only used for HLL4
234
+ return fmax(HllUtil<A>::LG_AUX_ARR_INTS[lgConfigK], HllUtil<A>::simpleIntLog2(ceilPwr2));
235
+ }
236
+
237
+ }
238
+
239
+ #endif /* _HLLUTIL_HPP_ */
@@ -0,0 +1,112 @@
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 _RELATIVEERRORTABLES_INTERNAL_HPP_
21
+ #define _RELATIVEERRORTABLES_INTERNAL_HPP_
22
+
23
+ #include "RelativeErrorTables.hpp"
24
+
25
+ namespace datasketches {
26
+
27
+ //case 0
28
+ static double HIP_LB[] = //sd 1, 2, 3
29
+ { //Q(.84134), Q(.97725), Q(.99865) respectively
30
+ 0.207316195, 0.502865572, 0.882303765, //4
31
+ 0.146981579, 0.335426881, 0.557052, //5
32
+ 0.104026721, 0.227683872, 0.365888317, //6
33
+ 0.073614601, 0.156781585, 0.245740374, //7
34
+ 0.05205248, 0.108783763, 0.168030442, //8
35
+ 0.036770852, 0.075727545, 0.11593785, //9
36
+ 0.025990219, 0.053145536, 0.080772263, //10
37
+ 0.018373987, 0.037266176, 0.056271814, //11
38
+ 0.012936253, 0.02613829, 0.039387631, //12
39
+ };
40
+
41
+ //case 1
42
+ static double HIP_UB[] = //sd 1, 2, 3
43
+ { //Q(.15866), Q(.02275), Q(.00135) respectively
44
+ -0.207805347, -0.355574279, -0.475535095, //4
45
+ -0.146988328, -0.262390832, -0.360864026, //5
46
+ -0.103877775, -0.191503663, -0.269311582, //6
47
+ -0.073452978, -0.138513438, -0.198487447, //7
48
+ -0.051982806, -0.099703123, -0.144128618, //8
49
+ -0.036768609, -0.07138158, -0.104430324, //9
50
+ -0.025991325, -0.050854296, -0.0748143, //10
51
+ -0.01834533, -0.036121138, -0.05327616, //11
52
+ -0.012920332, -0.025572893, -0.037896952, //12
53
+ };
54
+
55
+ //case 2
56
+ static double NON_HIP_LB[] = //sd 1, 2, 3`
57
+ { //Q(.84134), Q(.97725), Q(.99865) respectively
58
+ 0.254409839, 0.682266712, 1.304022158, //4
59
+ 0.181817353, 0.443389054, 0.778776219, //5
60
+ 0.129432281, 0.295782195, 0.49252279, //6
61
+ 0.091640655, 0.201175925, 0.323664385, //7
62
+ 0.064858051, 0.138523393, 0.218805328, //8
63
+ 0.045851855, 0.095925072, 0.148635751, //9
64
+ 0.032454144, 0.067009668, 0.102660669, //10
65
+ 0.022921382, 0.046868565, 0.071307398, //11
66
+ 0.016155679, 0.032825719, 0.049677541 //12
67
+ };
68
+
69
+ //case 3
70
+ static double NON_HIP_UB[] = //sd 1, 2, 3
71
+ { //Q(.15866), Q(.02275), Q(.00135) respectively
72
+ -0.256980172, -0.411905944, -0.52651057, //4
73
+ -0.182332109, -0.310275547, -0.412660505, //5
74
+ -0.129314228, -0.230142294, -0.315636197, //6
75
+ -0.091584836, -0.16834013, -0.236346847, //7
76
+ -0.06487411, -0.122045231, -0.174112107, //8
77
+ -0.04591465, -0.08784505, -0.126917615, //9
78
+ -0.032433119, -0.062897613, -0.091862929, //10
79
+ -0.022960633, -0.044875401, -0.065736049, //11
80
+ -0.016186662, -0.031827816, -0.046973459 //12
81
+ };
82
+
83
+ template<typename A>
84
+ double RelativeErrorTables<A>::getRelErr(const bool upperBound, const bool oooFlag,
85
+ const int lgK, const int stdDev) {
86
+ const int idx = ((lgK - 4) * 3) + (stdDev - 1);
87
+ const int sw = (oooFlag ? 2 : 0) | (upperBound ? 1 : 0);
88
+ double f = 0;
89
+ switch (sw) {
90
+ case 0 : { // HIP, LB
91
+ f = HIP_LB[idx];
92
+ break;
93
+ }
94
+ case 1 : { // HIP, UB
95
+ f = HIP_UB[idx];
96
+ break;
97
+ }
98
+ case 2 : { // NON_HIP, LB
99
+ f = NON_HIP_LB[idx];
100
+ break;
101
+ }
102
+ case 3 : { // NON_HIP, UB
103
+ f = NON_HIP_UB[idx];
104
+ break;
105
+ }
106
+ }
107
+ return f;
108
+ }
109
+
110
+ }
111
+
112
+ #endif // _RELATIVEERRORTABLES_INTERNAL_HPP_
@@ -0,0 +1,46 @@
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 _RELATIVEERRORTABLES_HPP_
21
+ #define _RELATIVEERRORTABLES_HPP_
22
+
23
+ #include <memory>
24
+
25
+ namespace datasketches {
26
+
27
+ template<typename A = std::allocator<char>>
28
+ class RelativeErrorTables {
29
+ public:
30
+ /**
31
+ * Return Relative Error for UB or LB for HIP or Non-HIP as a function of numStdDev.
32
+ * @param upperBound true if for upper bound
33
+ * @param oooFlag true if for Non-HIP
34
+ * @param lgK must be between 4 and 12 inclusive
35
+ * @param stdDev must be between 1 and 3 inclusive
36
+ * @return Relative Error for UB or LB for HIP or Non-HIP as a function of numStdDev.
37
+ */
38
+ static double getRelErr(bool upperBound, bool oooFlag,
39
+ int lgK, int stdDev);
40
+ };
41
+
42
+ }
43
+
44
+ #include "RelativeErrorTables-internal.hpp"
45
+
46
+ #endif /* _RELATIVEERRORTABLES_HPP_ */
@@ -0,0 +1,56 @@
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 _INTARRAYPAIRITERATOR_INTERNAL_HPP_
21
+ #define _INTARRAYPAIRITERATOR_INTERNAL_HPP_
22
+
23
+ #include "HllUtil.hpp"
24
+
25
+ namespace datasketches {
26
+
27
+ template<typename A>
28
+ coupon_iterator<A>::coupon_iterator(const int* array, size_t array_size, size_t index, bool all):
29
+ array(array), array_size(array_size), index(index), all(all) {
30
+ while (this->index < array_size) {
31
+ if (all || array[this->index] != HllUtil<A>::EMPTY) break;
32
+ this->index++;
33
+ }
34
+ }
35
+
36
+ template<typename A>
37
+ coupon_iterator<A>& coupon_iterator<A>::operator++() {
38
+ while (++index < array_size) {
39
+ if (all || array[index] != HllUtil<A>::EMPTY) break;
40
+ }
41
+ return *this;
42
+ }
43
+
44
+ template<typename A>
45
+ bool coupon_iterator<A>::operator!=(const coupon_iterator& other) const {
46
+ return index != other.index;
47
+ }
48
+
49
+ template<typename A>
50
+ uint32_t coupon_iterator<A>::operator*() const {
51
+ return array[index];
52
+ }
53
+
54
+ }
55
+
56
+ #endif // _INTARRAYPAIRITERATOR_INTERNAL_HPP_