-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_list.go
330 lines (273 loc) · 7.07 KB
/
array_list.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
package value
import (
"fmt"
"strings"
)
// ::Std::ArrayList
//
// Represents a dynamically sized array,
// that can shrink and grow.
var ArrayListClass *Class
// ::Std::ArrayList::Iterator
//
// ArrayList iterator class.
var ArrayListIteratorClass *Class
var NilArrayList ArrayList = nil
// Elk's ArrayList value
type ArrayList []Value
func NewArrayList(capacity int) *ArrayList {
l := make(ArrayList, 0, capacity)
return &l
}
func NewArrayListWithLength(length int) *ArrayList {
l := make(ArrayList, length)
return &l
}
func NewArrayListWithElements(capacity int, elements ...Value) *ArrayList {
l := make(ArrayList, len(elements), len(elements)+capacity)
copy(l, elements)
return &l
}
func (*ArrayList) Class() *Class {
return ArrayListClass
}
func (*ArrayList) DirectClass() *Class {
return ArrayListClass
}
func (*ArrayList) SingletonClass() *Class {
return nil
}
func (l *ArrayList) Copy() Value {
if l == nil {
return l
}
newList := make(ArrayList, len(*l))
copy(newList, *l)
return &newList
}
func (l *ArrayList) Inspect() string {
var builder strings.Builder
builder.WriteString("[")
for i, element := range *l {
if i != 0 {
builder.WriteString(", ")
}
builder.WriteString(element.Inspect())
}
builder.WriteString("]")
leftCap := l.LeftCapacity()
if leftCap > 0 {
builder.WriteByte(':')
fmt.Fprintf(&builder, "%d", leftCap)
}
return builder.String()
}
func (*ArrayList) InstanceVariables() SymbolMap {
return nil
}
func (l *ArrayList) Capacity() int {
return cap(*l)
}
func (l *ArrayList) LeftCapacity() int {
return l.Capacity() - l.Length()
}
func (l *ArrayList) Length() int {
return len(*l)
}
// Add new elements.
func (l *ArrayList) Append(elements ...Value) {
*l = append(*l, elements...)
}
// Expand the array list to have
// empty slots for new elements.
func (l *ArrayList) Grow(newSlots int) {
newList := make(ArrayList, l.Length(), l.Capacity()+newSlots)
copy(newList, *l)
*l = newList
}
// Get an element under the given index.
func GetFromSlice(collection *[]Value, index int) (Value, *Error) {
l := len(*collection)
if index >= l || index < -l {
return nil, NewIndexOutOfRangeError(fmt.Sprint(index), len(*collection))
}
if index < 0 {
index = l + index
}
return (*collection)[index], nil
}
// Set an element under the given index.
func SetInSlice(collection *[]Value, index int, val Value) *Error {
l := len(*collection)
if index >= l || index < -l {
return NewIndexOutOfRangeError(fmt.Sprint(index), len(*collection))
}
if index < 0 {
index = l + index
}
(*collection)[index] = val
return nil
}
// Get an element under the given index.
func (l *ArrayList) Get(index int) (Value, *Error) {
return GetFromSlice((*[]Value)(l), index)
}
// Get an element under the given index without bounds checking
func (l *ArrayList) At(i int) Value {
return (*l)[i]
}
// Get an element under the given index.
func (l *ArrayList) Subscript(key Value) (Value, *Error) {
var i int
i, ok := ToGoInt(key)
if !ok {
if i == -1 {
return nil, NewIndexOutOfRangeError(key.Inspect(), len(*l))
}
return nil, NewCoerceError(IntClass, key.Class())
}
return l.Get(i)
}
// Set an element under the given index.
func (l *ArrayList) Set(index int, val Value) *Error {
return SetInSlice((*[]Value)(l), index, val)
}
// Set an element under the given index without bounds checking.
func (l *ArrayList) SetAt(index int, val Value) {
(*l)[index] = val
}
// Set an element under the given index.
func (l *ArrayList) SubscriptSet(key, val Value) *Error {
length := len(*l)
i, ok := ToGoInt(key)
if !ok {
if i == -1 {
return NewIndexOutOfRangeError(key.Inspect(), length)
}
return NewCoerceError(IntClass, key.Class())
}
return l.Set(i, val)
}
// Concatenate another value with this list, creating a new list, and return the result.
// If the operation is illegal an error will be returned.
func (l *ArrayList) Concat(other Value) (*ArrayList, *Error) {
switch o := other.(type) {
case *ArrayList:
newList := make(ArrayList, len(*l), len(*l)+len(*o))
copy(newList, *l)
newList = append(newList, *o...)
return &newList, nil
case *ArrayTuple:
newList := make(ArrayList, len(*l), len(*l)+len(*o))
copy(newList, *l)
newList = append(newList, *o...)
return &newList, nil
default:
return nil, Errorf(TypeErrorClass, "cannot concat %s with list %s", other.Inspect(), l.Inspect())
}
}
// Repeat the content of this list n times and return a new list containing the result.
// If the operation is illegal an error will be returned.
func (l *ArrayList) Repeat(other Value) (*ArrayList, *Error) {
switch o := other.(type) {
case SmallInt:
if o < 0 {
return nil, Errorf(
OutOfRangeErrorClass,
"list repeat count cannot be negative: %s",
o.Inspect(),
)
}
newLen, ok := o.MultiplyOverflow(SmallInt(len(*l)))
if !ok {
return nil, Errorf(
OutOfRangeErrorClass,
"list repeat count is too large %s",
o.Inspect(),
)
}
newList := make(ArrayList, 0, newLen)
for i := 0; i < int(o); i++ {
newList = append(newList, *l...)
}
return &newList, nil
case *BigInt:
return nil, Errorf(
OutOfRangeErrorClass,
"list repeat count is too large %s",
o.Inspect(),
)
default:
return nil, Errorf(TypeErrorClass, "cannot repeat a list using %s", other.Inspect())
}
}
// Expands the list by n nil elements.
func (l *ArrayList) Expand(newElements int) {
if newElements < 1 {
return
}
newCollection := make(ArrayList, len(*l), cap(*l)+newElements)
copy(newCollection, *l)
for i := 0; i < newElements; i++ {
newCollection = append(newCollection, Nil)
}
*l = newCollection
}
type ArrayListIterator struct {
ArrayList *ArrayList
Index int
}
func NewArrayListIterator(list *ArrayList) *ArrayListIterator {
return &ArrayListIterator{
ArrayList: list,
}
}
func NewArrayListIteratorWithIndex(list *ArrayList, index int) *ArrayListIterator {
return &ArrayListIterator{
ArrayList: list,
Index: index,
}
}
func (*ArrayListIterator) Class() *Class {
return ArrayListIteratorClass
}
func (*ArrayListIterator) DirectClass() *Class {
return ArrayListIteratorClass
}
func (*ArrayListIterator) SingletonClass() *Class {
return nil
}
func (l *ArrayListIterator) Copy() Value {
return &ArrayListIterator{
ArrayList: l.ArrayList,
Index: l.Index,
}
}
func (l *ArrayListIterator) Inspect() string {
return fmt.Sprintf("Std::ArrayList::Iterator{list: %s, index: %d}", l.ArrayList.Inspect(), l.Index)
}
func (*ArrayListIterator) InstanceVariables() SymbolMap {
return nil
}
var stopIterationSymbol = ToSymbol("stop_iteration")
func (l *ArrayListIterator) Next() (Value, Value) {
if l.Index >= l.ArrayList.Length() {
return nil, stopIterationSymbol
}
next := (*l.ArrayList)[l.Index]
l.Index++
return next, nil
}
func initArrayList() {
ArrayListClass = NewClassWithOptions(
ClassWithSealed(),
ClassWithNoInstanceVariables(),
)
ArrayListClass.IncludeMixin(ListMixin)
StdModule.AddConstantString("ArrayList", ArrayListClass)
ArrayListIteratorClass = NewClassWithOptions(
ClassWithSealed(),
ClassWithNoInstanceVariables(),
)
ArrayListClass.AddConstantString("Iterator", ArrayListIteratorClass)
}