-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathizip.java
More file actions
130 lines (103 loc) · 3.69 KB
/
izip.java
File metadata and controls
130 lines (103 loc) · 3.69 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
/* Copyright (c) Jython Developers */
package org.python.modules.itertools;
import org.python.core.Py;
import org.python.core.PyIterator;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.PyXRange;
import org.python.core.Visitproc;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
@ExposedType(name = "itertools.izip", base = PyObject.class, doc = izip.izip_doc)
public class izip extends PyIterator {
public static final PyType TYPE = PyType.fromClass(izip.class);
private PyIterator iter;
public static final String izip_doc =
"izip(iter1 [,iter2 [...]]) --> izip object\n\n" +
"Return an izip object whose .next() method returns a tuple where\n" +
"the i-th element comes from the i-th iterable argument. The .next()\n" +
"method continues until the shortest iterable in the argument sequence\n" +
"is exhausted and then it raises StopIteration. Works like the zip()\n" +
"function but consumes less memory by returning an iterator instead of\n" +
"a list.";
public izip() {
super();
}
public izip(PyType subType) {
super(subType);
}
public izip(PyObject... args) {
super();
izip___init__(args);
}
/**
* Create an iterator whose <code>next()</code> method returns a tuple where the i-th element
* comes from the i-th iterable argument. Continues until the shortest iterable is exhausted.
* (Code in this method is based on __builtin__.zip()).
*
*/
@ExposedNew
@ExposedMethod
final void izip___init__(PyObject[] args, String[] kwds) {
if (kwds.length > 0) {
throw Py.TypeError(String.format("izip does not take keyword arguments"));
}
izip___init__(args);
}
private void izip___init__(final PyObject[] argstar) {
final int itemsize = argstar.length;
if (itemsize == 0) {
iter = (PyIterator)(new PyXRange(0).__iter__());
return;
}
// Type check the arguments; they must be sequences.
final PyObject[] iters = new PyObject[itemsize];
for (int i = 0; i < itemsize; i++) {
PyObject iter = argstar[i].__iter__();
if (iter == null) {
throw Py.TypeError("izip argument #" + (i + 1)
+ " must support iteration");
}
iters[i] = iter;
}
iter = new itertools.ItertoolsIterator() {
public PyObject __iternext__() {
if (itemsize == 0)
return null;
PyObject[] next = new PyObject[itemsize];
PyObject item;
for (int i = 0; i < itemsize; i++) {
item = nextElement(iters[i]);
if (item == null) {
return null;
}
next[i] = item;
}
return new PyTuple(next);
}
};
}
public PyObject __iternext__() {
return iter.__iternext__();
}
@ExposedMethod
@Override
public PyObject next() {
return doNext(__iternext__());
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal = super.traverse(visit, arg);
if (retVal != 0) {
return retVal;
}
return iter != null ? visit.visit(iter, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (iter == ob || super.refersDirectlyTo(ob));
}
}