datasketches 0.1.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/LICENSE +40 -3
- data/NOTICE +1 -1
- data/ext/datasketches/cpc_wrapper.cpp +12 -13
- data/ext/datasketches/ext.cpp +1 -1
- data/ext/datasketches/ext.h +4 -0
- data/ext/datasketches/extconf.rb +1 -1
- data/ext/datasketches/fi_wrapper.cpp +6 -8
- data/ext/datasketches/hll_wrapper.cpp +13 -14
- data/ext/datasketches/kll_wrapper.cpp +28 -76
- data/ext/datasketches/theta_wrapper.cpp +27 -41
- data/ext/datasketches/vo_wrapper.cpp +4 -6
- data/lib/datasketches/version.rb +1 -1
- data/vendor/datasketches-cpp/CMakeLists.txt +10 -0
- data/vendor/datasketches-cpp/LICENSE +40 -3
- data/vendor/datasketches-cpp/NOTICE +1 -1
- data/vendor/datasketches-cpp/README.md +4 -4
- data/vendor/datasketches-cpp/common/include/MurmurHash3.h +18 -7
- data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +8 -8
- data/vendor/datasketches-cpp/common/include/bounds_binomial_proportions.hpp +12 -15
- data/vendor/datasketches-cpp/common/include/common_defs.hpp +26 -0
- data/vendor/datasketches-cpp/common/include/conditional_forward.hpp +20 -8
- data/vendor/datasketches-cpp/common/include/count_zeros.hpp +2 -2
- data/vendor/datasketches-cpp/common/include/memory_operations.hpp +12 -0
- data/vendor/datasketches-cpp/common/include/serde.hpp +7 -7
- data/vendor/datasketches-cpp/common/test/CMakeLists.txt +24 -0
- data/vendor/datasketches-cpp/common/test/integration_test.cpp +77 -0
- data/vendor/datasketches-cpp/common/test/test_allocator.hpp +9 -1
- data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +13 -3
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +20 -20
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +116 -105
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +22 -6
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +140 -101
- data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +2 -2
- data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +20 -20
- data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +10 -16
- data/vendor/datasketches-cpp/cpc/include/icon_estimator.hpp +6 -6
- data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +10 -10
- data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +21 -21
- data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +1 -0
- data/vendor/datasketches-cpp/cpc/test/compression_test.cpp +10 -10
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_allocation_test.cpp +237 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +25 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +1 -1
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +15 -10
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +102 -105
- data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +19 -13
- data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map_impl.hpp +141 -125
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +15 -12
- data/vendor/datasketches-cpp/fi/test/reverse_purge_hash_map_test.cpp +5 -5
- data/vendor/datasketches-cpp/hll/CMakeLists.txt +3 -0
- data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +81 -109
- data/vendor/datasketches-cpp/hll/include/AuxHashMap.hpp +25 -24
- data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +15 -15
- data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable.hpp +5 -5
- data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +89 -105
- data/vendor/datasketches-cpp/hll/include/CouponHashSet.hpp +13 -13
- data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +130 -165
- data/vendor/datasketches-cpp/hll/include/CouponList.hpp +21 -22
- data/vendor/datasketches-cpp/hll/include/CubicInterpolation-internal.hpp +2 -4
- data/vendor/datasketches-cpp/hll/include/CubicInterpolation.hpp +2 -2
- data/vendor/datasketches-cpp/hll/include/HarmonicNumbers-internal.hpp +1 -1
- data/vendor/datasketches-cpp/hll/include/HarmonicNumbers.hpp +2 -2
- data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +88 -83
- data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +9 -9
- data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +34 -45
- data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +7 -8
- data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +41 -52
- data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +7 -8
- data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +220 -251
- data/vendor/datasketches-cpp/hll/include/HllArray.hpp +42 -42
- data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +36 -38
- data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +22 -22
- data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +15 -14
- data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +47 -44
- data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +62 -87
- data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +121 -128
- data/vendor/datasketches-cpp/hll/include/RelativeErrorTables.hpp +1 -1
- data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +9 -9
- data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +5 -5
- data/vendor/datasketches-cpp/hll/include/hll.hpp +25 -53
- data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +8 -8
- data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +36 -36
- data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +28 -28
- data/vendor/datasketches-cpp/hll/test/CrossCountingTest.cpp +2 -2
- data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +37 -37
- data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +57 -61
- data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +10 -14
- data/vendor/datasketches-cpp/hll/test/IsomorphicTest.cpp +3 -3
- data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +4 -4
- data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +5 -4
- data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +6 -6
- data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +14 -6
- data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +40 -25
- data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +50 -6
- data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +164 -136
- data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov.hpp +67 -0
- data/vendor/datasketches-cpp/kll/include/kolmogorov_smirnov_impl.hpp +78 -0
- data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +1 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_custom_type_test.cpp +11 -10
- data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +178 -88
- data/vendor/datasketches-cpp/kll/test/kolmogorov_smirnov_test.cpp +111 -0
- data/vendor/datasketches-cpp/pyproject.toml +4 -2
- data/vendor/datasketches-cpp/python/CMakeLists.txt +12 -6
- data/vendor/datasketches-cpp/python/README.md +52 -49
- data/vendor/datasketches-cpp/python/pybind11Path.cmd +3 -0
- data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +1 -1
- data/vendor/datasketches-cpp/python/src/datasketches.cpp +2 -0
- data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +4 -6
- data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +4 -2
- data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +246 -0
- data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +38 -28
- data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +11 -5
- data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +2 -2
- data/vendor/datasketches-cpp/python/tests/hll_test.py +1 -2
- data/vendor/datasketches-cpp/python/tests/kll_test.py +5 -5
- data/vendor/datasketches-cpp/python/tests/req_test.py +126 -0
- data/vendor/datasketches-cpp/python/tests/theta_test.py +28 -3
- data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +4 -4
- data/vendor/datasketches-cpp/python/tests/vo_test.py +3 -3
- data/vendor/datasketches-cpp/req/CMakeLists.txt +60 -0
- data/vendor/datasketches-cpp/{tuple/include/theta_a_not_b_experimental_impl.hpp → req/include/req_common.hpp} +18 -8
- data/vendor/datasketches-cpp/req/include/req_compactor.hpp +137 -0
- data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +488 -0
- data/vendor/datasketches-cpp/req/include/req_quantile_calculator.hpp +69 -0
- data/vendor/datasketches-cpp/req/include/req_quantile_calculator_impl.hpp +60 -0
- data/vendor/datasketches-cpp/req/include/req_sketch.hpp +395 -0
- data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +810 -0
- data/vendor/datasketches-cpp/req/test/CMakeLists.txt +43 -0
- 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/req/test/req_sketch_custom_type_test.cpp +128 -0
- data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +494 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +19 -13
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +130 -127
- data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +5 -5
- data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +41 -49
- data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +1 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_allocation_test.cpp +96 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +6 -6
- data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +13 -44
- data/vendor/datasketches-cpp/setup.py +11 -6
- data/vendor/datasketches-cpp/theta/CMakeLists.txt +30 -3
- data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_sampled_sets.hpp +3 -2
- data/vendor/datasketches-cpp/{tuple → theta}/include/bounds_on_ratios_in_theta_sketched_sets.hpp +1 -1
- data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser.hpp +67 -0
- data/vendor/datasketches-cpp/theta/include/compact_theta_sketch_parser_impl.hpp +70 -0
- data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +12 -29
- data/vendor/datasketches-cpp/theta/include/theta_a_not_b_impl.hpp +5 -46
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_comparators.hpp +0 -0
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_constants.hpp +11 -4
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_helpers.hpp +0 -0
- data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +26 -28
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base.hpp +0 -0
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_intersection_base_impl.hpp +0 -0
- data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +8 -90
- data/vendor/datasketches-cpp/{tuple/test/theta_union_experimental_test.cpp → theta/include/theta_jaccard_similarity.hpp} +11 -18
- data/vendor/datasketches-cpp/{tuple/include/jaccard_similarity.hpp → theta/include/theta_jaccard_similarity_base.hpp} +24 -36
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base.hpp +0 -0
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_set_difference_base_impl.hpp +5 -0
- data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +163 -256
- data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +250 -651
- data/vendor/datasketches-cpp/theta/include/theta_union.hpp +27 -60
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base.hpp +1 -1
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_union_base_impl.hpp +6 -1
- data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +13 -69
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base.hpp +10 -21
- data/vendor/datasketches-cpp/{tuple → theta}/include/theta_update_sketch_base_impl.hpp +44 -30
- data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +1 -0
- data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +23 -1
- data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +21 -1
- data/vendor/datasketches-cpp/{tuple → theta}/test/theta_jaccard_similarity_test.cpp +60 -5
- data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +74 -235
- data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +22 -2
- data/vendor/datasketches-cpp/tuple/CMakeLists.txt +3 -35
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +47 -60
- data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +38 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +28 -13
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +57 -70
- data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +1 -6
- data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +1 -1
- data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +18 -21
- data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +13 -16
- data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +7 -6
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_allocation_test.cpp +3 -3
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +20 -20
- data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +13 -16
- metadata +51 -36
- data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental.hpp +0 -53
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental.hpp +0 -78
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental_impl.hpp +0 -43
- data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental.hpp +0 -393
- data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental_impl.hpp +0 -481
- data/vendor/datasketches-cpp/tuple/include/theta_union_experimental.hpp +0 -88
- data/vendor/datasketches-cpp/tuple/include/theta_union_experimental_impl.hpp +0 -47
- data/vendor/datasketches-cpp/tuple/test/theta_a_not_b_experimental_test.cpp +0 -250
- data/vendor/datasketches-cpp/tuple/test/theta_compact_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_compact_single_item_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_intersection_experimental_test.cpp +0 -224
- data/vendor/datasketches-cpp/tuple/test/theta_sketch_experimental_test.cpp +0 -247
@@ -0,0 +1,137 @@
|
|
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 REQ_COMPACTOR_HPP_
|
21
|
+
#define REQ_COMPACTOR_HPP_
|
22
|
+
|
23
|
+
#include <memory>
|
24
|
+
|
25
|
+
namespace datasketches {
|
26
|
+
|
27
|
+
template<
|
28
|
+
typename T,
|
29
|
+
typename Comparator,
|
30
|
+
typename Allocator
|
31
|
+
>
|
32
|
+
class req_compactor {
|
33
|
+
public:
|
34
|
+
req_compactor(bool hra, uint8_t lg_weight, uint32_t section_size, const Allocator& allocator, bool sorted = true);
|
35
|
+
~req_compactor();
|
36
|
+
req_compactor(const req_compactor& other);
|
37
|
+
req_compactor(req_compactor&& other) noexcept;
|
38
|
+
req_compactor& operator=(const req_compactor& other);
|
39
|
+
req_compactor& operator=(req_compactor&& other);
|
40
|
+
|
41
|
+
bool is_sorted() const;
|
42
|
+
uint32_t get_num_items() const;
|
43
|
+
uint32_t get_nom_capacity() const;
|
44
|
+
uint8_t get_lg_weight() const;
|
45
|
+
const T* begin() const;
|
46
|
+
const T* end() const;
|
47
|
+
T* begin();
|
48
|
+
T* end();
|
49
|
+
|
50
|
+
template<bool inclusive>
|
51
|
+
uint64_t compute_weight(const T& item) const;
|
52
|
+
|
53
|
+
template<typename FwdT>
|
54
|
+
void append(FwdT&& item);
|
55
|
+
|
56
|
+
template<typename FwdC>
|
57
|
+
void merge(FwdC&& other);
|
58
|
+
|
59
|
+
void sort();
|
60
|
+
|
61
|
+
std::pair<uint32_t, uint32_t> compact(req_compactor& next);
|
62
|
+
|
63
|
+
/**
|
64
|
+
* Computes size needed to serialize the current state of the compactor.
|
65
|
+
* This version is for fixed-size arithmetic types (integral and floating point).
|
66
|
+
* @return size in bytes needed to serialize this compactor
|
67
|
+
*/
|
68
|
+
template<typename S, typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
|
69
|
+
size_t get_serialized_size_bytes(const S& serde) const;
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Computes size needed to serialize the current state of the compactor.
|
73
|
+
* This version is for all other types and can be expensive since every item needs to be looked at.
|
74
|
+
* @return size in bytes needed to serialize this compactor
|
75
|
+
*/
|
76
|
+
template<typename S, typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
|
77
|
+
size_t get_serialized_size_bytes(const S& serde) const;
|
78
|
+
|
79
|
+
template<typename S>
|
80
|
+
void serialize(std::ostream& os, const S& serde) const;
|
81
|
+
|
82
|
+
template<typename S>
|
83
|
+
size_t serialize(void* dst, size_t capacity, const S& serde) const;
|
84
|
+
|
85
|
+
template<typename S>
|
86
|
+
static req_compactor deserialize(std::istream& is, const S& serde, const Allocator& allocator, bool sorted, bool hra);
|
87
|
+
|
88
|
+
template<typename S>
|
89
|
+
static std::pair<req_compactor, size_t> deserialize(const void* bytes, size_t size, const S& serde, const Allocator& allocator, bool sorted, bool hra);
|
90
|
+
|
91
|
+
template<typename S>
|
92
|
+
static req_compactor deserialize(std::istream& is, const S& serde, const Allocator& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra);
|
93
|
+
|
94
|
+
template<typename S>
|
95
|
+
static std::pair<req_compactor, size_t> deserialize(const void* bytes, size_t size, const S& serde, const Allocator& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra);
|
96
|
+
|
97
|
+
private:
|
98
|
+
Allocator allocator_;
|
99
|
+
uint8_t lg_weight_;
|
100
|
+
bool hra_;
|
101
|
+
bool coin_; // random bit for compaction
|
102
|
+
bool sorted_;
|
103
|
+
float section_size_raw_;
|
104
|
+
uint32_t section_size_;
|
105
|
+
uint8_t num_sections_;
|
106
|
+
uint64_t state_; // state of the deterministic compaction schedule
|
107
|
+
uint32_t num_items_;
|
108
|
+
uint32_t capacity_;
|
109
|
+
T* items_;
|
110
|
+
|
111
|
+
bool ensure_enough_sections();
|
112
|
+
std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
|
113
|
+
void grow(uint32_t new_capacity);
|
114
|
+
void ensure_space(uint32_t num);
|
115
|
+
|
116
|
+
static uint32_t nearest_even(float value);
|
117
|
+
|
118
|
+
template<typename InIter, typename OutIter>
|
119
|
+
static void promote_evens_or_odds(InIter from, InIter to, bool flag, OutIter dst);
|
120
|
+
|
121
|
+
// for deserialization
|
122
|
+
class items_deleter;
|
123
|
+
req_compactor(bool hra, uint8_t lg_weight, bool sorted, float section_size_raw, uint8_t num_sections, uint64_t state, std::unique_ptr<T, items_deleter> items, uint32_t num_items, const Allocator& allocator);
|
124
|
+
|
125
|
+
template<typename S>
|
126
|
+
static std::unique_ptr<T, items_deleter> deserialize_items(std::istream& is, const S& serde, const Allocator& allocator, uint32_t num);
|
127
|
+
|
128
|
+
template<typename S>
|
129
|
+
static std::pair<std::unique_ptr<T, items_deleter>, size_t> deserialize_items(const void* bytes, size_t size, const S& serde, const Allocator& allocator, uint32_t num);
|
130
|
+
|
131
|
+
};
|
132
|
+
|
133
|
+
} /* namespace datasketches */
|
134
|
+
|
135
|
+
#include "req_compactor_impl.hpp"
|
136
|
+
|
137
|
+
#endif
|
@@ -0,0 +1,488 @@
|
|
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 REQ_COMPACTOR_IMPL_HPP_
|
21
|
+
#define REQ_COMPACTOR_IMPL_HPP_
|
22
|
+
|
23
|
+
#include <stdexcept>
|
24
|
+
#include <cmath>
|
25
|
+
#include <algorithm>
|
26
|
+
|
27
|
+
#include "count_zeros.hpp"
|
28
|
+
#include "conditional_forward.hpp"
|
29
|
+
|
30
|
+
#include <iomanip>
|
31
|
+
|
32
|
+
namespace datasketches {
|
33
|
+
|
34
|
+
template<typename T, typename C, typename A>
|
35
|
+
req_compactor<T, C, A>::req_compactor(bool hra, uint8_t lg_weight, uint32_t section_size, const A& allocator, bool sorted):
|
36
|
+
allocator_(allocator),
|
37
|
+
lg_weight_(lg_weight),
|
38
|
+
hra_(hra),
|
39
|
+
coin_(false),
|
40
|
+
sorted_(sorted),
|
41
|
+
section_size_raw_(static_cast<float>(section_size)),
|
42
|
+
section_size_(section_size),
|
43
|
+
num_sections_(req_constants::INIT_NUM_SECTIONS),
|
44
|
+
state_(0),
|
45
|
+
num_items_(0),
|
46
|
+
capacity_(2 * get_nom_capacity()),
|
47
|
+
items_(allocator_.allocate(capacity_))
|
48
|
+
{}
|
49
|
+
|
50
|
+
template<typename T, typename C, typename A>
|
51
|
+
req_compactor<T, C, A>::~req_compactor() {
|
52
|
+
if (items_ != nullptr) {
|
53
|
+
for (auto it = begin(); it != end(); ++it) (*it).~T();
|
54
|
+
allocator_.deallocate(items_, capacity_);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
template<typename T, typename C, typename A>
|
59
|
+
req_compactor<T, C, A>::req_compactor(const req_compactor& other):
|
60
|
+
allocator_(other.allocator_),
|
61
|
+
lg_weight_(other.lg_weight_),
|
62
|
+
hra_(other.hra_),
|
63
|
+
coin_(other.coin_),
|
64
|
+
sorted_(other.sorted_),
|
65
|
+
section_size_raw_(other.section_size_raw_),
|
66
|
+
section_size_(other.section_size_),
|
67
|
+
num_sections_(other.num_sections_),
|
68
|
+
state_(other.state_),
|
69
|
+
num_items_(other.num_items_),
|
70
|
+
capacity_(other.capacity_),
|
71
|
+
items_(nullptr)
|
72
|
+
{
|
73
|
+
if (other.items_ != nullptr) {
|
74
|
+
items_ = allocator_.allocate(capacity_);
|
75
|
+
const uint32_t from = hra_ ? capacity_ - num_items_ : 0;
|
76
|
+
const uint32_t to = hra_ ? capacity_ : num_items_;
|
77
|
+
for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
template<typename T, typename C, typename A>
|
82
|
+
req_compactor<T, C, A>::req_compactor(req_compactor&& other) noexcept :
|
83
|
+
allocator_(std::move(other.allocator_)),
|
84
|
+
lg_weight_(other.lg_weight_),
|
85
|
+
hra_(other.hra_),
|
86
|
+
coin_(other.coin_),
|
87
|
+
sorted_(other.sorted_),
|
88
|
+
section_size_raw_(other.section_size_raw_),
|
89
|
+
section_size_(other.section_size_),
|
90
|
+
num_sections_(other.num_sections_),
|
91
|
+
state_(other.state_),
|
92
|
+
num_items_(other.num_items_),
|
93
|
+
capacity_(other.capacity_),
|
94
|
+
items_(other.items_)
|
95
|
+
{
|
96
|
+
other.items_ = nullptr;
|
97
|
+
}
|
98
|
+
|
99
|
+
template<typename T, typename C, typename A>
|
100
|
+
req_compactor<T, C, A>& req_compactor<T, C, A>::operator=(const req_compactor& other) {
|
101
|
+
req_compactor copy(other);
|
102
|
+
std::swap(allocator_, copy.allocator_);
|
103
|
+
std::swap(lg_weight_, copy.lg_weight_);
|
104
|
+
std::swap(hra_, copy.hra_);
|
105
|
+
std::swap(coin_, copy.coin_);
|
106
|
+
std::swap(sorted_, copy.sorted_);
|
107
|
+
std::swap(section_size_raw_, copy.section_size_raw_);
|
108
|
+
std::swap(section_size_, copy.section_size_);
|
109
|
+
std::swap(num_sections_, copy.num_sections_);
|
110
|
+
std::swap(state_, copy.state_);
|
111
|
+
std::swap(num_items_, copy.num_items_);
|
112
|
+
std::swap(capacity_, copy.capacity_);
|
113
|
+
std::swap(items_, copy.items_);
|
114
|
+
return *this;
|
115
|
+
}
|
116
|
+
|
117
|
+
template<typename T, typename C, typename A>
|
118
|
+
req_compactor<T, C, A>& req_compactor<T, C, A>::operator=(req_compactor&& other) {
|
119
|
+
std::swap(allocator_, other.allocator_);
|
120
|
+
std::swap(lg_weight_, other.lg_weight_);
|
121
|
+
std::swap(hra_, other.hra_);
|
122
|
+
std::swap(coin_, other.coin_);
|
123
|
+
std::swap(sorted_, other.sorted_);
|
124
|
+
std::swap(section_size_raw_, other.section_size_raw_);
|
125
|
+
std::swap(section_size_, other.section_size_);
|
126
|
+
std::swap(num_sections_, other.num_sections_);
|
127
|
+
std::swap(state_, other.state_);
|
128
|
+
std::swap(num_items_, other.num_items_);
|
129
|
+
std::swap(capacity_, other.capacity_);
|
130
|
+
std::swap(items_, other.items_);
|
131
|
+
return *this;
|
132
|
+
}
|
133
|
+
|
134
|
+
template<typename T, typename C, typename A>
|
135
|
+
bool req_compactor<T, C, A>::is_sorted() const {
|
136
|
+
return sorted_;
|
137
|
+
}
|
138
|
+
|
139
|
+
template<typename T, typename C, typename A>
|
140
|
+
uint32_t req_compactor<T, C, A>::get_num_items() const {
|
141
|
+
return num_items_;
|
142
|
+
}
|
143
|
+
|
144
|
+
template<typename T, typename C, typename A>
|
145
|
+
uint32_t req_compactor<T, C, A>::get_nom_capacity() const {
|
146
|
+
return req_constants::MULTIPLIER * num_sections_ * section_size_;
|
147
|
+
}
|
148
|
+
|
149
|
+
template<typename T, typename C, typename A>
|
150
|
+
uint8_t req_compactor<T, C, A>::get_lg_weight() const {
|
151
|
+
return lg_weight_;
|
152
|
+
}
|
153
|
+
|
154
|
+
template<typename T, typename C, typename A>
|
155
|
+
template<bool inclusive>
|
156
|
+
uint64_t req_compactor<T, C, A>::compute_weight(const T& item) const {
|
157
|
+
if (!sorted_) const_cast<req_compactor*>(this)->sort(); // allow sorting as a side effect
|
158
|
+
auto it = inclusive ?
|
159
|
+
std::upper_bound(begin(), end(), item, C()) :
|
160
|
+
std::lower_bound(begin(), end(), item, C());
|
161
|
+
return std::distance(begin(), it) << lg_weight_;
|
162
|
+
}
|
163
|
+
|
164
|
+
template<typename T, typename C, typename A>
|
165
|
+
template<typename FwdT>
|
166
|
+
void req_compactor<T, C, A>::append(FwdT&& item) {
|
167
|
+
if (num_items_ == capacity_) grow(capacity_ + get_nom_capacity());
|
168
|
+
const uint32_t i = hra_ ? capacity_ - num_items_ - 1 : num_items_;
|
169
|
+
new (items_ + i) T(std::forward<FwdT>(item));
|
170
|
+
++num_items_;
|
171
|
+
if (num_items_ > 1) sorted_ = false;
|
172
|
+
}
|
173
|
+
|
174
|
+
template<typename T, typename C, typename A>
|
175
|
+
void req_compactor<T, C, A>::grow(uint32_t new_capacity) {
|
176
|
+
T* new_items = allocator_.allocate(new_capacity);
|
177
|
+
uint32_t new_i = hra_ ? new_capacity - num_items_ : 0;
|
178
|
+
for (auto it = begin(); it != end(); ++it, ++new_i) {
|
179
|
+
new (new_items + new_i) T(std::move(*it));
|
180
|
+
(*it).~T();
|
181
|
+
}
|
182
|
+
allocator_.deallocate(items_, capacity_);
|
183
|
+
items_ = new_items;
|
184
|
+
capacity_ = new_capacity;
|
185
|
+
}
|
186
|
+
|
187
|
+
template<typename T, typename C, typename A>
|
188
|
+
void req_compactor<T, C, A>::ensure_space(uint32_t num) {
|
189
|
+
if (num_items_ + num > capacity_) grow(num_items_ + num + get_nom_capacity());
|
190
|
+
}
|
191
|
+
|
192
|
+
template<typename T, typename C, typename A>
|
193
|
+
const T* req_compactor<T, C, A>::begin() const {
|
194
|
+
return items_ + (hra_ ? capacity_ - num_items_ : 0);
|
195
|
+
}
|
196
|
+
|
197
|
+
template<typename T, typename C, typename A>
|
198
|
+
const T* req_compactor<T, C, A>::end() const {
|
199
|
+
return items_ + (hra_ ? capacity_ : num_items_);
|
200
|
+
}
|
201
|
+
|
202
|
+
template<typename T, typename C, typename A>
|
203
|
+
T* req_compactor<T, C, A>::begin() {
|
204
|
+
return items_ + (hra_ ? capacity_ - num_items_ : 0);
|
205
|
+
}
|
206
|
+
|
207
|
+
template<typename T, typename C, typename A>
|
208
|
+
T* req_compactor<T, C, A>::end() {
|
209
|
+
return items_ + (hra_ ? capacity_ : num_items_);
|
210
|
+
}
|
211
|
+
|
212
|
+
template<typename T, typename C, typename A>
|
213
|
+
template<typename FwdC>
|
214
|
+
void req_compactor<T, C, A>::merge(FwdC&& other) {
|
215
|
+
// TODO: swap if other is larger?
|
216
|
+
if (lg_weight_ != other.lg_weight_) throw std::logic_error("weight mismatch");
|
217
|
+
state_ |= other.state_;
|
218
|
+
while (ensure_enough_sections()) {}
|
219
|
+
ensure_space(other.get_num_items());
|
220
|
+
sort();
|
221
|
+
auto offset = hra_ ? capacity_ - num_items_ : num_items_;
|
222
|
+
auto from = hra_ ? begin() - other.get_num_items() : end();
|
223
|
+
auto to = from + other.get_num_items();
|
224
|
+
auto other_it = other.begin();
|
225
|
+
for (auto it = from; it != to; ++it, ++other_it) new (it) T(conditional_forward<FwdC>(*other_it));
|
226
|
+
if (!other.sorted_) std::sort(from, to, C());
|
227
|
+
if (num_items_ > 0) std::inplace_merge(hra_ ? from : begin(), items_ + offset, hra_ ? end() : to, C());
|
228
|
+
num_items_ += other.get_num_items();
|
229
|
+
}
|
230
|
+
|
231
|
+
template<typename T, typename C, typename A>
|
232
|
+
void req_compactor<T, C, A>::sort() {
|
233
|
+
if (!sorted_) {
|
234
|
+
std::sort(begin(), end(), C());
|
235
|
+
sorted_ = true;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
template<typename T, typename C, typename A>
|
240
|
+
std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& next) {
|
241
|
+
const uint32_t starting_nom_capacity = get_nom_capacity();
|
242
|
+
// choose a part of the buffer to compact
|
243
|
+
const uint32_t secs_to_compact = std::min<uint32_t>(count_trailing_zeros_in_u64(~state_) + 1, num_sections_);
|
244
|
+
auto compaction_range = compute_compaction_range(secs_to_compact);
|
245
|
+
if (compaction_range.second - compaction_range.first < 2) throw std::logic_error("compaction range error");
|
246
|
+
|
247
|
+
if ((state_ & 1) == 1) { coin_ = !coin_; } // for odd flip coin;
|
248
|
+
else { coin_ = req_random_bit(); } // random coin flip
|
249
|
+
|
250
|
+
const auto num = (compaction_range.second - compaction_range.first) / 2;
|
251
|
+
next.ensure_space(num);
|
252
|
+
auto next_middle = hra_ ? next.begin() : next.end();
|
253
|
+
auto next_empty = hra_ ? next.begin() - num : next.end();
|
254
|
+
promote_evens_or_odds(begin() + compaction_range.first, begin() + compaction_range.second, coin_, next_empty);
|
255
|
+
next.num_items_ += num;
|
256
|
+
std::inplace_merge(next.begin(), next_middle, next.end(), C());
|
257
|
+
for (size_t i = compaction_range.first; i < compaction_range.second; ++i) (*(begin() + i)).~T();
|
258
|
+
num_items_ -= compaction_range.second - compaction_range.first;
|
259
|
+
|
260
|
+
++state_;
|
261
|
+
ensure_enough_sections();
|
262
|
+
return std::pair<uint32_t, uint32_t>(
|
263
|
+
num,
|
264
|
+
get_nom_capacity() - starting_nom_capacity
|
265
|
+
);
|
266
|
+
}
|
267
|
+
|
268
|
+
template<typename T, typename C, typename A>
|
269
|
+
bool req_compactor<T, C, A>::ensure_enough_sections() {
|
270
|
+
const float ssr = section_size_raw_ / sqrtf(2);
|
271
|
+
const uint32_t ne = nearest_even(ssr);
|
272
|
+
if (state_ >= static_cast<uint64_t>(1ULL << (num_sections_ - 1)) && ne >= req_constants::MIN_K) {
|
273
|
+
section_size_raw_ = ssr;
|
274
|
+
section_size_ = ne;
|
275
|
+
num_sections_ <<= 1;
|
276
|
+
if (capacity_ < 2 * get_nom_capacity()) grow(2 * get_nom_capacity());
|
277
|
+
return true;
|
278
|
+
}
|
279
|
+
return false;
|
280
|
+
}
|
281
|
+
|
282
|
+
template<typename T, typename C, typename A>
|
283
|
+
std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compute_compaction_range(uint32_t secs_to_compact) const {
|
284
|
+
uint32_t non_compact = get_nom_capacity() / 2 + (num_sections_ - secs_to_compact) * section_size_;
|
285
|
+
// make compacted region even
|
286
|
+
if (((num_items_ - non_compact) & 1) == 1) ++non_compact;
|
287
|
+
const uint32_t low = hra_ ? 0 : non_compact;
|
288
|
+
const uint32_t high = hra_ ? num_items_ - non_compact : num_items_;
|
289
|
+
return std::pair<uint32_t, uint32_t>(low, high);
|
290
|
+
}
|
291
|
+
|
292
|
+
template<typename T, typename C, typename A>
|
293
|
+
uint32_t req_compactor<T, C, A>::nearest_even(float value) {
|
294
|
+
return static_cast<uint32_t>(round(value / 2)) << 1;
|
295
|
+
}
|
296
|
+
|
297
|
+
template<typename T, typename C, typename A>
|
298
|
+
template<typename InIter, typename OutIter>
|
299
|
+
void req_compactor<T, C, A>::promote_evens_or_odds(InIter from, InIter to, bool odds, OutIter dst) {
|
300
|
+
if (from == to) return;
|
301
|
+
InIter i = from;
|
302
|
+
if (odds) ++i;
|
303
|
+
while (i != to) {
|
304
|
+
new (dst) T(std::move(*i));
|
305
|
+
++dst;
|
306
|
+
++i;
|
307
|
+
if (i == to) break;
|
308
|
+
++i;
|
309
|
+
}
|
310
|
+
}
|
311
|
+
|
312
|
+
// implementation for fixed-size arithmetic types (integral and floating point)
|
313
|
+
template<typename T, typename C, typename A>
|
314
|
+
template<typename S, typename TT, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type>
|
315
|
+
size_t req_compactor<T, C, A>::get_serialized_size_bytes(const S&) const {
|
316
|
+
return sizeof(state_) + sizeof(section_size_raw_) + sizeof(lg_weight_) + sizeof(num_sections_) +
|
317
|
+
sizeof(uint16_t) + // padding
|
318
|
+
sizeof(uint32_t) + // num_items
|
319
|
+
sizeof(TT) * num_items_;
|
320
|
+
}
|
321
|
+
|
322
|
+
// implementation for all other types
|
323
|
+
template<typename T, typename C, typename A>
|
324
|
+
template<typename S, typename TT, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type>
|
325
|
+
size_t req_compactor<T, C, A>::get_serialized_size_bytes(const S& serde) const {
|
326
|
+
size_t size = sizeof(state_) + sizeof(section_size_raw_) + sizeof(lg_weight_) + sizeof(num_sections_) +
|
327
|
+
sizeof(uint16_t) + // padding
|
328
|
+
sizeof(uint32_t); // num_items
|
329
|
+
for (auto it = begin(); it != end(); ++it) size += serde.size_of_item(*it);
|
330
|
+
return size;
|
331
|
+
}
|
332
|
+
|
333
|
+
template<typename T, typename C, typename A>
|
334
|
+
template<typename S>
|
335
|
+
void req_compactor<T, C, A>::serialize(std::ostream& os, const S& serde) const {
|
336
|
+
write(os, state_);
|
337
|
+
write(os, section_size_raw_);
|
338
|
+
write(os, lg_weight_);
|
339
|
+
write(os, num_sections_);
|
340
|
+
const uint16_t padding = 0;
|
341
|
+
write(os, padding);
|
342
|
+
write(os, num_items_);
|
343
|
+
serde.serialize(os, begin(), num_items_);
|
344
|
+
}
|
345
|
+
|
346
|
+
template<typename T, typename C, typename A>
|
347
|
+
template<typename S>
|
348
|
+
size_t req_compactor<T, C, A>::serialize(void* dst, size_t capacity, const S& serde) const {
|
349
|
+
uint8_t* ptr = static_cast<uint8_t*>(dst);
|
350
|
+
const uint8_t* end_ptr = ptr + capacity;
|
351
|
+
ptr += copy_to_mem(state_, ptr);
|
352
|
+
ptr += copy_to_mem(section_size_raw_, ptr);
|
353
|
+
ptr += copy_to_mem(lg_weight_, ptr);
|
354
|
+
ptr += copy_to_mem(num_sections_, ptr);
|
355
|
+
const uint16_t padding = 0;
|
356
|
+
ptr += copy_to_mem(padding, ptr);
|
357
|
+
ptr += copy_to_mem(num_items_, ptr);
|
358
|
+
ptr += serde.serialize(ptr, end_ptr - ptr, begin(), num_items_);
|
359
|
+
return ptr - static_cast<uint8_t*>(dst);
|
360
|
+
}
|
361
|
+
|
362
|
+
template<typename T, typename C, typename A>
|
363
|
+
template<typename S>
|
364
|
+
req_compactor<T, C, A> req_compactor<T, C, A>::deserialize(std::istream& is, const S& serde, const A& allocator, bool sorted, bool hra) {
|
365
|
+
auto state = read<decltype(state_)>(is);
|
366
|
+
auto section_size_raw = read<decltype(section_size_raw_)>(is);
|
367
|
+
auto lg_weight = read<decltype(lg_weight_)>(is);
|
368
|
+
auto num_sections = read<decltype(num_sections_)>(is);
|
369
|
+
read<uint16_t>(is); // padding
|
370
|
+
auto num_items = read<uint32_t>(is);
|
371
|
+
auto items = deserialize_items(is, serde, allocator, num_items);
|
372
|
+
return req_compactor(hra, lg_weight, sorted, section_size_raw, num_sections, state, std::move(items), num_items, allocator);
|
373
|
+
}
|
374
|
+
|
375
|
+
template<typename T, typename C, typename A>
|
376
|
+
template<typename S>
|
377
|
+
req_compactor<T, C, A> req_compactor<T, C, A>::deserialize(std::istream& is, const S& serde, const A& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra) {
|
378
|
+
auto items = deserialize_items(is, serde, allocator, num_items);
|
379
|
+
return req_compactor(hra, 0, sorted, k, req_constants::INIT_NUM_SECTIONS, 0, std::move(items), num_items, allocator);
|
380
|
+
}
|
381
|
+
|
382
|
+
template<typename T, typename C, typename A>
|
383
|
+
template<typename S>
|
384
|
+
auto req_compactor<T, C, A>::deserialize_items(std::istream& is, const S& serde, const A& allocator, uint32_t num)
|
385
|
+
-> std::unique_ptr<T, items_deleter> {
|
386
|
+
A alloc(allocator);
|
387
|
+
std::unique_ptr<T, items_deleter> items(alloc.allocate(num), items_deleter(allocator, false, num));
|
388
|
+
serde.deserialize(is, items.get(), num);
|
389
|
+
// serde did not throw, enable destructors
|
390
|
+
items.get_deleter().set_destroy(true);
|
391
|
+
if (!is.good()) throw std::runtime_error("error reading from std::istream");
|
392
|
+
return items;
|
393
|
+
}
|
394
|
+
|
395
|
+
template<typename T, typename C, typename A>
|
396
|
+
template<typename S>
|
397
|
+
std::pair<req_compactor<T, C, A>, size_t> req_compactor<T, C, A>::deserialize(const void* bytes, size_t size, const S& serde, const A& allocator, bool sorted, bool hra) {
|
398
|
+
ensure_minimum_memory(size, 8);
|
399
|
+
const char* ptr = static_cast<const char*>(bytes);
|
400
|
+
const char* end_ptr = static_cast<const char*>(bytes) + size;
|
401
|
+
|
402
|
+
uint64_t state;
|
403
|
+
ptr += copy_from_mem(ptr, state);
|
404
|
+
float section_size_raw;
|
405
|
+
ptr += copy_from_mem(ptr, section_size_raw);
|
406
|
+
uint8_t lg_weight;
|
407
|
+
ptr += copy_from_mem(ptr, lg_weight);
|
408
|
+
uint8_t num_sections;
|
409
|
+
ptr += copy_from_mem(ptr, num_sections);
|
410
|
+
ptr += 2; // padding
|
411
|
+
uint32_t num_items;
|
412
|
+
ptr += copy_from_mem(ptr, num_items);
|
413
|
+
auto pair = deserialize_items(ptr, end_ptr - ptr, serde, allocator, num_items);
|
414
|
+
ptr += pair.second;
|
415
|
+
return std::pair<req_compactor, size_t>(
|
416
|
+
req_compactor(hra, lg_weight, sorted, section_size_raw, num_sections, state, std::move(pair.first), num_items, allocator),
|
417
|
+
ptr - static_cast<const char*>(bytes)
|
418
|
+
);
|
419
|
+
}
|
420
|
+
|
421
|
+
template<typename T, typename C, typename A>
|
422
|
+
template<typename S>
|
423
|
+
std::pair<req_compactor<T, C, A>, size_t> req_compactor<T, C, A>::deserialize(const void* bytes, size_t size, const S& serde, const A& allocator, bool sorted, uint16_t k, uint8_t num_items, bool hra) {
|
424
|
+
auto pair = deserialize_items(bytes, size, serde, allocator, num_items);
|
425
|
+
return std::pair<req_compactor, size_t>(
|
426
|
+
req_compactor(hra, 0, sorted, k, req_constants::INIT_NUM_SECTIONS, 0, std::move(pair.first), num_items, allocator),
|
427
|
+
pair.second
|
428
|
+
);
|
429
|
+
}
|
430
|
+
|
431
|
+
template<typename T, typename C, typename A>
|
432
|
+
template<typename S>
|
433
|
+
auto req_compactor<T, C, A>::deserialize_items(const void* bytes, size_t size, const S& serde, const A& allocator, uint32_t num)
|
434
|
+
-> std::pair<std::unique_ptr<T, items_deleter>, size_t> {
|
435
|
+
const char* ptr = static_cast<const char*>(bytes);
|
436
|
+
const char* end_ptr = static_cast<const char*>(bytes) + size;
|
437
|
+
A alloc(allocator);
|
438
|
+
std::unique_ptr<T, items_deleter> items(alloc.allocate(num), items_deleter(allocator, false, num));
|
439
|
+
ptr += serde.deserialize(ptr, end_ptr - ptr, items.get(), num);
|
440
|
+
// serde did not throw, enable destructors
|
441
|
+
items.get_deleter().set_destroy(true);
|
442
|
+
return std::pair<std::unique_ptr<T, items_deleter>, size_t>(
|
443
|
+
std::move(items),
|
444
|
+
ptr - static_cast<const char*>(bytes)
|
445
|
+
);
|
446
|
+
}
|
447
|
+
|
448
|
+
|
449
|
+
template<typename T, typename C, typename A>
|
450
|
+
req_compactor<T, C, A>::req_compactor(bool hra, uint8_t lg_weight, bool sorted, float section_size_raw, uint8_t num_sections, uint64_t state, std::unique_ptr<T, items_deleter> items, uint32_t num_items, const A& allocator):
|
451
|
+
allocator_(allocator),
|
452
|
+
lg_weight_(lg_weight),
|
453
|
+
hra_(hra),
|
454
|
+
coin_(req_random_bit()),
|
455
|
+
sorted_(sorted),
|
456
|
+
section_size_raw_(section_size_raw),
|
457
|
+
section_size_(nearest_even(section_size_raw)),
|
458
|
+
num_sections_(num_sections),
|
459
|
+
state_(state),
|
460
|
+
num_items_(num_items),
|
461
|
+
capacity_(num_items),
|
462
|
+
items_(items.release())
|
463
|
+
{}
|
464
|
+
|
465
|
+
template<typename T, typename C, typename A>
|
466
|
+
class req_compactor<T, C, A>::items_deleter {
|
467
|
+
public:
|
468
|
+
items_deleter(const A& allocator, bool destroy, size_t num): allocator_(allocator), destroy_(destroy), num_(num) {}
|
469
|
+
void operator() (T* ptr) {
|
470
|
+
if (ptr != nullptr) {
|
471
|
+
if (destroy_) {
|
472
|
+
for (size_t i = 0; i < num_; ++i) {
|
473
|
+
ptr[i].~T();
|
474
|
+
}
|
475
|
+
}
|
476
|
+
allocator_.deallocate(ptr, num_);
|
477
|
+
}
|
478
|
+
}
|
479
|
+
void set_destroy(bool destroy) { destroy_ = destroy; }
|
480
|
+
private:
|
481
|
+
A allocator_;
|
482
|
+
bool destroy_;
|
483
|
+
size_t num_;
|
484
|
+
};
|
485
|
+
|
486
|
+
} /* namespace datasketches */
|
487
|
+
|
488
|
+
#endif
|
@@ -0,0 +1,69 @@
|
|
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 REQ_QUANTILE_CALCULATOR_HPP_
|
21
|
+
#define REQ_QUANTILE_CALCULATOR_HPP_
|
22
|
+
|
23
|
+
#include <functional>
|
24
|
+
|
25
|
+
namespace datasketches {
|
26
|
+
|
27
|
+
template<
|
28
|
+
typename T,
|
29
|
+
typename Comparator,
|
30
|
+
typename Allocator
|
31
|
+
>
|
32
|
+
class req_quantile_calculator {
|
33
|
+
public:
|
34
|
+
req_quantile_calculator(uint64_t n, const Allocator& allocator);
|
35
|
+
|
36
|
+
void add(const T* begin, const T* end, uint8_t lg_weight);
|
37
|
+
|
38
|
+
template<bool inclusive>
|
39
|
+
void convert_to_cummulative();
|
40
|
+
|
41
|
+
const T* get_quantile(double rank) const;
|
42
|
+
|
43
|
+
private:
|
44
|
+
using Entry = std::pair<const T*, uint64_t>;
|
45
|
+
using AllocEntry = typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
|
46
|
+
using Container = std::vector<Entry, AllocEntry>;
|
47
|
+
|
48
|
+
template<typename C>
|
49
|
+
struct compare_pairs_by_first_ptr {
|
50
|
+
bool operator()(const Entry& a, const Entry& b) {
|
51
|
+
return C()(*a.first, *b.first);
|
52
|
+
}
|
53
|
+
};
|
54
|
+
|
55
|
+
struct compare_pairs_by_second {
|
56
|
+
bool operator()(const Entry& a, const Entry& b) {
|
57
|
+
return a.second < b.second;
|
58
|
+
}
|
59
|
+
};
|
60
|
+
|
61
|
+
uint64_t n_;
|
62
|
+
Container entries_;
|
63
|
+
};
|
64
|
+
|
65
|
+
} /* namespace datasketches */
|
66
|
+
|
67
|
+
#include "req_quantile_calculator_impl.hpp"
|
68
|
+
|
69
|
+
#endif
|