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