forked from DataDog/sketches-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapsing_highest_dense_store.go
187 lines (170 loc) · 5.13 KB
/
collapsing_highest_dense_store.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
184
185
186
187
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2021 Datadog, Inc.
package store
import (
"math"
enc "github.com/bahlo/sketches-go/ddsketch/encoding"
)
type CollapsingHighestDenseStore struct {
DenseStore
maxNumBins int
isCollapsed bool
}
func NewCollapsingHighestDenseStore(maxNumBins int) *CollapsingHighestDenseStore {
return &CollapsingHighestDenseStore{
DenseStore: DenseStore{minIndex: math.MaxInt32, maxIndex: math.MinInt32},
maxNumBins: maxNumBins,
isCollapsed: false,
}
}
func (s *CollapsingHighestDenseStore) Add(index int) {
s.AddWithCount(index, float64(1))
}
func (s *CollapsingHighestDenseStore) AddBin(bin Bin) {
index := bin.Index()
count := bin.Count()
if count == 0 {
return
}
s.AddWithCount(index, count)
}
func (s *CollapsingHighestDenseStore) AddWithCount(index int, count float64) {
if count == 0 {
return
}
arrayIndex := s.normalize(index)
s.bins[arrayIndex] += count
s.count += count
}
// Normalize the store, if necessary, so that the counter of the specified index can be updated.
func (s *CollapsingHighestDenseStore) normalize(index int) int {
if index > s.maxIndex {
if s.isCollapsed {
return len(s.bins) - 1
} else {
s.extendRange(index, index)
if s.isCollapsed {
return len(s.bins) - 1
}
}
} else if index < s.minIndex {
s.extendRange(index, index)
}
return index - s.offset
}
func (s *CollapsingHighestDenseStore) getNewLength(newMinIndex, newMaxIndex int) int {
return min(s.DenseStore.getNewLength(newMinIndex, newMaxIndex), s.maxNumBins)
}
func (s *CollapsingHighestDenseStore) extendRange(newMinIndex, newMaxIndex int) {
newMinIndex = min(newMinIndex, s.minIndex)
newMaxIndex = max(newMaxIndex, s.maxIndex)
if s.IsEmpty() {
initialLength := s.getNewLength(newMinIndex, newMaxIndex)
s.bins = append(s.bins, make([]float64, initialLength)...)
s.offset = newMinIndex
s.minIndex = newMinIndex
s.maxIndex = newMaxIndex
s.adjust(newMinIndex, newMaxIndex)
} else if newMinIndex >= s.offset && newMaxIndex < s.offset+len(s.bins) {
s.minIndex = newMinIndex
s.maxIndex = newMaxIndex
} else {
// To avoid shifting too often when nearing the capacity of the array,
// we may grow it before we actually reach the capacity.
newLength := s.getNewLength(newMinIndex, newMaxIndex)
if newLength > len(s.bins) {
s.bins = append(s.bins, make([]float64, newLength-len(s.bins))...)
}
s.adjust(newMinIndex, newMaxIndex)
}
}
// Adjust bins, offset, minIndex and maxIndex, without resizing the bins slice in order to make it fit the
// specified range.
func (s *CollapsingHighestDenseStore) adjust(newMinIndex, newMaxIndex int) {
if newMaxIndex-newMinIndex+1 > len(s.bins) {
// The range of indices is too wide, buckets of lowest indices need to be collapsed.
newMaxIndex = newMinIndex + len(s.bins) - 1
if newMaxIndex <= s.minIndex {
// There will be only one non-empty bucket.
s.bins = make([]float64, len(s.bins))
s.offset = newMinIndex
s.maxIndex = newMaxIndex
s.bins[len(s.bins)-1] = s.count
} else {
shift := s.offset - newMinIndex
if shift > 0 {
// Collapse the buckets.
n := float64(0)
for i := newMaxIndex + 1; i <= s.maxIndex; i++ {
n += s.bins[i-s.offset]
}
s.resetBins(newMaxIndex+1, s.maxIndex)
s.bins[newMaxIndex-s.offset] += n
s.maxIndex = newMaxIndex
// Shift the buckets to make room for newMinIndex.
s.shiftCounts(shift)
} else {
// Shift the buckets to make room for newMaxIndex.
s.shiftCounts(shift)
s.maxIndex = newMaxIndex
}
}
s.minIndex = newMinIndex
s.isCollapsed = true
} else {
s.centerCounts(newMinIndex, newMaxIndex)
}
}
func (s *CollapsingHighestDenseStore) MergeWith(other Store) {
if other.IsEmpty() {
return
}
o, ok := other.(*CollapsingHighestDenseStore)
if !ok {
for bin := range other.Bins() {
s.AddBin(bin)
}
return
}
if o.minIndex < s.minIndex || o.maxIndex > s.maxIndex {
s.extendRange(o.minIndex, o.maxIndex)
}
idx := o.maxIndex
for ; idx > s.maxIndex && idx >= o.minIndex; idx-- {
s.bins[len(s.bins)-1] += o.bins[idx-o.offset]
}
for ; idx > o.minIndex; idx-- {
s.bins[idx-s.offset] += o.bins[idx-o.offset]
}
// This is a separate test so that the comparison in the previous loop is strict (>) and handles
// o.minIndex = Integer.MIN_VALUE.
if idx == o.minIndex {
s.bins[idx-s.offset] += o.bins[idx-o.offset]
}
s.count += o.count
}
func (s *CollapsingHighestDenseStore) Copy() Store {
bins := make([]float64, len(s.bins))
copy(bins, s.bins)
return &CollapsingHighestDenseStore{
DenseStore: DenseStore{
bins: bins,
count: s.count,
offset: s.offset,
minIndex: s.minIndex,
maxIndex: s.maxIndex,
},
maxNumBins: s.maxNumBins,
isCollapsed: s.isCollapsed,
}
}
func (s *CollapsingHighestDenseStore) Clear() {
s.DenseStore.Clear()
s.isCollapsed = false
}
func (s *CollapsingHighestDenseStore) DecodeAndMergeWith(r *[]byte, encodingMode enc.SubFlag) error {
return DecodeAndMergeWith(s, r, encodingMode)
}
var _ Store = (*CollapsingHighestDenseStore)(nil)