Mercurial > lasercutter
diff src/clojure/lang/LazySeq.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/LazySeq.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,251 @@ 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 +/* rich Jan 31, 2009 */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.util.*; 1.19 + 1.20 +public final class LazySeq extends Obj implements ISeq, List{ 1.21 + 1.22 +private IFn fn; 1.23 +private Object sv; 1.24 +private ISeq s; 1.25 + 1.26 +public LazySeq(IFn fn){ 1.27 + this.fn = fn; 1.28 +} 1.29 + 1.30 +private LazySeq(IPersistentMap meta, ISeq s){ 1.31 + super(meta); 1.32 + this.fn = null; 1.33 + this.s = s; 1.34 +} 1.35 + 1.36 +public Obj withMeta(IPersistentMap meta){ 1.37 + return new LazySeq(meta, seq()); 1.38 +} 1.39 + 1.40 +final synchronized Object sval(){ 1.41 + if(fn != null) 1.42 + { 1.43 + try 1.44 + { 1.45 + sv = fn.invoke(); 1.46 + fn = null; 1.47 + } 1.48 + catch(Exception e) 1.49 + { 1.50 + throw new RuntimeException(e); 1.51 + } 1.52 + } 1.53 + if(sv != null) 1.54 + return sv; 1.55 + return s; 1.56 +} 1.57 + 1.58 +final synchronized public ISeq seq(){ 1.59 + sval(); 1.60 + if(sv != null) 1.61 + { 1.62 + Object ls = sv; 1.63 + sv = null; 1.64 + while(ls instanceof LazySeq) 1.65 + { 1.66 + ls = ((LazySeq)ls).sval(); 1.67 + } 1.68 + s = RT.seq(ls); 1.69 + } 1.70 + return s; 1.71 +} 1.72 + 1.73 +public int count(){ 1.74 + int c = 0; 1.75 + for(ISeq s = seq(); s != null; s = s.next()) 1.76 + ++c; 1.77 + return c; 1.78 +} 1.79 + 1.80 +public Object first(){ 1.81 + seq(); 1.82 + if(s == null) 1.83 + return null; 1.84 + return s.first(); 1.85 +} 1.86 + 1.87 +public ISeq next(){ 1.88 + seq(); 1.89 + if(s == null) 1.90 + return null; 1.91 + return s.next(); 1.92 +} 1.93 + 1.94 +public ISeq more(){ 1.95 + seq(); 1.96 + if(s == null) 1.97 + return PersistentList.EMPTY; 1.98 + return s.more(); 1.99 +} 1.100 + 1.101 +public ISeq cons(Object o){ 1.102 + return RT.cons(o, seq()); 1.103 +} 1.104 + 1.105 +public IPersistentCollection empty(){ 1.106 + return PersistentList.EMPTY; 1.107 +} 1.108 + 1.109 +public boolean equiv(Object o){ 1.110 + return equals(o); 1.111 +} 1.112 + 1.113 +public int hashCode(){ 1.114 + return Util.hash(seq()); 1.115 +} 1.116 + 1.117 +public boolean equals(Object o){ 1.118 + ISeq s = seq(); 1.119 + if(s != null) 1.120 + return s.equiv(o); 1.121 + else 1.122 + return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; 1.123 +} 1.124 + 1.125 + 1.126 +// java.util.Collection implementation 1.127 + 1.128 +public Object[] toArray(){ 1.129 + return RT.seqToArray(seq()); 1.130 +} 1.131 + 1.132 +public boolean add(Object o){ 1.133 + throw new UnsupportedOperationException(); 1.134 +} 1.135 + 1.136 +public boolean remove(Object o){ 1.137 + throw new UnsupportedOperationException(); 1.138 +} 1.139 + 1.140 +public boolean addAll(Collection c){ 1.141 + throw new UnsupportedOperationException(); 1.142 +} 1.143 + 1.144 +public void clear(){ 1.145 + throw new UnsupportedOperationException(); 1.146 +} 1.147 + 1.148 +public boolean retainAll(Collection c){ 1.149 + throw new UnsupportedOperationException(); 1.150 +} 1.151 + 1.152 +public boolean removeAll(Collection c){ 1.153 + throw new UnsupportedOperationException(); 1.154 +} 1.155 + 1.156 +public boolean containsAll(Collection c){ 1.157 + for(Object o : c) 1.158 + { 1.159 + if(!contains(o)) 1.160 + return false; 1.161 + } 1.162 + return true; 1.163 +} 1.164 + 1.165 +public Object[] toArray(Object[] a){ 1.166 + if(a.length >= count()) 1.167 + { 1.168 + ISeq s = seq(); 1.169 + for(int i = 0; s != null; ++i, s = s.next()) 1.170 + { 1.171 + a[i] = s.first(); 1.172 + } 1.173 + if(a.length > count()) 1.174 + a[count()] = null; 1.175 + return a; 1.176 + } 1.177 + else 1.178 + return toArray(); 1.179 +} 1.180 + 1.181 +public int size(){ 1.182 + return count(); 1.183 +} 1.184 + 1.185 +public boolean isEmpty(){ 1.186 + return seq() == null; 1.187 +} 1.188 + 1.189 +public boolean contains(Object o){ 1.190 + for(ISeq s = seq(); s != null; s = s.next()) 1.191 + { 1.192 + if(Util.equiv(s.first(), o)) 1.193 + return true; 1.194 + } 1.195 + return false; 1.196 +} 1.197 + 1.198 +public Iterator iterator(){ 1.199 + return new SeqIterator(seq()); 1.200 +} 1.201 + 1.202 +//////////// List stuff ///////////////// 1.203 +private List reify(){ 1.204 + return new ArrayList(this); 1.205 +} 1.206 + 1.207 +public List subList(int fromIndex, int toIndex){ 1.208 + return reify().subList(fromIndex, toIndex); 1.209 +} 1.210 + 1.211 +public Object set(int index, Object element){ 1.212 + throw new UnsupportedOperationException(); 1.213 +} 1.214 + 1.215 +public Object remove(int index){ 1.216 + throw new UnsupportedOperationException(); 1.217 +} 1.218 + 1.219 +public int indexOf(Object o){ 1.220 + ISeq s = seq(); 1.221 + for(int i = 0; s != null; s = s.next(), i++) 1.222 + { 1.223 + if(Util.equiv(s.first(), o)) 1.224 + return i; 1.225 + } 1.226 + return -1; 1.227 +} 1.228 + 1.229 +public int lastIndexOf(Object o){ 1.230 + return reify().lastIndexOf(o); 1.231 +} 1.232 + 1.233 +public ListIterator listIterator(){ 1.234 + return reify().listIterator(); 1.235 +} 1.236 + 1.237 +public ListIterator listIterator(int index){ 1.238 + return reify().listIterator(index); 1.239 +} 1.240 + 1.241 +public Object get(int index){ 1.242 + return RT.nth(this, index); 1.243 +} 1.244 + 1.245 +public void add(int index, Object element){ 1.246 + throw new UnsupportedOperationException(); 1.247 +} 1.248 + 1.249 +public boolean addAll(int index, Collection c){ 1.250 + throw new UnsupportedOperationException(); 1.251 +} 1.252 + 1.253 + 1.254 +}