datasketches 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/NOTICE +1 -1
- data/README.md +0 -2
- data/ext/datasketches/cpc_wrapper.cpp +3 -3
- data/ext/datasketches/kll_wrapper.cpp +0 -10
- data/lib/datasketches/version.rb +1 -1
- data/lib/datasketches.rb +1 -1
- data/vendor/datasketches-cpp/CMakeLists.txt +23 -20
- data/vendor/datasketches-cpp/CODE_OF_CONDUCT.md +3 -0
- data/vendor/datasketches-cpp/CONTRIBUTING.md +50 -0
- data/vendor/datasketches-cpp/Doxyfile +2827 -0
- data/vendor/datasketches-cpp/LICENSE +0 -76
- data/vendor/datasketches-cpp/README.md +1 -3
- data/vendor/datasketches-cpp/common/CMakeLists.txt +12 -11
- data/vendor/datasketches-cpp/common/include/MurmurHash3.h +25 -27
- data/vendor/datasketches-cpp/common/include/common_defs.hpp +15 -10
- data/vendor/datasketches-cpp/common/include/count_zeros.hpp +11 -2
- data/vendor/datasketches-cpp/common/include/kolmogorov_smirnov.hpp +9 -6
- data/vendor/datasketches-cpp/common/include/memory_operations.hpp +5 -4
- data/vendor/datasketches-cpp/common/include/optional.hpp +148 -0
- data/vendor/datasketches-cpp/common/include/quantiles_sorted_view.hpp +95 -2
- data/vendor/datasketches-cpp/common/include/quantiles_sorted_view_impl.hpp +1 -1
- data/vendor/datasketches-cpp/common/include/serde.hpp +69 -20
- data/vendor/datasketches-cpp/common/test/CMakeLists.txt +2 -2
- data/vendor/datasketches-cpp/common/test/integration_test.cpp +6 -0
- data/vendor/datasketches-cpp/common/test/optional_test.cpp +85 -0
- data/vendor/datasketches-cpp/common/test/test_allocator.hpp +14 -14
- data/vendor/datasketches-cpp/{python/src/__init__.py → count/CMakeLists.txt} +25 -1
- data/vendor/datasketches-cpp/count/include/count_min.hpp +405 -0
- data/vendor/datasketches-cpp/count/include/count_min_impl.hpp +497 -0
- data/vendor/datasketches-cpp/{MANIFEST.in → count/test/CMakeLists.txt} +23 -20
- data/vendor/datasketches-cpp/count/test/count_min_allocation_test.cpp +155 -0
- data/vendor/datasketches-cpp/count/test/count_min_test.cpp +303 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +14 -20
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +7 -4
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +17 -17
- data/vendor/datasketches-cpp/cpc/include/cpc_confidence.hpp +3 -3
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +40 -40
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +14 -11
- data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +35 -11
- data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +8 -8
- data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +16 -8
- data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +3 -2
- data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +5 -5
- data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +20 -7
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_deserialize_from_java_test.cpp +60 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_serialize_for_java.cpp +38 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +4 -29
- data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +4 -4
- data/vendor/datasketches-cpp/{tox.ini → density/CMakeLists.txt} +24 -8
- data/vendor/datasketches-cpp/density/include/density_sketch.hpp +256 -0
- data/vendor/datasketches-cpp/density/include/density_sketch_impl.hpp +543 -0
- data/vendor/datasketches-cpp/{python/datasketches/__init__.py → density/test/CMakeLists.txt} +15 -3
- data/vendor/datasketches-cpp/density/test/density_sketch_test.cpp +244 -0
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +21 -9
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +6 -4
- data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +9 -3
- data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +14 -1
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_deserialize_from_java_test.cpp +95 -0
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_serialize_for_java.cpp +83 -0
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +3 -42
- data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +2 -2
- data/vendor/datasketches-cpp/hll/include/CouponList.hpp +3 -1
- data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +19 -11
- data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +2 -5
- data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +19 -7
- data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +1 -1
- data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +98 -42
- data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +2 -0
- data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +94 -61
- data/vendor/datasketches-cpp/hll/include/HllArray.hpp +20 -8
- data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +4 -4
- data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +3 -1
- data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +3 -21
- data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +8 -0
- data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +14 -18
- data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +1 -1
- data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +8 -2
- data/vendor/datasketches-cpp/hll/include/hll.hpp +79 -65
- data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +14 -1
- data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +7 -1
- data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +0 -68
- data/vendor/datasketches-cpp/hll/test/hll_sketch_deserialize_from_java_test.cpp +69 -0
- data/vendor/datasketches-cpp/hll/test/hll_sketch_serialize_for_java.cpp +52 -0
- data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +0 -1
- data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +2 -2
- data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +79 -53
- data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +61 -132
- data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +14 -1
- data/vendor/datasketches-cpp/kll/test/kll_sketch_deserialize_from_java_test.cpp +103 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_serialize_for_java.cpp +62 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +5 -40
- data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch.hpp +76 -54
- data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch_impl.hpp +66 -136
- data/vendor/datasketches-cpp/quantiles/test/CMakeLists.txt +14 -1
- data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_deserialize_from_java_test.cpp +84 -0
- data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_serialize_for_java.cpp +52 -0
- data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_test.cpp +15 -39
- data/vendor/datasketches-cpp/req/include/req_common.hpp +7 -3
- data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +2 -4
- data/vendor/datasketches-cpp/req/include/req_sketch.hpp +105 -26
- data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +50 -111
- data/vendor/datasketches-cpp/req/test/CMakeLists.txt +14 -1
- data/vendor/datasketches-cpp/req/test/req_sketch_deserialize_from_java_test.cpp +55 -0
- data/vendor/datasketches-cpp/{tuple/include/array_of_doubles_intersection_impl.hpp → req/test/req_sketch_serialize_for_java.cpp} +12 -7
- data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +3 -89
- data/vendor/datasketches-cpp/sampling/CMakeLists.txt +4 -0
- data/vendor/datasketches-cpp/sampling/include/ebpps_sample.hpp +210 -0
- data/vendor/datasketches-cpp/sampling/include/ebpps_sample_impl.hpp +535 -0
- data/vendor/datasketches-cpp/sampling/include/ebpps_sketch.hpp +281 -0
- data/vendor/datasketches-cpp/sampling/include/ebpps_sketch_impl.hpp +531 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +89 -32
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +33 -19
- data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +13 -10
- data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +23 -19
- data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +55 -8
- data/vendor/datasketches-cpp/sampling/test/ebpps_allocation_test.cpp +96 -0
- data/vendor/datasketches-cpp/sampling/test/ebpps_sample_test.cpp +137 -0
- data/vendor/datasketches-cpp/sampling/test/ebpps_sketch_test.cpp +266 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_deserialize_from_java_test.cpp +81 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_serialize_for_java.cpp +54 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +33 -51
- data/vendor/datasketches-cpp/sampling/test/var_opt_union_deserialize_from_java_test.cpp +50 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_union_serialize_for_java.cpp +56 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +0 -20
- data/vendor/datasketches-cpp/theta/CMakeLists.txt +1 -0
- data/vendor/datasketches-cpp/theta/include/bit_packing.hpp +6279 -0
- data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_sampled_sets.hpp +1 -0
- data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_theta_sketched_sets.hpp +7 -6
- data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser.hpp +14 -8
- data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +60 -46
- data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +20 -5
- data/vendor/datasketches-cpp/theta/include/theta_constants.hpp +10 -4
- data/vendor/datasketches-cpp/theta/include/theta_helpers.hpp +3 -1
- data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +13 -5
- data/vendor/datasketches-cpp/theta/include/theta_intersection_base_impl.hpp +5 -5
- data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +3 -3
- data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity.hpp +2 -1
- data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity_base.hpp +1 -0
- data/vendor/datasketches-cpp/theta/include/theta_set_difference_base_impl.hpp +1 -1
- data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +180 -33
- data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +430 -130
- data/vendor/datasketches-cpp/theta/include/theta_union.hpp +17 -10
- data/vendor/datasketches-cpp/theta/include/theta_union_base_impl.hpp +10 -10
- data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +3 -3
- data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base.hpp +21 -6
- data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base_impl.hpp +13 -3
- data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +15 -1
- data/vendor/datasketches-cpp/theta/test/bit_packing_test.cpp +80 -0
- data/vendor/datasketches-cpp/theta/test/theta_sketch_deserialize_from_java_test.cpp +57 -0
- data/vendor/datasketches-cpp/theta/test/theta_sketch_serialize_for_java.cpp +61 -0
- data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +39 -188
- data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +25 -0
- data/vendor/datasketches-cpp/tuple/CMakeLists.txt +8 -7
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +19 -144
- data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b.hpp → array_tuple_a_not_b.hpp} +24 -16
- data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b_impl.hpp → array_tuple_a_not_b_impl.hpp} +4 -4
- data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection.hpp +65 -0
- data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection_impl.hpp +31 -0
- data/vendor/datasketches-cpp/tuple/include/array_tuple_sketch.hpp +237 -0
- data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_sketch_impl.hpp → array_tuple_sketch_impl.hpp} +40 -41
- data/vendor/datasketches-cpp/tuple/include/array_tuple_union.hpp +81 -0
- data/vendor/datasketches-cpp/tuple/include/array_tuple_union_impl.hpp +43 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +11 -2
- data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +17 -10
- data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +2 -1
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +95 -32
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +2 -1
- data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +19 -11
- data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +16 -1
- data/vendor/datasketches-cpp/tuple/test/aod_sketch_deserialize_from_java_test.cpp +76 -0
- data/vendor/datasketches-cpp/tuple/test/aod_sketch_serialize_for_java.cpp +62 -0
- data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +5 -129
- data/vendor/datasketches-cpp/tuple/test/engagement_test.cpp +85 -89
- data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +3 -1
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_deserialize_from_java_test.cpp +47 -0
- data/vendor/datasketches-cpp/{python/src/datasketches.cpp → tuple/test/tuple_sketch_serialize_for_java.cpp} +16 -30
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +1 -1
- data/vendor/datasketches-cpp/version.cfg.in +1 -1
- metadata +61 -79
- data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
- data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
- data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
- data/vendor/datasketches-cpp/pyproject.toml +0 -23
- data/vendor/datasketches-cpp/python/CMakeLists.txt +0 -81
- data/vendor/datasketches-cpp/python/README.md +0 -85
- data/vendor/datasketches-cpp/python/datasketches/PySerDe.py +0 -104
- data/vendor/datasketches-cpp/python/include/py_serde.hpp +0 -113
- data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +0 -345
- data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +0 -354
- data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +0 -346
- data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +0 -463
- data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +0 -403
- data/vendor/datasketches-cpp/python/pybind11Path.cmd +0 -21
- data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +0 -90
- data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +0 -128
- data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +0 -134
- data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +0 -210
- data/vendor/datasketches-cpp/python/src/ks_wrapper.cpp +0 -68
- data/vendor/datasketches-cpp/python/src/py_serde.cpp +0 -111
- data/vendor/datasketches-cpp/python/src/quantiles_wrapper.cpp +0 -204
- data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +0 -215
- data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +0 -172
- data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +0 -490
- data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +0 -173
- data/vendor/datasketches-cpp/python/tests/__init__.py +0 -16
- data/vendor/datasketches-cpp/python/tests/cpc_test.py +0 -64
- data/vendor/datasketches-cpp/python/tests/fi_test.py +0 -110
- data/vendor/datasketches-cpp/python/tests/hll_test.py +0 -130
- data/vendor/datasketches-cpp/python/tests/kll_test.py +0 -125
- data/vendor/datasketches-cpp/python/tests/quantiles_test.py +0 -126
- data/vendor/datasketches-cpp/python/tests/req_test.py +0 -126
- data/vendor/datasketches-cpp/python/tests/theta_test.py +0 -146
- data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +0 -148
- data/vendor/datasketches-cpp/python/tests/vo_test.py +0 -125
- data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
- data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
- data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
- data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +0 -67
- data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
- data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
- data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
- data/vendor/datasketches-cpp/setup.py +0 -110
- data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_exact_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +0 -52
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +0 -81
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +0 -43
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +0 -1
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +0 -1
@@ -0,0 +1,405 @@
|
|
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 COUNT_MIN_HPP_
|
21
|
+
#define COUNT_MIN_HPP_
|
22
|
+
|
23
|
+
#include <iterator>
|
24
|
+
#include "common_defs.hpp"
|
25
|
+
|
26
|
+
namespace datasketches {
|
27
|
+
|
28
|
+
/**
|
29
|
+
* C++ implementation of the CountMin sketch data structure of Cormode and Muthukrishnan.
|
30
|
+
* [1] - http://dimacs.rutgers.edu/~graham/pubs/papers/cm-full.pdf
|
31
|
+
* The template type W is the type of the vector that contains the weights of the objects inserted into the sketch,
|
32
|
+
* not the type of the input items themselves.
|
33
|
+
* @author Charlie Dickens
|
34
|
+
*/
|
35
|
+
template <typename W,
|
36
|
+
typename Allocator = std::allocator<W>>
|
37
|
+
class count_min_sketch{
|
38
|
+
static_assert(std::is_arithmetic<W>::value, "Arithmetic type expected");
|
39
|
+
public:
|
40
|
+
using allocator_type = Allocator;
|
41
|
+
using const_iterator = typename std::vector<W, Allocator>::const_iterator;
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Creates an instance of the sketch given parameters _num_hashes, _num_buckets and hash seed, `seed`.
|
45
|
+
* @param num_hashes number of hash functions in the sketch. Equivalently the number of rows in the array
|
46
|
+
* @param num_buckets number of buckets that hash functions map into. Equivalently the number of columns in the array
|
47
|
+
* @param seed for hash function
|
48
|
+
* @param allocator to acquire and release memory
|
49
|
+
*
|
50
|
+
* The items inserted into the sketch can be arbitrary type, so long as they are hashable via murmurhash.
|
51
|
+
* Only update and estimate methods are added for uint64_t and string types.
|
52
|
+
*/
|
53
|
+
count_min_sketch(uint8_t num_hashes, uint32_t num_buckets, uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
|
54
|
+
|
55
|
+
/**
|
56
|
+
* @return configured _num_hashes of this sketch
|
57
|
+
*/
|
58
|
+
uint8_t get_num_hashes() const;
|
59
|
+
|
60
|
+
/**
|
61
|
+
* @return configured _num_buckets of this sketch
|
62
|
+
*/
|
63
|
+
uint32_t get_num_buckets() const;
|
64
|
+
|
65
|
+
/**
|
66
|
+
* @return configured seed of this sketch
|
67
|
+
*/
|
68
|
+
uint64_t get_seed() const;
|
69
|
+
|
70
|
+
/**
|
71
|
+
* @return epsilon
|
72
|
+
* The maximum permissible error for any frequency estimate query.
|
73
|
+
* epsilon = ceil(e / _num_buckets)
|
74
|
+
*/
|
75
|
+
double get_relative_error() const;
|
76
|
+
|
77
|
+
/**
|
78
|
+
* @return _total_weight
|
79
|
+
* The total weight currently inserted into the stream.
|
80
|
+
*/
|
81
|
+
W get_total_weight() const;
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Suggests the number of buckets required to achieve the given relative error
|
85
|
+
* @param relative_error the desired accuracy within which estimates should lie.
|
86
|
+
* For example, when relative_error = 0.05, then the returned frequency estimates satisfy the
|
87
|
+
* `relative_error` guarantee that never overestimates the weights but may underestimate the weights
|
88
|
+
* by 5% of the total weight in the sketch.
|
89
|
+
* @return the number of hash buckets at every level of the
|
90
|
+
* sketch required in order to obtain the specified relative error.
|
91
|
+
* [1] - Section 3 ``Data Structure'', page 6.
|
92
|
+
*/
|
93
|
+
static uint32_t suggest_num_buckets(double relative_error);
|
94
|
+
|
95
|
+
/**
|
96
|
+
* Suggests the number of hash functions required to achieve the given confidence
|
97
|
+
* @param confidence the desired confidence with which estimates should be correct.
|
98
|
+
* For example, with 95% confidence, frequency estimates satisfy the `relative_error` guarantee.
|
99
|
+
* @return the number of hash functions that are required in
|
100
|
+
* order to achieve the specified confidence of the sketch.
|
101
|
+
* confidence = 1 - delta, with delta denoting the sketch failure probability in the literature.
|
102
|
+
* [1] - Section 3 ``Data Structure'', page 6.
|
103
|
+
*/
|
104
|
+
static uint8_t suggest_num_hashes(double confidence);
|
105
|
+
|
106
|
+
/**
|
107
|
+
* Specific get_estimate function for uint64_t type
|
108
|
+
* see generic get_estimate function
|
109
|
+
* @param item uint64_t type.
|
110
|
+
* @return an estimate of the item's frequency.
|
111
|
+
*/
|
112
|
+
W get_estimate(uint64_t item) const;
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Specific get_estimate function for int64_t type
|
116
|
+
* see generic get_estimate function
|
117
|
+
* @param item int64_t type.
|
118
|
+
* @return an estimate of the item's frequency.
|
119
|
+
*/
|
120
|
+
W get_estimate(int64_t item) const;
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Specific get_estimate function for std::string type
|
124
|
+
* see generic get_estimate function
|
125
|
+
* @param item std::string type
|
126
|
+
* @return an estimate of the item's frequency.
|
127
|
+
*/
|
128
|
+
W get_estimate(const std::string& item) const;
|
129
|
+
|
130
|
+
/**
|
131
|
+
* This is the generic estimate query function for any of the given datatypes.
|
132
|
+
* Query the sketch for the estimate of a given item.
|
133
|
+
* @param item pointer to the data item to be query from the sketch.
|
134
|
+
* @param size size of the item in bytes
|
135
|
+
* @return the estimated frequency of the item denoted f_est satisfying
|
136
|
+
* f_true - relative_error*_total_weight <= f_est <= f_true
|
137
|
+
*/
|
138
|
+
W get_estimate(const void* item, size_t size) const;
|
139
|
+
|
140
|
+
/**
|
141
|
+
* Query the sketch for the upper bound of a given item.
|
142
|
+
* @param item to query
|
143
|
+
* @param size of the item in bytes
|
144
|
+
* @return the upper bound on the true frequency of the item
|
145
|
+
* f_true <= f_est + relative_error*_total_weight
|
146
|
+
*/
|
147
|
+
W get_upper_bound(const void* item, size_t size) const;
|
148
|
+
|
149
|
+
/**
|
150
|
+
* Query the sketch for the upper bound of a given item.
|
151
|
+
* @param item to query
|
152
|
+
* @return the upper bound on the true frequency of the item
|
153
|
+
* f_true <= f_est + relative_error*_total_weight
|
154
|
+
*/
|
155
|
+
W get_upper_bound(int64_t item) const;
|
156
|
+
|
157
|
+
/**
|
158
|
+
* Query the sketch for the upper bound of a given item.
|
159
|
+
* @param item to query
|
160
|
+
* @return the upper bound on the true frequency of the item
|
161
|
+
* f_true <= f_est + relative_error*_total_weight
|
162
|
+
*/
|
163
|
+
W get_upper_bound(uint64_t item) const;
|
164
|
+
|
165
|
+
/**
|
166
|
+
* Query the sketch for the upper bound of a given item.
|
167
|
+
* @param item to query
|
168
|
+
* @return the upper bound on the true frequency of the item
|
169
|
+
* f_true <= f_est + relative_error*_total_weight
|
170
|
+
*/
|
171
|
+
W get_upper_bound(const std::string& item) const;
|
172
|
+
|
173
|
+
/**
|
174
|
+
* Query the sketch for the lower bound of a given item.
|
175
|
+
* @param item to query
|
176
|
+
* @param size of the item in bytes
|
177
|
+
* @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
|
178
|
+
* f_true - relative_error*_total_weight <= f_est
|
179
|
+
*/
|
180
|
+
W get_lower_bound(const void* item, size_t size) const;
|
181
|
+
|
182
|
+
/**
|
183
|
+
* Query the sketch for the lower bound of a given item.
|
184
|
+
* @param item to query
|
185
|
+
* @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
|
186
|
+
* f_true - relative_error*_total_weight <= f_est
|
187
|
+
*/
|
188
|
+
W get_lower_bound(int64_t item) const;
|
189
|
+
|
190
|
+
/**
|
191
|
+
* Query the sketch for the lower bound of a given item.
|
192
|
+
* @param item to query
|
193
|
+
* @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
|
194
|
+
* f_true - relative_error*_total_weight <= f_est
|
195
|
+
*/
|
196
|
+
W get_lower_bound(uint64_t item) const;
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Query the sketch for the lower bound of a given item.
|
200
|
+
* @param item to query
|
201
|
+
* @return the lower bound for the query result, f_est, on the true frequency, f_est of the item
|
202
|
+
* f_true - relative_error*_total_weight <= f_est
|
203
|
+
*/
|
204
|
+
W get_lower_bound(const std::string& item) const;
|
205
|
+
|
206
|
+
/**
|
207
|
+
* Update this sketch with given data of any type.
|
208
|
+
* This is a "universal" update that covers all cases,
|
209
|
+
* but may produce different hashes compared to specialized update methods.
|
210
|
+
* @param item pointer to the data item to be inserted into the sketch.
|
211
|
+
* @param size of the data in bytes
|
212
|
+
* @param weight arithmetic type
|
213
|
+
*/
|
214
|
+
void update(const void* item, size_t size, W weight);
|
215
|
+
|
216
|
+
/**
|
217
|
+
* Update this sketch with a given item.
|
218
|
+
* @param item to update the sketch with
|
219
|
+
* @param weight arithmetic type
|
220
|
+
*/
|
221
|
+
void update(uint64_t item, W weight = 1);
|
222
|
+
|
223
|
+
/**
|
224
|
+
* Update this sketch with a given item.
|
225
|
+
* @param item to update the sketch with
|
226
|
+
* @param weight arithmetic type
|
227
|
+
*/
|
228
|
+
void update(int64_t item, W weight = 1);
|
229
|
+
|
230
|
+
/**
|
231
|
+
* Update this sketch with a given string.
|
232
|
+
* @param item string to update the sketch with
|
233
|
+
* @param weight arithmetic type
|
234
|
+
*/
|
235
|
+
void update(const std::string& item, W weight = 1);
|
236
|
+
|
237
|
+
/**
|
238
|
+
* Merges another count_min_sketch into this count_min_sketch.
|
239
|
+
* @param other_sketch
|
240
|
+
*/
|
241
|
+
void merge(const count_min_sketch& other_sketch);
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Returns true if this sketch is empty.
|
245
|
+
* A Count Min Sketch is defined to be empty iff weight == 0
|
246
|
+
* This can only ever happen if all items inserted to the sketch have weights that cancel each other out.
|
247
|
+
* @return empty flag
|
248
|
+
*/
|
249
|
+
bool is_empty() const;
|
250
|
+
|
251
|
+
/**
|
252
|
+
* @brief Returns a string describing the sketch
|
253
|
+
* @return A string with a human-readable description of the sketch
|
254
|
+
*/
|
255
|
+
string<Allocator> to_string() const;
|
256
|
+
|
257
|
+
/**
|
258
|
+
* Iterator pointing to the first item in the sketch.
|
259
|
+
* If the sketch is empty, the returned iterator must not be dereferenced or incremented.
|
260
|
+
* @return iterator pointing to the first item in the sketch
|
261
|
+
*/
|
262
|
+
const_iterator begin() const;
|
263
|
+
|
264
|
+
/**
|
265
|
+
* Iterator pointing to the past-the-end item in the sketch.
|
266
|
+
* The past-the-end item is the hypothetical item that would follow the last item.
|
267
|
+
* It does not point to any item, and must not be dereferenced or incremented.
|
268
|
+
* @return iterator pointing to the past-the-end item in the sketch
|
269
|
+
*/
|
270
|
+
const_iterator end() const;
|
271
|
+
|
272
|
+
/*
|
273
|
+
* The serialized sketch binary form has the following structure
|
274
|
+
* Byte 0:
|
275
|
+
* 1 - if and only if the sketch is empty
|
276
|
+
* 0 - otherwise
|
277
|
+
*
|
278
|
+
* Byte 1 (serial version), byte 2 (family id), byte 3 (flags):
|
279
|
+
* 00000001 - default for now.
|
280
|
+
*
|
281
|
+
* Bytes 4 - 7:
|
282
|
+
* uint8_t zero corresponding to ``empty''
|
283
|
+
*
|
284
|
+
* Byte 8:
|
285
|
+
* uint_8 for number of hash functions
|
286
|
+
*
|
287
|
+
* Bytes 9, 13
|
288
|
+
* 4 bytes : uint32 for number of buckets.
|
289
|
+
*
|
290
|
+
* Bytes 14, 15:
|
291
|
+
* seed_hash
|
292
|
+
*
|
293
|
+
* Byte 16:
|
294
|
+
* uint8_t zero corresponding to ``empty''
|
295
|
+
*
|
296
|
+
* All remaining bytes from 17-24 follow the pattern of
|
297
|
+
* Bytes 17-24:
|
298
|
+
* Sketch array entry
|
299
|
+
*
|
300
|
+
|
301
|
+
0 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
302
|
+
||is_empty|ser__ver|familyId| flags |xxxxxxxx|xxxxxxxx|xxxxxxxx|xxxxxxxx|
|
303
|
+
|
304
|
+
1 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
305
|
+
||---------- _num_buckets -----------|num_hash|__seed__ __hash__|xxxxxxxx|
|
306
|
+
|
307
|
+
2 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
308
|
+
||---------------------------- total weight ----------------------------|
|
309
|
+
|
310
|
+
3 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
311
|
+
||---------------------------- sketch entries ---------------------------|
|
312
|
+
...
|
313
|
+
|
314
|
+
*/
|
315
|
+
|
316
|
+
|
317
|
+
/**
|
318
|
+
* Computes size needed to serialize the current state of the sketch.
|
319
|
+
* @return size in bytes needed to serialize this sketch
|
320
|
+
*/
|
321
|
+
size_t get_serialized_size_bytes() const;
|
322
|
+
|
323
|
+
/**
|
324
|
+
* This method serializes the sketch into a given stream in a binary form
|
325
|
+
* @param os output stream
|
326
|
+
*/
|
327
|
+
void serialize(std::ostream& os) const;
|
328
|
+
|
329
|
+
// This is a convenience alias for users
|
330
|
+
// The type returned by the following serialize method
|
331
|
+
using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
|
332
|
+
|
333
|
+
/**
|
334
|
+
* This method serializes the sketch as a vector of bytes.
|
335
|
+
* An optional header can be reserved in front of the sketch.
|
336
|
+
* It is an uninitialized space of a given size.
|
337
|
+
* This header is used in Datasketches PostgreSQL extension.
|
338
|
+
* @param header_size_bytes space to reserve in front of the sketch
|
339
|
+
*/
|
340
|
+
vector_bytes serialize(unsigned header_size_bytes = 0) const;
|
341
|
+
|
342
|
+
/**
|
343
|
+
* This method deserializes a sketch from a given stream.
|
344
|
+
* @param is input stream
|
345
|
+
* @param seed the seed for the hash function that was used to create the sketch
|
346
|
+
* @param allocator instance of an Allocator
|
347
|
+
* @return an instance of a sketch
|
348
|
+
*/
|
349
|
+
static count_min_sketch deserialize(std::istream& is, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
|
350
|
+
|
351
|
+
/**
|
352
|
+
* This method deserializes a sketch from a given array of bytes.
|
353
|
+
* @param bytes pointer to the array of bytes
|
354
|
+
* @param size the size of the array
|
355
|
+
* @param seed the seed for the hash function that was used to create the sketch
|
356
|
+
* @param allocator instance of an Allocator
|
357
|
+
* @return an instance of the sketch
|
358
|
+
*/
|
359
|
+
static count_min_sketch deserialize(const void* bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator& allocator = Allocator());
|
360
|
+
|
361
|
+
/**
|
362
|
+
* @return allocator
|
363
|
+
*/
|
364
|
+
allocator_type get_allocator() const;
|
365
|
+
|
366
|
+
private:
|
367
|
+
Allocator _allocator;
|
368
|
+
uint8_t _num_hashes;
|
369
|
+
uint32_t _num_buckets;
|
370
|
+
std::vector<W, Allocator> _sketch_array; // the array stored by the sketch
|
371
|
+
uint64_t _seed;
|
372
|
+
W _total_weight;
|
373
|
+
std::vector<uint64_t> hash_seeds;
|
374
|
+
|
375
|
+
enum flags {IS_EMPTY};
|
376
|
+
static const uint8_t PREAMBLE_LONGS_SHORT = 2; // Empty -> need second byte for sketch parameters
|
377
|
+
static const uint8_t PREAMBLE_LONGS_FULL = 3; // Not empty -> need (at least) third byte for total weight.
|
378
|
+
static const uint8_t SERIAL_VERSION_1 = 1;
|
379
|
+
static const uint8_t FAMILY_ID = 18;
|
380
|
+
static const uint8_t NULL_8 = 0;
|
381
|
+
static const uint32_t NULL_32 = 0;
|
382
|
+
|
383
|
+
/**
|
384
|
+
* Throws an error if the header is not valid.
|
385
|
+
* @param preamble_longs
|
386
|
+
* @param serial_version
|
387
|
+
* @param flags_byte
|
388
|
+
*/
|
389
|
+
static void check_header_validity(uint8_t preamble_longs, uint8_t serial_version, uint8_t family_id, uint8_t flags_byte);
|
390
|
+
|
391
|
+
/*
|
392
|
+
* Obtain the hash values when inserting an item into the sketch.
|
393
|
+
* @param item pointer to the data item to be inserted into the sketch.
|
394
|
+
* @param size of the data in bytes
|
395
|
+
* @return vector of uint64_t which each represent the index to which `value' must update in the sketch
|
396
|
+
*/
|
397
|
+
std::vector<uint64_t> get_hashes(const void* item, size_t size) const;
|
398
|
+
|
399
|
+
};
|
400
|
+
|
401
|
+
} /* namespace datasketches */
|
402
|
+
|
403
|
+
#include "count_min_impl.hpp"
|
404
|
+
|
405
|
+
#endif
|