-
Notifications
You must be signed in to change notification settings - Fork 24
/
summary.go
206 lines (176 loc) · 4.39 KB
/
summary.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
package tdigest
import (
"fmt"
"math"
"sort"
)
type summary struct {
means []float64
counts []uint64
}
func newSummary(initialCapacity int) *summary {
s := &summary{
means: make([]float64, 0, initialCapacity),
counts: make([]uint64, 0, initialCapacity),
}
return s
}
func (s *summary) Len() int {
return len(s.means)
}
func (s *summary) Add(key float64, value uint64) error {
if math.IsNaN(key) {
return fmt.Errorf("key must not be NaN")
}
if value == 0 {
return fmt.Errorf("Count must be >0")
}
idx := s.findInsertionIndex(key)
s.means = append(s.means, math.NaN())
s.counts = append(s.counts, 0)
copy(s.means[idx+1:], s.means[idx:])
copy(s.counts[idx+1:], s.counts[idx:])
s.means[idx] = key
s.counts[idx] = value
return nil
}
// Always insert to the right
func (s *summary) findInsertionIndex(x float64) int {
// Binary search is only worthwhile if we have a lot of keys.
if len(s.means) < 250 {
for i, mean := range s.means {
if mean > x {
return i
}
}
return len(s.means)
}
return sort.Search(len(s.means), func(i int) bool {
return s.means[i] > x
})
}
// This method is the hotspot when calling Add(), which in turn is called by
// Compress() and Merge().
func (s *summary) HeadSum(idx int) (sum float64) {
return float64(sumUntilIndex(s.counts, idx))
}
func (s *summary) Floor(x float64) int {
return s.findIndex(x) - 1
}
func (s *summary) findIndex(x float64) int {
// Binary search is only worthwhile if we have a lot of keys.
if len(s.means) < 250 {
for i, mean := range s.means {
if mean >= x {
return i
}
}
return len(s.means)
}
return sort.Search(len(s.means), func(i int) bool {
return s.means[i] >= x
})
}
func (s *summary) Mean(uncheckedIndex int) float64 {
return s.means[uncheckedIndex]
}
func (s *summary) Count(uncheckedIndex int) uint64 {
return s.counts[uncheckedIndex]
}
// return the index of the last item which the sum of counts
// of items before it is less than or equal to `sum`. -1 in
// case no centroid satisfies the requirement.
// Since it's cheap, this also returns the `HeadSum` until
// the found index (i.e. cumSum = HeadSum(FloorSum(x)))
func (s *summary) FloorSum(sum float64) (index int, cumSum float64) {
index = -1
for i, count := range s.counts {
if cumSum <= sum {
index = i
} else {
break
}
cumSum += float64(count)
}
if index != -1 {
cumSum -= float64(s.counts[index])
}
return index, cumSum
}
func (s *summary) setAt(index int, mean float64, count uint64) {
s.means[index] = mean
s.counts[index] = count
s.adjustRight(index)
s.adjustLeft(index)
}
func (s *summary) adjustRight(index int) {
for i := index + 1; i < len(s.means) && s.means[i-1] > s.means[i]; i++ {
s.means[i-1], s.means[i] = s.means[i], s.means[i-1]
s.counts[i-1], s.counts[i] = s.counts[i], s.counts[i-1]
}
}
func (s *summary) adjustLeft(index int) {
for i := index - 1; i >= 0 && s.means[i] > s.means[i+1]; i-- {
s.means[i], s.means[i+1] = s.means[i+1], s.means[i]
s.counts[i], s.counts[i+1] = s.counts[i+1], s.counts[i]
}
}
func (s *summary) ForEach(f func(float64, uint64) bool) {
for i, mean := range s.means {
if !f(mean, s.counts[i]) {
break
}
}
}
func (s *summary) Perm(rng RNG, f func(float64, uint64) bool) {
for _, i := range perm(rng, s.Len()) {
if !f(s.means[i], s.counts[i]) {
break
}
}
}
func (s *summary) Clone() *summary {
return &summary{
means: append([]float64{}, s.means...),
counts: append([]uint64{}, s.counts...),
}
}
// Randomly shuffles summary contents, so they can be added to another summary
// with being pathological. Renders summary invalid.
func (s *summary) shuffle(rng RNG) {
for i := len(s.means) - 1; i > 1; i-- {
s.Swap(i, rng.Intn(i+1))
}
}
// for sort.Interface
func (s *summary) Swap(i, j int) {
s.means[i], s.means[j] = s.means[j], s.means[i]
s.counts[i], s.counts[j] = s.counts[j], s.counts[i]
}
func (s *summary) Less(i, j int) bool {
return s.means[i] < s.means[j]
}
// A simple loop unroll saves a surprising amount of time.
func sumUntilIndex(s []uint64, idx int) uint64 {
var cumSum uint64
var i int
for i = idx - 1; i >= 3; i -= 4 {
cumSum += uint64(s[i])
cumSum += uint64(s[i-1])
cumSum += uint64(s[i-2])
cumSum += uint64(s[i-3])
}
for ; i >= 0; i-- {
cumSum += uint64(s[i])
}
return cumSum
}
func perm(rng RNG, n int) []int {
m := make([]int, n)
for i := 1; i < n; i++ {
j := rng.Intn(i + 1)
m[i] = m[j]
m[j] = i
}
return m
}