-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.go
196 lines (165 loc) · 3.24 KB
/
reduce.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
package jsong
import (
"strings"
"sync"
)
type Reducer interface {
Add(any)
Value() any
}
type HashReducer[T comparable] struct {
New func() Reducer
Hash func(any) T
Partitions map[T]Reducer
}
func (a *HashReducer[T]) Add(x any) {
h := a.Hash(x)
r, ok := a.Partitions[h]
if !ok {
r = a.New()
a.Partitions[h] = r
}
r.Add(x)
}
func (a *HashReducer[T]) Value() any {
res := make(array, 0, len(a.Partitions))
for _, r := range a.Partitions {
res = append(res, r.Value())
}
return res
}
type PartitionReducer struct {
New func() Reducer
Key string
partitions map[valueInterface]Reducer
once sync.Once
}
func (a *PartitionReducer) Reset() {
a.partitions = nil
a.once = sync.Once{}
}
func (a *PartitionReducer) Add(x any) {
h := Extract(x, a.Key).(valueInterface)
a.once.Do(func() { a.partitions = make(map[valueInterface]Reducer) })
r, ok := a.partitions[h]
if !ok {
r = a.New()
a.partitions[h] = r
}
r.Add(x)
}
func (a *PartitionReducer) Value() any {
res := make(array, 0, len(a.partitions))
for _, r := range a.partitions {
res = append(res, r.Value())
}
return res
}
type ObjectReducer map[string]Reducer
func (a ObjectReducer) Add(x any) {
val := ValueOf(x).(object)
for k, r := range a {
r.Add(val[k])
}
}
func (a ObjectReducer) Value() any {
res := make(object, len(a))
for k, r := range a {
res[k] = r.Value()
}
return res
}
type ArrayReducer []Reducer
func (a ArrayReducer) Add(x any) {
val := ValueOf(x).(array)
for i, r := range a {
r.Add(val[i])
}
}
func (a ArrayReducer) Value() any {
res := make(array, len(a))
for i, r := range a {
res[i] = r.Value()
}
return res
}
type StringAgg struct {
strings.Builder
}
func (a *StringAgg) Add(x any) {
a.Builder.WriteString(x.(string))
}
type NullReducer struct{}
func (NullReducer) Add(any) {}
func (NullReducer) Value() any { return nil }
type ReduceOp string
const (
ReduceUndefined ReduceOp = ""
ReduceSum ReduceOp = "sum"
ReduceMin ReduceOp = "min"
ReduceMax ReduceOp = "max"
ReduceAny ReduceOp = "any"
ReduceMean ReduceOp = "avg"
)
type NumericReducer struct {
Op ReduceOp
val float64
cnt int
set bool
}
func (a *NumericReducer) Add(x any) {
v := x.(float64)
switch a.Op {
case ReduceUndefined, ReduceSum:
a.set = true
a.val += v
case ReduceMin:
if !a.set || v < a.val {
a.set = true
a.val = v
}
case ReduceMax:
if !a.set || v > a.val {
a.set = true
a.val = v
}
case ReduceAny:
if !a.set {
a.set = true
a.val = v
}
case ReduceMean:
a.set = true
a.val += v
a.cnt++
}
}
func (a *NumericReducer) Value() any {
if a.Op == ReduceMean {
return a.val / float64(a.cnt)
}
if !a.set {
return (*num)(nil)
}
return num(a.val)
}
type SumReducer struct {
sum float64
}
func (a *SumReducer) Add(x any) {
a.sum += MustOk(Float64(x))
}
type TrueCounter struct {
count int
}
func (a *TrueCounter) Add(x any) {
if x.(bool) {
a.count++
}
}
func (a *StringAgg) Value() any { return str(a.String()) }
func (a *SumReducer) Value() any { return num(a.sum) }
func (a *TrueCounter) Value() any { return num(a.count) }
type AnyReducer struct{ V any }
func (a *AnyReducer) Add(v any) { a.V = v }
func (a *AnyReducer) Value() any { return a.V }