comparison src/clojure/lang/PersistentList.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 /**
2 * Copyright (c) Rich Hickey. All rights reserved.
3 * The use and distribution terms for this software are covered by the
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5 * which can be found in the file epl-v10.html at the root of this distribution.
6 * By using this software in any fashion, you are agreeing to be bound by
7 * the terms of this license.
8 * You must not remove this notice, or any other, from this software.
9 **/
10
11 package clojure.lang;
12
13 import java.io.Serializable;
14 import java.util.*;
15
16 public class PersistentList extends ASeq implements IPersistentList, IReduce, List, Counted {
17
18 private final Object _first;
19 private final IPersistentList _rest;
20 private final int _count;
21
22 public static IFn creator = new RestFn(){
23 final public int getRequiredArity(){
24 return 0;
25 }
26
27 final protected Object doInvoke(Object args) throws Exception{
28 if(args instanceof ArraySeq)
29 {
30 Object[] argsarray = (Object[]) ((ArraySeq) args).array;
31 IPersistentList ret = EMPTY;
32 for(int i = argsarray.length - 1; i >= 0; --i)
33 ret = (IPersistentList) ret.cons(argsarray[i]);
34 return ret;
35 }
36 LinkedList list = new LinkedList();
37 for(ISeq s = RT.seq(args); s != null; s = s.next())
38 list.add(s.first());
39 return create(list);
40 }
41
42 public IObj withMeta(IPersistentMap meta){
43 throw new UnsupportedOperationException();
44 }
45
46 public IPersistentMap meta(){
47 return null;
48 }
49 };
50
51 final public static EmptyList EMPTY = new EmptyList(null);
52
53 public PersistentList(Object first){
54 this._first = first;
55 this._rest = null;
56
57 this._count = 1;
58 }
59
60 PersistentList(IPersistentMap meta, Object _first, IPersistentList _rest, int _count){
61 super(meta);
62 this._first = _first;
63 this._rest = _rest;
64 this._count = _count;
65 }
66
67 public static IPersistentList create(List init){
68 IPersistentList ret = EMPTY;
69 for(ListIterator i = init.listIterator(init.size()); i.hasPrevious();)
70 {
71 ret = (IPersistentList) ret.cons(i.previous());
72 }
73 return ret;
74 }
75
76 public Object first(){
77 return _first;
78 }
79
80 public ISeq next(){
81 if(_count == 1)
82 return null;
83 return (ISeq) _rest;
84 }
85
86 public Object peek(){
87 return first();
88 }
89
90 public IPersistentList pop(){
91 if(_rest == null)
92 return EMPTY.withMeta(_meta);
93 return _rest;
94 }
95
96 public int count(){
97 return _count;
98 }
99
100 public PersistentList cons(Object o){
101 return new PersistentList(meta(), o, this, _count + 1);
102 }
103
104 public IPersistentCollection empty(){
105 return EMPTY.withMeta(meta());
106 }
107
108 public PersistentList withMeta(IPersistentMap meta){
109 if(meta != _meta)
110 return new PersistentList(meta, _first, _rest, _count);
111 return this;
112 }
113
114 public Object reduce(IFn f) throws Exception{
115 Object ret = first();
116 for(ISeq s = next(); s != null; s = s.next())
117 ret = f.invoke(ret, s.first());
118 return ret;
119 }
120
121 public Object reduce(IFn f, Object start) throws Exception{
122 Object ret = f.invoke(start, first());
123 for(ISeq s = next(); s != null; s = s.next())
124 ret = f.invoke(ret, s.first());
125 return ret;
126 }
127
128
129 static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted{
130
131 public int hashCode(){
132 return 1;
133 }
134
135 public boolean equals(Object o) {
136 return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null;
137 }
138
139 public boolean equiv(Object o){
140 return equals(o);
141 }
142
143 EmptyList(IPersistentMap meta){
144 super(meta);
145 }
146
147 public Object first() {
148 return null;
149 }
150
151 public ISeq next() {
152 return null;
153 }
154
155 public ISeq more() {
156 return this;
157 }
158
159 public PersistentList cons(Object o){
160 return new PersistentList(meta(), o, null, 1);
161 }
162
163 public IPersistentCollection empty(){
164 return this;
165 }
166
167 public EmptyList withMeta(IPersistentMap meta){
168 if(meta != meta())
169 return new EmptyList(meta);
170 return this;
171 }
172
173 public Object peek(){
174 return null;
175 }
176
177 public IPersistentList pop(){
178 throw new IllegalStateException("Can't pop empty list");
179 }
180
181 public int count(){
182 return 0;
183 }
184
185 public ISeq seq(){
186 return null;
187 }
188
189
190 public int size(){
191 return 0;
192 }
193
194 public boolean isEmpty(){
195 return true;
196 }
197
198 public boolean contains(Object o){
199 return false;
200 }
201
202 public Iterator iterator(){
203 return new Iterator(){
204
205 public boolean hasNext(){
206 return false;
207 }
208
209 public Object next(){
210 throw new NoSuchElementException();
211 }
212
213 public void remove(){
214 throw new UnsupportedOperationException();
215 }
216 };
217 }
218
219 public Object[] toArray(){
220 return RT.EMPTY_ARRAY;
221 }
222
223 public boolean add(Object o){
224 throw new UnsupportedOperationException();
225 }
226
227 public boolean remove(Object o){
228 throw new UnsupportedOperationException();
229 }
230
231 public boolean addAll(Collection collection){
232 throw new UnsupportedOperationException();
233 }
234
235 public void clear(){
236 throw new UnsupportedOperationException();
237 }
238
239 public boolean retainAll(Collection collection){
240 throw new UnsupportedOperationException();
241 }
242
243 public boolean removeAll(Collection collection){
244 throw new UnsupportedOperationException();
245 }
246
247 public boolean containsAll(Collection collection){
248 return collection.isEmpty();
249 }
250
251 public Object[] toArray(Object[] objects){
252 if(objects.length > 0)
253 objects[0] = null;
254 return objects;
255 }
256
257 //////////// List stuff /////////////////
258 private List reify(){
259 return Collections.unmodifiableList(new ArrayList(this));
260 }
261
262 public List subList(int fromIndex, int toIndex){
263 return reify().subList(fromIndex, toIndex);
264 }
265
266 public Object set(int index, Object element){
267 throw new UnsupportedOperationException();
268 }
269
270 public Object remove(int index){
271 throw new UnsupportedOperationException();
272 }
273
274 public int indexOf(Object o){
275 ISeq s = seq();
276 for(int i = 0; s != null; s = s.next(), i++)
277 {
278 if(Util.equiv(s.first(), o))
279 return i;
280 }
281 return -1;
282 }
283
284 public int lastIndexOf(Object o){
285 return reify().lastIndexOf(o);
286 }
287
288 public ListIterator listIterator(){
289 return reify().listIterator();
290 }
291
292 public ListIterator listIterator(int index){
293 return reify().listIterator(index);
294 }
295
296 public Object get(int index){
297 return RT.nth(this, index);
298 }
299
300 public void add(int index, Object element){
301 throw new UnsupportedOperationException();
302 }
303
304 public boolean addAll(int index, Collection c){
305 throw new UnsupportedOperationException();
306 }
307
308
309 }
310
311 }