diff src/clojure/lang/APersistentMap.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/APersistentMap.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,384 @@
     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 abstract class APersistentMap extends AFn implements IPersistentMap, Map, Iterable, Serializable, MapEquivalence {
    1.20 +int _hash = -1;
    1.21 +
    1.22 +public String toString(){
    1.23 +	return RT.printString(this);
    1.24 +}
    1.25 +
    1.26 +public IPersistentCollection cons(Object o){
    1.27 +	if(o instanceof Map.Entry)
    1.28 +		{
    1.29 +		Map.Entry e = (Map.Entry) o;
    1.30 +
    1.31 +		return assoc(e.getKey(), e.getValue());
    1.32 +		}
    1.33 +	else if(o instanceof IPersistentVector)
    1.34 +		{
    1.35 +		IPersistentVector v = (IPersistentVector) o;
    1.36 +		if(v.count() != 2)
    1.37 +			throw new IllegalArgumentException("Vector arg to map conj must be a pair");
    1.38 +		return assoc(v.nth(0), v.nth(1));
    1.39 +		}
    1.40 +
    1.41 +	IPersistentMap ret = this;
    1.42 +	for(ISeq es = RT.seq(o); es != null; es = es.next())
    1.43 +		{
    1.44 +		Map.Entry e = (Map.Entry) es.first();
    1.45 +		ret = ret.assoc(e.getKey(), e.getValue());
    1.46 +		}
    1.47 +	return ret;
    1.48 +}
    1.49 +
    1.50 +public boolean equals(Object obj){
    1.51 +	return mapEquals(this, obj);
    1.52 +}
    1.53 +
    1.54 +static public boolean mapEquals(IPersistentMap m1, Object obj){
    1.55 +	if(m1 == obj) return true;
    1.56 +	if(!(obj instanceof Map))
    1.57 +		return false;
    1.58 +	Map m = (Map) obj;
    1.59 +
    1.60 +	if(m.size() != m1.count() || m.hashCode() != m1.hashCode())
    1.61 +		return false;
    1.62 +
    1.63 +	for(ISeq s = m1.seq(); s != null; s = s.next())
    1.64 +		{
    1.65 +		Map.Entry e = (Map.Entry) s.first();
    1.66 +		boolean found = m.containsKey(e.getKey());
    1.67 +
    1.68 +		if(!found || !Util.equals(e.getValue(), m.get(e.getKey())))
    1.69 +			return false;
    1.70 +		}
    1.71 +
    1.72 +	return true;
    1.73 +}
    1.74 +
    1.75 +public boolean equiv(Object obj){
    1.76 +	if(!(obj instanceof Map))
    1.77 +		return false;
    1.78 +	if(obj instanceof IPersistentMap && !(obj instanceof MapEquivalence))
    1.79 +		return false;
    1.80 +	
    1.81 +	Map m = (Map) obj;
    1.82 +
    1.83 +	if(m.size() != size())
    1.84 +		return false;
    1.85 +
    1.86 +	for(ISeq s = seq(); s != null; s = s.next())
    1.87 +		{
    1.88 +		Map.Entry e = (Map.Entry) s.first();
    1.89 +		boolean found = m.containsKey(e.getKey());
    1.90 +
    1.91 +		if(!found || !Util.equiv(e.getValue(), m.get(e.getKey())))
    1.92 +			return false;
    1.93 +		}
    1.94 +
    1.95 +	return true;
    1.96 +}
    1.97 +public int hashCode(){
    1.98 +	if(_hash == -1)
    1.99 +		{
   1.100 +		this._hash = mapHash(this);
   1.101 +		}
   1.102 +	return _hash;
   1.103 +}
   1.104 +
   1.105 +static public int mapHash(IPersistentMap m){
   1.106 +	int hash = 0;
   1.107 +	for(ISeq s = m.seq(); s != null; s = s.next())
   1.108 +		{
   1.109 +		Map.Entry e = (Map.Entry) s.first();
   1.110 +		hash += (e.getKey() == null ? 0 : e.getKey().hashCode()) ^
   1.111 +				(e.getValue() == null ? 0 : e.getValue().hashCode());
   1.112 +		}
   1.113 +	return hash;
   1.114 +}
   1.115 +
   1.116 +static public class KeySeq extends ASeq{
   1.117 +	ISeq seq;
   1.118 +
   1.119 +	static public KeySeq create(ISeq seq){
   1.120 +		if(seq == null)
   1.121 +			return null;
   1.122 +		return new KeySeq(seq);
   1.123 +	}
   1.124 +
   1.125 +	private KeySeq(ISeq seq){
   1.126 +		this.seq = seq;
   1.127 +	}
   1.128 +
   1.129 +	private KeySeq(IPersistentMap meta, ISeq seq){
   1.130 +		super(meta);
   1.131 +		this.seq = seq;
   1.132 +	}
   1.133 +
   1.134 +	public Object first(){
   1.135 +		return ((Map.Entry) seq.first()).getKey();
   1.136 +	}
   1.137 +
   1.138 +	public ISeq next(){
   1.139 +		return create(seq.next());
   1.140 +	}
   1.141 +
   1.142 +	public KeySeq withMeta(IPersistentMap meta){
   1.143 +		return new KeySeq(meta, seq);
   1.144 +	}
   1.145 +}
   1.146 +
   1.147 +static public class ValSeq extends ASeq{
   1.148 +	ISeq seq;
   1.149 +
   1.150 +	static public ValSeq create(ISeq seq){
   1.151 +		if(seq == null)
   1.152 +			return null;
   1.153 +		return new ValSeq(seq);
   1.154 +	}
   1.155 +
   1.156 +	private ValSeq(ISeq seq){
   1.157 +		this.seq = seq;
   1.158 +	}
   1.159 +
   1.160 +	private ValSeq(IPersistentMap meta, ISeq seq){
   1.161 +		super(meta);
   1.162 +		this.seq = seq;
   1.163 +	}
   1.164 +
   1.165 +	public Object first(){
   1.166 +		return ((Map.Entry) seq.first()).getValue();
   1.167 +	}
   1.168 +
   1.169 +	public ISeq next(){
   1.170 +		return create(seq.next());
   1.171 +	}
   1.172 +
   1.173 +	public ValSeq withMeta(IPersistentMap meta){
   1.174 +		return new ValSeq(meta, seq);
   1.175 +	}
   1.176 +}
   1.177 +
   1.178 +
   1.179 +public Object invoke(Object arg1) throws Exception{
   1.180 +	return valAt(arg1);
   1.181 +}
   1.182 +
   1.183 +public Object invoke(Object arg1, Object notFound) throws Exception{
   1.184 +	return valAt(arg1, notFound);
   1.185 +}
   1.186 +
   1.187 +// java.util.Map implementation
   1.188 +
   1.189 +public void clear(){
   1.190 +	throw new UnsupportedOperationException();
   1.191 +}
   1.192 +
   1.193 +public boolean containsValue(Object value){
   1.194 +	return values().contains(value);
   1.195 +}
   1.196 +
   1.197 +public Set entrySet(){
   1.198 +	return new AbstractSet(){
   1.199 +
   1.200 +		public Iterator iterator(){
   1.201 +			return APersistentMap.this.iterator();
   1.202 +		}
   1.203 +
   1.204 +		public int size(){
   1.205 +			return count();
   1.206 +		}
   1.207 +
   1.208 +		public int hashCode(){
   1.209 +			return APersistentMap.this.hashCode();
   1.210 +		}
   1.211 +
   1.212 +		public boolean contains(Object o){
   1.213 +			if(o instanceof Entry)
   1.214 +				{
   1.215 +				Entry e = (Entry) o;
   1.216 +				Entry found = entryAt(e.getKey());
   1.217 +				if(found != null && Util.equals(found.getValue(), e.getValue()))
   1.218 +					return true;
   1.219 +				}
   1.220 +			return false;
   1.221 +		}
   1.222 +	};
   1.223 +}
   1.224 +
   1.225 +public Object get(Object key){
   1.226 +	return valAt(key);
   1.227 +}
   1.228 +
   1.229 +public boolean isEmpty(){
   1.230 +	return count() == 0;
   1.231 +}
   1.232 +
   1.233 +public Set keySet(){
   1.234 +	return new AbstractSet(){
   1.235 +
   1.236 +		public Iterator iterator(){
   1.237 +			final Iterator mi = APersistentMap.this.iterator();
   1.238 +
   1.239 +			return new Iterator(){
   1.240 +
   1.241 +
   1.242 +				public boolean hasNext(){
   1.243 +					return mi.hasNext();
   1.244 +				}
   1.245 +
   1.246 +				public Object next(){
   1.247 +					Entry e = (Entry) mi.next();
   1.248 +					return e.getKey();
   1.249 +				}
   1.250 +
   1.251 +				public void remove(){
   1.252 +					throw new UnsupportedOperationException();
   1.253 +				}
   1.254 +			};
   1.255 +		}
   1.256 +
   1.257 +		public int size(){
   1.258 +			return count();
   1.259 +		}
   1.260 +
   1.261 +		public boolean contains(Object o){
   1.262 +			return APersistentMap.this.containsKey(o);
   1.263 +		}
   1.264 +	};
   1.265 +}
   1.266 +
   1.267 +public Object put(Object key, Object value){
   1.268 +	throw new UnsupportedOperationException();
   1.269 +}
   1.270 +
   1.271 +public void putAll(Map t){
   1.272 +	throw new UnsupportedOperationException();
   1.273 +}
   1.274 +
   1.275 +public Object remove(Object key){
   1.276 +	throw new UnsupportedOperationException();
   1.277 +}
   1.278 +
   1.279 +public int size(){
   1.280 +	return count();
   1.281 +}
   1.282 +
   1.283 +public Collection values(){
   1.284 +	return new AbstractCollection(){
   1.285 +
   1.286 +		public Iterator iterator(){
   1.287 +			final Iterator mi = APersistentMap.this.iterator();
   1.288 +
   1.289 +			return new Iterator(){
   1.290 +
   1.291 +
   1.292 +				public boolean hasNext(){
   1.293 +					return mi.hasNext();
   1.294 +				}
   1.295 +
   1.296 +				public Object next(){
   1.297 +					Entry e = (Entry) mi.next();
   1.298 +					return e.getValue();
   1.299 +				}
   1.300 +
   1.301 +				public void remove(){
   1.302 +					throw new UnsupportedOperationException();
   1.303 +				}
   1.304 +			};
   1.305 +		}
   1.306 +
   1.307 +		public int size(){
   1.308 +			return count();
   1.309 +		}
   1.310 +	};
   1.311 +}
   1.312 +
   1.313 +/*
   1.314 +// java.util.Collection implementation
   1.315 +
   1.316 +public Object[] toArray(){
   1.317 +	return RT.seqToArray(seq());
   1.318 +}
   1.319 +
   1.320 +public boolean add(Object o){
   1.321 +	throw new UnsupportedOperationException();
   1.322 +}
   1.323 +
   1.324 +public boolean remove(Object o){
   1.325 +	throw new UnsupportedOperationException();
   1.326 +}
   1.327 +
   1.328 +public boolean addAll(Collection c){
   1.329 +	throw new UnsupportedOperationException();
   1.330 +}
   1.331 +
   1.332 +public void clear(){
   1.333 +	throw new UnsupportedOperationException();
   1.334 +}
   1.335 +
   1.336 +public boolean retainAll(Collection c){
   1.337 +	throw new UnsupportedOperationException();
   1.338 +}
   1.339 +
   1.340 +public boolean removeAll(Collection c){
   1.341 +	throw new UnsupportedOperationException();
   1.342 +}
   1.343 +
   1.344 +public boolean containsAll(Collection c){
   1.345 +	for(Object o : c)
   1.346 +		{
   1.347 +		if(!contains(o))
   1.348 +			return false;
   1.349 +		}
   1.350 +	return true;
   1.351 +}
   1.352 +
   1.353 +public Object[] toArray(Object[] a){
   1.354 +	if(a.length >= count())
   1.355 +		{
   1.356 +		ISeq s = seq();
   1.357 +		for(int i = 0; s != null; ++i, s = s.rest())
   1.358 +			{
   1.359 +			a[i] = s.first();
   1.360 +			}
   1.361 +		if(a.length > count())
   1.362 +			a[count()] = null;
   1.363 +		return a;
   1.364 +		}
   1.365 +	else
   1.366 +		return toArray();
   1.367 +}
   1.368 +
   1.369 +public int size(){
   1.370 +	return count();
   1.371 +}
   1.372 +
   1.373 +public boolean isEmpty(){
   1.374 +	return count() == 0;
   1.375 +}
   1.376 +
   1.377 +public boolean contains(Object o){
   1.378 +	if(o instanceof Map.Entry)
   1.379 +		{
   1.380 +		Map.Entry e = (Map.Entry) o;
   1.381 +		Map.Entry v = entryAt(e.getKey());
   1.382 +		return (v != null && Util.equal(v.getValue(), e.getValue()));
   1.383 +		}
   1.384 +	return false;
   1.385 +}
   1.386 +*/
   1.387 +}