-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyFastSequenceIter.java
More file actions
68 lines (55 loc) · 1.73 KB
/
PyFastSequenceIter.java
File metadata and controls
68 lines (55 loc) · 1.73 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
/* Copyright (c) Jython Developers */
package org.python.core;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
/**
* Sequence iterator specialized for accessing the underlying sequence directly.
*/
@ExposedType(name = "fastsequenceiterator", base = PyObject.class, isBaseType = false)
public class PyFastSequenceIter extends PyIterator {
//note: Already implements Traverseproc, inheriting it from PyIterator
public static final PyType TYPE = PyType.fromClass(PyFastSequenceIter.class);
private PySequence seq;
private int index;
public PyFastSequenceIter(PySequence seq) {
super(TYPE);
this.seq = seq;
}
@ExposedMethod(doc = "x.next() -> the next value, or raise StopIteration")
final PyObject fastsequenceiterator_next() {
return super.next();
}
@Override
public PyObject __iternext__() {
if (seq == null) {
return null;
}
PyObject result;
try {
result = seq.seq___finditem__(index++);
} catch (PyException pye) {
if (pye.match(Py.StopIteration)) {
seq = null;
return null;
}
throw pye;
}
if (result == null) {
seq = null;
}
return result;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retValue = super.traverse(visit, arg);
if (retValue != 0) {
return retValue;
}
return seq == null ? 0 : visit.visit(seq, arg);
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == seq || super.refersDirectlyTo(ob));
}
}