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 pathflip.go
262 lines (249 loc) · 4.91 KB
/
flip.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
package goal
// flip returns +x.
func flip(x V) V {
if xv, ok := x.bv.(*D); ok {
return NewV(&D{keys: xv.values, values: xv.keys})
}
x = toArray(x)
switch xv := x.bv.(type) {
case *AV:
if xv.Len() == 0 {
x.MarkImmutable()
return newAVu([]V{x})
}
l := -1
for _, xi := range xv.elts {
xia, ok := xi.bv.(Array)
if !ok {
x = extendAV(xv)
return flip(x)
}
nl := xia.Len()
switch {
case l == -1:
l = nl
case nl != l:
x = extendAV(xv)
return flip(x)
}
}
t := eType(xv)
switch {
case l == 0:
x.MarkImmutable()
return newAVu([]V{x})
case l == 1:
switch t {
case tAb:
return newAVu([]V{flipAB(xv, true)})
case tAB:
return newAVu([]V{flipAB(xv, false)})
case tAF:
return newAVu([]V{flipAF(xv)})
case tAI:
return newAVu([]V{flipAI(xv)})
case tAS:
return newAVu([]V{flipAS(xv)})
default:
return newAVu([]V{flipAV(xv)})
}
default:
switch t {
case tAb:
return flipAVAB(xv, l, true)
case tAB:
return flipAVAB(xv, l, false)
case tAF:
return flipAVAF(xv, l)
case tAI:
return flipAVAI(xv, l)
case tAS:
return flipAVAS(xv, l)
default:
return flipAVAV(xv, l)
}
}
default:
x.MarkImmutable()
return newAVu([]V{x})
}
}
func extendAV(x *AV) V {
n := 0
for _, xi := range x.elts {
xl := xi.Len()
if n < xl {
n = xl
}
}
r := x.reuse()
for i, xi := range x.elts {
xia, ok := xi.bv.(Array)
if ok {
if xia.Len() == n {
r.elts[i] = xi
continue
}
ri := takeN(int64(n), xia)
ri.MarkImmutable()
r.elts[i] = ri
continue
}
ri := takeNAtom(int64(n), xi)
ri.MarkImmutable()
r.elts[i] = ri
}
return NewV(r)
}
// getAB retrieves the *getAB value. It assumes Value type is *getAB.
func (x V) getAB() *AB {
return x.bv.(*AB)
}
func flipAB(x *AV, b bool) V {
r := make([]byte, x.Len())
for i, xi := range x.elts {
r[i] = xi.getAB().At(0)
}
var fl flags
if b {
fl |= flagBool
}
return NewV(&AB{elts: r, flags: fl | flagImmutable})
}
func flipAVAB(x *AV, lines int, b bool) V {
r := make([]V, lines)
a := make([]byte, lines*x.Len())
xlen := x.Len()
for i, xi := range x.elts {
xia := xi.getAB()
for j := 0; j < lines; j++ {
a[i+j*xlen] = xia.At(j)
}
}
var fl flags
if b {
fl |= flagBool
}
for j := range r {
r[j] = NewV(&AB{elts: a[j*xlen : (j+1)*xlen], flags: fl | flagImmutable})
}
return newAVu(r)
}
func flipAF(x *AV) V {
r := make([]float64, x.Len())
for i, xi := range x.elts {
switch xiv := xi.bv.(type) {
case *AB:
r[i] = float64(xiv.At(0))
case *AF:
r[i] = xiv.At(0)
case *AI:
r[i] = float64(xiv.At(0))
}
}
return NewV(&AF{elts: r, flags: flagImmutable})
}
func flipAVAF(x *AV, lines int) V {
r := make([]V, lines)
a := make([]float64, lines*x.Len())
xlen := x.Len()
for i, xi := range x.elts {
switch xiv := xi.bv.(type) {
case *AB:
for j := 0; j < lines; j++ {
a[i+j*xlen] = float64(xiv.At(j))
}
case *AF:
for j := 0; j < lines; j++ {
a[i+j*xlen] = xiv.At(j)
}
case *AI:
for j := 0; j < lines; j++ {
a[i+j*xlen] = float64(xiv.At(j))
}
}
}
for j := range r {
r[j] = NewV(&AF{elts: a[j*xlen : (j+1)*xlen], flags: flagImmutable})
}
return newAVu(r)
}
func flipAI(x *AV) V {
r := make([]int64, x.Len())
for i, xi := range x.elts {
switch xiv := xi.bv.(type) {
case *AB:
r[i] = int64(xiv.At(0))
case *AI:
r[i] = xiv.At(0)
}
}
return NewV(&AI{elts: r, flags: flagImmutable})
}
func flipAVAI(x *AV, lines int) V {
r := make([]V, lines)
a := make([]int64, lines*x.Len())
xlen := x.Len()
for i, xi := range x.elts {
switch xiv := xi.bv.(type) {
case *AB:
for j := 0; j < lines; j++ {
a[i+j*xlen] = int64(xiv.At(j))
}
case *AI:
for j := 0; j < lines; j++ {
a[i+j*xlen] = xiv.At(j)
}
}
}
for j := range r {
r[j] = NewV(&AI{elts: a[j*xlen : (j+1)*xlen], flags: flagImmutable})
}
return newAVu(r)
}
func flipAS(x *AV) V {
r := make([]string, x.Len())
for i, xi := range x.elts {
xiv := xi.bv.(*AS)
r[i] = xiv.At(0)
}
return NewV(&AS{elts: r, flags: flagImmutable})
}
func flipAVAS(x *AV, lines int) V {
r := make([]V, lines)
a := make([]string, lines*x.Len())
xlen := x.Len()
for i, xi := range x.elts {
xiv := xi.bv.(*AS)
for j := 0; j < lines; j++ {
a[i+j*xlen] = xiv.At(j)
}
}
for j := range r {
r[j] = NewV(&AS{elts: a[j*xlen : (j+1)*xlen], flags: flagImmutable})
}
return newAVu(r)
}
func flipAV(x *AV) V {
r := make([]V, x.Len())
for i, xi := range x.elts {
xiv := xi.bv.(Array)
r[i] = xiv.VAt(0)
}
return canonicalAVImmut(&AV{elts: r, flags: flagImmutable})
}
func flipAVAV(x *AV, lines int) V {
r := make([]V, lines)
a := make([]V, lines*x.Len())
xlen := x.Len()
for i, xi := range x.elts {
xiv := xi.bv.(Array)
for j := 0; j < lines; j++ {
a[i+j*xlen] = xiv.VAt(j)
}
}
for j := range r {
r[j] = canonicalAVImmut(&AV{elts: a[j*xlen : (j+1)*xlen], flags: flagImmutable})
}
return newAVu(r)
}