/
hll_8array.go
183 lines (163 loc) · 4.71 KB
/
hll_8array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hll
import (
"fmt"
)
// hll8ArrayImpl Uses 6 bits per slot in a packed byte array
type hll8ArrayImpl struct {
hllArrayImpl
}
type hll8Iterator struct {
hllPairIterator
hll *hll8ArrayImpl
}
func (h *hll8ArrayImpl) iterator() pairIterator {
a := newHll8Iterator(1<<h.lgConfigK, h)
return &a
}
func (h *hll8ArrayImpl) copyAs(tgtHllType TgtHllType) (hllSketchStateI, error) {
if tgtHllType == h.tgtHllType {
return h.copy()
}
if tgtHllType == TgtHllTypeHll4 {
return convertToHll4(h)
}
if tgtHllType == TgtHllTypeHll6 {
return convertToHll6(h)
}
return nil, fmt.Errorf("cannot convert to TgtHllType id: %d", int(tgtHllType))
}
func (h *hll8ArrayImpl) copy() (hllSketchStateI, error) {
return &hll8ArrayImpl{
hllArrayImpl: h.copyCommon(),
}, nil
}
func (h *hll8ArrayImpl) ToCompactSlice() ([]byte, error) {
return h.ToUpdatableSlice()
}
func (h *hll8ArrayImpl) ToUpdatableSlice() ([]byte, error) {
return toHllByteArr(h, false)
}
// newHll8Array returns a new Hll8Array.
func newHll8Array(lgConfigK int) hllArray {
return &hll8ArrayImpl{
hllArrayImpl: hllArrayImpl{
hllSketchConfig: hllSketchConfig{
lgConfigK: lgConfigK,
tgtHllType: TgtHllTypeHll8,
curMode: curModeHll,
},
curMin: 0,
numAtCurMin: 1 << lgConfigK,
hipAccum: 0,
kxq0: float64(uint64(1 << lgConfigK)),
kxq1: 0,
hllByteArr: make([]byte, 1<<lgConfigK),
auxStart: hllByteArrStart + 1<<(lgConfigK-1),
},
}
}
// deserializeHll8 returns a new Hll8Array from the given byte array.
func deserializeHll8(byteArray []byte) hllArray {
lgConfigK := extractLgK(byteArray)
hll8 := newHll8Array(lgConfigK)
hll8.extractCommonHll(byteArray)
return hll8
}
func convertToHll8(srcAbsHllArr hllArray) (hllSketchStateI, error) {
lgConfigK := srcAbsHllArr.GetLgConfigK()
hll8Array := newHll8Array(lgConfigK)
hll8Array.putOutOfOrder(srcAbsHllArr.isOutOfOrder())
numZeros := 1 << lgConfigK
itr := srcAbsHllArr.iterator()
for itr.nextAll() {
v, err := itr.getValue()
if err != nil {
return nil, err
}
if v != empty {
numZeros--
p, err := itr.getPair()
if err != nil {
return nil, err
}
_, err = hll8Array.couponUpdate(p) //creates KxQ registers
if err != nil {
return nil, err
}
}
}
hll8Array.putNumAtCurMin(numZeros)
hll8Array.putHipAccum(srcAbsHllArr.getHipAccum()) //intentional overwrite
hll8Array.putRebuildCurMinNumKxQFlag(false)
return hll8Array, nil
}
func (h *hll8ArrayImpl) couponUpdate(coupon int) (hllSketchStateI, error) {
newValue := coupon >> keyBits26
configKmask := (1 << h.lgConfigK) - 1
slotNo := coupon & configKmask
err := h.updateSlotWithKxQ(slotNo, newValue)
return h, err
}
func (h *hll8ArrayImpl) updateSlotWithKxQ(slotNo int, newValue int) error {
oldValue := h.getSlotValue(slotNo)
if newValue > oldValue {
h.hllByteArr[slotNo] = byte(newValue & valMask6)
err := h.hipAndKxQIncrementalUpdate(oldValue, newValue)
if err != nil {
return err
}
if oldValue == 0 {
h.numAtCurMin-- //interpret numAtCurMin as num Zeros
if h.numAtCurMin < 0 {
return fmt.Errorf("numAtCurMin < 0")
}
}
}
return nil
}
func (h *hll8ArrayImpl) updateSlotNoKxQ(slotNo int, newValue int) {
oldValue := h.getSlotValue(slotNo)
h.hllByteArr[slotNo] = byte(max(newValue, oldValue))
}
func (h *hll8ArrayImpl) getSlotValue(slotNo int) int {
return int(h.hllByteArr[slotNo] & valMask6)
}
func newHll8Iterator(lengthPairs int, hll *hll8ArrayImpl) hll8Iterator {
return hll8Iterator{
hllPairIterator: newHllPairIterator(lengthPairs),
hll: hll,
}
}
func (h *hll8Iterator) nextValid() bool {
for h.index+1 < h.lengthPairs {
h.index++
h.value = int(h.hll.hllByteArr[h.index]) & valMask6
if h.value != empty {
return true
}
}
return false
}
func (h *hll8Iterator) getValue() (int, error) {
return int(h.hll.hllByteArr[h.index]) & valMask6, nil
}
func (h *hll8Iterator) getPair() (int, error) {
v, err := h.getValue()
return pair(h.index, v), err
}