-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.go
310 lines (298 loc) · 8.37 KB
/
combine.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package sexp
import (
"github.com/feyeleanor/jittery"
"reflect"
)
/*
func Merge(d, s Mapping) {
Each(s.Keys(), func(k interface{}) {
d.Store(k, s.At(k))
})
}
*/
/*
Combine takes two parameters and applies a function to them.
In the case of scalar parameters the function is applied once and the result returned.
In the case of vector parameters the function is applied to each matched pair of elements and the result returned.
A vector result will always have the same type as the left-hand parameter and the number of elements in the result
vector will be the same as the number of elements in the lef-hand parameter with trailing unmatched values combined
with the correct zero value for the contained type.
*/
func Combine(left, right interface{}, f func(interface{}, interface{}) interface{}) (r interface{}) {
switch left := left.(type) {
case Combinable:
r = left.Combine(right, f)
case Indexable:
r = combineIndexable(left, right, f)
default:
r = combineValue(left, right, f)
}
return
}
func blank(container interface{}) reflect.Value {
return reflect.Zero(reflect.ValueOf(container).Type().Elem())
}
func combineIndexable(left Indexable, right interface{}, f func(interface{}, interface{}) interface{}) (result interface{}) {
switch right := right.(type) {
case Indexable:
newSlice := func(length int, f func(Indexable)) {
var i Indexable
jittery.CatchAll(func() {
i = Reallocate(left, length, length).(Indexable)
})
if i != nil {
f(i)
}
}
var s Indexable
switch l, r := left.Len(), right.Len(); {
case l == r:
newSlice(l, func(s Indexable) {
for i := 0; i < l; i++ {
s.Set(i, f(left.At(i), right.At(i)))
}
})
case l > r:
newSlice(l, func(s Indexable) {
for i := 0; i < r; i++ {
s.Set(i, f(left.At(i), right.At(i)))
}
blank := reflect.Zero(reflect.ValueOf(left).Type().Elem()).Interface()
for i := r; i < l; i++ {
s.Set(i, f(left.At(i), blank))
}
})
case l < r:
newSlice(l, func(s Indexable) {
for i := 0; i < l; i++ {
s.Set(i, f(left.At(i), right.At(i)))
}
blank := reflect.Zero(reflect.ValueOf(left).Type().Elem()).Interface()
for i := l; i < r; i++ {
s.Set(i, f(blank, right.At(i)))
}
})
}
result = s
default:
switch right := reflect.ValueOf(right); right.Kind() {
case reflect.Slice:
newSlice := func(length int, f func(reflect.Value)) {
var v reflect.Value
jittery.CatchAll(func() {
Reallocate(left, length, length)
v = reflect.ValueOf(left)
})
if v.IsValid() {
f(v)
}
}
var s reflect.Value
CombineAndSet := func(i int, l interface{}, r reflect.Value) {
s.Index(i).Set(reflect.ValueOf(f(l, r.Interface())))
}
switch l, r := left.Len(), right.Len(); {
case l == r:
newSlice(l, func(s reflect.Value) {
for i := 0; i < l; i++ {
CombineAndSet(i, left.At(i), right.Index(i))
}
})
case l > r:
newSlice(l, func(s reflect.Value) {
for i := 0; i < r; i++ {
CombineAndSet(i, left.At(i), right.Index(i))
}
for i := r; i < l; i++ {
CombineAndSet(i, left.At(i), reflect.Value{})
}
})
case l < r:
newSlice(l, func(s reflect.Value) {
for i := 0; i < l; i++ {
CombineAndSet(i, left.At(i), right.Index(i))
}
for i := l; i < r; i++ {
CombineAndSet(i, nil, right.Index(i))
}
})
}
result = s.Interface()
case reflect.Map:
m := reflect.MakeMap(right.Type())
CombineAndSet := func(i, l interface{}, r reflect.Value) {
m.SetMapIndex(reflect.ValueOf(i), reflect.ValueOf(f(l, r.Interface())))
}
for i := left.Len() - 1; i > 0; i-- {
CombineAndSet(i, left.At(i), right.MapIndex(reflect.ValueOf(i)))
}
for iter := right.MapRange(); iter.Next(); {
i := int(iter.Key().Int())
if left.At(i) == nil {
CombineAndSet(i, left.At(i), iter.Value())
}
}
result = m.Interface()
}
}
return
}
func combineValue(Left, Right interface{}, f func(interface{}, interface{}) interface{}) (result interface{}) {
left := reflect.ValueOf(Left)
blank := reflect.Zero(left.Type().Elem())
switch left.Kind() {
case reflect.Slice:
switch right := Right.(type) {
case Indexable:
makeSlice := func(length int) (r reflect.Value) {
jittery.CatchAll(func() {
Reallocate(left, length, length)
r = reflect.ValueOf(left)
})
return
}
var s reflect.Value
CombineAndSet := func(i int, l reflect.Value, r interface{}) {
s.Index(i).Set(reflect.ValueOf(f(l.Interface(), r)))
}
switch l, r := left.Len(), right.Len(); {
case l == r:
if s = makeSlice(l); s.IsValid() {
for i := 0; i < l; i++ {
CombineAndSet(i, left.Index(i), right.At(i))
}
}
case l > r:
if s = makeSlice(l); s.IsValid() {
for i := 0; i < r; i++ {
CombineAndSet(i, left.Index(i), right.At(i))
}
for i := r; i < l; i++ {
CombineAndSet(i, left.Index(i), blank)
}
}
case l < r:
if s = makeSlice(r); s.IsValid() {
for i := 0; i < l; i++ {
CombineAndSet(i, left.Index(i), right.At(i))
}
for i := l; i < r; i++ {
CombineAndSet(i, blank, right.At(i))
}
}
}
result = s.Interface()
default:
switch right := reflect.ValueOf(right); right.Kind() {
case reflect.Slice:
makeSlice := func(length int) reflect.Value {
return reflect.MakeSlice(left.Type(), length, length)
}
var s reflect.Value
CombineAndSet := func(i int, l, r reflect.Value) {
s.Index(i).Set(reflect.ValueOf(f(l.Interface(), r.Interface())))
}
switch l, r := left.Len(), right.Len(); {
case l == r:
if s = makeSlice(l); s.IsValid() {
for i := 0; i < l; i++ {
CombineAndSet(i, left.Index(i), right.Index(i))
}
}
case l > r:
if s = makeSlice(l); s.IsValid() {
for i := 0; i < r; i++ {
CombineAndSet(i, left.Index(i), right.Index(i))
}
for i := r; i < l; i++ {
CombineAndSet(i, left.Index(i), blank)
}
}
case l < r:
if s = makeSlice(r); s.IsValid() {
for i := 0; i < l; i++ {
CombineAndSet(i, left.Index(i), right.Index(i))
}
for i := l; i < r; i++ {
CombineAndSet(i, blank, right.Index(i))
}
}
}
result = s.Interface()
case reflect.Map:
if map_type := right.Type(); map_type.Key().Kind() == reflect.Int {
n := reflect.MakeMap(map_type)
for i := 0; i < left.Len(); i++ {
n.SetMapIndex(reflect.ValueOf(i), left.Index(i))
}
result = combineValue(n.Interface(), Right, f)
}
}
}
case reflect.Map:
switch right := Right.(type) {
/* case Map:
m := reflect.MakeMap(left.Type())
CombineAndSet := func(k interface{}, l reflect.Value, r interface{}) {
m.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(f(l.Interface(), r)))
}
right.EachWithKey(func(k, r interface{}) {
if l := left.MapIndex(reflect.ValueOf(k)); l.IsValid() {
CombineAndSet(k, l, r)
}
})
*/
case Indexable:
m := reflect.MakeMap(left.Type())
CombineAndSet := func(i int, l reflect.Value, r interface{}) {
m.SetMapIndex(reflect.ValueOf(i), reflect.ValueOf(f(l.Interface(), r)))
}
for i := left.Len() - 1; i > 0; i-- {
CombineAndSet(i, left.MapIndex(reflect.ValueOf(i)), right.At(i))
}
for i := right.Len() - 1; i > 0; i-- {
k := reflect.ValueOf(i)
if !m.MapIndex(k).IsValid() {
CombineAndSet(i, left.MapIndex(k), right.At(i))
}
}
result = m.Interface()
default:
switch right := reflect.ValueOf(right); right.Kind() {
case reflect.Map:
m := reflect.MakeMap(left.Type())
CombineAndSet := func(k reflect.Value) {
lv := left.MapIndex(k)
rv := right.MapIndex(k)
var x interface{}
if lv.IsValid() {
if rv.IsValid() {
x = f(lv.Interface(), rv.Interface())
} else {
x = f(lv.Interface(), blank.Interface())
}
} else {
x = f(blank.Interface(), rv.Interface())
}
m.SetMapIndex(k, reflect.ValueOf(x))
}
for _, k := range left.MapKeys() {
CombineAndSet(k)
}
for _, k := range right.MapKeys() {
CombineAndSet(k)
}
result = m.Interface()
case reflect.Slice:
if map_type := left.Type(); map_type.Key().Kind() == reflect.Int {
n := reflect.MakeMap(map_type)
for i := 0; i < right.Len(); i++ {
n.SetMapIndex(reflect.ValueOf(i), right.Index(i))
}
result = combineValue(Left, n.Interface(), f)
}
}
}
}
return
}