-
Notifications
You must be signed in to change notification settings - Fork 1
/
itertools.go
299 lines (249 loc) · 6.09 KB
/
itertools.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
package sun
import (
"fmt"
"github.com/google/uuid"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
)
// A note on Hash functions:
// In the CPython implementation of itertools, some itertools methods, such as
// count and islice, inherit tp_hash from object where object's hash is
// calculated by id() >> 4 and where id(), in some Python implementations,
// returns the memory address of the underlying object.
// In this itertools module, a UUID.ID() is used.
/* count object ************************************************************/
// Iterator implementation for countObject.
type countIter struct {
co *countObject
}
func (c *countIter) Next(p *starlark.Value) bool {
if c.co.frozen {
return false
}
*p = c.co.cnt
// Numeric types for count and step should be guaranteed in countObject
// creation in count_ function.
count, step := c.co.cnt, c.co.step
c.co.cnt, _ = starlark.Binary(syntax.PLUS, count, step)
return true
}
func (c *countIter) Done() {}
// countObject implementation as a starlark.Value.
type countObject struct {
cnt, step starlark.Value
frozen bool
id uint32
}
func newCountObject(start, step starlark.Value) *countObject {
return &countObject{cnt: start, step: step, id: uuid.New().ID()}
}
func (co countObject) String() string {
// As with the cpython implementation, we don't display
// step when it is an integer equal to 1 (default step value).
step, ok := co.step.(starlark.Int)
if ok {
if x, ok := step.Int64(); ok && x == 1 {
return fmt.Sprintf("count(%v)", co.cnt.String())
}
}
return fmt.Sprintf("count(%v, %v)", co.cnt.String(), co.step.String())
}
func (co *countObject) Type() string {
return "itertools.count"
}
func (co *countObject) Freeze() {
if !co.frozen {
co.frozen = true
}
}
func (co *countObject) Truth() starlark.Bool {
return starlark.True
}
func (co *countObject) Hash() (uint32, error) {
return co.id, nil
}
func (co *countObject) Iterate() starlark.Iterator {
return &countIter{co: co}
}
func count_(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
defaultStart = starlark.MakeInt(0)
defaultStep = starlark.MakeInt(1)
start starlark.Value
step starlark.Value
)
if err := starlark.UnpackPositionalArgs(
"count", args, kwargs, 0, &start, &step,
); err != nil {
return nil, err
}
if start == nil {
start = defaultStart
}
if step == nil {
step = defaultStep
}
// Assert that count and step are numeric starlark Values.
switch start.(type) {
case starlark.Float, starlark.Int:
default:
return nil, fmt.Errorf("a number is required")
}
switch step.(type) {
case starlark.Float, starlark.Int:
default:
return nil, fmt.Errorf("a number is required")
}
return newCountObject(start, step), nil
}
/* islice object ************************************************************/
// isliceObject as a starlark.Value
type isliceObject struct {
// Store the iterator directly on the object; see
// https://github.com/tdakkota/sun/issues/14
iterator starlark.Iterator
next int
stop int
step int
id uint32
// TODO(algebra8): Need itercount?
}
func (is isliceObject) String() string {
return "<itertools.islice object>"
}
func (is isliceObject) Type() string {
return "itertools.islice"
}
func (is isliceObject) Freeze() {
// Since isliceObject does not hold onto the iterable,
// the iterable value is not reachable from it so Freeze
// doesn't need to do anything.
}
func (is isliceObject) Truth() starlark.Bool {
return starlark.True
}
func (is isliceObject) Hash() (uint32, error) {
return is.id, nil
}
// Iterator for islice object
type isliceIter struct {
islice *isliceObject
cnt int
}
func (it *isliceIter) Next(p *starlark.Value) bool {
var (
x starlark.Value
oldNext int
)
stop := it.islice.stop
// Get iterator up to the "next" iteration.
for it.cnt < it.islice.next {
if !it.islice.iterator.Next(&x) {
return false
}
it.cnt += 1
}
if it.cnt >= stop {
return false
}
if !it.islice.iterator.Next(&x) {
return false
}
*p = x
it.cnt += 1
oldNext = it.islice.next
it.islice.next += it.islice.step
if it.islice.next < oldNext || it.islice.next > stop {
it.islice.next = it.islice.stop
}
return true
}
func (it *isliceIter) Done() {
it.islice.iterator.Done()
}
// isliceObject as a starlark.Iteratable
func (is isliceObject) Iterate() starlark.Iterator {
return &isliceIter{islice: &is}
}
func assertPosIntOrNone(vs ...starlark.Value) error {
for _, v := range vs {
i, ok := v.(starlark.Int)
if !ok && v != starlark.None {
return fmt.Errorf("expected int or None, got %s\n", v.Type())
}
if ok && i.Sign() == -1 {
return fmt.Errorf("expected non-negative values, got %s\n", v.String())
}
}
return nil
}
func islice(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
iterable starlark.Iterable
// Positional args from islice call
a starlark.Value
b starlark.Value
c starlark.Value
// islice values
start int = 0
stop int = (1 << 63) - 1
step int = 1
)
if err := starlark.UnpackPositionalArgs(
"islice", args, kwargs, 2, &iterable,
&a, &b, &c,
); err != nil {
return nil, err
}
if a == nil {
a = starlark.None
}
if b == nil {
b = starlark.None
}
if c == nil {
c = starlark.None
}
if err := assertPosIntOrNone(a, b, c); err != nil {
return nil, err
}
if len(args) > 2 { // itertools.islice(iterable, start, stop[, step])
if a != starlark.None {
if err := starlark.AsInt(a, &start); err != nil {
return nil, err
}
}
if b != starlark.None {
if err := starlark.AsInt(b, &stop); err != nil {
return nil, err
}
}
if c != starlark.None {
if err := starlark.AsInt(c, &step); err != nil {
return nil, err
}
}
} else { // 2 args; itertools.islice(iterable, stop)
if a != starlark.None {
if err := starlark.AsInt(a, &stop); err != nil {
return nil, err
}
}
}
return isliceObject{
iterator: iterable.Iterate(),
next: start,
stop: stop,
step: step,
}, nil
}