20#ifndef _HLLSKETCHIMPLFACTORY_HPP_
21#define _HLLSKETCHIMPLFACTORY_HPP_
26#include "HllSketchImpl.hpp"
27#include "CouponList.hpp"
28#include "CouponHashSet.hpp"
29#include "HllArray.hpp"
30#include "Hll4Array.hpp"
31#include "Hll6Array.hpp"
32#include "Hll8Array.hpp"
37class HllSketchImplFactory final {
39 static HllSketchImpl<A>* deserialize(std::istream& os,
const A& allocator);
40 static HllSketchImpl<A>* deserialize(
const void* bytes,
size_t len,
const A& allocator);
42 static CouponHashSet<A>* promoteListToSet(
const CouponList<A>& list);
43 static HllArray<A>* promoteListOrSetToHll(
const CouponList<A>& list);
44 static HllArray<A>* newHll(uint8_t lgConfigK,
target_hll_type tgtHllType,
const A& allocator);
48 static HllSketchImpl<A>* reset(HllSketchImpl<A>* impl,
bool full_size);
50 static Hll4Array<A>* convertToHll4(
const HllArray<A>& srcHllArr);
51 static Hll6Array<A>* convertToHll6(
const HllArray<A>& srcHllArr);
52 static Hll8Array<A>* convertToHll8(
const HllArray<A>& srcHllArr);
56CouponHashSet<A>* HllSketchImplFactory<A>::promoteListToSet(
const CouponList<A>& list) {
57 using ChsAlloc =
typename std::allocator_traits<A>::template rebind_alloc<CouponHashSet<A>>;
58 CouponHashSet<A>* chSet =
new (ChsAlloc(list.getAllocator()).allocate(1)) CouponHashSet<A>(list.getLgConfigK(), list.getTgtHllType(), list.getAllocator());
59 for (
const auto coupon: list) {
60 chSet->couponUpdate(coupon);
66HllArray<A>* HllSketchImplFactory<A>::promoteListOrSetToHll(
const CouponList<A>& src) {
67 HllArray<A>* tgtHllArr = HllSketchImplFactory<A>::newHll(src.getLgConfigK(), src.getTgtHllType(), src.getAllocator());
68 tgtHllArr->putKxQ0(1 << src.getLgConfigK());
69 for (
const auto coupon: src) {
70 tgtHllArr->couponUpdate(coupon);
72 tgtHllArr->putHipAccum(src.getEstimate());
73 tgtHllArr->putOutOfOrderFlag(
false);
78HllSketchImpl<A>* HllSketchImplFactory<A>::deserialize(std::istream& is,
const A& allocator) {
81 const uint8_t preInts =
static_cast<uint8_t
>(is.peek());
82 if (preInts == hll_constants::HLL_PREINTS) {
83 return HllArray<A>::newHll(is, allocator);
84 }
else if (preInts == hll_constants::HASH_SET_PREINTS) {
85 return CouponHashSet<A>::newSet(is, allocator);
86 }
else if (preInts == hll_constants::LIST_PREINTS) {
87 return CouponList<A>::newList(is, allocator);
89 throw std::invalid_argument(
"Attempt to deserialize unknown object type");
94HllSketchImpl<A>* HllSketchImplFactory<A>::deserialize(
const void* bytes,
size_t len,
const A& allocator) {
96 const uint8_t preInts =
static_cast<const uint8_t*
>(bytes)[0];
97 if (preInts == hll_constants::HLL_PREINTS) {
98 return HllArray<A>::newHll(bytes, len, allocator);
99 }
else if (preInts == hll_constants::HASH_SET_PREINTS) {
100 return CouponHashSet<A>::newSet(bytes, len, allocator);
101 }
else if (preInts == hll_constants::LIST_PREINTS) {
102 return CouponList<A>::newList(bytes, len, allocator);
104 throw std::invalid_argument(
"Attempt to deserialize unknown object type");
109HllArray<A>* HllSketchImplFactory<A>::newHll(uint8_t lgConfigK,
target_hll_type tgtHllType,
const A& allocator) {
110 switch (tgtHllType) {
112 using Hll8Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>>;
113 return new (Hll8Alloc(allocator).allocate(1)) Hll8Array<A>(lgConfigK, allocator);
115 using Hll6Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>>;
116 return new (Hll6Alloc(allocator).allocate(1)) Hll6Array<A>(lgConfigK, allocator);
118 using Hll4Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll4Array<A>>;
119 return new (Hll4Alloc(allocator).allocate(1)) Hll4Array<A>(lgConfigK, allocator);
121 throw std::logic_error(
"Invalid target_hll_type");
125HllSketchImpl<A>* HllSketchImplFactory<A>::reset(HllSketchImpl<A>* impl,
bool full_size) {
127 HllArray<A>* hll = newHll(impl->getLgConfigK(), impl->getTgtHllType(), impl->getAllocator());
128 impl->get_deleter()(impl);
131 using ClAlloc =
typename std::allocator_traits<A>::template rebind_alloc<CouponList<A>>;
132 CouponList<A>* cl =
new (ClAlloc(impl->getAllocator()).allocate(1)) CouponList<A>(impl->getLgConfigK(), impl->getTgtHllType(), hll_mode::LIST, impl->getAllocator());
133 impl->get_deleter()(impl);
139Hll4Array<A>* HllSketchImplFactory<A>::convertToHll4(
const HllArray<A>& srcHllArr) {
140 using Hll4Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll4Array<A>>;
141 return new (Hll4Alloc(srcHllArr.getAllocator()).allocate(1)) Hll4Array<A>(srcHllArr);
145Hll6Array<A>* HllSketchImplFactory<A>::convertToHll6(
const HllArray<A>& srcHllArr) {
146 using Hll6Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>>;
147 return new (Hll6Alloc(srcHllArr.getAllocator()).allocate(1)) Hll6Array<A>(srcHllArr);
151Hll8Array<A>* HllSketchImplFactory<A>::convertToHll8(
const HllArray<A>& srcHllArr) {
152 using Hll8Alloc =
typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>>;
153 return new (Hll8Alloc(srcHllArr.getAllocator()).allocate(1)) Hll8Array<A>(srcHllArr);
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_6
6 bits per entry (fixed size)
Definition hll.hpp:74
@ HLL_8
8 bits per entry (fastest, fixed size)
Definition hll.hpp:75
@ HLL_4
4 bits per entry (most compact, size may vary)
Definition hll.hpp:73