Skip to content

Commit aaa39be

Browse files
committed
add __length_hint__ support for set iterators
- fix (c) headers
1 parent f7523de commit aaa39be

File tree

6 files changed

+50
-30
lines changed

6 files changed

+50
-30
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonImmutableBuiltinType.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/*
2-
* Copyright (c) 2017, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34
*
45
* The Universal Permissive License (UPL), Version 1.0
56
*
67
* Subject to the condition set forth below, permission is hereby granted to any
7-
* person obtaining a copy of this software, associated documentation and/or data
8-
* (collectively the "Software"), free of charge and under any and all copyright
9-
* rights in the Software, and any and all patent rights owned or freely
10-
* licensable by each licensor hereunder covering either (i) the unmodified
11-
* Software as contributed to or provided by such licensor, or (ii) the Larger
12-
* Works (as defined below), to deal in both
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
1314
*
1415
* (a) the Software, and
16+
*
1517
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
* one is included with the Software (each a "Larger Work" to which the
17-
* Software is contributed by such licensors),
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
1820
*
1921
* without restriction, including without limitation the rights to copy, create
2022
* derivative works of, display, perform, and distribute the Software and make,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/PJavaIteratorIterator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public abstract class PJavaIteratorIterator<T> extends PythonBuiltinObject {
3535

3636
private final Iterator<T> iterator;
37+
private int index;
3738

3839
public PJavaIteratorIterator(PythonClass clazz, Iterator<T> iterator) {
3940
super(clazz);
@@ -47,11 +48,16 @@ public Iterator<T> getIterator() {
4748
@TruffleBoundary
4849
public final Object next() {
4950
assert hasNext();
51+
index++;
5052
return iterator.next();
5153
}
5254

5355
@TruffleBoundary
5456
public final boolean hasNext() {
5557
return iterator.hasNext();
5658
}
59+
60+
public int getIndex() {
61+
return index;
62+
}
5763
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/IteratorBuiltins.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,57 +192,62 @@ public Object __iter__(PythonBuiltinObject self) {
192192
@GenerateNodeFactory
193193
public abstract static class LengthHintNode extends PythonUnaryBuiltinNode {
194194
@Specialization
195-
public int next(PIntArrayIterator self) {
195+
public int lengthHint(PIntArrayIterator self) {
196196
return self.array.len() - self.index;
197197
}
198198

199199
@Specialization
200-
public int next(PIntegerSequenceIterator self) {
200+
public int lengthHint(PIntegerSequenceIterator self) {
201201
return self.sequence.length() - self.index;
202202
}
203203

204204
@Specialization
205-
public int next(PRangeIterator self) {
205+
public int lengthHint(PRangeIterator self) {
206206
return self.getStop() - self.getStart();
207207
}
208208

209209
@Specialization
210-
public int next(PRangeReverseIterator self) {
210+
public int lengthHint(PRangeReverseIterator self) {
211211
return self.getStart() - self.getStop();
212212
}
213213

214214
@Specialization
215-
public double next(PDoubleArrayIterator self) {
215+
public double lengthHint(PDoubleArrayIterator self) {
216216
return self.array.len() - self.index;
217217
}
218218

219219
@Specialization
220-
public double next(PDoubleSequenceIterator self) {
220+
public double lengthHint(PDoubleSequenceIterator self) {
221221
return self.sequence.length() - self.index;
222222
}
223223

224224
@Specialization
225-
public long next(PLongArrayIterator self) {
225+
public long lengthHint(PLongArrayIterator self) {
226226
return self.array.len() - self.index;
227227
}
228228

229229
@Specialization
230-
public long next(PLongSequenceIterator self) {
230+
public long lengthHint(PLongSequenceIterator self) {
231231
return self.sequence.length() - self.index;
232232
}
233233

234+
@Specialization
235+
public long lengthHint(PBaseSetIterator self) {
236+
return self.getSet().size() - self.getIndex();
237+
}
238+
234239
@Specialization(guards = "self.isPSequence()")
235-
public Object next(PSequenceIterator self) {
240+
public Object lengthHint(PSequenceIterator self) {
236241
return self.getPSequence().len() - self.index;
237242
}
238243

239244
@Specialization
240-
public Object next(PStringIterator self) {
245+
public Object lengthHint(PStringIterator self) {
241246
return self.value.length() - self.index;
242247
}
243248

244249
@Specialization(guards = "!self.isPSequence()")
245-
public Object next(PSequenceIterator self,
250+
public Object lengthHint(PSequenceIterator self,
246251
@Cached("create(__LEN__)") LookupAndCallUnaryNode callLen,
247252
@Cached("create(__SUB__, __RSUB__)") LookupAndCallBinaryNode callSub) {
248253
return callSub.executeObject(callLen.executeObject(self.getObject()), self.index);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/PBaseSetIterator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
import com.oracle.graal.python.builtins.objects.type.PythonClass;
3131

3232
public final class PBaseSetIterator extends PJavaIteratorIterator<Object> {
33+
private final PBaseSet set;
3334

3435
public PBaseSetIterator(PythonClass clazz, PBaseSet set) {
3536
super(clazz, set.getDictStorage().keys().iterator());
37+
this.set = set;
38+
}
39+
40+
public PBaseSet getSet() {
41+
return set;
3642
}
3743
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/HiddenAttributes.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34
*
45
* The Universal Permissive License (UPL), Version 1.0
56
*
67
* Subject to the condition set forth below, permission is hereby granted to any
7-
* person obtaining a copy of this software, associated documentation and/or data
8-
* (collectively the "Software"), free of charge and under any and all copyright
9-
* rights in the Software, and any and all patent rights owned or freely
10-
* licensable by each licensor hereunder covering either (i) the unmodified
11-
* Software as contributed to or provided by such licensor, or (ii) the Larger
12-
* Works (as defined below), to deal in both
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
1314
*
1415
* (a) the Software, and
16+
*
1517
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
* one is included with the Software (each a "Larger Work" to which the
17-
* Software is contributed by such licensors),
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
1820
*
1921
* without restriction, including without limitation the rights to copy, create
2022
* derivative works of, display, perform, and distribute the Software and make,

mx.graalpython/copyrights/overrides

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/JavaType
499499
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java,zippy.copyright
500500
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonCore.java,zippy.copyright
501501
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java,zippy.copyright
502-
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonParseResult.java,zippy.copyright
503502
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonParser.java,zippy.copyright
504503
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/BreakException.java,zippy.copyright
505504
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/ContinueException.java,zippy.copyright

0 commit comments

Comments
 (0)