-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathArrayIter.java
More file actions
278 lines (219 loc) · 6.5 KB
/
ArrayIter.java
File metadata and controls
278 lines (219 loc) · 6.5 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
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
/**
* 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.
**/
package clojure.lang;
import java.lang.reflect.Array;
import java.util.Iterator;
public class ArrayIter implements Iterator {
final Object[] array;
int i;
static public Iterator EMPTY_ITERATOR = new Iterator() {
public boolean hasNext() { return false; }
public Object next() { throw new java.util.NoSuchElementException(); }
public void remove() { throw new UnsupportedOperationException("remove() not supported"); }
};
static public Iterator create(){
return EMPTY_ITERATOR;
}
static public Iterator create(Object... array){
if(array == null || array.length == 0)
return EMPTY_ITERATOR;
return new ArrayIter(array, 0);
}
static public Iterator createFromObject(Object array){
if(array == null || Array.getLength(array) == 0)
return EMPTY_ITERATOR;
Class aclass = array.getClass();
if(aclass == int[].class)
return new ArrayIter_int((int[]) array, 0);
if(aclass == float[].class)
return new ArrayIter_float((float[]) array, 0);
if(aclass == double[].class)
return new ArrayIter_double((double[]) array, 0);
if(aclass == long[].class)
return new ArrayIter_long((long[]) array, 0);
if(aclass == byte[].class)
return new ArrayIter_byte((byte[]) array, 0);
if(aclass == char[].class)
return new ArrayIter_char((char[]) array, 0);
if(aclass == short[].class)
return new ArrayIter_short((short[]) array, 0);
if(aclass == boolean[].class)
return new ArrayIter_boolean((boolean[]) array, 0);
return new ArrayIter(array, 0);
}
ArrayIter(Object array, int i){
this.i = i;
this.array = (Object[]) array;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Object next() {
if(array != null && i < array.length)
return array[i++];
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
//////////////////////////////////// specialized primitive versions ///////////////////////////////
static public class ArrayIter_int implements Iterator<Long> {
final int[] array;
int i;
ArrayIter_int(int[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Long next() {
if(array != null && i < array.length)
return Long.valueOf(array[i++]);
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_float implements Iterator<Double> {
final float[] array;
int i;
ArrayIter_float(float[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Double next() {
if(array != null && i < array.length)
return Double.valueOf(array[i++]);
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_double implements Iterator<Double> {
final double[] array;
int i;
ArrayIter_double(double[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Double next() {
if(array != null && i < array.length)
return array[i++];
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_long implements Iterator<Long> {
final long[] array;
int i;
ArrayIter_long(long[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Long next() {
if(array != null && i < array.length)
return Long.valueOf(array[i++]);
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_byte implements Iterator<Byte> {
final byte[] array;
int i;
ArrayIter_byte(byte[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Byte next() {
if(array != null && i < array.length)
return array[i++];
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_char implements Iterator<Character> {
final char[] array;
int i;
ArrayIter_char(char[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Character next() {
if(array != null && i < array.length)
return array[i++];
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_short implements Iterator<Long> {
final short[] array;
int i;
ArrayIter_short(short[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Long next() {
if(array != null && i < array.length)
return Long.valueOf(array[i++]);
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
static public class ArrayIter_boolean implements Iterator<Boolean> {
final boolean[] array;
int i;
ArrayIter_boolean(boolean[] array, int i){
this.array = array;
this.i = i;
}
public boolean hasNext() {
return array != null && i < array.length;
}
public Boolean next() {
if(array != null && i < array.length)
return Boolean.valueOf(array[i++]);
throw new java.util.NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
}
}