This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
444 lines (422 loc) · 9.54 KB
/
group.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package goal
// icountGroup returns =x.
func icountGroup(x V) V {
switch xv := x.bv.(type) {
case S:
return NewAS(lineSplit(string(xv)))
case *AB:
if xv.Len() == 0 {
return newABb(nil)
}
if xv.IsBoolean() {
n := sumIs(xv.elts)
if n == 0 {
if xv.Len() < 256 {
return NewAB([]byte{byte(xv.Len())})
}
return NewAI([]int64{int64(xv.Len())})
}
if xv.Len() < 256 {
return NewAB([]byte{byte(int64(xv.Len()) - n), byte(n)})
}
return NewAI([]int64{int64(xv.Len()) - n, n})
}
if xv.Len() < 256 {
return NewAB(icountBytes[byte](xv.elts))
}
return NewAI(icountBytes[int64](xv.elts))
case *AI:
if xv.Len() == 0 {
return newABb(nil)
}
if xv.Len() < 256 {
return NewAB(icountIs[byte](xv.elts))
}
return NewAI(icountIs[int64](xv.elts))
case *AF:
x = toAI(xv)
if x.IsPanic() {
return ppanic("=x : ", x)
}
return icountGroup(x)
case *AS:
r := make([]V, xv.Len())
for i, xi := range xv.elts {
r[i] = NewV(&AS{elts: lineSplit(xi), flags: flagImmutable})
}
return newAVu(r)
case *D:
return groupBy(NewV(xv.values), NewV(xv.keys))
case *AV:
return mapAV(xv, icountGroup)
default:
return panicType("=x", "x", x)
}
}
func icountIs[I integer](x []int64) []I {
max := maxIs(x)
if max < 0 {
max = -1
}
icounts := make([]I, max+1)
for _, xi := range x {
if xi >= 0 {
icounts[xi]++
}
}
return icounts
}
func icountBytes[I integer](x []byte) []I {
max := int(maxBytes(x))
icounts := make([]I, max+1)
for _, xi := range x {
icounts[xi]++
}
return icounts
}
// groupBy by returns {x}=y.
func groupBy(x, y V) V {
xlen := x.Len()
if xlen != y.Len() {
return Panicf("f=Y : length mismatch for f[Y] and Y: %d vs %d ",
x.Len(), y.Len())
}
if xlen == 0 {
return protoAV()
}
switch xv := x.bv.(type) {
case *AB:
if xv.IsBoolean() {
return groupByBoolsV(xv.elts, y)
}
max := maxBytes(xv.elts)
if ascending(xv) {
switch yv := y.bv.(type) {
case Array:
return groupBySorted(xv.elts, yv, int64(max))
default:
return panicType("f=Y", "Y", y)
}
}
switch yv := y.bv.(type) {
case *AB:
return groupByBytesBytes(xv.elts, yv.elts, max, yv.IsBoolean())
case *AI:
return groupByBytesInt64s(xv.elts, yv.elts, max)
case *AF:
return groupByBytesFloat64s(xv.elts, yv.elts, max)
case *AS:
return groupByBytesStrings(xv.elts, yv.elts, max)
case *AV:
return groupByBytesVs(xv.elts, yv.elts, max)
default:
return panicType("f=Y", "Y", y)
}
case *AI:
max := maxIs(xv.elts)
if max < 0 {
return protoAV()
}
if ascending(xv) {
switch yv := y.bv.(type) {
case Array:
return groupBySorted(xv.elts, yv, int64(max))
default:
return panicType("f=Y", "Y", y)
}
}
switch yv := y.bv.(type) {
case *AB:
return groupByInt64sBytes(xv.elts, yv.elts, max, yv.IsBoolean())
case *AI:
return groupByInt64sInt64s(xv.elts, yv.elts, max)
case *AF:
return groupByInt64sFloat64s(xv.elts, yv.elts, max)
case *AS:
return groupByInt64sStrings(xv.elts, yv.elts, max)
case *AV:
return groupByInt64sVs(xv.elts, yv.elts, max)
default:
return panicType("f=Y", "Y", y)
}
case *AF:
ix := toAI(xv)
if ix.IsPanic() {
return ppanic("f=x : f[Y]", ix)
}
return groupBy(ix, y)
default:
return panicType("f=Y", "f[Y]", x)
}
}
func groupByBoolsV(x []byte, y V) V {
n := int(sumIs(x))
r := make([]V, int(b2I(n > 0)+1))
switch yv := y.bv.(type) {
case *AB:
if n == 0 {
y.MarkImmutable()
r[0] = y
return newAVu(r)
}
rf, rt := groupByBools[byte](x, yv.elts, n)
r[0] = NewV(&AB{elts: rf, flags: flagImmutable})
r[1] = NewV(&AB{elts: rt, flags: flagImmutable})
return newAVu(r)
case *AI:
if n == 0 {
y.MarkImmutable()
r[0] = y
return newAVu(r)
}
rf, rt := groupByBools[int64](x, yv.elts, n)
r[0] = NewV(&AI{elts: rf, flags: flagImmutable})
r[1] = NewV(&AI{elts: rt, flags: flagImmutable})
return newAVu(r)
case *AF:
if n == 0 {
y.MarkImmutable()
r[0] = y
return newAVu(r)
}
rf, rt := groupByBools[float64](x, yv.elts, n)
r[0] = NewV(&AF{elts: rf, flags: flagImmutable})
r[1] = NewV(&AF{elts: rt, flags: flagImmutable})
return newAVu(r)
case *AS:
if n == 0 {
y.MarkImmutable()
r[0] = y
return newAVu(r)
}
rf, rt := groupByBools[string](x, yv.elts, n)
r[0] = NewV(&AS{elts: rf, flags: flagImmutable})
r[1] = NewV(&AS{elts: rt, flags: flagImmutable})
return newAVu(r)
case *AV:
if n == 0 {
y.MarkImmutable()
r[0] = y
return newAVu(r)
}
rf, rt := groupByBools[V](x, yv.elts, n)
r[0] = canonicalVs(rf)
r[0].MarkImmutable()
r[1] = canonicalVs(rt)
r[1].MarkImmutable()
return newAVu(r)
default:
return panicType("f=Y", "Y", y)
}
}
func groupByBools[T any](x []byte, y []T, n int) (rf, rt []T) {
r := make([]T, len(x))
rf = r[:len(r)-n]
rt = r[len(r)-n:]
var offset [2]int
offset[1] = len(r) - n
for i, xi := range x {
j := offset[xi]
offset[xi]++
r[j] = y[i]
}
return rf, rt
}
func groupByBytesBytes(x []byte, y []byte, max byte, b bool) V {
r, offset, yg := groupByPrepareBytes[byte](x, max)
var fl flags
if b {
fl = flagBool
}
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AB{elts: yg[count : count+n], flags: fl | flagImmutable})
count += n
}
groupByScatterBytes[byte](x, y, yg, offset)
return newAVu(r)
}
func groupByBytesInt64s(x []byte, y []int64, max byte) V {
r, offset, yg := groupByPrepareBytes[int64](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AI{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatterBytes[int64](x, y, yg, offset)
return newAVu(r)
}
func groupByBytesFloat64s(x []byte, y []float64, max byte) V {
r, offset, yg := groupByPrepareBytes[float64](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AF{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatterBytes[float64](x, y, yg, offset)
return newAVu(r)
}
func groupByBytesStrings(x []byte, y []string, max byte) V {
r, offset, yg := groupByPrepareBytes[string](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AS{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatterBytes[string](x, y, yg, offset)
return newAVu(r)
}
func groupByBytesVs(x []byte, y []V, max byte) V {
r, offset, yg := groupByPrepareBytes[V](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AV{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatterBytes[V](x, y, yg, offset)
for i, ri := range r {
r[i] = canonicalImmut(ri)
}
return newAVu(r)
}
func groupByPrepareBytes[T any](x []byte, max byte) ([]V, []int, []T) {
l := int(max) + 1
r := make([]V, l)
offset := make([]int, l)
count := 0
for _, xi := range x {
count++
offset[xi]++
}
yg := make([]T, count)
return r, offset, yg
}
func groupByScatterBytes[T any](x []byte, y []T, yg []T, offset []int) {
for i, xi := range x {
j := offset[xi]
offset[xi]++
yg[j] = y[i]
}
}
func groupByInt64sBytes(x []int64, y []byte, max int64, b bool) V {
r, offset, yg := groupByPrepare[byte](x, max)
var fl flags
if b {
fl = flagBool
}
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AB{elts: yg[count : count+n], flags: fl | flagImmutable})
count += n
}
groupByScatter[byte](x, y, yg, offset)
return newAVu(r)
}
func groupByInt64sInt64s(x []int64, y []int64, max int64) V {
r, offset, yg := groupByPrepare[int64](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AI{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatter[int64](x, y, yg, offset)
return newAVu(r)
}
func groupByInt64sFloat64s(x []int64, y []float64, max int64) V {
r, offset, yg := groupByPrepare[float64](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AF{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatter[float64](x, y, yg, offset)
return newAVu(r)
}
func groupByInt64sStrings(x []int64, y []string, max int64) V {
r, offset, yg := groupByPrepare[string](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AS{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatter[string](x, y, yg, offset)
return newAVu(r)
}
func groupByInt64sVs(x []int64, y []V, max int64) V {
r, offset, yg := groupByPrepare[V](x, max)
count := 0
for i, n := range offset {
offset[i] = count
r[i] = NewV(&AV{elts: yg[count : count+n], flags: flagImmutable})
count += n
}
groupByScatter[V](x, y, yg, offset)
for i, ri := range r {
r[i] = canonicalImmut(ri)
}
return newAVu(r)
}
func groupByPrepare[T any](x []int64, max int64) ([]V, []int, []T) {
r := make([]V, max+1)
offset := make([]int, max+1)
count := 0
for _, xi := range x {
if xi < 0 {
continue
}
count++
offset[xi]++
}
yg := make([]T, count)
return r, offset, yg
}
func groupByScatter[T any](x []int64, y []T, yg []T, offset []int) {
for i, xi := range x {
if xi < 0 {
continue
}
j := offset[xi]
offset[xi]++
yg[j] = y[i]
}
}
func groupBySorted[I integer](x []I, y Array, max int64) V {
r := make([]V, max+1)
var from, i0 int
var n int64
for i, xi := range x {
if xi >= 0 {
i0 = i
break
}
}
from = i0
var p V
y.MarkImmutable()
for i, xi := range x[i0:] {
if int64(xi) == n {
continue
}
r[n] = NewV(y.slice(from, i+i0))
from = i + i0
n++
for n < int64(xi) {
if p.kind == valNil {
p = NewV(&AV{flags: flagImmutable})
}
r[n] = p
n++
}
}
r[n] = NewV(y.slice(from, len(x)))
return newAVu(r)
}