forked from DataDog/sketches-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapsing_lowest_dense_store.go
206 lines (187 loc) · 5.54 KB
/
collapsing_lowest_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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 2020 Datadog, Inc.
package store
import (
"math"
enc "github.com/KoddiDev/sketches-go/ddsketch/encoding"
)
// CollapsingLowestDenseStore is a dynamically growing contiguous (non-sparse) store.
// The lower bins get combined so that the total number of bins do not exceed maxNumBins.
type CollapsingLowestDenseStore struct {
DenseStore
maxNumBins int
isCollapsed bool
}
func NewCollapsingLowestDenseStore(maxNumBins int) *CollapsingLowestDenseStore {
// Bins are not allocated until values are added.
// When the first value is added, a small number of bins are allocated. The number of bins will
// grow as needed up to maxNumBins.
return &CollapsingLowestDenseStore{
DenseStore: DenseStore{minIndex: math.MaxInt32, maxIndex: math.MinInt32},
maxNumBins: maxNumBins,
isCollapsed: false,
}
}
func (s *CollapsingLowestDenseStore) Add(index int) {
s.AddWithCount(index, float64(1))
}
func (s *CollapsingLowestDenseStore) AddBin(bin Bin) {
index := bin.Index()
count := bin.Count()
if count == 0 {
return
}
s.AddWithCount(index, count)
}
func (s *CollapsingLowestDenseStore) 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 *CollapsingLowestDenseStore) normalize(index int) int {
if index < s.minIndex {
if s.isCollapsed {
return 0
} else {
s.extendRange(index, index)
if s.isCollapsed {
return 0
}
}
} else if index > s.maxIndex {
s.extendRange(index, index)
}
return index - s.offset
}
func (s *CollapsingLowestDenseStore) getNewLength(newMinIndex, newMaxIndex int) int {
return min(s.DenseStore.getNewLength(newMinIndex, newMaxIndex), s.maxNumBins)
}
func (s *CollapsingLowestDenseStore) 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 *CollapsingLowestDenseStore) 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.
newMinIndex = newMaxIndex - len(s.bins) + 1
if newMinIndex >= s.maxIndex {
// There will be only one non-empty bucket.
s.bins = make([]float64, len(s.bins))
s.offset = newMinIndex
s.minIndex = newMinIndex
s.bins[0] = s.count
} else {
shift := s.offset - newMinIndex
if shift < 0 {
// Collapse the buckets.
n := float64(0)
for i := s.minIndex; i < newMinIndex; i++ {
n += s.bins[i-s.offset]
}
s.resetBins(s.minIndex, newMinIndex-1)
s.bins[newMinIndex-s.offset] += n
s.minIndex = newMinIndex
// Shift the buckets to make room for newMaxIndex.
s.shiftCounts(shift)
} else {
// Shift the buckets to make room for newMinIndex.
s.shiftCounts(shift)
s.minIndex = newMinIndex
}
}
s.maxIndex = newMaxIndex
s.isCollapsed = true
} else {
s.centerCounts(newMinIndex, newMaxIndex)
}
}
func (s *CollapsingLowestDenseStore) MergeWith(other Store) {
if other.IsEmpty() {
return
}
o, ok := other.(*CollapsingLowestDenseStore)
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.minIndex
for ; idx < s.minIndex && idx <= o.maxIndex; idx++ {
s.bins[0] += o.bins[idx-o.offset]
}
for ; idx < o.maxIndex; 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
// store.maxIndex = Integer.MAX_VALUE.
if idx == o.maxIndex {
s.bins[idx-s.offset] += o.bins[idx-o.offset]
}
s.count += o.count
}
func (s *CollapsingLowestDenseStore) Copy() Store {
bins := make([]float64, len(s.bins))
copy(bins, s.bins)
return &CollapsingLowestDenseStore{
DenseStore: DenseStore{
bins: bins,
count: s.count,
offset: s.offset,
minIndex: s.minIndex,
maxIndex: s.maxIndex,
},
maxNumBins: s.maxNumBins,
isCollapsed: s.isCollapsed,
}
}
func (s *CollapsingLowestDenseStore) Clear() {
s.DenseStore.Clear()
s.isCollapsed = false
}
func (s *CollapsingLowestDenseStore) DecodeAndMergeWith(r *[]byte, encodingMode enc.SubFlag) error {
return DecodeAndMergeWith(s, r, encodingMode)
}
var _ Store = (*CollapsingLowestDenseStore)(nil)
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x < y {
return x
}
return y
}