Mercurial > lasercutter
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/PersistentList.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,311 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +package clojure.lang; 1.15 + 1.16 +import java.io.Serializable; 1.17 +import java.util.*; 1.18 + 1.19 +public class PersistentList extends ASeq implements IPersistentList, IReduce, List, Counted { 1.20 + 1.21 +private final Object _first; 1.22 +private final IPersistentList _rest; 1.23 +private final int _count; 1.24 + 1.25 +public static IFn creator = new RestFn(){ 1.26 + final public int getRequiredArity(){ 1.27 + return 0; 1.28 + } 1.29 + 1.30 + final protected Object doInvoke(Object args) throws Exception{ 1.31 + if(args instanceof ArraySeq) 1.32 + { 1.33 + Object[] argsarray = (Object[]) ((ArraySeq) args).array; 1.34 + IPersistentList ret = EMPTY; 1.35 + for(int i = argsarray.length - 1; i >= 0; --i) 1.36 + ret = (IPersistentList) ret.cons(argsarray[i]); 1.37 + return ret; 1.38 + } 1.39 + LinkedList list = new LinkedList(); 1.40 + for(ISeq s = RT.seq(args); s != null; s = s.next()) 1.41 + list.add(s.first()); 1.42 + return create(list); 1.43 + } 1.44 + 1.45 + public IObj withMeta(IPersistentMap meta){ 1.46 + throw new UnsupportedOperationException(); 1.47 + } 1.48 + 1.49 + public IPersistentMap meta(){ 1.50 + return null; 1.51 + } 1.52 +}; 1.53 + 1.54 +final public static EmptyList EMPTY = new EmptyList(null); 1.55 + 1.56 +public PersistentList(Object first){ 1.57 + this._first = first; 1.58 + this._rest = null; 1.59 + 1.60 + this._count = 1; 1.61 +} 1.62 + 1.63 +PersistentList(IPersistentMap meta, Object _first, IPersistentList _rest, int _count){ 1.64 + super(meta); 1.65 + this._first = _first; 1.66 + this._rest = _rest; 1.67 + this._count = _count; 1.68 +} 1.69 + 1.70 +public static IPersistentList create(List init){ 1.71 + IPersistentList ret = EMPTY; 1.72 + for(ListIterator i = init.listIterator(init.size()); i.hasPrevious();) 1.73 + { 1.74 + ret = (IPersistentList) ret.cons(i.previous()); 1.75 + } 1.76 + return ret; 1.77 +} 1.78 + 1.79 +public Object first(){ 1.80 + return _first; 1.81 +} 1.82 + 1.83 +public ISeq next(){ 1.84 + if(_count == 1) 1.85 + return null; 1.86 + return (ISeq) _rest; 1.87 +} 1.88 + 1.89 +public Object peek(){ 1.90 + return first(); 1.91 +} 1.92 + 1.93 +public IPersistentList pop(){ 1.94 + if(_rest == null) 1.95 + return EMPTY.withMeta(_meta); 1.96 + return _rest; 1.97 +} 1.98 + 1.99 +public int count(){ 1.100 + return _count; 1.101 +} 1.102 + 1.103 +public PersistentList cons(Object o){ 1.104 + return new PersistentList(meta(), o, this, _count + 1); 1.105 +} 1.106 + 1.107 +public IPersistentCollection empty(){ 1.108 + return EMPTY.withMeta(meta()); 1.109 +} 1.110 + 1.111 +public PersistentList withMeta(IPersistentMap meta){ 1.112 + if(meta != _meta) 1.113 + return new PersistentList(meta, _first, _rest, _count); 1.114 + return this; 1.115 +} 1.116 + 1.117 +public Object reduce(IFn f) throws Exception{ 1.118 + Object ret = first(); 1.119 + for(ISeq s = next(); s != null; s = s.next()) 1.120 + ret = f.invoke(ret, s.first()); 1.121 + return ret; 1.122 +} 1.123 + 1.124 +public Object reduce(IFn f, Object start) throws Exception{ 1.125 + Object ret = f.invoke(start, first()); 1.126 + for(ISeq s = next(); s != null; s = s.next()) 1.127 + ret = f.invoke(ret, s.first()); 1.128 + return ret; 1.129 +} 1.130 + 1.131 + 1.132 + static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted{ 1.133 + 1.134 + public int hashCode(){ 1.135 + return 1; 1.136 + } 1.137 + 1.138 + public boolean equals(Object o) { 1.139 + return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; 1.140 + } 1.141 + 1.142 + public boolean equiv(Object o){ 1.143 + return equals(o); 1.144 + } 1.145 + 1.146 + EmptyList(IPersistentMap meta){ 1.147 + super(meta); 1.148 + } 1.149 + 1.150 + public Object first() { 1.151 + return null; 1.152 + } 1.153 + 1.154 + public ISeq next() { 1.155 + return null; 1.156 + } 1.157 + 1.158 + public ISeq more() { 1.159 + return this; 1.160 + } 1.161 + 1.162 + public PersistentList cons(Object o){ 1.163 + return new PersistentList(meta(), o, null, 1); 1.164 + } 1.165 + 1.166 + public IPersistentCollection empty(){ 1.167 + return this; 1.168 + } 1.169 + 1.170 + public EmptyList withMeta(IPersistentMap meta){ 1.171 + if(meta != meta()) 1.172 + return new EmptyList(meta); 1.173 + return this; 1.174 + } 1.175 + 1.176 + public Object peek(){ 1.177 + return null; 1.178 + } 1.179 + 1.180 + public IPersistentList pop(){ 1.181 + throw new IllegalStateException("Can't pop empty list"); 1.182 + } 1.183 + 1.184 + public int count(){ 1.185 + return 0; 1.186 + } 1.187 + 1.188 + public ISeq seq(){ 1.189 + return null; 1.190 + } 1.191 + 1.192 + 1.193 + public int size(){ 1.194 + return 0; 1.195 + } 1.196 + 1.197 + public boolean isEmpty(){ 1.198 + return true; 1.199 + } 1.200 + 1.201 + public boolean contains(Object o){ 1.202 + return false; 1.203 + } 1.204 + 1.205 + public Iterator iterator(){ 1.206 + return new Iterator(){ 1.207 + 1.208 + public boolean hasNext(){ 1.209 + return false; 1.210 + } 1.211 + 1.212 + public Object next(){ 1.213 + throw new NoSuchElementException(); 1.214 + } 1.215 + 1.216 + public void remove(){ 1.217 + throw new UnsupportedOperationException(); 1.218 + } 1.219 + }; 1.220 + } 1.221 + 1.222 + public Object[] toArray(){ 1.223 + return RT.EMPTY_ARRAY; 1.224 + } 1.225 + 1.226 + public boolean add(Object o){ 1.227 + throw new UnsupportedOperationException(); 1.228 + } 1.229 + 1.230 + public boolean remove(Object o){ 1.231 + throw new UnsupportedOperationException(); 1.232 + } 1.233 + 1.234 + public boolean addAll(Collection collection){ 1.235 + throw new UnsupportedOperationException(); 1.236 + } 1.237 + 1.238 + public void clear(){ 1.239 + throw new UnsupportedOperationException(); 1.240 + } 1.241 + 1.242 + public boolean retainAll(Collection collection){ 1.243 + throw new UnsupportedOperationException(); 1.244 + } 1.245 + 1.246 + public boolean removeAll(Collection collection){ 1.247 + throw new UnsupportedOperationException(); 1.248 + } 1.249 + 1.250 + public boolean containsAll(Collection collection){ 1.251 + return collection.isEmpty(); 1.252 + } 1.253 + 1.254 + public Object[] toArray(Object[] objects){ 1.255 + if(objects.length > 0) 1.256 + objects[0] = null; 1.257 + return objects; 1.258 + } 1.259 + 1.260 + //////////// List stuff ///////////////// 1.261 + private List reify(){ 1.262 + return Collections.unmodifiableList(new ArrayList(this)); 1.263 + } 1.264 + 1.265 + public List subList(int fromIndex, int toIndex){ 1.266 + return reify().subList(fromIndex, toIndex); 1.267 + } 1.268 + 1.269 + public Object set(int index, Object element){ 1.270 + throw new UnsupportedOperationException(); 1.271 + } 1.272 + 1.273 + public Object remove(int index){ 1.274 + throw new UnsupportedOperationException(); 1.275 + } 1.276 + 1.277 + public int indexOf(Object o){ 1.278 + ISeq s = seq(); 1.279 + for(int i = 0; s != null; s = s.next(), i++) 1.280 + { 1.281 + if(Util.equiv(s.first(), o)) 1.282 + return i; 1.283 + } 1.284 + return -1; 1.285 + } 1.286 + 1.287 + public int lastIndexOf(Object o){ 1.288 + return reify().lastIndexOf(o); 1.289 + } 1.290 + 1.291 + public ListIterator listIterator(){ 1.292 + return reify().listIterator(); 1.293 + } 1.294 + 1.295 + public ListIterator listIterator(int index){ 1.296 + return reify().listIterator(index); 1.297 + } 1.298 + 1.299 + public Object get(int index){ 1.300 + return RT.nth(this, index); 1.301 + } 1.302 + 1.303 + public void add(int index, Object element){ 1.304 + throw new UnsupportedOperationException(); 1.305 + } 1.306 + 1.307 + public boolean addAll(int index, Collection c){ 1.308 + throw new UnsupportedOperationException(); 1.309 + } 1.310 + 1.311 + 1.312 +} 1.313 + 1.314 +}