forked from go-python/gpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
330 lines (292 loc) · 7.31 KB
/
list.go
File metadata and controls
330 lines (292 loc) · 7.31 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
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// List objects
package py
var ListType = ObjectType.NewType("list", "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items", ListNew, nil)
// FIXME lists are mutable so this should probably be struct { Tuple } then can use the sub methods on Tuple
type List struct {
Items []Object
}
func init() {
ListType.Dict["append"] = MustNewMethod("append", func(self Object, args Tuple) (Object, error) {
listSelf := self.(*List)
if len(args) != 1 {
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
}
listSelf.Items = append(listSelf.Items, args[0])
return NoneType{}, nil
}, 0, "append(item)")
ListType.Dict["extend"] = MustNewMethod("extend", func(self Object, args Tuple) (Object, error) {
listSelf := self.(*List)
if len(args) != 1 {
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
}
if oList, ok := args[0].(*List); ok {
listSelf.Items = append(listSelf.Items, oList.Items...)
}
return NoneType{}, nil
}, 0, "extend([item])")
}
// Type of this List object
func (o *List) Type() *Type {
return ListType
}
// ListNew
func ListNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
var iterable Object
err = UnpackTuple(args, kwargs, "list", 0, 1, &iterable)
if err != nil {
return nil, err
}
if iterable != nil {
return SequenceList(iterable)
}
return NewList(), nil
}
// Make a new empty list
func NewList() *List {
return &List{}
}
// Make a new empty list with given capacity
func NewListWithCapacity(n int) *List {
l := &List{}
if n != 0 {
l.Items = make([]Object, 0, n)
}
return l
}
// Make a list with n nil elements
func NewListSized(n int) *List {
l := &List{}
if n != 0 {
l.Items = make([]Object, n)
}
return l
}
// Make a new list from an []Object
//
// The []Object is copied into the list
func NewListFromItems(items []Object) *List {
l := NewListSized(len(items))
copy(l.Items, items)
return l
}
// Copy a list object
func (l *List) Copy() *List {
return NewListFromItems(l.Items)
}
// Append an item
func (l *List) Append(item Object) {
l.Items = append(l.Items, item)
}
// Resize the list
func (l *List) Resize(newSize int) {
l.Items = l.Items[:newSize]
}
// Extend the list with items
func (l *List) Extend(items []Object) {
l.Items = append(l.Items, items...)
}
// Extends the list with the sequence passed in
func (l *List) ExtendSequence(seq Object) error {
return Iterate(seq, func(item Object) bool {
l.Append(item)
return false
})
}
// Len of list
func (l *List) Len() int {
return len(l.Items)
}
func (l *List) M__str__() (Object, error) {
return l.M__repr__()
}
func (l *List) M__repr__() (Object, error) {
return Tuple(l.Items).repr("[", "]")
}
func (l *List) M__len__() (Object, error) {
return Int(len(l.Items)), nil
}
func (l *List) M__bool__() (Object, error) {
return NewBool(len(l.Items) > 0), nil
}
func (l *List) M__iter__() (Object, error) {
return NewIterator(l.Items), nil
}
func (l *List) M__getitem__(key Object) (Object, error) {
if slice, ok := key.(*Slice); ok {
start, _, step, slicelength, err := slice.GetIndices(len(l.Items))
if err != nil {
return nil, err
}
newList := NewListSized(slicelength)
for i, j := start, 0; j < slicelength; i, j = i+step, j+1 {
newList.Items[j] = l.Items[i]
}
return newList, nil
}
i, err := IndexIntCheck(key, len(l.Items))
if err != nil {
return nil, err
}
return l.Items[i], nil
}
func (l *List) M__setitem__(key, value Object) (Object, error) {
if slice, ok := key.(*Slice); ok {
start, stop, step, slicelength, err := slice.GetIndices(len(l.Items))
if err != nil {
return nil, err
}
if step == 1 {
// Make a copy of the tail
tailSlice := l.Items[stop:]
tail := make([]Object, len(tailSlice))
copy(tail, tailSlice)
l.Items = l.Items[:start]
err = l.ExtendSequence(value)
if err != nil {
return nil, err
}
l.Items = append(l.Items, tail...)
} else {
newItems, err := SequenceTuple(value)
if err != nil {
return nil, err
}
if len(newItems) != slicelength {
return nil, ExceptionNewf(ValueError, "attempt to assign sequence of size %d to extended slice of size %d", len(newItems), slicelength)
}
j := 0
for i := start; i < stop; i += step {
l.Items[i] = newItems[j]
j++
}
}
} else {
i, err := IndexIntCheck(key, len(l.Items))
if err != nil {
return nil, err
}
l.Items[i] = value
}
return None, nil
}
// Removes the item at i
func (a *List) DelItem(i int) {
a.Items = append(a.Items[:i], a.Items[i+1:]...)
}
// Removes items from a list
func (a *List) M__delitem__(key Object) (Object, error) {
if slice, ok := key.(*Slice); ok {
start, stop, step, _, err := slice.GetIndices(len(a.Items))
if err != nil {
return nil, err
}
if step == 1 {
a.Items = append(a.Items[:start], a.Items[stop:]...)
} else {
j := 0
for i := start; i < stop; i += step {
a.DelItem(i - j)
j++
}
}
} else {
i, err := IndexIntCheck(key, len(a.Items))
if err != nil {
return nil, err
}
a.DelItem(i)
}
return None, nil
}
func (a *List) M__add__(other Object) (Object, error) {
if b, ok := other.(*List); ok {
newList := NewListSized(len(a.Items) + len(b.Items))
copy(newList.Items, a.Items)
copy(newList.Items[len(a.Items):], b.Items)
return newList, nil
}
return NotImplemented, nil
}
func (a *List) M__radd__(other Object) (Object, error) {
if b, ok := other.(*List); ok {
return b.M__add__(a)
}
return NotImplemented, nil
}
func (a *List) M__iadd__(other Object) (Object, error) {
if b, ok := other.(*List); ok {
a.Extend(b.Items)
return a, nil
}
return NotImplemented, nil
}
func (l *List) M__mul__(other Object) (Object, error) {
if b, ok := convertToInt(other); ok {
m := len(l.Items)
n := int(b) * m
newList := NewListSized(n)
for i := 0; i < n; i += m {
copy(newList.Items[i:i+m], l.Items)
}
return newList, nil
}
return NotImplemented, nil
}
func (a *List) M__rmul__(other Object) (Object, error) {
return a.M__mul__(other)
}
func (a *List) M__imul__(other Object) (Object, error) {
return a.M__mul__(other)
}
// Check interface is satisfied
var _ sequenceArithmetic = (*List)(nil)
var _ I__str__ = (*List)(nil)
var _ I__repr__ = (*List)(nil)
var _ I__len__ = (*List)(nil)
var _ I__len__ = (*List)(nil)
var _ I__bool__ = (*List)(nil)
var _ I__iter__ = (*List)(nil)
var _ I__getitem__ = (*List)(nil)
var _ I__setitem__ = (*List)(nil)
// var _ richComparison = (*List)(nil)
func (a *List) M__eq__(other Object) (Object, error) {
b, ok := other.(*List)
if !ok {
return NotImplemented, nil
}
if len(a.Items) != len(b.Items) {
return False, nil
}
for i := range a.Items {
eq, err := Eq(a.Items[i], b.Items[i])
if err != nil {
return nil, err
}
if eq == False {
return False, nil
}
}
return True, nil
}
func (a *List) M__ne__(other Object) (Object, error) {
b, ok := other.(*List)
if !ok {
return NotImplemented, nil
}
if len(a.Items) != len(b.Items) {
return True, nil
}
for i := range a.Items {
eq, err := Eq(a.Items[i], b.Items[i])
if err != nil {
return nil, err
}
if eq == False {
return True, nil
}
}
return False, nil
}