datasketches-cpp
Loading...
Searching...
No Matches
HllSketch-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 _HLLSKETCH_INTERNAL_HPP_
21#define _HLLSKETCH_INTERNAL_HPP_
22
23#include "hll.hpp"
24#include "HllUtil.hpp"
25#include "HllSketchImplFactory.hpp"
26#include "CouponList.hpp"
27#include "HllArray.hpp"
28#include "common_defs.hpp"
29
30#include <cstdio>
31#include <cstdlib>
32#include <string>
33#include <iostream>
34#include <sstream>
35#include <iomanip>
36
37namespace datasketches {
38
39typedef union {
40 int64_t longBytes;
41 double doubleBytes;
42} longDoubleUnion;
43
44template<typename A>
45hll_sketch_alloc<A>::hll_sketch_alloc(uint8_t lg_config_k, target_hll_type tgt_type, bool start_full_size, const A& allocator) {
46 HllUtil<A>::checkLgK(lg_config_k);
47 if (start_full_size) {
48 sketch_impl = HllSketchImplFactory<A>::newHll(lg_config_k, tgt_type, allocator);
49 } else {
50 typedef typename std::allocator_traits<A>::template rebind_alloc<CouponList<A>> clAlloc;
51 sketch_impl = new (clAlloc(allocator).allocate(1)) CouponList<A>(lg_config_k, tgt_type, hll_mode::LIST, allocator);
52 }
53}
54
55template<typename A>
56hll_sketch_alloc<A> hll_sketch_alloc<A>::deserialize(std::istream& is, const A& allocator) {
57 HllSketchImpl<A>* impl = HllSketchImplFactory<A>::deserialize(is, allocator);
58 return hll_sketch_alloc<A>(impl);
59}
60
61template<typename A>
62hll_sketch_alloc<A> hll_sketch_alloc<A>::deserialize(const void* bytes, size_t len, const A& allocator) {
63 HllSketchImpl<A>* impl = HllSketchImplFactory<A>::deserialize(bytes, len, allocator);
64 return hll_sketch_alloc<A>(impl);
65}
66
67template<typename A>
68hll_sketch_alloc<A>::~hll_sketch_alloc() {
69 if (sketch_impl != nullptr) {
70 sketch_impl->get_deleter()(sketch_impl);
71 }
72}
73
74template<typename A>
75hll_sketch_alloc<A>::hll_sketch_alloc(const hll_sketch_alloc<A>& that) :
76 sketch_impl(that.sketch_impl->copy())
77{}
78
79template<typename A>
80hll_sketch_alloc<A>::hll_sketch_alloc(const hll_sketch_alloc<A>& that, target_hll_type tgt_type) :
81 sketch_impl(that.sketch_impl->copyAs(tgt_type))
82{}
83
84template<typename A>
85hll_sketch_alloc<A>::hll_sketch_alloc(hll_sketch_alloc<A>&& that) noexcept :
86 sketch_impl(nullptr)
87{
88 std::swap(sketch_impl, that.sketch_impl);
89}
90
91template<typename A>
92hll_sketch_alloc<A>::hll_sketch_alloc(HllSketchImpl<A>* that) :
93 sketch_impl(that)
94{}
95
96template<typename A>
97hll_sketch_alloc<A>& hll_sketch_alloc<A>::operator=(const hll_sketch_alloc<A>& other) {
98 sketch_impl->get_deleter()(sketch_impl);
99 sketch_impl = other.sketch_impl->copy();
100 return *this;
101}
102
103template<typename A>
104hll_sketch_alloc<A>& hll_sketch_alloc<A>::operator=(hll_sketch_alloc<A>&& other) {
105 std::swap(sketch_impl, other.sketch_impl);
106 return *this;
107}
108
109template<typename A>
110void hll_sketch_alloc<A>::reset(bool full_size) {
111 sketch_impl = sketch_impl->reset(full_size);
112}
113
114template<typename A>
115void hll_sketch_alloc<A>::update(const std::string& datum) {
116 if (datum.empty()) { return; }
117 HashState hashResult;
118 HllUtil<A>::hash(datum.c_str(), datum.length(), DEFAULT_SEED, hashResult);
119 coupon_update(HllUtil<A>::coupon(hashResult));
120}
121
122template<typename A>
123void hll_sketch_alloc<A>::update(uint64_t datum) {
124 // no sign extension with 64 bits so no need to cast to signed value
125 HashState hashResult;
126 HllUtil<A>::hash(&datum, sizeof(uint64_t), DEFAULT_SEED, hashResult);
127 coupon_update(HllUtil<A>::coupon(hashResult));
128}
129
130template<typename A>
131void hll_sketch_alloc<A>::update(uint32_t datum) {
132 update(static_cast<int32_t>(datum));
133}
134
135template<typename A>
136void hll_sketch_alloc<A>::update(uint16_t datum) {
137 update(static_cast<int16_t>(datum));
138}
139
140template<typename A>
141void hll_sketch_alloc<A>::update(uint8_t datum) {
142 update(static_cast<int8_t>(datum));
143}
144
145template<typename A>
146void hll_sketch_alloc<A>::update(int64_t datum) {
147 HashState hashResult;
148 HllUtil<A>::hash(&datum, sizeof(int64_t), DEFAULT_SEED, hashResult);
149 coupon_update(HllUtil<A>::coupon(hashResult));
150}
151
152template<typename A>
153void hll_sketch_alloc<A>::update(int32_t datum) {
154 const int64_t val = static_cast<int64_t>(datum);
155 HashState hashResult;
156 HllUtil<A>::hash(&val, sizeof(int64_t), DEFAULT_SEED, hashResult);
157 coupon_update(HllUtil<A>::coupon(hashResult));
158}
159
160template<typename A>
161void hll_sketch_alloc<A>::update(int16_t datum) {
162 const int64_t val = static_cast<int64_t>(datum);
163 HashState hashResult;
164 HllUtil<A>::hash(&val, sizeof(int64_t), DEFAULT_SEED, hashResult);
165 coupon_update(HllUtil<A>::coupon(hashResult));
166}
167
168template<typename A>
169void hll_sketch_alloc<A>::update(int8_t datum) {
170 const int64_t val = static_cast<int64_t>(datum);
171 HashState hashResult;
172 HllUtil<A>::hash(&val, sizeof(int64_t), DEFAULT_SEED, hashResult);
173 coupon_update(HllUtil<A>::coupon(hashResult));
174}
175
176template<typename A>
177void hll_sketch_alloc<A>::update(double datum) {
178 longDoubleUnion d;
179 d.doubleBytes = static_cast<double>(datum);
180 if (datum == 0.0) {
181 d.doubleBytes = 0.0; // canonicalize -0.0 to 0.0
182 } else if (std::isnan(d.doubleBytes)) {
183 d.longBytes = 0x7ff8000000000000L; // canonicalize NaN using value from Java's Double.doubleToLongBits()
184 }
185 HashState hashResult;
186 HllUtil<A>::hash(&d, sizeof(double), DEFAULT_SEED, hashResult);
187 coupon_update(HllUtil<A>::coupon(hashResult));
188}
189
190template<typename A>
191void hll_sketch_alloc<A>::update(float datum) {
192 longDoubleUnion d;
193 d.doubleBytes = static_cast<double>(datum);
194 if (datum == 0.0) {
195 d.doubleBytes = 0.0; // canonicalize -0.0 to 0.0
196 } else if (std::isnan(d.doubleBytes)) {
197 d.longBytes = 0x7ff8000000000000L; // canonicalize NaN using value from Java's Double.doubleToLongBits()
198 }
199 HashState hashResult;
200 HllUtil<A>::hash(&d, sizeof(double), DEFAULT_SEED, hashResult);
201 coupon_update(HllUtil<A>::coupon(hashResult));
202}
203
204template<typename A>
205void hll_sketch_alloc<A>::update(const void* data, size_t lengthBytes) {
206 if (data == nullptr) { return; }
207 HashState hashResult;
208 HllUtil<A>::hash(data, lengthBytes, DEFAULT_SEED, hashResult);
209 coupon_update(HllUtil<A>::coupon(hashResult));
210}
211
212template<typename A>
213void hll_sketch_alloc<A>::coupon_update(uint32_t coupon) {
214 if (coupon == hll_constants::EMPTY) { return; }
215 HllSketchImpl<A>* result = this->sketch_impl->couponUpdate(coupon);
216 if (result != this->sketch_impl) {
217 this->sketch_impl->get_deleter()(this->sketch_impl);
218 this->sketch_impl = result;
219 }
220}
221
222template<typename A>
223void hll_sketch_alloc<A>::serialize_compact(std::ostream& os) const {
224 return sketch_impl->serialize(os, true);
225}
226
227template<typename A>
228void hll_sketch_alloc<A>::serialize_updatable(std::ostream& os) const {
229 return sketch_impl->serialize(os, false);
230}
231
232template<typename A>
233auto hll_sketch_alloc<A>::serialize_compact(unsigned header_size_bytes) const -> vector_bytes {
234 return sketch_impl->serialize(true, header_size_bytes);
235}
236
237template<typename A>
238auto hll_sketch_alloc<A>::serialize_updatable() const -> vector_bytes {
239 return sketch_impl->serialize(false, 0);
240}
241
242template<typename A>
243string<A> hll_sketch_alloc<A>::to_string(const bool summary,
244 const bool detail,
245 const bool aux_detail,
246 const bool all) const {
247 // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
248 // The stream does not support passing an allocator instance, and alternatives are complicated.
249 std::stringstream os;
250 if (summary) {
251 os << "### HLL sketch summary:" << std::endl
252 << " Log Config K : " << std::to_string(get_lg_config_k()) << std::endl
253 << " Hll Target : " << type_as_string() << std::endl
254 << " Current Mode : " << mode_as_string() << std::endl
255 << " LB : " << get_lower_bound(1) << std::endl
256 << " Estimate : " << get_estimate() << std::endl
257 << " UB : " << get_upper_bound(1) << std::endl
258 << " OutOfOrder flag: " << (is_out_of_order_flag() ? "true" : "false") << std::endl;
259 if (get_current_mode() == HLL) {
260 HllArray<A>* hllArray = (HllArray<A>*) sketch_impl;
261 os << " CurMin : " << std::to_string(hllArray->getCurMin()) << std::endl
262 << " NumAtCurMin : " << hllArray->getNumAtCurMin() << std::endl
263 << " HipAccum : " << hllArray->getHipAccum() << std::endl
264 << " KxQ0 : " << hllArray->getKxQ0() << std::endl
265 << " KxQ1 : " << hllArray->getKxQ1() << std::endl;
266 if (get_target_type() == HLL_4) {
267 const Hll4Array<A>* hll4_ptr = static_cast<const Hll4Array<A>*>(sketch_impl);
268 os << " Aux table? : " << (hll4_ptr->getAuxHashMap() != nullptr ? "true" : "false") << std::endl;
269 }
270 } else {
271 os << " Coupon count : "
272 << std::to_string(((CouponList<A>*) sketch_impl)->getCouponCount()) << std::endl;
273 }
274 os << "### End HLL sketch summary" << std::endl;
275 }
276
277 if (detail) {
278 os << "### HLL sketch data detail:" << std::endl;
279 if (get_current_mode() == HLL) {
280 const HllArray<A>* hll_ptr = static_cast<const HllArray<A>*>(sketch_impl);
281 os << std::left << std::setw(10) << "Slot" << std::setw(6) << "Value" << std::endl;
282 auto it = hll_ptr->begin(all);
283 while (it != hll_ptr->end()) {
284 os << std::setw(10) << HllUtil<A>::getLow26(*it);
285 os << std::setw(6) << HllUtil<A>::getValue(*it);
286 os << std::endl;
287 ++it;
288 }
289 } else {
290 const CouponList<A>* list_ptr = static_cast<const CouponList<A>*>(sketch_impl);
291 os << std::left;
292 os << std::setw(10) << "Index";
293 os << std::setw(10) << "Key";
294 os << std::setw(10) << "Slot";
295 os << std::setw(6) << "Value";
296 os << std::endl;
297 auto it = list_ptr->begin(all);
298 int i = 0;
299 int mask = (1 << get_lg_config_k()) - 1;
300 while (it != list_ptr->end()) {
301 os << std::setw(10) << i;
302 os << std::setw(10) << HllUtil<A>::getLow26(*it);
303 os << std::setw(10) << (HllUtil<A>::getLow26(*it) & mask);
304 os << std::setw(6) << HllUtil<A>::getValue(*it);
305 os << std::endl;
306 ++it;
307 ++i;
308 }
309 }
310 os << "### End HLL sketch data detail" << std::endl;
311 }
312 if (aux_detail) {
313 if ((get_current_mode() == HLL) && (get_target_type() == HLL_4)) {
314 const Hll4Array<A>* hll4_ptr = static_cast<const Hll4Array<A>*>(sketch_impl);
315 const AuxHashMap<A>* aux_ptr = hll4_ptr->getAuxHashMap();
316 if (aux_ptr != nullptr) {
317 os << "### HLL sketch aux detail:" << std::endl;
318 os << std::left;
319 os << std::setw(10) << "Index";
320 os << std::setw(10) << "Key";
321 os << std::setw(10) << "Slot";
322 os << std::setw(6) << "Value";
323 os << std::endl;
324 auto it = aux_ptr->begin(all);
325 int i = 0;
326 int mask = (1 << get_lg_config_k()) - 1;
327 while (it != aux_ptr->end()) {
328 os << std::setw(10) << i;
329 os << std::setw(10) << HllUtil<A>::getLow26(*it);
330 os << std::setw(10) << (HllUtil<A>::getLow26(*it) & mask);
331 os << std::setw(6) << HllUtil<A>::getValue(*it);
332 os << std::endl;
333 ++it;
334 ++i;
335 }
336 os << "### End HLL sketch aux detail" << std::endl;
337 }
338 }
339 }
340
341 return string<A>(os.str().c_str(), sketch_impl->getAllocator());
342}
343
344template<typename A>
345double hll_sketch_alloc<A>::get_estimate() const {
346 return sketch_impl->getEstimate();
347}
348
349template<typename A>
350double hll_sketch_alloc<A>::get_composite_estimate() const {
351 return sketch_impl->getCompositeEstimate();
352}
353
354template<typename A>
355double hll_sketch_alloc<A>::get_lower_bound(uint8_t numStdDev) const {
356 return sketch_impl->getLowerBound(numStdDev);
357}
358
359template<typename A>
360double hll_sketch_alloc<A>::get_upper_bound(uint8_t numStdDev) const {
361 return sketch_impl->getUpperBound(numStdDev);
362}
363
364template<typename A>
365hll_mode hll_sketch_alloc<A>::get_current_mode() const {
366 return sketch_impl->getCurMode();
367}
368
369template<typename A>
370uint8_t hll_sketch_alloc<A>::get_lg_config_k() const {
371 return sketch_impl->getLgConfigK();
372}
373
374template<typename A>
375target_hll_type hll_sketch_alloc<A>::get_target_type() const {
376 return sketch_impl->getTgtHllType();
377}
378
379template<typename A>
380bool hll_sketch_alloc<A>::is_out_of_order_flag() const {
381 return sketch_impl->isOutOfOrderFlag();
382}
383
384template<typename A>
385bool hll_sketch_alloc<A>::is_estimation_mode() const {
386 return true;
387}
388
389template<typename A>
390uint32_t hll_sketch_alloc<A>::get_updatable_serialization_bytes() const {
391 return sketch_impl->getUpdatableSerializationBytes();
392}
393
394template<typename A>
395uint32_t hll_sketch_alloc<A>::get_compact_serialization_bytes() const {
396 return sketch_impl->getCompactSerializationBytes();
397}
398
399template<typename A>
400bool hll_sketch_alloc<A>::is_compact() const {
401 return sketch_impl->isCompact();
402}
403
404template<typename A>
405bool hll_sketch_alloc<A>::is_empty() const {
406 return sketch_impl->isEmpty();
407}
408
409template<typename A>
410std::string hll_sketch_alloc<A>::type_as_string() const {
411 switch (sketch_impl->getTgtHllType()) {
412 case target_hll_type::HLL_4:
413 return std::string("HLL_4");
414 case target_hll_type::HLL_6:
415 return std::string("HLL_6");
416 case target_hll_type::HLL_8:
417 return std::string("HLL_8");
418 default:
419 throw std::runtime_error("Sketch state error: Invalid target_hll_type");
420 }
421}
422
423template<typename A>
424std::string hll_sketch_alloc<A>::mode_as_string() const {
425 switch (sketch_impl->getCurMode()) {
426 case LIST:
427 return std::string("LIST");
428 case SET:
429 return std::string("SET");
430 case HLL:
431 return std::string("HLL");
432 default:
433 throw std::runtime_error("Sketch state error: Invalid hll_mode");
434 }
435}
436
437template<typename A>
438uint32_t hll_sketch_alloc<A>::get_max_updatable_serialization_bytes(uint8_t lg_config_k,
439 const target_hll_type tgtHllType) {
440 uint32_t arrBytes;
441 if (tgtHllType == target_hll_type::HLL_4) {
442 const uint32_t auxBytes = 4 << hll_constants::LG_AUX_ARR_INTS[lg_config_k];
443 arrBytes = HllArray<A>::hll4ArrBytes(lg_config_k) + auxBytes;
444 } else if (tgtHllType == target_hll_type::HLL_6) {
445 arrBytes = HllArray<A>::hll6ArrBytes(lg_config_k);
446 } else { //HLL_8
447 arrBytes = HllArray<A>::hll8ArrBytes(lg_config_k);
448 }
449 return hll_constants::HLL_BYTE_ARR_START + arrBytes;
450}
451
452template<typename A>
453double hll_sketch_alloc<A>::get_rel_err(bool upperBound, bool unioned,
454 uint8_t lg_config_k, uint8_t numStdDev) {
455 return HllUtil<A>::getRelErr(upperBound, unioned, lg_config_k, numStdDev);
456}
457
458}
459
460#endif // _HLLSKETCH_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