-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray.go
More file actions
394 lines (360 loc) · 9.15 KB
/
Copy patharray.go
File metadata and controls
394 lines (360 loc) · 9.15 KB
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
// Copyright (c) 2022, Peter Ohler, All rights reserved.
package slip
import (
"fmt"
)
// ArraySymbol is the symbol with a value of "array".
const (
ArraySymbol = Symbol("array")
ArrayMaxRank = 1024
ArrayMaxDimension = 0x10000000
)
// Array is an n dimensional collection of Objects.
type Array struct {
dims []int
sizes []int
elements []Object
elementType Symbol // defaults to TrueSymbol which is empty string
adjustable bool
}
// NewArray creates a new array with the specified dimensions and initial
// value.
func NewArray(
dimensions []int,
elementType Symbol,
initElement Object,
initContent List,
adjustable bool) *Array {
a := Array{
dims: dimensions,
sizes: make([]int, len(dimensions)),
elementType: elementType,
adjustable: adjustable,
}
size := 1
for i := len(dimensions) - 1; 0 <= i; i-- {
a.sizes[i] = size
if ArrayMaxDimension < dimensions[i] {
TypePanic(NewScope(), 0, "dimension", Fixnum(dimensions[i]),
fmt.Sprintf("positive fixnum less than %d", ArrayMaxDimension))
}
size *= dimensions[i]
}
a.elements = make([]Object, size)
if initContent != nil {
a.SetAll(initContent)
} else if initElement != nil {
for i := len(a.elements) - 1; 0 <= i; i-- {
a.elements[i] = initElement
}
}
return &a
}
// ArrayType returns 'array.
func (obj *Array) ArrayType() Symbol {
return ArraySymbol
}
// Assumes dims and size arrays already allocated. Used by the reader.
func (obj *Array) calcAndSet(list List) {
if 0 < len(obj.dims) {
orig := list
var ok bool
size := 1
for i := 0; i < len(obj.dims); i++ {
obj.dims[i] = len(list)
obj.sizes[i] = size
size *= len(list)
if i < len(obj.dims)-1 {
if list, ok = list[0].(List); !ok {
ErrorPanic(NewScope(), 0, "Invalid data for a %d dimension array. %s", len(obj.dims), list)
}
}
}
obj.elements = make([]Object, size)
obj.setDim(orig, 0, 0)
}
}
// String representation of the Object.
func (obj *Array) String() string {
return string(obj.Append([]byte{}))
}
// Append a buffer with a representation of the Object.
func (obj *Array) Append(b []byte) []byte {
return printer.Append(b, obj, 0)
}
// Simplify the Object into set of nested slices.
func (obj *Array) Simplify() any {
list, _ := obj.simplifyDim(0, 0)
return list
}
func (obj *Array) simplifyDim(di, ei int) ([]any, int) {
d := obj.dims[di]
list := make([]any, d)
if di == len(obj.dims)-1 {
for i := 0; i < d; i++ {
e := obj.elements[ei]
if e == nil {
list[i] = nil
} else {
list[i] = e.Simplify()
}
ei++
}
} else {
d2 := di + 1
for i := 0; i < d; i++ {
list[i], ei = obj.simplifyDim(d2, ei)
}
}
return list, ei
}
// Equal returns true if this Object and the other are equal in value.
func (obj *Array) Equal(other Object) (eq bool) {
to, ok := other.(*Array)
if !ok {
return false
}
if len(obj.dims) != len(to.dims) || len(obj.elements) != len(to.elements) {
return false
}
for i, d := range obj.dims {
if d != to.dims[i] {
return false
}
}
for i, e := range obj.elements {
if !ObjectEqual(e, to.elements[i]) {
return false
}
}
return true
}
// Hierarchy returns the class hierarchy as symbols for the instance.
func (obj *Array) Hierarchy() []Symbol {
return []Symbol{ArraySymbol, TrueSymbol}
}
// Eval returns self.
func (obj *Array) Eval(s *Scope, depth int) Object {
return obj
}
// Dimensions of the array.
func (obj *Array) Dimensions() []int {
return obj.dims
}
// Length returns the length of the object.
func (obj *Array) Length() int {
return len(obj.elements)
}
// Get the value at the location identified by the indexes.
func (obj *Array) Get(indexes ...int) Object {
return obj.elements[obj.MajorIndex(indexes...)]
}
// Set a value at the location identified by the indexes.
func (obj *Array) Set(value Object, indexes ...int) {
pos := obj.MajorIndex(indexes...)
obj.elements[pos] = Coerce(value, obj.elementType)
}
// MajorIndex for the indexes provided.
func (obj *Array) MajorIndex(indexes ...int) int {
if len(indexes) != len(obj.dims) {
ErrorPanic(NewScope(), 0, "Wrong number of subscripts, %d, for array of rank %d.", len(indexes), len(obj.dims))
}
pos := 0
for i, index := range indexes {
d := obj.dims[i]
if index < 0 || d <= index {
dims := make(List, len(obj.dims))
for j, d2 := range obj.dims {
dims[j] = Fixnum(d2)
}
ErrorPanic(NewScope(), 0, "Invalid index %d for axis %d of (array %s). Should be between 0 and %d.",
index, i, dims, d)
}
pos += index * obj.sizes[i]
}
return pos
}
// MajorGet for the index provided.
func (obj *Array) MajorGet(index int) Object {
if index < 0 || len(obj.elements) <= index {
ErrorPanic(NewScope(), 0, "Invalid major index %d for (array %s). Should be between 0 and %d.",
index, obj, len(obj.elements))
}
return obj.elements[index]
}
// MajorSet for the index provided.
func (obj *Array) MajorSet(index int, value Object) {
if index < 0 || len(obj.elements) <= index {
ErrorPanic(NewScope(), 0, "Invalid major index %d for (array %s). Should be between 0 and %d.",
index, obj, len(obj.elements))
}
obj.elements[index] = value
}
// AsList the Object into set of nested lists.
func (obj *Array) AsList() (list List) {
if 0 < len(obj.dims) {
list, _ = obj.listifyDim(0, 0)
}
return
}
func (obj *Array) listifyDim(di, ei int) (List, int) {
d := obj.dims[di]
list := make(List, d)
if di == len(obj.dims)-1 {
for i := 0; i < d; i++ {
e := obj.elements[ei]
list[i] = e
ei++
}
} else {
d2 := di + 1
for i := 0; i < d; i++ {
list[i], ei = obj.listifyDim(d2, ei)
}
}
return list, ei
}
// SetAll element from the nested list provided.
func (obj *Array) SetAll(all List) {
if 0 < len(obj.dims) {
_ = obj.setDim(all, 0, 0)
}
}
func (obj *Array) setDim(list List, di, ei int) int {
d := obj.dims[di]
if d != len(list) {
ErrorPanic(NewScope(), 0,
"Malformed :initial-contents: Dimension of axis %d is %d but received %d.", di, d, len(list))
}
if di == len(obj.dims)-1 {
for i := 0; i < d; i++ {
obj.elements[ei] = Coerce(list[i], obj.elementType)
ei++
}
} else {
d2 := di + 1
for i := 0; i < d; i++ {
sub, ok := list[i].(List)
if !ok {
TypePanic(NewScope(), 0, "array initial-content", list[i], "list")
}
ei = obj.setDim(sub, d2, ei)
}
}
return ei
}
// Adjust array with new parameters.
func (obj *Array) Adjust(dimensions []int, elementType Symbol, initElement Object, initContent List) *Array {
if len(dimensions) != len(obj.dims) {
ErrorPanic(NewScope(), 0,
"Expected %d new dimensions for array %s, but received %d.", len(obj.dims), obj, len(dimensions))
}
adjustable := obj.adjustable
if !adjustable {
dup := *obj
dup.adjustable = true
dup.elements = make(List, len(obj.elements))
copy(dup.elements, obj.elements)
dup.dims = make([]int, len(obj.dims))
copy(dup.dims, obj.dims)
dup.sizes = make([]int, len(obj.sizes))
copy(dup.sizes, obj.sizes)
obj = &dup
}
if initContent != nil { // start over
obj.dims = dimensions
obj.sizes = make([]int, len(dimensions))
obj.elementType = elementType
size := 1
for i := len(dimensions) - 1; 0 <= i; i-- {
obj.sizes[i] = size
size *= dimensions[i]
}
obj.elements = make([]Object, size)
obj.SetAll(initContent)
return obj
}
if TrueSymbol != elementType {
for _, v := range obj.elements {
_ = Coerce(v, elementType)
}
}
tmp := NewArray(dimensions, elementType, initElement, nil, true)
index := make([]int, len(obj.dims))
top:
for pos := len(tmp.elements) - 1; 0 <= pos; pos-- {
off := pos
for i, sz := range tmp.sizes {
index[i] = off / sz
off %= sz
}
opos := 0
for i, idx := range index {
d := obj.dims[i]
if d <= idx {
tmp.elements[pos] = initElement
continue top
}
opos += idx * obj.sizes[i]
}
tmp.elements[pos] = obj.elements[opos]
}
obj.dims = tmp.dims
obj.sizes = tmp.sizes
obj.elements = tmp.elements
obj.elementType = elementType
obj.adjustable = adjustable
return obj
}
// Rank of the array is returned,
func (obj *Array) Rank() int {
return len(obj.dims)
}
// Adjustable returns true if the array is adjustable.
func (obj *Array) Adjustable() bool {
return obj.adjustable
}
// ElementType returns the element-type of the array.
func (obj *Array) ElementType() Symbol {
return obj.elementType
}
// SetElementType returns the element-type of the array.
func (obj *Array) SetElementType(ts Object) {
sym, ok := ts.(Symbol)
if !ok {
TypePanic(NewScope(), 0, "type-specification", ts, "symbol")
}
for _, v := range obj.elements {
_ = Coerce(v, sym)
}
obj.elementType = sym
}
// Elements returns the elements of the array.
func (obj *Array) Elements() []Object {
return obj.elements
}
// LoadForm returns a form that can be evaluated to create the object.
func (obj *Array) LoadForm() Object {
dims := make(List, len(obj.dims))
for i, d := range obj.dims {
dims[i] = Fixnum(d)
}
var et Object
if obj.elementType == TrueSymbol {
et = True
} else {
et = List{quoteSymbol, obj.elementType}
}
form := List{
Symbol("make-array"),
List{quoteSymbol, dims},
Symbol(":element-type"),
et,
Symbol(":initial-contents"),
List{quoteSymbol, obj.AsList()},
}
if obj.adjustable {
form = append(form, Symbol(":adjustable"), True)
}
return form
}