view 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
line wrap: on
line source
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 **/
11 package clojure.lang;
13 import java.io.Serializable;
14 import java.util.*;
16 public class PersistentList extends ASeq implements IPersistentList, IReduce, List, Counted {
18 private final Object _first;
19 private final IPersistentList _rest;
20 private final int _count;
22 public static IFn creator = new RestFn(){
23 final public int getRequiredArity(){
24 return 0;
25 }
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 }
42 public IObj withMeta(IPersistentMap meta){
43 throw new UnsupportedOperationException();
44 }
46 public IPersistentMap meta(){
47 return null;
48 }
49 };
51 final public static EmptyList EMPTY = new EmptyList(null);
53 public PersistentList(Object first){
54 this._first = first;
55 this._rest = null;
57 this._count = 1;
58 }
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 }
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 }
76 public Object first(){
77 return _first;
78 }
80 public ISeq next(){
81 if(_count == 1)
82 return null;
83 return (ISeq) _rest;
84 }
86 public Object peek(){
87 return first();
88 }
90 public IPersistentList pop(){
91 if(_rest == null)
92 return EMPTY.withMeta(_meta);
93 return _rest;
94 }
96 public int count(){
97 return _count;
98 }
100 public PersistentList cons(Object o){
101 return new PersistentList(meta(), o, this, _count + 1);
102 }
104 public IPersistentCollection empty(){
105 return EMPTY.withMeta(meta());
106 }
108 public PersistentList withMeta(IPersistentMap meta){
109 if(meta != _meta)
110 return new PersistentList(meta, _first, _rest, _count);
111 return this;
112 }
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 }
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 }
129 static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted{
131 public int hashCode(){
132 return 1;
133 }
135 public boolean equals(Object o) {
136 return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null;
137 }
139 public boolean equiv(Object o){
140 return equals(o);
141 }
143 EmptyList(IPersistentMap meta){
144 super(meta);
145 }
147 public Object first() {
148 return null;
149 }
151 public ISeq next() {
152 return null;
153 }
155 public ISeq more() {
156 return this;
157 }
159 public PersistentList cons(Object o){
160 return new PersistentList(meta(), o, null, 1);
161 }
163 public IPersistentCollection empty(){
164 return this;
165 }
167 public EmptyList withMeta(IPersistentMap meta){
168 if(meta != meta())
169 return new EmptyList(meta);
170 return this;
171 }
173 public Object peek(){
174 return null;
175 }
177 public IPersistentList pop(){
178 throw new IllegalStateException("Can't pop empty list");
179 }
181 public int count(){
182 return 0;
183 }
185 public ISeq seq(){
186 return null;
187 }
190 public int size(){
191 return 0;
192 }
194 public boolean isEmpty(){
195 return true;
196 }
198 public boolean contains(Object o){
199 return false;
200 }
202 public Iterator iterator(){
203 return new Iterator(){
205 public boolean hasNext(){
206 return false;
207 }
209 public Object next(){
210 throw new NoSuchElementException();
211 }
213 public void remove(){
214 throw new UnsupportedOperationException();
215 }
216 };
217 }
219 public Object[] toArray(){
220 return RT.EMPTY_ARRAY;
221 }
223 public boolean add(Object o){
224 throw new UnsupportedOperationException();
225 }
227 public boolean remove(Object o){
228 throw new UnsupportedOperationException();
229 }
231 public boolean addAll(Collection collection){
232 throw new UnsupportedOperationException();
233 }
235 public void clear(){
236 throw new UnsupportedOperationException();
237 }
239 public boolean retainAll(Collection collection){
240 throw new UnsupportedOperationException();
241 }
243 public boolean removeAll(Collection collection){
244 throw new UnsupportedOperationException();
245 }
247 public boolean containsAll(Collection collection){
248 return collection.isEmpty();
249 }
251 public Object[] toArray(Object[] objects){
252 if(objects.length > 0)
253 objects[0] = null;
254 return objects;
255 }
257 //////////// List stuff /////////////////
258 private List reify(){
259 return Collections.unmodifiableList(new ArrayList(this));
260 }
262 public List subList(int fromIndex, int toIndex){
263 return reify().subList(fromIndex, toIndex);
264 }
266 public Object set(int index, Object element){
267 throw new UnsupportedOperationException();
268 }
270 public Object remove(int index){
271 throw new UnsupportedOperationException();
272 }
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 }
284 public int lastIndexOf(Object o){
285 return reify().lastIndexOf(o);
286 }
288 public ListIterator listIterator(){
289 return reify().listIterator();
290 }
292 public ListIterator listIterator(int index){
293 return reify().listIterator(index);
294 }
296 public Object get(int index){
297 return RT.nth(this, index);
298 }
300 public void add(int index, Object element){
301 throw new UnsupportedOperationException();
302 }
304 public boolean addAll(int index, Collection c){
305 throw new UnsupportedOperationException();
306 }
309 }
311 }