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 pathjoin.go
489 lines (461 loc) · 9.09 KB
/
join.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package goal
// join returns x,y.
func join(x, y V) V {
if x.IsI() {
return joinI(x.I(), y)
}
if x.IsF() {
return joinF(x.F(), y)
}
switch xv := x.bv.(type) {
case S:
return joinS(xv, y)
case *AB:
return joinAB(xv, y, false)
case *AF:
return joinAF(xv, y, false)
case *AI:
return joinAI(xv, y, false)
case *AS:
return joinAS(xv, y, false)
case *AV:
return joinAV(xv, y, false)
case *D:
switch yv := y.bv.(type) {
case *D:
return dictMerge(xv, yv)
case Array:
return joinAtomToArray(x, yv, true)
default:
return newAVv([]V{x, y})
}
default:
switch yv := y.bv.(type) {
case Array:
return joinAtomToArray(x, yv, true)
default:
return newAVv([]V{x, y})
}
}
}
func joinI(x int64, y V) V {
if y.IsI() {
return NewAI([]int64{int64(x), y.I()})
}
if y.IsF() {
return NewAF([]float64{float64(x), float64(y.F())})
}
const left = true
switch yv := y.bv.(type) {
case S:
return newAVv([]V{NewI(x), y})
case *AB:
return joinAB(yv, NewI(x), left)
case *AF:
return joinAF(yv, NewI(x), left)
case *AI:
return joinAI(yv, NewI(x), left)
case *AS:
return joinAS(yv, NewI(x), left)
case *AV:
return joinAV(yv, NewI(x), left)
default:
return newAVv([]V{NewI(x), y})
}
}
func joinF(x float64, y V) V {
if y.IsI() {
return NewAF([]float64{float64(x), float64(y.I())})
}
if y.IsF() {
return NewAF([]float64{float64(x), float64(y.F())})
}
const left = true
switch yv := y.bv.(type) {
case S:
return newAVv([]V{NewF(x), y})
case *AB:
return joinAB(yv, NewF(x), left)
case *AF:
return joinAF(yv, NewF(x), left)
case *AI:
return joinAI(yv, NewF(x), left)
case *AS:
return joinAS(yv, NewF(x), left)
case *AV:
return joinAV(yv, NewF(x), left)
default:
return newAVv([]V{NewF(x), y})
}
}
func joinS(x S, y V) V {
if y.IsI() {
return newAVv([]V{NewV(x), y})
}
if y.IsF() {
return newAVv([]V{NewV(x), y})
}
const left = true
switch yv := y.bv.(type) {
case S:
return NewAS([]string{string(x), string(yv)})
case *AB:
return joinAB(yv, NewV(x), left)
case *AF:
return joinAF(yv, NewV(x), left)
case *AI:
return joinAI(yv, NewV(x), left)
case *AS:
return joinAS(yv, NewV(x), left)
case *AV:
return joinAV(yv, NewV(x), left)
default:
return newAVv([]V{NewV(x), y})
}
}
func joinAB(x *AB, y V, left bool) V {
if y.IsI() {
if isBI(y.I()) {
var fl flags
b := x.IsBoolean() && isbI(y.I())
if b {
fl = flagBool
}
if left {
return NewV(&AB{elts: joinSliceLeft(x.elts, byte(y.I())), flags: fl})
}
if x.reusable() {
x.elts = append(x.elts, byte(y.I()))
x.flags = fl
return NewV(x)
}
return NewV(&AB{elts: joinSlice(x.elts, byte(y.I())), flags: fl})
}
return NewAI(joinIsN(x.elts, y.I(), left))
}
if y.IsF() {
return NewAF(joinIsN(x.elts, y.F(), left))
}
switch yv := y.bv.(type) {
case *AB:
// left == false
return joinABAB(x, yv)
case *AI:
// left == false
return joinABAI(x, yv)
case *AF:
// left == false
return joinABAF(x, yv)
case Array:
// left == false
return joinArrays(x, yv)
default:
return joinAtomToArray(y, x, left)
}
}
func joinIsN[I integer, N number](x []I, y N, left bool) []N {
r := make([]N, len(x)+1)
if left {
r[0] = y
for i := 1; i < len(r); i++ {
r[i] = N(x[i-1])
}
} else {
r[len(r)-1] = y
for i := 0; i < len(r)-1; i++ {
r[i] = N(x[i])
}
}
return r
}
func joinSlice[T any](x []T, y T) []T {
r := make([]T, len(x)+1)
r[len(r)-1] = y
copy(r[:len(r)-1], x)
return r
}
func joinSliceLeft[T any](x []T, y T) []T {
r := make([]T, len(x)+1)
r[0] = y
copy(r[1:], x)
return r
}
func joinAI(x *AI, y V, left bool) V {
if y.IsI() {
if left {
return NewAI(joinSliceLeft(x.elts, y.I()))
}
if x.reusable() {
x.elts = append(x.elts, y.I())
x.flags = flagNone
return NewV(x)
}
return NewAI(joinSlice(x.elts, y.I()))
}
if y.IsF() {
return NewAF(joinIsN(x.elts, y.F(), left))
}
switch yv := y.bv.(type) {
case *AB:
// left == false
return joinAIAB(x, yv)
case *AI:
// left == false
return joinAIAI(x, yv)
case *AF:
// left == false
return joinAIAF(x, yv)
case Array:
// left == false
return joinArrays(x, yv)
default:
return joinAtomToArray(y, x, left)
}
}
func joinAF(x *AF, y V, left bool) V {
if y.IsI() {
if left {
return NewAF(joinSliceLeft(x.elts, float64(y.I())))
}
if x.reusable() {
x.elts = append(x.elts, float64(y.I()))
x.flags = flagNone
return NewV(x)
}
return NewAF(joinSlice(x.elts, float64(y.I())))
}
if y.IsF() {
if left {
return NewAF(joinSliceLeft(x.elts, y.F()))
}
if x.reusable() {
x.elts = append(x.elts, y.F())
x.flags = flagNone
return NewV(x)
}
return NewAF(joinSlice(x.elts, y.F()))
}
switch yv := y.bv.(type) {
case *AB:
// left == false
return joinAFAB(x, yv)
case *AI:
// left == false
return joinAFAI(x, yv)
case *AF:
// left == false
return joinAFAF(x, yv)
case Array:
// left == false
return joinArrays(x, yv)
default:
return joinAtomToArray(y, x, left)
}
}
func joinABAB(x *AB, y *AB) V {
b := x.IsBoolean() && y.IsBoolean()
var fl flags
if b {
fl = flagBool
}
if x.reusable() {
x.elts = append(x.elts, y.elts...)
x.flags = fl
return NewV(x)
}
return NewV(&AB{elts: joinSlices(x.elts, y.elts), flags: fl})
}
func joinSlices[T any](x, y []T) []T {
r := make([]T, len(x)+len(y))
copy(r[:len(x)], x)
copy(r[len(x):], y)
return r
}
func joinAIAI(x *AI, y *AI) V {
if x.reusable() {
x.elts = append(x.elts, y.elts...)
x.flags = flagNone
return NewV(x)
}
return NewAI(joinSlices(x.elts, y.elts))
}
func joinAFAF(x *AF, y *AF) V {
if x.reusable() {
x.elts = append(x.elts, y.elts...)
x.flags = flagNone
return NewV(x)
}
return NewAF(joinSlices(x.elts, y.elts))
}
func joinABAI(x *AB, y *AI) V {
return NewAI(joinSliceToNums(x.elts, y.elts))
}
func joinSliceToNums[N number, M number](x []N, y []M) []M {
r := make([]M, len(x)+len(y))
for i := 0; i < len(x); i++ {
r[i] = M(x[i])
}
copy(r[len(x):], y)
return r
}
func joinAIAB(x *AI, y *AB) V {
if x.reusable() {
for _, yi := range y.elts {
x.elts = append(x.elts, int64(yi))
}
x.flags = flagNone
return NewV(x)
}
return NewAI(joinNumsToSlice(x.elts, y.elts))
}
func joinNumsToSlice[N number, M number](x []N, y []M) []N {
r := make([]N, len(x)+len(y))
copy(r[:len(x)], x)
for i := len(x); i < len(r); i++ {
r[i] = N(y[i-len(x)])
}
return r
}
func joinABAF(x *AB, y *AF) V {
return NewAF(joinSliceToNums(x.elts, y.elts))
}
func joinAFAB(x *AF, y *AB) V {
if x.reusable() {
for _, yi := range y.elts {
x.elts = append(x.elts, float64(yi))
}
x.flags = flagNone
return NewV(x)
}
return NewAF(joinNumsToSlice(x.elts, y.elts))
}
func joinAIAF(x *AI, y *AF) V {
return NewAF(joinSliceToNums(x.elts, y.elts))
}
func joinAFAI(x *AF, y *AI) V {
if x.reusable() {
for _, yi := range y.elts {
x.elts = append(x.elts, float64(yi))
}
x.flags = flagNone
return NewV(x)
}
return NewAF(joinNumsToSlice(x.elts, y.elts))
}
func joinAS(x *AS, y V, left bool) V {
switch yv := y.bv.(type) {
case S:
if left {
return NewAS(joinSliceLeft(x.elts, string(yv)))
}
if x.reusable() {
x.elts = append(x.elts, string(yv))
x.flags = flagNone
return NewV(x)
}
return NewAS(joinSlice(x.elts, string(yv)))
case *AS:
// left == false
if x.reusable() {
x.elts = append(x.elts, yv.elts...)
x.flags = flagNone
return NewV(x)
}
return NewAS(joinSlices(x.elts, yv.elts))
case Array:
// left == false
return joinArrays(x, yv)
default:
return joinAtomToArray(y, x, left)
}
}
func joinAV(x *AV, y V, left bool) V {
switch yv := y.bv.(type) {
case *AV:
// left == false
if x.reusable() {
x.elts = append(x.elts, yv.elts...)
x.flags = flagNone
return NewV(x)
}
return joinArrays(x, yv)
case Array:
// left == false
return joinArrays(x, yv)
default:
if x.Len() == 0 {
return toArray(y)
}
y.MarkImmutable()
if left {
return NewV(&AV{elts: joinSliceLeft(x.elts, y)})
}
if x.reusable() {
x.elts = append(x.elts, y)
x.flags = flagNone
return NewV(x)
}
return NewV(&AV{elts: joinSlice(x.elts, y)})
}
}
func joinArrays(x, y Array) V {
if y.Len() == 0 {
return NewV(x)
}
if x.Len() == 0 {
return NewV(y)
}
r := make([]V, y.Len()+x.Len())
for i := 0; i < x.Len(); i++ {
r[i] = x.VAt(i)
}
for i := x.Len(); i < len(r); i++ {
r[i] = y.VAt(i - x.Len())
}
return NewV(&AV{elts: r})
}
func joinAtomToArray(x V, y Array, left bool) V {
yv, ok := y.(*AV)
if ok {
return joinAV(yv, x, left)
}
r := make([]V, y.Len()+1)
if left {
r[0] = x
x.MarkImmutable()
for i := 1; i < len(r); i++ {
r[i] = y.VAt(i - 1)
}
} else {
r[len(r)-1] = x
x.MarkImmutable()
for i := 0; i < len(r)-1; i++ {
r[i] = y.VAt(i)
}
}
return NewV(&AV{elts: r})
}
// enlist returns ,x.
func enlist(x V) V {
if x.IsI() {
if isBI(x.I()) {
b := isbI(x.I())
var fl flags
if b {
fl = flagBool
}
return NewV(&AB{elts: []byte{byte(x.I())}, flags: fl})
}
return NewAI([]int64{x.I()})
}
if x.IsF() {
return NewAF([]float64{x.F()})
}
switch xv := x.bv.(type) {
case S:
return NewAS([]string{string(xv)})
default:
x.MarkImmutable()
return newAVu([]V{x})
}
}