20 #ifndef _HLLUNION_INTERNAL_HPP_
21 #define _HLLUNION_INTERNAL_HPP_
25 #include "HllSketchImpl.hpp"
26 #include "HllArray.hpp"
27 #include "HllUtil.hpp"
36 lg_max_k_(HllUtil<A>::checkLgK(lg_max_k)),
42 return hll_sketch_alloc<A>(gadget_, target_type);
47 if (sketch.is_empty())
return;
48 union_impl(sketch, lg_max_k_);
53 if (sketch.is_empty())
return;
54 if (gadget_.is_empty() && sketch.get_target_type() ==
HLL_8 && sketch.get_lg_config_k() <= lg_max_k_) {
55 if (sketch.get_current_mode() == HLL || sketch.get_lg_config_k() == lg_max_k_) {
56 gadget_ = std::move(sketch);
59 union_impl(sketch, lg_max_k_);
64 gadget_.update(datum);
69 gadget_.update(datum);
74 gadget_.update(datum);
79 gadget_.update(datum);
84 gadget_.update(datum);
89 gadget_.update(datum);
94 gadget_.update(datum);
99 gadget_.update(datum);
104 gadget_.update(datum);
109 gadget_.update(datum);
114 gadget_.update(datum);
119 gadget_.update(data, length_bytes);
124 if (coupon == HllUtil<A>::EMPTY) {
return; }
125 HllSketchImpl<A>* result = gadget_.sketch_impl->coupon_update(coupon);
126 if (result != gadget_.sketch_impl) {
127 if (gadget_.sketch_impl !=
nullptr) { gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl); }
128 gadget_.sketch_impl = result;
134 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL)
135 static_cast<HllArray<A>*
>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
136 return gadget_.get_estimate();
141 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL)
142 static_cast<HllArray<A>*
>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
143 return gadget_.get_composite_estimate();
148 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL)
149 static_cast<HllArray<A>*
>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
150 return gadget_.get_lower_bound(num_std_dev);
155 if (gadget_.sketch_impl->getCurMode() == hll_mode::HLL)
156 static_cast<HllArray<A>*
>(gadget_.sketch_impl)->check_rebuild_kxq_cur_min();
157 return gadget_.get_upper_bound(num_std_dev);
162 return gadget_.get_lg_config_k();
172 return gadget_.is_empty();
177 return gadget_.is_out_of_order_flag();
181 hll_mode hll_union_alloc<A>::get_current_mode()
const {
182 return gadget_.get_current_mode();
186 bool hll_union_alloc<A>::is_estimation_mode()
const {
187 return gadget_.is_estimation_mode();
192 return target_hll_type::HLL_8;
197 uint8_t lg_config_k, uint8_t num_std_dev) {
198 return HllUtil<A>::getRelErr(upper_bound, unioned, lg_config_k, num_std_dev);
203 if (src_impl->getCurMode() != HLL) {
204 throw std::logic_error(
"Attempt to downsample non-HLL sketch");
206 const HllArray<A>* src =
static_cast<const HllArray<A>*
>(src_impl);
207 const int src_lg_k = src->getLgConfigK();
208 if (src_lg_k <= tgt_lg_k) {
209 return src->copyAs(
HLL_8);
211 typedef typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>> hll8Alloc;
212 Hll8Array<A>* tgtHllArr =
new (hll8Alloc(src->getAllocator()).allocate(1)) Hll8Array<A>(tgt_lg_k,
false, src->getAllocator());
213 tgtHllArr->mergeHll(*src);
215 tgtHllArr->putHipAccum(src->getHipAccum());
216 tgtHllArr->putOutOfOrderFlag(src->isOutOfOrderFlag());
221 inline HllSketchImpl<A>* hll_union_alloc<A>::leak_free_coupon_update(HllSketchImpl<A>* impl, uint32_t coupon) {
222 HllSketchImpl<A>* result = impl->couponUpdate(coupon);
223 if (result != impl) {
224 impl->get_deleter()(impl);
230 void hll_union_alloc<A>::union_impl(
const hll_sketch_alloc<A>& sketch, uint8_t lg_max_k) {
231 const HllSketchImpl<A>* src_impl = sketch.sketch_impl;
232 HllSketchImpl<A>* dst_impl = gadget_.sketch_impl;
233 if (src_impl->getCurMode() == LIST || src_impl->getCurMode() == SET) {
234 if (dst_impl->isEmpty() && src_impl->getLgConfigK() == dst_impl->getLgConfigK()) {
235 dst_impl = src_impl->copyAs(
HLL_8);
236 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl);
238 const CouponList<A>* src =
static_cast<const CouponList<A>*
>(src_impl);
239 for (
auto coupon: *src) {
240 dst_impl = leak_free_coupon_update(dst_impl, coupon);
243 }
else if (!dst_impl->isEmpty()) {
244 if (dst_impl->getCurMode() == LIST || dst_impl->getCurMode() == SET) {
247 const CouponList<A>* src =
static_cast<const CouponList<A>*
>(dst_impl);
248 dst_impl = copy_or_downsample(src_impl, lg_max_k);
249 static_cast<Hll8Array<A>*
>(dst_impl)->mergeList(*src);
250 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl);
252 if (src_impl->getLgConfigK() < dst_impl->getLgConfigK()) {
253 dst_impl = copy_or_downsample(dst_impl, sketch.get_lg_config_k());
254 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl);
256 const HllArray<A>* src =
static_cast<const HllArray<A>*
>(src_impl);
257 static_cast<Hll8Array<A>*
>(dst_impl)->mergeHll(*src);
258 dst_impl->putOutOfOrderFlag(
true);
259 static_cast<Hll8Array<A>*
>(dst_impl)->putHipAccum(0);
262 dst_impl = copy_or_downsample(src_impl, lg_max_k);
263 gadget_.sketch_impl->get_deleter()(gadget_.sketch_impl);
265 gadget_.sketch_impl = dst_impl;
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:446
target_hll_type get_target_type() const
Returns the union's target HLL mode (from target_hll_type).
Definition: HllUnion-internal.hpp:191
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:140
double get_estimate() const
Returns the current cardinality estimate.
Definition: HllUnion-internal.hpp:133
void update(const hll_sketch_alloc< A > &sketch)
Update this union operator with the given sketch.
Definition: HllUnion-internal.hpp:46
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:41
bool is_empty() const
Indicates if the union is currently empty.
Definition: HllUnion-internal.hpp:171
uint8_t get_lg_config_k() const
Returns union's configured lg_k value.
Definition: HllUnion-internal.hpp:161
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:147
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
void reset()
Resets the union to an empty state in coupon collection mode.
Definition: HllUnion-internal.hpp:166
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:196
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:154
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