view src/clojure/lang/ASeq.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 abstract class ASeq extends Obj implements ISeq, List, Serializable {
17 transient int _hash = -1;
19 public String toString(){
20 return RT.printString(this);
21 }
23 public IPersistentCollection empty(){
24 return PersistentList.EMPTY;
25 }
27 protected ASeq(IPersistentMap meta){
28 super(meta);
29 }
32 protected ASeq(){
33 }
35 public boolean equiv(Object obj){
37 if(!(obj instanceof Sequential || obj instanceof List))
38 return false;
39 ISeq ms = RT.seq(obj);
40 for(ISeq s = seq(); s != null; s = s.next(), ms = ms.next())
41 {
42 if(ms == null || !Util.equiv(s.first(), ms.first()))
43 return false;
44 }
45 return ms == null;
47 }
49 public boolean equals(Object obj){
50 if(this == obj) return true;
51 if(!(obj instanceof Sequential || obj instanceof List))
52 return false;
53 ISeq ms = RT.seq(obj);
54 for(ISeq s = seq(); s != null; s = s.next(), ms = ms.next())
55 {
56 if(ms == null || !Util.equals(s.first(), ms.first()))
57 return false;
58 }
59 return ms == null;
61 }
63 public int hashCode(){
64 if(_hash == -1)
65 {
66 int hash = 1;
67 for(ISeq s = seq(); s != null; s = s.next())
68 {
69 hash = 31 * hash + (s.first() == null ? 0 : s.first().hashCode());
70 }
71 this._hash = hash;
72 }
73 return _hash;
74 }
77 //public Object reduce(IFn f) throws Exception{
78 // Object ret = first();
79 // for(ISeq s = rest(); s != null; s = s.rest())
80 // ret = f.invoke(ret, s.first());
81 // return ret;
82 //}
83 //
84 //public Object reduce(IFn f, Object start) throws Exception{
85 // Object ret = f.invoke(start, first());
86 // for(ISeq s = rest(); s != null; s = s.rest())
87 // ret = f.invoke(ret, s.first());
88 // return ret;
89 //}
91 //public Object peek(){
92 // return first();
93 //}
94 //
95 //public IPersistentList pop(){
96 // return rest();
97 //}
99 public int count(){
100 int i = 1;
101 for(ISeq s = next(); s != null; s = s.next(), i++)
102 if(s instanceof Counted)
103 return i + s.count();
104 return i;
105 }
107 final public ISeq seq(){
108 return this;
109 }
111 public ISeq cons(Object o){
112 return new Cons(o, this);
113 }
115 public ISeq more(){
116 ISeq s = next();
117 if(s == null)
118 return PersistentList.EMPTY;
119 return s;
120 }
122 //final public ISeq rest(){
123 // Seqable m = more();
124 // if(m == null)
125 // return null;
126 // return m.seq();
127 //}
129 // java.util.Collection implementation
131 public Object[] toArray(){
132 return RT.seqToArray(seq());
133 }
135 public boolean add(Object o){
136 throw new UnsupportedOperationException();
137 }
139 public boolean remove(Object o){
140 throw new UnsupportedOperationException();
141 }
143 public boolean addAll(Collection c){
144 throw new UnsupportedOperationException();
145 }
147 public void clear(){
148 throw new UnsupportedOperationException();
149 }
151 public boolean retainAll(Collection c){
152 throw new UnsupportedOperationException();
153 }
155 public boolean removeAll(Collection c){
156 throw new UnsupportedOperationException();
157 }
159 public boolean containsAll(Collection c){
160 for(Object o : c)
161 {
162 if(!contains(o))
163 return false;
164 }
165 return true;
166 }
168 public Object[] toArray(Object[] a){
169 if(a.length >= count())
170 {
171 ISeq s = seq();
172 for(int i = 0; s != null; ++i, s = s.next())
173 {
174 a[i] = s.first();
175 }
176 if(a.length > count())
177 a[count()] = null;
178 return a;
179 }
180 else
181 return toArray();
182 }
184 public int size(){
185 return count();
186 }
188 public boolean isEmpty(){
189 return seq() == null;
190 }
192 public boolean contains(Object o){
193 for(ISeq s = seq(); s != null; s = s.next())
194 {
195 if(Util.equiv(s.first(), o))
196 return true;
197 }
198 return false;
199 }
202 public Iterator iterator(){
203 return new SeqIterator(this);
204 }
208 //////////// List stuff /////////////////
209 private List reify(){
210 return Collections.unmodifiableList(new ArrayList(this));
211 }
213 public List subList(int fromIndex, int toIndex){
214 return reify().subList(fromIndex, toIndex);
215 }
217 public Object set(int index, Object element){
218 throw new UnsupportedOperationException();
219 }
221 public Object remove(int index){
222 throw new UnsupportedOperationException();
223 }
225 public int indexOf(Object o){
226 ISeq s = seq();
227 for(int i = 0; s != null; s = s.next(), i++)
228 {
229 if(Util.equiv(s.first(), o))
230 return i;
231 }
232 return -1;
233 }
235 public int lastIndexOf(Object o){
236 return reify().lastIndexOf(o);
237 }
239 public ListIterator listIterator(){
240 return reify().listIterator();
241 }
243 public ListIterator listIterator(int index){
244 return reify().listIterator(index);
245 }
247 public Object get(int index){
248 return RT.nth(this, index);
249 }
251 public void add(int index, Object element){
252 throw new UnsupportedOperationException();
253 }
255 public boolean addAll(int index, Collection c){
256 throw new UnsupportedOperationException();
257 }
259 }