datasketches-cpp
Loading...
Searching...
No Matches
Hll8Array-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 _HLL8ARRAY_INTERNAL_HPP_
21#define _HLL8ARRAY_INTERNAL_HPP_
22
23#include "Hll8Array.hpp"
24
25namespace datasketches {
26
27template<typename A>
28Hll8Array<A>::Hll8Array(uint8_t lgConfigK, const A& allocator):
29HllArray<A>(lgConfigK, target_hll_type::HLL_8, allocator)
30{
31 const int numBytes = this->hll8ArrBytes(lgConfigK);
32 this->hllByteArr_.resize(numBytes, 0);
33}
34
35template<typename A>
36Hll8Array<A>::Hll8Array(const HllArray<A>& other):
37 HllArray<A>(other.getLgConfigK(), target_hll_type::HLL_8, other.getAllocator())
38{
39 const int numBytes = this->hll8ArrBytes(this->lgConfigK_);
40 this->hllByteArr_.resize(numBytes, 0);
41 this->oooFlag_ = other.isOutOfOrderFlag();
42 uint32_t num_zeros = 1 << this->lgConfigK_;
43
44 for (const auto coupon : other) { // all = false, so skip empty values
45 num_zeros--;
46 internalCouponUpdate(coupon); // updates KxQ registers
47 }
48
49 this->numAtCurMin_ = num_zeros;
50 this->hipAccum_ = other.getHipAccum();
51 this->rebuild_kxq_curmin_ = false;
52}
53
54template<typename A>
55std::function<void(HllSketchImpl<A>*)> Hll8Array<A>::get_deleter() const {
56 return [](HllSketchImpl<A>* ptr) {
57 Hll8Array<A>* hll = static_cast<Hll8Array<A>*>(ptr);
58 using Hll8Alloc = typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>>;
59 Hll8Alloc hll8Alloc(hll->getAllocator());
60 hll->~Hll8Array();
61 hll8Alloc.deallocate(hll, 1);
62 };
63}
64
65template<typename A>
66Hll8Array<A>* Hll8Array<A>::copy() const {
67 using Hll8Alloc = typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>>;
68 Hll8Alloc hll8Alloc(this->getAllocator());
69 return new (hll8Alloc.allocate(1)) Hll8Array<A>(*this);
70}
71
72template<typename A>
73uint8_t Hll8Array<A>::getSlot(uint32_t slotNo) const {
74 return this->hllByteArr_[slotNo];
75}
76
77template<typename A>
78void Hll8Array<A>::putSlot(uint32_t slotNo, uint8_t value) {
79 this->hllByteArr_[slotNo] = value;
80}
81
82template<typename A>
83uint32_t Hll8Array<A>::getHllByteArrBytes() const {
84 return this->hll8ArrBytes(this->lgConfigK_);
85}
86
87template<typename A>
88HllSketchImpl<A>* Hll8Array<A>::couponUpdate(uint32_t coupon) {
89 internalCouponUpdate(coupon);
90 return this;
91}
92
93template<typename A>
94void Hll8Array<A>::internalCouponUpdate(uint32_t coupon) {
95 const uint32_t configKmask = (1 << this->lgConfigK_) - 1;
96 const uint32_t slotNo = HllUtil<A>::getLow26(coupon) & configKmask;
97 const uint8_t newVal = HllUtil<A>::getValue(coupon);
98
99 const uint8_t curVal = this->hllByteArr_[slotNo];
100 if (newVal > curVal) {
101 // A prior HLL merge may have left the estimator state stale. Rebuild it before applying
102 // an incremental update for a changed register.
103 this->check_rebuild_kxq_cur_min();
104 this->hllByteArr_[slotNo] = newVal;
105 this->hipAndKxQIncrementalUpdate(curVal, newVal);
106 this->numAtCurMin_ -= curVal == 0; // interpret numAtCurMin as num zeros
107 }
108}
109
110template<typename A>
111void Hll8Array<A>::mergeList(const CouponList<A>& src) {
112 for (const auto coupon: src) {
113 internalCouponUpdate(coupon);
114 }
115}
116
117template<typename A>
118void Hll8Array<A>::mergeHll(const HllArray<A>& src) {
119 // at this point src_k >= dst_k
120 // we can optimize further when the k values are equal
121 if (this->getLgConfigK() == src.getLgConfigK()) {
122 if (src.getTgtHllType() == target_hll_type::HLL_8) {
123 uint32_t i = 0;
124 for (const auto value: src.getHllArray()) {
125 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], value);
126 ++i;
127 }
128 } else if (src.getTgtHllType() == target_hll_type::HLL_6) {
129 const uint32_t src_k = 1 << src.getLgConfigK();
130 uint32_t i = 0;
131 const uint8_t* ptr = src.getHllArray().data();
132 while (i < src_k) {
133 uint8_t value = *ptr & 0x3f;
134 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], value);
135 ++i;
136 value = *ptr++ >> 6;
137 value |= (*ptr & 0x0f) << 2;
138 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], value);
139 ++i;
140 value = *ptr++ >> 4;
141 value |= (*ptr & 3) << 4;
142 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], value);
143 ++i;
144 value = *ptr++ >> 2;
145 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], value);
146 ++i;
147 }
148 } else { // HLL_4
149 const auto& src4 = static_cast<const Hll4Array<A>&>(src);
150 uint32_t i = 0;
151 for (const auto byte: src.getHllArray()) {
152 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], src4.adjustRawValue(i, byte & hll_constants::loNibbleMask));
153 ++i;
154 this->hllByteArr_[i] = std::max(this->hllByteArr_[i], src4.adjustRawValue(i, byte >> 4));
155 ++i;
156 }
157 }
158 } else {
159 // src_k > dst_k
160 const uint32_t dst_mask = (1 << this->getLgConfigK()) - 1;
161 // special treatment below to optimize performance
162 if (src.getTgtHllType() == target_hll_type::HLL_8) {
163 uint32_t i = 0;
164 for (const auto value: src.getHllArray()) {
165 processValue(i++, dst_mask, value);
166 }
167 } else if (src.getTgtHllType() == target_hll_type::HLL_6) {
168 const uint32_t src_k = 1 << src.getLgConfigK();
169 uint32_t i = 0;
170 const uint8_t* ptr = src.getHllArray().data();
171 while (i < src_k) {
172 uint8_t value = *ptr & 0x3f;
173 processValue(i++, dst_mask, value);
174 value = *ptr++ >> 6;
175 value |= (*ptr & 0x0f) << 2;
176 processValue(i++, dst_mask, value);
177 value = *ptr++ >> 4;
178 value |= (*ptr & 3) << 4;
179 processValue(i++, dst_mask, value);
180 value = *ptr++ >> 2;
181 processValue(i++, dst_mask, value);
182 }
183 } else { // HLL_4
184 const auto& src4 = static_cast<const Hll4Array<A>&>(src);
185 uint32_t i = 0;
186 for (const auto byte: src.getHllArray()) {
187 processValue(i, dst_mask, src4.adjustRawValue(i, byte & hll_constants::loNibbleMask));
188 ++i;
189 processValue(i, dst_mask, src4.adjustRawValue(i, byte >> 4));
190 ++i;
191 }
192 }
193 }
194 this->setRebuildKxqCurminFlag(true);
195}
196
197
198template<typename A>
199void Hll8Array<A>::processValue(uint32_t slot, uint32_t mask, uint8_t new_val) {
200 const size_t index = slot & mask;
201 this->hllByteArr_[index] = std::max(this->hllByteArr_[index], new_val);
202}
203
204}
205
206#endif // _HLL8ARRAY_INTERNAL_HPP_
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