datasketches-cpp
Loading...
Searching...
No Matches
HllSketchImpl-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 _HLLSKETCHIMPL_INTERNAL_HPP_
21#define _HLLSKETCHIMPL_INTERNAL_HPP_
22
23#include "HllSketchImpl.hpp"
24#include "HllSketchImplFactory.hpp"
25
26#include <stdexcept>
27
28namespace datasketches {
29
30template<typename A>
31HllSketchImpl<A>::HllSketchImpl(uint8_t lgConfigK, target_hll_type tgtHllType,
32 hll_mode mode)
33 : lgConfigK_(lgConfigK),
34 tgtHllType_(tgtHllType),
35 mode_(mode)
36{
37}
38
39template<typename A>
40HllSketchImpl<A>::~HllSketchImpl() {
41}
42
43template<typename A>
44target_hll_type HllSketchImpl<A>::extractTgtHllType(uint8_t modeByte) {
45 switch ((modeByte >> 2) & 0x3) {
46 case 0:
47 return target_hll_type::HLL_4;
48 case 1:
49 return target_hll_type::HLL_6;
50 case 2:
51 return target_hll_type::HLL_8;
52 default:
53 throw std::invalid_argument("Invalid target HLL type");
54 }
55}
56
57template<typename A>
58hll_mode HllSketchImpl<A>::extractCurMode(uint8_t modeByte) {
59 switch (modeByte & 0x3) {
60 case 0:
61 return hll_mode::LIST;
62 case 1:
63 return hll_mode::SET;
64 case 2:
65 return hll_mode::HLL;
66 default:
67 throw std::invalid_argument("Invalid current sketch mode");
68 }
69}
70
71template<typename A>
72uint8_t HllSketchImpl<A>::makeFlagsByte(bool compact) const {
73 uint8_t flags = 0;
74 flags |= (isEmpty() ? hll_constants::EMPTY_FLAG_MASK : 0);
75 flags |= (compact ? hll_constants::COMPACT_FLAG_MASK : 0);
76 flags |= (isOutOfOrderFlag() ? hll_constants::OUT_OF_ORDER_FLAG_MASK : 0);
77 // bit 32 is reserved: see RESERVED_FLAG_MASK_32 in HllUtil.hpp
78 return flags;
79}
80
81// lo2bits = curMode, next 2 bits = tgtHllType
82// Dec Lo4Bits TgtHllType, CurMode
83// 0 0000 HLL_4, LIST
84// 1 0001 HLL_4, SET
85// 2 0010 HLL_4, HLL
86// 4 0100 HLL_6, LIST
87// 5 0101 HLL_6, SET
88// 6 0110 HLL_6, HLL
89// 8 1000 HLL_8, LIST
90// 9 1001 HLL_8, SET
91// 10 1010 HLL_8, HLL
92template<typename A>
93uint8_t HllSketchImpl<A>::makeModeByte() const {
94 uint8_t byte = 0;
95
96 switch (mode_) {
97 case LIST:
98 byte = 0;
99 break;
100 case SET:
101 byte = 1;
102 break;
103 case HLL:
104 byte = 2;
105 break;
106 }
107
108 switch (tgtHllType_) {
109 case HLL_4:
110 byte |= (0 << 2); // for completeness
111 break;
112 case HLL_6:
113 byte |= (1 << 2);
114 break;
115 case HLL_8:
116 byte |= (2 << 2);
117 break;
118 }
119
120 return byte;
121}
122
123template<typename A>
124HllSketchImpl<A>* HllSketchImpl<A>::reset(bool full_size) {
125 return HllSketchImplFactory<A>::reset(this, full_size);
126}
127
128template<typename A>
129target_hll_type HllSketchImpl<A>::getTgtHllType() const {
130 return tgtHllType_;
131}
132
133template<typename A>
134uint8_t HllSketchImpl<A>::getLgConfigK() const {
135 return lgConfigK_;
136}
137
138template<typename A>
139hll_mode HllSketchImpl<A>::getCurMode() const {
140 return mode_;
141}
142
143}
144
145#endif // _HLLSKETCHIMPL_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_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