datasketches-cpp
Loading...
Searching...
No Matches
HllUnion-internal.hpp
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 _HLLUNION_INTERNAL_HPP_
21#define _HLLUNION_INTERNAL_HPP_
22
23#include "hll.hpp"
24
25#include "HllSketchImpl.hpp"
26#include "HllArray.hpp"
27#include "HllUtil.hpp"
28
29#include <stdexcept>
30#include <string>
31
32namespace datasketches {
33
34template<typename A>
35hll_union_alloc<A>::hll_union_alloc(uint8_t lg_max_k, const A& allocator):
36 lg_max_k_(HllUtil<A>::checkLgK(lg_max_k)),
37 gadget_(lg_max_k, target_hll_type::HLL_8, false, allocator)
38{}
39
40template<typename A>
42 hll_union_alloc<A> copy(other);
43 std::swap(lg_max_k_, copy.lg_max_k_);
44 std::swap(gadget_, copy.gadget_);
45 return *this;
46}
47
48template<typename A>
49hll_sketch_alloc<A> hll_union_alloc<A>::get_result(target_hll_type target_type) const {
50 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL) {
51 static_cast<HllArray<A>*>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
52 }
53 return hll_sketch_alloc<A>(gadget_, target_type);
54}
55
56template<typename A>
57void hll_union_alloc<A>::update(const hll_sketch_alloc<A>& sketch) {
58 if (sketch.is_empty()) { return; }
59 union_impl(sketch, lg_max_k_);
60}
61
62template<typename A>
63void hll_union_alloc<A>::update(hll_sketch_alloc<A>&& sketch) {
64 if (sketch.is_empty()) { return; }
65 // taking over the sketch is only possible if its memory can be released with the union's allocator
66 if (gadget_.is_empty() && sketch.get_target_type() == HLL_8 && sketch.get_lg_config_k() <= lg_max_k_
67 && sketch.sketch_impl->getAllocator() == gadget_.sketch_impl->getAllocator()) {
68 if (sketch.get_current_mode() == HLL || sketch.get_lg_config_k() == lg_max_k_) {
69 gadget_ = std::move(sketch);
70 }
71 }
72 union_impl(sketch, lg_max_k_);
73}
74
75template<typename A>
76void hll_union_alloc<A>::update(const std::string& datum) {
77 gadget_.update(datum);
78}
79
80template<typename A>
81void hll_union_alloc<A>::update(uint64_t datum) {
82 gadget_.update(datum);
83}
84
85template<typename A>
86void hll_union_alloc<A>::update(uint32_t datum) {
87 gadget_.update(datum);
88}
89
90template<typename A>
91void hll_union_alloc<A>::update(uint16_t datum) {
92 gadget_.update(datum);
93}
94
95template<typename A>
96void hll_union_alloc<A>::update(uint8_t datum) {
97 gadget_.update(datum);
98}
99
100template<typename A>
101void hll_union_alloc<A>::update(int64_t datum) {
102 gadget_.update(datum);
103}
104
105template<typename A>
106void hll_union_alloc<A>::update(int32_t datum) {
107 gadget_.update(datum);
108}
109
110template<typename A>
111void hll_union_alloc<A>::update(int16_t datum) {
112 gadget_.update(datum);
113}
114
115template<typename A>
116void hll_union_alloc<A>::update(int8_t datum) {
117 gadget_.update(datum);
118}
119
120template<typename A>
121void hll_union_alloc<A>::update(double datum) {
122 gadget_.update(datum);
123}
124
125template<typename A>
126void hll_union_alloc<A>::update(float datum) {
127 gadget_.update(datum);
128}
129
130template<typename A>
131void hll_union_alloc<A>::update(const void* data, size_t length_bytes) {
132 gadget_.update(data, length_bytes);
133}
134
135template<typename A>
136void hll_union_alloc<A>::coupon_update(uint32_t coupon) {
137 if (coupon == HllUtil<A>::EMPTY) { return; }
138 HllSketchImpl<A>* result = gadget_.sketch_impl->coupon_update(coupon);
139 if (result != gadget_.sketch_impl) {
140 if (gadget_.sketch_impl != nullptr) { gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); }
141 gadget_.sketch_impl = result;
142 }
143}
144
145template<typename A>
147 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL) {
148 static_cast<HllArray<A>*>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
149 }
150 return gadget_.get_estimate();
151}
152
153template<typename A>
155 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL) {
156 static_cast<HllArray<A>*>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
157 }
158 return gadget_.get_composite_estimate();
159}
160
161template<typename A>
162double hll_union_alloc<A>::get_lower_bound(uint8_t num_std_dev) const {
163 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL) {
164 static_cast<HllArray<A>*>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
165 }
166 return gadget_.get_lower_bound(num_std_dev);
167}
168
169template<typename A>
170double hll_union_alloc<A>::get_upper_bound(uint8_t num_std_dev) const {
171 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL) {
172 static_cast<HllArray<A>*>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
173 }
174 return gadget_.get_upper_bound(num_std_dev);
175}
176
177template<typename A>
179 return gadget_.get_lg_config_k();
180}
181
182template<typename A>
184 // always coupon collection mode: the gadget is an internal detail and must not
185 // inherit a full-size state from whatever sketches happen to have been unioned in
186 gadget_.reset(false);
187}
188
189template<typename A>
191 return gadget_.is_empty();
192}
193
194template<typename A>
195bool hll_union_alloc<A>::is_out_of_order_flag() const {
196 return gadget_.is_out_of_order_flag();
197}
198
199template<typename A>
200hll_mode hll_union_alloc<A>::get_current_mode() const {
201 return gadget_.get_current_mode();
202}
203
204template<typename A>
205bool hll_union_alloc<A>::is_estimation_mode() const {
206 return gadget_.is_estimation_mode();
207}
208
209template<typename A>
213
214template<typename A>
215double hll_union_alloc<A>::get_rel_err(bool upper_bound, bool unioned,
216 uint8_t lg_config_k, uint8_t num_std_dev) {
217 return HllUtil<A>::getRelErr(upper_bound, unioned, lg_config_k, num_std_dev);
218}
219
220template<typename A>
221HllSketchImpl<A>* hll_union_alloc<A>::copy_or_downsample(const HllSketchImpl<A>* src_impl, uint8_t tgt_lg_k,
222 const A& allocator) {
223 if (src_impl->getCurMode() != HLL) {
224 throw std::logic_error("Attempt to downsample non-HLL sketch");
225 }
226 const HllArray<A>* src = static_cast<const HllArray<A>*>(src_impl);
227 const uint8_t src_lg_k = src->getLgConfigK();
228 // a copy keeps the source's allocator, so it is only made if that is the union's allocator
229 if (src_lg_k <= tgt_lg_k && src->getAllocator() == allocator) {
230 return src->copyAs(HLL_8);
231 }
232 const uint8_t lg_k = std::min(src_lg_k, tgt_lg_k);
233 auto tgtHllArr = HllUtil<A>::template newObject<Hll8Array<A>>(allocator, lg_k, allocator);
234 std::unique_ptr<Hll8Array<A>, std::function<void(HllSketchImpl<A>*)>> ptr(tgtHllArr, tgtHllArr->get_deleter());
235 tgtHllArr->mergeHll(*src);
236 //both of these are required for isomorphism
237 tgtHllArr->putHipAccum(src->getHipAccum());
238 tgtHllArr->putOutOfOrderFlag(src->isOutOfOrderFlag());
239 return ptr.release();
240}
241
242template<typename A>
243void hll_union_alloc<A>::union_impl(const hll_sketch_alloc<A>& sketch, uint8_t lg_max_k) {
244 const HllSketchImpl<A>* src_impl = sketch.sketch_impl; //default
245 HllSketchImpl<A>* dst_impl = gadget_.sketch_impl; //default
246 // the gadget allocates with the union's allocator: the input sketch may use another allocator
247 // instance (e.g. a stateful one), and copies of it would keep that allocator
248 const A allocator = dst_impl->getAllocator();
249 if (src_impl->getCurMode() == LIST || src_impl->getCurMode() == SET) {
250 if (dst_impl->isEmpty() && src_impl->getLgConfigK() == dst_impl->getLgConfigK()
251 && src_impl->getAllocator() == allocator) {
252 dst_impl = src_impl->copyAs(HLL_8);
253 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); // gadget to be replaced
254 } else {
255 const CouponList<A>* src = static_cast<const CouponList<A>*>(src_impl);
256 for (auto coupon: *src) {
257 HllSketchImpl<A>* result = dst_impl->couponUpdate(coupon);
258 if (result != dst_impl) {
259 dst_impl->get_deleter()(dst_impl);
260 dst_impl = result;
261 gadget_.sketch_impl = dst_impl;
262 }
263 }
264 }
265 } else if (!dst_impl->isEmpty()) { // src is HLL
266 if (dst_impl->getCurMode() == LIST || dst_impl->getCurMode() == SET) {
267 // swap so that src is LIST or SET, tgt is HLL
268 // use lg_max_k because LIST has effective K of 2^26
269 const CouponList<A>* src = static_cast<const CouponList<A>*>(dst_impl);
270 dst_impl = copy_or_downsample(src_impl, lg_max_k, allocator);
271 static_cast<Hll8Array<A>*>(dst_impl)->mergeList(*src);
272 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); // gadget to be replaced
273 } else { // gadget is HLL
274 if (src_impl->getLgConfigK() < dst_impl->getLgConfigK()) {
275 dst_impl = copy_or_downsample(dst_impl, sketch.get_lg_config_k(), allocator);
276 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); // gadget to be replaced
277 }
278 const HllArray<A>* src = static_cast<const HllArray<A>*>(src_impl);
279 static_cast<Hll8Array<A>*>(dst_impl)->mergeHll(*src);
280 dst_impl->putOutOfOrderFlag(true);
281 static_cast<Hll8Array<A>*>(dst_impl)->putHipAccum(0);
282 }
283 } else { // src is HLL, gadget is empty
284 dst_impl = copy_or_downsample(src_impl, lg_max_k, allocator);
285 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); // gadget to be replaced
286 }
287 gadget_.sketch_impl = dst_impl; // gadget replaced
288}
289
290}
291
292#endif // _HLLUNION_INTERNAL_HPP_
This is a high performance implementation of Phillipe Flajolet's HLL sketch but with significantly im...
Definition HllSketchImpl.hpp:31
This performs union operations for HLL sketches.
Definition hll.hpp:462
target_hll_type get_target_type() const
Returns the union's target HLL mode (from target_hll_type).
Definition HllUnion-internal.hpp:210
double get_composite_estimate() const
This is less accurate than the get_estimate() method and is automatically used when the union has gon...
Definition HllUnion-internal.hpp:154
double get_estimate() const
Returns the current cardinality estimate.
Definition HllUnion-internal.hpp:146
void update(const hll_sketch_alloc< A > &sketch)
Update this union operator with the given sketch.
Definition HllUnion-internal.hpp:57
hll_sketch_alloc< A > get_result(target_hll_type tgt_type=HLL_4) const
Returns the result of this union operator with the specified target_hll_type.
Definition HllUnion-internal.hpp:49
bool is_empty() const
Indicates if the union is currently empty.
Definition HllUnion-internal.hpp:190
uint8_t get_lg_config_k() const
Returns union's configured lg_k value.
Definition HllUnion-internal.hpp:178
double get_lower_bound(uint8_t num_std_dev) const
Returns the approximate lower error bound given the specified number of standard deviations.
Definition HllUnion-internal.hpp:162
hll_union_alloc(uint8_t lg_max_k, const A &allocator=A())
Construct an hll_union operator with the given maximum log2 of k.
Definition HllUnion-internal.hpp:35
hll_union_alloc & operator=(const hll_union_alloc &other)
Copy assignment operator.
void reset()
Resets the union to an empty state in coupon collection mode.
Definition HllUnion-internal.hpp:183
static double get_rel_err(bool upper_bound, bool unioned, uint8_t lg_config_k, uint8_t num_std_dev)
Gets the current (approximate) Relative Error (RE) asymptotic values given several parameters.
Definition HllUnion-internal.hpp:215
double get_upper_bound(uint8_t num_std_dev) const
Returns the approximate upper error bound given the specified number of standard deviations.
Definition HllUnion-internal.hpp:170
DataSketches namespace.
Definition binomial_bounds.hpp:38
target_hll_type
Specifies the target type of HLL sketch to be created.
Definition hll.hpp:72
@ HLL_8
8 bits per entry (fastest, fixed size)
Definition hll.hpp:75