datasketches 0.1.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/LICENSE +40 -3
  4. data/NOTICE +1 -1
  5. data/ext/datasketches/cpc_wrapper.cpp +12 -13
  6. data/ext/datasketches/ext.cpp +1 -1
  7. data/ext/datasketches/ext.h +4 -0
  8. data/ext/datasketches/extconf.rb +1 -1
  9. data/ext/datasketches/fi_wrapper.cpp +6 -8
  10. data/ext/datasketches/hll_wrapper.cpp +13 -14
  11. data/ext/datasketches/kll_wrapper.cpp +28 -76
  12. data/ext/datasketches/theta_wrapper.cpp +27 -41
  13. data/ext/datasketches/vo_wrapper.cpp +4 -6
  14. data/lib/datasketches/version.rb +1 -1
  15. data/vendor/datasketches-cpp/CMakeLists.txt +10 -0
  16. data/vendor/datasketches-cpp/LICENSE +40 -3
  17. data/vendor/datasketches-cpp/NOTICE +1 -1
  18. data/vendor/datasketches-cpp/README.md +4 -4
  19. data/vendor/datasketches-cpp/common/include/MurmurHash3.h +18 -7
  20. data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +8 -8
  21. data/vendor/datasketches-cpp/common/include/bounds_binomial_proportions.hpp +12 -15
  22. data/vendor/datasketches-cpp/common/include/common_defs.hpp +26 -0
  23. data/vendor/datasketches-cpp/common/include/conditional_forward.hpp +20 -8
  24. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +2 -2
  25. data/vendor/datasketches-cpp/common/include/memory_operations.hpp +12 -0
  26. data/vendor/datasketches-cpp/common/include/serde.hpp +7 -7
  27. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +24 -0
  28. data/vendor/datasketches-cpp/common/test/integration_test.cpp +77 -0
  29. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +9 -1
  30. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +13 -3
  31. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +20 -20
  32. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +116 -105
  33. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +22 -6
  34. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +140 -101
  35. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +2 -2
  36. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +20 -20
  37. data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +10 -16
  38. data/vendor/datasketches-cpp/cpc/include/icon_estimator.hpp +6 -6
  39. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +10 -10
  40. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +21 -21
  41. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +1 -0
  42. data/vendor/datasketches-cpp/cpc/test/compression_test.cpp +10 -10
  43. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_allocation_test.cpp +237 -0
  44. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +25 -0
  45. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +1 -1
  46. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +15 -10
  47. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +102 -105
  48. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +19 -13
  49. data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map_impl.hpp +141 -125
  50. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +15 -12
  51. data/vendor/datasketches-cpp/fi/test/reverse_purge_hash_map_test.cpp +5 -5
  52. data/vendor/datasketches-cpp/hll/CMakeLists.txt +3 -0
  53. data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +81 -109
  54. data/vendor/datasketches-cpp/hll/include/AuxHashMap.hpp +25 -24
  55. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +15 -15
  56. data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable.hpp +5 -5
  57. data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +89 -105
  58. data/vendor/datasketches-cpp/hll/include/CouponHashSet.hpp +13 -13
  59. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +130 -165
  60. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +21 -22
  61. data/vendor/datasketches-cpp/hll/include/CubicInterpolation-internal.hpp +2 -4
  62. data/vendor/datasketches-cpp/hll/include/CubicInterpolation.hpp +2 -2
  63. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers-internal.hpp +1 -1
  64. data/vendor/datasketches-cpp/hll/include/HarmonicNumbers.hpp +2 -2
  65. data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +88 -83
  66. data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +9 -9
  67. data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +34 -45
  68. data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +7 -8
  69. data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +41 -52
  70. data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +7 -8
  71. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +220 -251
  72. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +42 -42
  73. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +36 -38
  74. data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +22 -22
  75. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +15 -14
  76. data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +47 -44
  77. data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +62 -87
  78. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +121 -128
  79. data/vendor/datasketches-cpp/hll/include/RelativeErrorTables.hpp +1 -1
  80. data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +9 -9
  81. data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +5 -5
  82. data/vendor/datasketches-cpp/hll/include/hll.hpp +25 -53
  83. data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +8 -8
  84. data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +36 -36
  85. data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +28 -28
  86. data/vendor/datasketches-cpp/hll/test/CrossCountingTest.cpp +2 -2
  87. data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +37 -37
  88. data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +57 -61
  89. data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +10 -14
  90. data/vendor/datasketches-cpp/hll/test/IsomorphicTest.cpp +3 -3
  91. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +4 -4
  92. data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +5 -4
  93. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +6 -6
  94. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +14 -6
  95. data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +40 -25
  96. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +50 -6
  97. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +164 -136
  98. data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov.hpp +67 -0
  99. data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov_impl.hpp +78 -0
  100. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +1 -0
  101. data/vendor/datasketches-cpp/kll/test/kll_sketch_custom_type_test.cpp +11 -10
  102. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +178 -88
  103. data/vendor/datasketches-cpp/kll/test/kolmogorov_smirnov_test.cpp +111 -0
  104. data/vendor/datasketches-cpp/pyproject.toml +4 -2
  105. data/vendor/datasketches-cpp/python/CMakeLists.txt +12 -6
  106. data/vendor/datasketches-cpp/python/README.md +52 -49
  107. data/vendor/datasketches-cpp/python/pybind11Path.cmd +3 -0
  108. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +1 -1
  109. data/vendor/datasketches-cpp/python/src/datasketches.cpp +2 -0
  110. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +4 -6
  111. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +4 -2
  112. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +246 -0
  113. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +38 -28
  114. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +11 -5
  115. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +2 -2
  116. data/vendor/datasketches-cpp/python/tests/hll_test.py +1 -2
  117. data/vendor/datasketches-cpp/python/tests/kll_test.py +5 -5
  118. data/vendor/datasketches-cpp/python/tests/req_test.py +126 -0
  119. data/vendor/datasketches-cpp/python/tests/theta_test.py +28 -3
  120. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +4 -4
  121. data/vendor/datasketches-cpp/python/tests/vo_test.py +3 -3
  122. data/vendor/datasketches-cpp/req/CMakeLists.txt +60 -0
  123. data/vendor/datasketches-cpp/{tuple/include/theta_a_not_b_experimental_impl.hpp → req/include/req_common.hpp} +18 -8
  124. data/vendor/datasketches-cpp/req/include/req_compactor.hpp +137 -0
  125. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +488 -0
  126. data/vendor/datasketches-cpp/req/include/req_quantile_calculator.hpp +69 -0
  127. data/vendor/datasketches-cpp/req/include/req_quantile_calculator_impl.hpp +60 -0
  128. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +395 -0
  129. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +810 -0
  130. data/vendor/datasketches-cpp/req/test/CMakeLists.txt +43 -0
  131. data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
  132. data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
  133. data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
  134. data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
  135. data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
  136. data/vendor/datasketches-cpp/req/test/req_sketch_custom_type_test.cpp +128 -0
  137. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +494 -0
  138. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +19 -13
  139. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +130 -127
  140. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +5 -5
  141. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +41 -49
  142. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +1 -0
  143. data/vendor/datasketches-cpp/sampling/test/var_opt_allocation_test.cpp +96 -0
  144. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +6 -6
  145. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +13 -44
  146. data/vendor/datasketches-cpp/setup.py +11 -6
  147. data/vendor/datasketches-cpp/theta/CMakeLists.txt +30 -3
  148. data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_sampled_sets.hpp +3 -2
  149. data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_theta_sketched_sets.hpp +1 -1
  150. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser.hpp +67 -0
  151. data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +70 -0
  152. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +12 -29
  153. data/vendor/datasketches-cpp/theta/include/theta_a_not_b_impl.hpp +5 -46
  154. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_comparators.hpp +0 -0
  155. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_constants.hpp +11 -4
  156. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_helpers.hpp +0 -0
  157. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +26 -28
  158. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base.hpp +0 -0
  159. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base_impl.hpp +0 -0
  160. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +8 -90
  161. data/vendor/datasketches-cpp/{tuple/test/theta_union_experimental_test.cpp → theta/include/theta_jaccard_similarity.hpp} +11 -18
  162. data/vendor/datasketches-cpp/{tuple/include/jaccard_similarity.hpp → theta/include/theta_jaccard_similarity_base.hpp} +24 -36
  163. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base.hpp +0 -0
  164. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base_impl.hpp +5 -0
  165. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +163 -256
  166. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +250 -651
  167. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +27 -60
  168. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base.hpp +1 -1
  169. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base_impl.hpp +6 -1
  170. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +13 -69
  171. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base.hpp +10 -21
  172. data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base_impl.hpp +44 -30
  173. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +1 -0
  174. data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +23 -1
  175. data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +21 -1
  176. data/vendor/datasketches-cpp/{tuple → theta}/test/theta_jaccard_similarity_test.cpp +60 -5
  177. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +74 -235
  178. data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +22 -2
  179. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +3 -35
  180. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +47 -60
  181. data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +38 -0
  182. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +28 -13
  183. data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +57 -70
  184. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +1 -6
  185. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +1 -1
  186. data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +18 -21
  187. data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +13 -16
  188. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +7 -6
  189. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_allocation_test.cpp +3 -3
  190. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +20 -20
  191. data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +13 -16
  192. metadata +51 -36
  193. data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental.hpp +0 -53
  194. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental.hpp +0 -78
  195. data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental_impl.hpp +0 -43
  196. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental.hpp +0 -393
  197. data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental_impl.hpp +0 -481
  198. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental.hpp +0 -88
  199. data/vendor/datasketches-cpp/tuple/include/theta_union_experimental_impl.hpp +0 -47
  200. data/vendor/datasketches-cpp/tuple/test/theta_a_not_b_experimental_test.cpp +0 -250
  201. data/vendor/datasketches-cpp/tuple/test/theta_compact_empty_from_java.sk +0 -0
  202. data/vendor/datasketches-cpp/tuple/test/theta_compact_estimation_from_java.sk +0 -0
  203. data/vendor/datasketches-cpp/tuple/test/theta_compact_single_item_from_java.sk +0 -0
  204. data/vendor/datasketches-cpp/tuple/test/theta_intersection_experimental_test.cpp +0 -224
  205. data/vendor/datasketches-cpp/tuple/test/theta_sketch_experimental_test.cpp +0 -247
@@ -28,11 +28,11 @@
28
28
  namespace datasketches {
29
29
 
30
30
  template<typename A>
31
- static int find(const int* array, const int lgArrInts, const int coupon);
31
+ static int32_t find(const uint32_t* array, uint8_t lgArrInts, uint32_t coupon);
32
32
 
33
33
  template<typename A>
34
- CouponHashSet<A>::CouponHashSet(const int lgConfigK, const target_hll_type tgtHllType)
35
- : CouponList<A>(lgConfigK, tgtHllType, hll_mode::SET)
34
+ CouponHashSet<A>::CouponHashSet(uint8_t lgConfigK, target_hll_type tgtHllType, const A& allocator)
35
+ : CouponList<A>(lgConfigK, tgtHllType, hll_mode::SET, allocator)
36
36
  {
37
37
  if (lgConfigK <= 7) {
38
38
  throw std::invalid_argument("CouponHashSet must be initialized with lgConfigK > 7. Found: "
@@ -40,154 +40,140 @@ CouponHashSet<A>::CouponHashSet(const int lgConfigK, const target_hll_type tgtHl
40
40
  }
41
41
  }
42
42
 
43
- template<typename A>
44
- CouponHashSet<A>::CouponHashSet(const CouponHashSet<A>& that)
45
- : CouponList<A>(that) {}
46
-
47
43
  template<typename A>
48
44
  CouponHashSet<A>::CouponHashSet(const CouponHashSet<A>& that, const target_hll_type tgtHllType)
49
45
  : CouponList<A>(that, tgtHllType) {}
50
46
 
51
- template<typename A>
52
- CouponHashSet<A>::~CouponHashSet() {}
53
-
54
47
  template<typename A>
55
48
  std::function<void(HllSketchImpl<A>*)> CouponHashSet<A>::get_deleter() const {
56
49
  return [](HllSketchImpl<A>* ptr) {
57
50
  CouponHashSet<A>* chs = static_cast<CouponHashSet<A>*>(ptr);
51
+ ChsAlloc chsa(chs->getAllocator());
58
52
  chs->~CouponHashSet();
59
- chsAlloc().deallocate(chs, 1);
53
+ chsa.deallocate(chs, 1);
60
54
  };
61
55
  }
62
56
 
63
57
  template<typename A>
64
- CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len) {
65
- if (len < HllUtil<A>::HASH_SET_INT_ARR_START) { // hard-coded
58
+ CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len, const A& allocator) {
59
+ if (len < hll_constants::HASH_SET_INT_ARR_START) { // hard-coded
66
60
  throw std::out_of_range("Input data length insufficient to hold CouponHashSet");
67
61
  }
68
62
 
69
63
  const uint8_t* data = static_cast<const uint8_t*>(bytes);
70
- if (data[HllUtil<A>::PREAMBLE_INTS_BYTE] != HllUtil<A>::HASH_SET_PREINTS) {
64
+ if (data[hll_constants::PREAMBLE_INTS_BYTE] != hll_constants::HASH_SET_PREINTS) {
71
65
  throw std::invalid_argument("Incorrect number of preInts in input stream");
72
66
  }
73
- if (data[HllUtil<A>::SER_VER_BYTE] != HllUtil<A>::SER_VER) {
67
+ if (data[hll_constants::SER_VER_BYTE] != hll_constants::SER_VER) {
74
68
  throw std::invalid_argument("Wrong ser ver in input stream");
75
69
  }
76
- if (data[HllUtil<A>::FAMILY_BYTE] != HllUtil<A>::FAMILY_ID) {
70
+ if (data[hll_constants::FAMILY_BYTE] != hll_constants::FAMILY_ID) {
77
71
  throw std::invalid_argument("Input stream is not an HLL sketch");
78
72
  }
79
73
 
80
- const hll_mode mode = HllSketchImpl<A>::extractCurMode(data[HllUtil<A>::MODE_BYTE]);
74
+ const hll_mode mode = HllSketchImpl<A>::extractCurMode(data[hll_constants::MODE_BYTE]);
81
75
  if (mode != SET) {
82
- throw std::invalid_argument("Calling set construtor with non-set mode data");
76
+ throw std::invalid_argument("Calling set constructor with non-set mode data");
83
77
  }
84
78
 
85
- const target_hll_type tgtHllType = HllSketchImpl<A>::extractTgtHllType(data[HllUtil<A>::MODE_BYTE]);
79
+ const target_hll_type tgtHllType = HllSketchImpl<A>::extractTgtHllType(data[hll_constants::MODE_BYTE]);
86
80
 
87
- const int lgK = data[HllUtil<A>::LG_K_BYTE];
81
+ const uint8_t lgK = data[hll_constants::LG_K_BYTE];
88
82
  if (lgK <= 7) {
89
83
  throw std::invalid_argument("Attempt to deserialize invalid CouponHashSet with lgConfigK <= 7. Found: "
90
84
  + std::to_string(lgK));
91
85
  }
92
- int lgArrInts = data[HllUtil<A>::LG_ARR_BYTE];
93
- const bool compactFlag = ((data[HllUtil<A>::FLAGS_BYTE] & HllUtil<A>::COMPACT_FLAG_MASK) ? true : false);
86
+ uint8_t lgArrInts = data[hll_constants::LG_ARR_BYTE];
87
+ const bool compactFlag = ((data[hll_constants::FLAGS_BYTE] & hll_constants::COMPACT_FLAG_MASK) ? true : false);
94
88
 
95
- int couponCount;
96
- std::memcpy(&couponCount, data + HllUtil<A>::HASH_SET_COUNT_INT, sizeof(couponCount));
97
- if (lgArrInts < HllUtil<A>::LG_INIT_SET_SIZE) {
98
- lgArrInts = HllUtil<A>::computeLgArrInts(SET, couponCount, lgK);
89
+ uint32_t couponCount;
90
+ std::memcpy(&couponCount, data + hll_constants::HASH_SET_COUNT_INT, sizeof(couponCount));
91
+ if (lgArrInts < hll_constants::LG_INIT_SET_SIZE) {
92
+ lgArrInts = HllUtil<>::computeLgArrInts(SET, couponCount, lgK);
99
93
  }
100
94
  // Don't set couponCount in sketch here;
101
95
  // we'll set later if updatable, and increment with updates if compact
102
- const int couponsInArray = (compactFlag ? couponCount : (1 << lgArrInts));
103
- const size_t expectedLength = HllUtil<A>::HASH_SET_INT_ARR_START + (couponsInArray * sizeof(int));
96
+ const uint32_t couponsInArray = (compactFlag ? couponCount : (1 << lgArrInts));
97
+ const size_t expectedLength = hll_constants::HASH_SET_INT_ARR_START + (couponsInArray * sizeof(uint32_t));
104
98
  if (len < expectedLength) {
105
99
  throw std::out_of_range("Byte array too short for sketch. Expected " + std::to_string(expectedLength)
106
100
  + ", found: " + std::to_string(len));
107
101
  }
108
102
 
109
- CouponHashSet<A>* sketch = new (chsAlloc().allocate(1)) CouponHashSet<A>(lgK, tgtHllType);
103
+ ChsAlloc chsa(allocator);
104
+ CouponHashSet<A>* sketch = new (chsa.allocate(1)) CouponHashSet<A>(lgK, tgtHllType, allocator);
110
105
 
111
106
  if (compactFlag) {
112
- const uint8_t* curPos = data + HllUtil<A>::HASH_SET_INT_ARR_START;
113
- int coupon;
114
- for (int i = 0; i < couponCount; ++i, curPos += sizeof(coupon)) {
107
+ const uint8_t* curPos = data + hll_constants::HASH_SET_INT_ARR_START;
108
+ uint32_t coupon;
109
+ for (uint32_t i = 0; i < couponCount; ++i, curPos += sizeof(coupon)) {
115
110
  std::memcpy(&coupon, curPos, sizeof(coupon));
116
111
  sketch->couponUpdate(coupon);
117
112
  }
118
113
  } else {
119
- int* oldArr = sketch->couponIntArr;
120
- const size_t oldArrLen = 1 << sketch->lgCouponArrInts;
121
- sketch->lgCouponArrInts = lgArrInts;
122
- typedef typename std::allocator_traits<A>::template rebind_alloc<int> intAlloc;
123
- sketch->couponIntArr = intAlloc().allocate(1 << lgArrInts);
124
- sketch->couponCount = couponCount;
114
+ sketch->coupons_.resize(1ULL << lgArrInts);
115
+ sketch->couponCount_ = couponCount;
125
116
  // only need to read valid coupons, unlike in stream case
126
- std::memcpy(sketch->couponIntArr,
127
- data + HllUtil<A>::HASH_SET_INT_ARR_START,
128
- couponCount * sizeof(int));
129
- intAlloc().deallocate(oldArr, oldArrLen);
117
+ std::memcpy(sketch->coupons_.data(),
118
+ data + hll_constants::HASH_SET_INT_ARR_START,
119
+ couponCount * sizeof(uint32_t));
130
120
  }
131
121
 
132
122
  return sketch;
133
123
  }
134
124
 
135
125
  template<typename A>
136
- CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is) {
126
+ CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is, const A& allocator) {
137
127
  uint8_t listHeader[8];
138
- is.read((char*)listHeader, 8 * sizeof(uint8_t));
128
+ read(is, listHeader, 8 * sizeof(uint8_t));
139
129
 
140
- if (listHeader[HllUtil<A>::PREAMBLE_INTS_BYTE] != HllUtil<A>::HASH_SET_PREINTS) {
130
+ if (listHeader[hll_constants::PREAMBLE_INTS_BYTE] != hll_constants::HASH_SET_PREINTS) {
141
131
  throw std::invalid_argument("Incorrect number of preInts in input stream");
142
132
  }
143
- if (listHeader[HllUtil<A>::SER_VER_BYTE] != HllUtil<A>::SER_VER) {
133
+ if (listHeader[hll_constants::SER_VER_BYTE] != hll_constants::SER_VER) {
144
134
  throw std::invalid_argument("Wrong ser ver in input stream");
145
135
  }
146
- if (listHeader[HllUtil<A>::FAMILY_BYTE] != HllUtil<A>::FAMILY_ID) {
136
+ if (listHeader[hll_constants::FAMILY_BYTE] != hll_constants::FAMILY_ID) {
147
137
  throw std::invalid_argument("Input stream is not an HLL sketch");
148
138
  }
149
139
 
150
- hll_mode mode = HllSketchImpl<A>::extractCurMode(listHeader[HllUtil<A>::MODE_BYTE]);
140
+ hll_mode mode = HllSketchImpl<A>::extractCurMode(listHeader[hll_constants::MODE_BYTE]);
151
141
  if (mode != SET) {
152
- throw std::invalid_argument("Calling set construtor with non-set mode data");
142
+ throw std::invalid_argument("Calling set constructor with non-set mode data");
153
143
  }
154
144
 
155
- target_hll_type tgtHllType = HllSketchImpl<A>::extractTgtHllType(listHeader[HllUtil<A>::MODE_BYTE]);
145
+ const target_hll_type tgtHllType = HllSketchImpl<A>::extractTgtHllType(listHeader[hll_constants::MODE_BYTE]);
156
146
 
157
- const int lgK = listHeader[HllUtil<A>::LG_K_BYTE];
147
+ const uint8_t lgK = listHeader[hll_constants::LG_K_BYTE];
158
148
  if (lgK <= 7) {
159
149
  throw std::invalid_argument("Attempt to deserialize invalid CouponHashSet with lgConfigK <= 7. Found: "
160
150
  + std::to_string(lgK));
161
151
  }
162
- int lgArrInts = listHeader[HllUtil<A>::LG_ARR_BYTE];
163
- const bool compactFlag = ((listHeader[HllUtil<A>::FLAGS_BYTE] & HllUtil<A>::COMPACT_FLAG_MASK) ? true : false);
152
+ uint8_t lgArrInts = listHeader[hll_constants::LG_ARR_BYTE];
153
+ const bool compactFlag = ((listHeader[hll_constants::FLAGS_BYTE] & hll_constants::COMPACT_FLAG_MASK) ? true : false);
164
154
 
165
- int couponCount;
166
- is.read((char*)&couponCount, sizeof(couponCount));
167
- if (lgArrInts < HllUtil<A>::LG_INIT_SET_SIZE) {
168
- lgArrInts = HllUtil<A>::computeLgArrInts(SET, couponCount, lgK);
155
+ const auto couponCount = read<uint32_t>(is);
156
+ if (lgArrInts < hll_constants::LG_INIT_SET_SIZE) {
157
+ lgArrInts = HllUtil<>::computeLgArrInts(SET, couponCount, lgK);
169
158
  }
170
159
 
171
- CouponHashSet<A>* sketch = new (chsAlloc().allocate(1)) CouponHashSet<A>(lgK, tgtHllType);
160
+ ChsAlloc chsa(allocator);
161
+ CouponHashSet<A>* sketch = new (chsa.allocate(1)) CouponHashSet<A>(lgK, tgtHllType, allocator);
172
162
  typedef std::unique_ptr<CouponHashSet<A>, std::function<void(HllSketchImpl<A>*)>> coupon_hash_set_ptr;
173
163
  coupon_hash_set_ptr ptr(sketch, sketch->get_deleter());
174
164
 
175
165
  // Don't set couponCount here;
176
166
  // we'll set later if updatable, and increment with updates if compact
177
167
  if (compactFlag) {
178
- for (int i = 0; i < couponCount; ++i) {
179
- int coupon;
180
- is.read((char*)&coupon, sizeof(coupon));
168
+ for (uint32_t i = 0; i < couponCount; ++i) {
169
+ const auto coupon = read<uint32_t>(is);
181
170
  sketch->couponUpdate(coupon);
182
171
  }
183
172
  } else {
184
- typedef typename std::allocator_traits<A>::template rebind_alloc<int> intAlloc;
185
- intAlloc().deallocate(sketch->couponIntArr, 1 << sketch->lgCouponArrInts);
186
- sketch->lgCouponArrInts = lgArrInts;
187
- sketch->couponIntArr = intAlloc().allocate(1 << lgArrInts);
188
- sketch->couponCount = couponCount;
173
+ sketch->coupons_.resize(1ULL << lgArrInts);
174
+ sketch->couponCount_ = couponCount;
189
175
  // for stream processing, read entire list so read pointer ends up set correctly
190
- is.read((char*)sketch->couponIntArr, (1 << sketch->lgCouponArrInts) * sizeof(int));
176
+ read(is, sketch->coupons_.data(), sketch->coupons_.size() * sizeof(uint32_t));
191
177
  }
192
178
 
193
179
  if (!is.good())
@@ -198,22 +184,25 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is) {
198
184
 
199
185
  template<typename A>
200
186
  CouponHashSet<A>* CouponHashSet<A>::copy() const {
201
- return new (chsAlloc().allocate(1)) CouponHashSet<A>(*this);
187
+ ChsAlloc chsa(this->coupons_.get_allocator());
188
+ return new (chsa.allocate(1)) CouponHashSet<A>(*this);
202
189
  }
203
190
 
204
191
  template<typename A>
205
- CouponHashSet<A>* CouponHashSet<A>::copyAs(const target_hll_type tgtHllType) const {
206
- return new (chsAlloc().allocate(1)) CouponHashSet<A>(*this, tgtHllType);
192
+ CouponHashSet<A>* CouponHashSet<A>::copyAs(target_hll_type tgtHllType) const {
193
+ ChsAlloc chsa(this->coupons_.get_allocator());
194
+ return new (chsa.allocate(1)) CouponHashSet<A>(*this, tgtHllType);
207
195
  }
208
196
 
209
197
  template<typename A>
210
- HllSketchImpl<A>* CouponHashSet<A>::couponUpdate(int coupon) {
211
- const int index = find<A>(this->couponIntArr, this->lgCouponArrInts, coupon);
198
+ HllSketchImpl<A>* CouponHashSet<A>::couponUpdate(uint32_t coupon) {
199
+ const uint8_t lgCouponArrInts = count_trailing_zeros_in_u32(static_cast<uint32_t>(this->coupons_.size()));
200
+ const int32_t index = find<A>(this->coupons_.data(), lgCouponArrInts, coupon);
212
201
  if (index >= 0) {
213
202
  return this; // found duplicate, ignore
214
203
  }
215
- this->couponIntArr[~index] = coupon; // found empty
216
- ++this->couponCount;
204
+ this->coupons_[~index] = coupon; // found empty
205
+ ++this->couponCount_;
217
206
  if (checkGrowOrPromote()) {
218
207
  return this->promoteHeapListOrSetToHll(*this);
219
208
  }
@@ -221,66 +210,61 @@ HllSketchImpl<A>* CouponHashSet<A>::couponUpdate(int coupon) {
221
210
  }
222
211
 
223
212
  template<typename A>
224
- int CouponHashSet<A>::getMemDataStart() const {
225
- return HllUtil<A>::HASH_SET_INT_ARR_START;
213
+ uint32_t CouponHashSet<A>::getMemDataStart() const {
214
+ return hll_constants::HASH_SET_INT_ARR_START;
226
215
  }
227
216
 
228
217
  template<typename A>
229
- int CouponHashSet<A>::getPreInts() const {
230
- return HllUtil<A>::HASH_SET_PREINTS;
218
+ uint8_t CouponHashSet<A>::getPreInts() const {
219
+ return hll_constants::HASH_SET_PREINTS;
231
220
  }
232
221
 
233
222
  template<typename A>
234
223
  bool CouponHashSet<A>::checkGrowOrPromote() {
235
- if ((HllUtil<A>::RESIZE_DENOM * this->couponCount) > (HllUtil<A>::RESIZE_NUMER * (1 << this->lgCouponArrInts))) {
236
- if (this->lgCouponArrInts == (this->lgConfigK - 3)) { // at max size
224
+ if (static_cast<size_t>(hll_constants::RESIZE_DENOM * this->couponCount_) > (hll_constants::RESIZE_NUMER * this->coupons_.size())) {
225
+ const uint8_t lgCouponArrInts = count_trailing_zeros_in_u32(static_cast<uint32_t>(this->coupons_.size()));
226
+ if (lgCouponArrInts == (this->lgConfigK_ - 3)) { // at max size
237
227
  return true; // promote to HLL
238
228
  }
239
- int tgtLgCoupArrSize = this->lgCouponArrInts + 1;
240
- growHashSet(this->lgCouponArrInts, tgtLgCoupArrSize);
229
+ growHashSet(lgCouponArrInts + 1);
241
230
  }
242
231
  return false;
243
232
  }
244
233
 
245
234
  template<typename A>
246
- void CouponHashSet<A>::growHashSet(const int srcLgCoupArrSize, const int tgtLgCoupArrSize) {
247
- const int tgtLen = 1 << tgtLgCoupArrSize;
248
- typedef typename std::allocator_traits<A>::template rebind_alloc<int> intAlloc;
249
- int* tgtCouponIntArr = intAlloc().allocate(tgtLen);
250
- std::fill(tgtCouponIntArr, tgtCouponIntArr + tgtLen, 0);
251
-
252
- const int srcLen = 1 << srcLgCoupArrSize;
253
- for (int i = 0; i < srcLen; ++i) { // scan existing array for non-zero values
254
- const int fetched = this->couponIntArr[i];
255
- if (fetched != HllUtil<A>::EMPTY) {
256
- const int idx = find<A>(tgtCouponIntArr, tgtLgCoupArrSize, fetched); // search TGT array
235
+ void CouponHashSet<A>::growHashSet(uint8_t tgtLgCoupArrSize) {
236
+ const uint32_t tgtLen = 1 << tgtLgCoupArrSize;
237
+ vector_int coupons_new(tgtLen, 0, this->coupons_.get_allocator());
238
+
239
+ const uint32_t srcLen = static_cast<uint32_t>(this->coupons_.size());
240
+ for (uint32_t i = 0; i < srcLen; ++i) { // scan existing array for non-zero values
241
+ const uint32_t fetched = this->coupons_[i];
242
+ if (fetched != hll_constants::EMPTY) {
243
+ const int32_t idx = find<A>(coupons_new.data(), tgtLgCoupArrSize, fetched); // search TGT array
257
244
  if (idx < 0) { // found EMPTY
258
- tgtCouponIntArr[~idx] = fetched; // insert
245
+ coupons_new[~idx] = fetched; // insert
259
246
  continue;
260
247
  }
261
248
  throw std::runtime_error("Error: Found duplicate coupon");
262
249
  }
263
250
  }
264
-
265
- intAlloc().deallocate(this->couponIntArr, 1 << this->lgCouponArrInts);
266
- this->couponIntArr = tgtCouponIntArr;
267
- this->lgCouponArrInts = tgtLgCoupArrSize;
251
+ this->coupons_ = std::move(coupons_new);
268
252
  }
269
253
 
270
254
  template<typename A>
271
- static int find(const int* array, const int lgArrInts, const int coupon) {
272
- const int arrMask = (1 << lgArrInts) - 1;
273
- int probe = coupon & arrMask;
274
- const int loopIndex = probe;
255
+ static int32_t find(const uint32_t* array, uint8_t lgArrInts, uint32_t coupon) {
256
+ const uint32_t arrMask = (1 << lgArrInts) - 1;
257
+ uint32_t probe = coupon & arrMask;
258
+ const uint32_t loopIndex = probe;
275
259
  do {
276
- const int couponAtIdx = array[probe];
277
- if (couponAtIdx == HllUtil<A>::EMPTY) {
260
+ const uint32_t couponAtIdx = array[probe];
261
+ if (couponAtIdx == hll_constants::EMPTY) {
278
262
  return ~probe; //empty
279
263
  }
280
264
  else if (coupon == couponAtIdx) {
281
265
  return probe; //duplicate
282
266
  }
283
- const int stride = ((coupon & HllUtil<A>::KEY_MASK_26) >> lgArrInts) | 1;
267
+ const uint32_t stride = ((coupon & hll_constants::KEY_MASK_26) >> lgArrInts) | 1;
284
268
  probe = (probe + stride) & arrMask;
285
269
  } while (probe != loopIndex);
286
270
  throw std::invalid_argument("Key not found and no empty slots!");
@@ -24,34 +24,34 @@
24
24
 
25
25
  namespace datasketches {
26
26
 
27
- template<typename A = std::allocator<char>>
27
+ template<typename A>
28
28
  class CouponHashSet : public CouponList<A> {
29
29
  public:
30
- static CouponHashSet* newSet(const void* bytes, size_t len);
31
- static CouponHashSet* newSet(std::istream& is);
32
- explicit CouponHashSet(int lgConfigK, target_hll_type tgtHllType);
33
- explicit CouponHashSet(const CouponHashSet& that, target_hll_type tgtHllType);
34
- explicit CouponHashSet(const CouponHashSet& that);
30
+ static CouponHashSet* newSet(const void* bytes, size_t len, const A& allocator);
31
+ static CouponHashSet* newSet(std::istream& is, const A& allocator);
32
+ CouponHashSet(uint8_t lgConfigK, target_hll_type tgtHllType, const A& allocator);
33
+ CouponHashSet(const CouponHashSet& that, target_hll_type tgtHllType);
35
34
 
36
- virtual ~CouponHashSet();
35
+ virtual ~CouponHashSet() = default;
37
36
  virtual std::function<void(HllSketchImpl<A>*)> get_deleter() const;
38
37
 
39
38
  protected:
40
-
39
+ using vector_int = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
40
+
41
41
  virtual CouponHashSet* copy() const;
42
42
  virtual CouponHashSet* copyAs(target_hll_type tgtHllType) const;
43
43
 
44
- virtual HllSketchImpl<A>* couponUpdate(int coupon);
44
+ virtual HllSketchImpl<A>* couponUpdate(uint32_t coupon);
45
45
 
46
- virtual int getMemDataStart() const;
47
- virtual int getPreInts() const;
46
+ virtual uint32_t getMemDataStart() const;
47
+ virtual uint8_t getPreInts() const;
48
48
 
49
49
  friend class HllSketchImplFactory<A>;
50
50
 
51
51
  private:
52
- typedef typename std::allocator_traits<A>::template rebind_alloc<CouponHashSet<A>> chsAlloc;
52
+ using ChsAlloc = typename std::allocator_traits<A>::template rebind_alloc<CouponHashSet<A>>;
53
53
  bool checkGrowOrPromote();
54
- void growHashSet(int srcLgCoupArrSize, int tgtLgCoupArrSize);
54
+ void growHashSet(uint8_t tgtLgCoupArrSize);
55
55
  };
56
56
 
57
57
  }