/
TransformerIterator.java
223 lines (182 loc) · 5.11 KB
/
TransformerIterator.java
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
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* Alex Miller 3/3/15 */
package clojure.lang;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.LinkedList;
public class TransformerIterator implements Iterator {
private static final Buffer EMPTY = new Empty();
private static final Object NONE = new Object();
// Source
private final Iterator sourceIter;
private final IFn xf;
private final boolean multi;
// Iteration state
private volatile Buffer buffer = EMPTY;
private volatile Object next = NONE;
private volatile boolean completed = false;
private TransformerIterator(IFn xform, Iterator sourceIter, boolean multi) {
this.sourceIter = sourceIter;
this.xf = (IFn) xform.invoke(new AFn() {
public Object invoke() {
return null;
}
public Object invoke(Object acc) {
return acc;
}
public Object invoke(Object acc, Object o) {
buffer = buffer.add(o);
return acc;
}
});
this.multi = multi;
}
public static Iterator create(IFn xform, Iterator source) {
return new TransformerIterator(xform, source, false);
}
public static Iterator createMulti(IFn xform, List sources) {
Iterator[] iters = new Iterator[sources.size()];
for(int i=0; i<sources.size(); i++)
iters[i] = (Iterator)sources.get(i);
return new TransformerIterator(xform, new MultiIterator(iters), true);
}
private boolean step() {
if(next != NONE)
return true;
while (next == NONE) {
if(buffer.isEmpty()) {
if(completed) {
return false;
} else if (sourceIter.hasNext()) {
Object iter = null;
if(multi)
iter = xf.applyTo(RT.cons(null, sourceIter.next()));
else
iter = xf.invoke(null, sourceIter.next());
if(RT.isReduced(iter)) {
xf.invoke(null);
completed = true;
}
} else {
xf.invoke(null);
completed = true;
}
} else {
next = buffer.remove();
}
}
return true;
}
public boolean hasNext() {
return step();
}
public Object next() {
if(hasNext()) {
Object ret = next;
next = NONE;
return ret;
}
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
private static interface Buffer {
Buffer add(Object o);
Object remove();
boolean isEmpty();
}
private static class Empty implements Buffer {
public Buffer add(Object o) {
return new Single(o);
}
public Object remove() {
throw new IllegalStateException("Removing object from empty buffer");
}
public boolean isEmpty() {
return true;
}
public String toString() {
return "Empty";
}
}
private static class Single implements Buffer {
private volatile Object val;
public Single(Object o) {
this.val = o;
}
public Buffer add(Object o) {
if (val == NONE) {
val = o;
return this;
} else {
return new Many(val, o);
}
}
public Object remove() {
if(val == NONE) {
throw new IllegalStateException("Removing object from empty buffer");
}
Object ret = val;
val = NONE;
return ret;
}
public boolean isEmpty() {
return val == NONE;
}
public String toString() {
return "Single: " + val;
}
}
private static class Many implements Buffer {
private final Queue vals = new LinkedList();
public Many(Object o1, Object o2) {
vals.add(o1);
vals.add(o2);
}
public Buffer add(Object o) {
vals.add(o);
return this;
}
public Object remove() {
return vals.remove();
}
public boolean isEmpty() {
return vals.isEmpty();
}
public String toString() {
return "Many: " + vals.toString();
}
}
private static class MultiIterator implements Iterator {
private final Iterator[] iters;
public MultiIterator(Iterator[] iters) {
this.iters = iters;
}
public boolean hasNext(){
for(Iterator iter : iters)
if(!iter.hasNext())
return false;
return true;
}
public Object next() {
Object[] nexts = new Object[iters.length];
for(int i = 0;i<iters.length;i++)
nexts[i] = iters[i].next();
return new ArraySeq(nexts,0);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}