datasketches-cpp
Hll6Array-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 _HLL6ARRAY_INTERNAL_HPP_
21 #define _HLL6ARRAY_INTERNAL_HPP_
22 
23 #include <cstring>
24 
25 #include "Hll6Array.hpp"
26 
27 namespace datasketches {
28 
29 template<typename A>
30 Hll6Array<A>::Hll6Array(uint8_t lgConfigK, bool startFullSize, const A& allocator):
31 HllArray<A>(lgConfigK, target_hll_type::HLL_6, startFullSize, allocator)
32 {
33  const int numBytes = this->hll6ArrBytes(lgConfigK);
34  this->hllByteArr_.resize(numBytes, 0);
35 }
36 
37 template<typename A>
38 Hll6Array<A>::Hll6Array(const HllArray<A>& other) :
39  HllArray<A>(other.getLgConfigK(), target_hll_type::HLL_6, other.isStartFullSize(), other.getAllocator())
40 {
41  const int numBytes = this->hll6ArrBytes(this->lgConfigK_);
42  this->hllByteArr_.resize(numBytes, 0);
43  this->oooFlag_ = other.isOutOfOrderFlag();
44  uint32_t num_zeros = 1 << this->lgConfigK_;
45 
46  for (const auto coupon : other) { // all = false, so skip empty values
47  num_zeros--;
48  internalCouponUpdate(coupon); // updates KxQ registers
49  }
50 
51  this->numAtCurMin_ = num_zeros;
52  this->hipAccum_ = other.getHipAccum();
53  this->rebuild_kxq_curmin_ = false;
54 }
55 
56 template<typename A>
57 std::function<void(HllSketchImpl<A>*)> Hll6Array<A>::get_deleter() const {
58  return [](HllSketchImpl<A>* ptr) {
59  using Hll6Alloc = typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>>;
60  Hll6Array<A>* hll = static_cast<Hll6Array<A>*>(ptr);
61  Hll6Alloc hll6Alloc(hll->getAllocator());
62  hll->~Hll6Array();
63  hll6Alloc.deallocate(hll, 1);
64  };
65 }
66 
67 template<typename A>
68 Hll6Array<A>* Hll6Array<A>::copy() const {
69  using Hll6Alloc = typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>>;
70  Hll6Alloc hll6Alloc(this->getAllocator());
71  return new (hll6Alloc.allocate(1)) Hll6Array<A>(*this);
72 }
73 
74 template<typename A>
75 uint8_t Hll6Array<A>::getSlot(uint32_t slotNo) const {
76  const uint32_t startBit = slotNo * 6;
77  const uint32_t shift = startBit & 0x7;
78  const uint32_t byteIdx = startBit >> 3;
79  const uint16_t twoByteVal = (this->hllByteArr_[byteIdx + 1] << 8) | this->hllByteArr_[byteIdx];
80  return (twoByteVal >> shift) & hll_constants::VAL_MASK_6;
81 }
82 
83 template<typename A>
84 void Hll6Array<A>::putSlot(uint32_t slotNo, uint8_t value) {
85  const uint32_t startBit = slotNo * 6;
86  const uint32_t shift = startBit & 0x7;
87  const uint32_t byteIdx = startBit >> 3;
88  const uint16_t valShifted = (value & 0x3F) << shift;
89  uint16_t curMasked = (this->hllByteArr_[byteIdx + 1] << 8) | this->hllByteArr_[byteIdx];
90  curMasked &= (~(hll_constants::VAL_MASK_6 << shift));
91  const uint16_t insert = curMasked | valShifted;
92  this->hllByteArr_[byteIdx] = insert & 0xFF;
93  this->hllByteArr_[byteIdx + 1] = (insert & 0xFF00) >> 8;
94 }
95 
96 template<typename A>
97 uint32_t Hll6Array<A>::getHllByteArrBytes() const {
98  return this->hll6ArrBytes(this->lgConfigK_);
99 }
100 
101 template<typename A>
102 HllSketchImpl<A>* Hll6Array<A>::couponUpdate(uint32_t coupon) {
103  internalCouponUpdate(coupon);
104  return this;
105 }
106 
107 template<typename A>
108 void Hll6Array<A>::internalCouponUpdate(uint32_t coupon) {
109  const uint32_t configKmask = (1 << this->lgConfigK_) - 1;
110  const uint32_t slotNo = HllUtil<A>::getLow26(coupon) & configKmask;
111  const uint8_t newVal = HllUtil<A>::getValue(coupon);
112 
113  const uint8_t curVal = getSlot(slotNo);
114  if (newVal > curVal) {
115  putSlot(slotNo, newVal);
116  this->hipAndKxQIncrementalUpdate(curVal, newVal);
117  if (curVal == 0) {
118  this->numAtCurMin_--; // interpret numAtCurMin as num zeros
119  }
120  }
121 }
122 
123 }
124 
125 #endif // _HLL6ARRAY_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