/
PersistentList.java
344 lines (269 loc) · 7.02 KB
/
PersistentList.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* 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.io.Serializable;
import java.util.*;
public class PersistentList extends ASeq implements IPersistentList, IReduce, List, Counted {
private static final long serialVersionUID = -8833289659955219995L;
private final Object _first;
private final IPersistentList _rest;
private final int _count;
static public class Primordial extends RestFn{
final public int getRequiredArity(){
return 0;
}
final protected Object doInvoke(Object args) {
if(args instanceof ArraySeq)
{
Object[] argsarray = ((ArraySeq) args).array;
IPersistentList ret = EMPTY;
for(int i = argsarray.length - 1; i >= ((ArraySeq)args).i; --i)
ret = (IPersistentList) ret.cons(argsarray[i]);
return ret;
}
LinkedList list = new LinkedList();
for(ISeq s = RT.seq(args); s != null; s = s.next())
list.add(s.first());
return create(list);
}
static public Object invokeStatic(ISeq args) {
if(args instanceof ArraySeq)
{
Object[] argsarray = ((ArraySeq) args).array;
IPersistentList ret = EMPTY;
for(int i = argsarray.length - 1; i >= 0; --i)
ret = (IPersistentList) ret.cons(argsarray[i]);
return ret;
}
LinkedList list = new LinkedList();
for(ISeq s = RT.seq(args); s != null; s = s.next())
list.add(s.first());
return create(list);
}
public IObj withMeta(IPersistentMap meta){
throw new UnsupportedOperationException();
}
public IPersistentMap meta(){
return null;
}
}
public static IFn creator = new Primordial();
final public static EmptyList EMPTY = new EmptyList(null);
public PersistentList(Object first){
this._first = first;
this._rest = null;
this._count = 1;
}
PersistentList(IPersistentMap meta, Object _first, IPersistentList _rest, int _count){
super(meta);
this._first = _first;
this._rest = _rest;
this._count = _count;
}
public static IPersistentList create(List init){
IPersistentList ret = EMPTY;
for(ListIterator i = init.listIterator(init.size()); i.hasPrevious();)
{
ret = (IPersistentList) ret.cons(i.previous());
}
return ret;
}
public Object first(){
return _first;
}
public ISeq next(){
if(_count == 1)
return null;
return (ISeq) _rest;
}
public Object peek(){
return first();
}
public IPersistentList pop(){
if(_rest == null)
return EMPTY.withMeta(_meta);
return _rest;
}
public int count(){
return _count;
}
public PersistentList cons(Object o){
return new PersistentList(meta(), o, this, _count + 1);
}
public IPersistentCollection empty(){
return EMPTY.withMeta(meta());
}
public PersistentList withMeta(IPersistentMap meta){
if(meta != _meta)
return new PersistentList(meta, _first, _rest, _count);
return this;
}
public Object reduce(IFn f) {
Object ret = first();
for(ISeq s = next(); s != null; s = s.next()) {
ret = f.invoke(ret, s.first());
if (RT.isReduced(ret)) return ((IDeref)ret).deref();;
}
return ret;
}
public Object reduce(IFn f, Object start) {
Object ret = f.invoke(start, first());
for(ISeq s = next(); s != null; s = s.next()) {
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
ret = f.invoke(ret, s.first());
}
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
return ret;
}
static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted, IHashEq{
static final int hasheq = Murmur3.hashOrdered(Collections.EMPTY_LIST);
public int hashCode(){
return 1;
}
public int hasheq(){
return hasheq;
}
public String toString() {
return "()";
}
public boolean equals(Object o) {
return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null;
}
public boolean equiv(Object o){
return equals(o);
}
EmptyList(IPersistentMap meta){
super(meta);
}
public Object first() {
return null;
}
public ISeq next() {
return null;
}
public ISeq more() {
return this;
}
public PersistentList cons(Object o){
return new PersistentList(meta(), o, null, 1);
}
public IPersistentCollection empty(){
return this;
}
public EmptyList withMeta(IPersistentMap meta){
if(meta != meta())
return new EmptyList(meta);
return this;
}
public Object peek(){
return null;
}
public IPersistentList pop(){
throw new IllegalStateException("Can't pop empty list");
}
public int count(){
return 0;
}
public ISeq seq(){
return null;
}
public int size(){
return 0;
}
public boolean isEmpty(){
return true;
}
public boolean contains(Object o){
return false;
}
public Iterator iterator(){
return new Iterator(){
public boolean hasNext(){
return false;
}
public Object next(){
throw new NoSuchElementException();
}
public void remove(){
throw new UnsupportedOperationException();
}
};
}
public Object[] toArray(){
return RT.EMPTY_ARRAY;
}
public boolean add(Object o){
throw new UnsupportedOperationException();
}
public boolean remove(Object o){
throw new UnsupportedOperationException();
}
public boolean addAll(Collection collection){
throw new UnsupportedOperationException();
}
public void clear(){
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection collection){
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection collection){
throw new UnsupportedOperationException();
}
public boolean containsAll(Collection collection){
return collection.isEmpty();
}
public Object[] toArray(Object[] objects){
if(objects.length > 0)
objects[0] = null;
return objects;
}
//////////// List stuff /////////////////
private List reify(){
return Collections.unmodifiableList(new ArrayList(this));
}
public List subList(int fromIndex, int toIndex){
return reify().subList(fromIndex, toIndex);
}
public Object set(int index, Object element){
throw new UnsupportedOperationException();
}
public Object remove(int index){
throw new UnsupportedOperationException();
}
public int indexOf(Object o){
ISeq s = seq();
for(int i = 0; s != null; s = s.next(), i++)
{
if(Util.equiv(s.first(), o))
return i;
}
return -1;
}
public int lastIndexOf(Object o){
return reify().lastIndexOf(o);
}
public ListIterator listIterator(){
return reify().listIterator();
}
public ListIterator listIterator(int index){
return reify().listIterator(index);
}
public Object get(int index){
return RT.nth(this, index);
}
public void add(int index, Object element){
throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection c){
throw new UnsupportedOperationException();
}
}
}