diff src/clojure/lang/APersistentSet.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/APersistentSet.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,160 @@
     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 Mar 3, 2008 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +import java.io.Serializable;
    1.19 +import java.util.Collection;
    1.20 +import java.util.Iterator;
    1.21 +import java.util.Set;
    1.22 +
    1.23 +public abstract class APersistentSet extends AFn implements IPersistentSet, Collection, Set, Serializable {
    1.24 +int _hash = -1;
    1.25 +final IPersistentMap impl;
    1.26 +
    1.27 +protected APersistentSet(IPersistentMap impl){
    1.28 +	this.impl = impl;
    1.29 +}
    1.30 +
    1.31 +public String toString(){
    1.32 +	return RT.printString(this);
    1.33 +}
    1.34 +
    1.35 +public boolean contains(Object key){
    1.36 +	return impl.containsKey(key);
    1.37 +}
    1.38 +
    1.39 +public Object get(Object key){
    1.40 +	return impl.valAt(key);
    1.41 +}
    1.42 +
    1.43 +public int count(){
    1.44 +	return impl.count();
    1.45 +}
    1.46 +
    1.47 +public ISeq seq(){
    1.48 +	return RT.keys(impl);
    1.49 +}
    1.50 +
    1.51 +public Object invoke(Object arg1) throws Exception{
    1.52 +	return get(arg1);
    1.53 +}
    1.54 +
    1.55 +public boolean equals(Object obj){
    1.56 +	if(this == obj) return true;
    1.57 +	if(!(obj instanceof Set))
    1.58 +		return false;
    1.59 +	Set m = (Set) obj;
    1.60 +
    1.61 +	if(m.size() != count() || m.hashCode() != hashCode())
    1.62 +		return false;
    1.63 +
    1.64 +	for(Object aM : m)
    1.65 +		{
    1.66 +		if(!contains(aM))
    1.67 +			return false;
    1.68 +		}
    1.69 +//	for(ISeq s = seq(); s != null; s = s.rest())
    1.70 +//		{
    1.71 +//		if(!m.contains(s.first()))
    1.72 +//			return false;
    1.73 +//		}
    1.74 +
    1.75 +	return true;
    1.76 +}
    1.77 +
    1.78 +public boolean equiv(Object o){
    1.79 +	return equals(o);
    1.80 +}
    1.81 +
    1.82 +public int hashCode(){
    1.83 +	if(_hash == -1)
    1.84 +		{
    1.85 +		//int hash = count();
    1.86 +		int hash = 0;
    1.87 +		for(ISeq s = seq(); s != null; s = s.next())
    1.88 +			{
    1.89 +			Object e = s.first();
    1.90 +//			hash = Util.hashCombine(hash, Util.hash(e));
    1.91 +			hash +=  Util.hash(e);
    1.92 +			}
    1.93 +		this._hash = hash;
    1.94 +		}
    1.95 +	return _hash;
    1.96 +}
    1.97 +
    1.98 +public Object[] toArray(){
    1.99 +	return RT.seqToArray(seq());
   1.100 +}
   1.101 +
   1.102 +public boolean add(Object o){
   1.103 +	throw new UnsupportedOperationException();
   1.104 +}
   1.105 +
   1.106 +public boolean remove(Object o){
   1.107 +	throw new UnsupportedOperationException();
   1.108 +}
   1.109 +
   1.110 +public boolean addAll(Collection c){
   1.111 +	throw new UnsupportedOperationException();
   1.112 +}
   1.113 +
   1.114 +public void clear(){
   1.115 +	throw new UnsupportedOperationException();
   1.116 +}
   1.117 +
   1.118 +public boolean retainAll(Collection c){
   1.119 +	throw new UnsupportedOperationException();
   1.120 +}
   1.121 +
   1.122 +public boolean removeAll(Collection c){
   1.123 +	throw new UnsupportedOperationException();
   1.124 +}
   1.125 +
   1.126 +public boolean containsAll(Collection c){
   1.127 +	for(Object o : c)
   1.128 +		{
   1.129 +		if(!contains(o))
   1.130 +			return false;
   1.131 +		}
   1.132 +	return true;
   1.133 +}
   1.134 +
   1.135 +public Object[] toArray(Object[] a){
   1.136 +	if(a.length >= count())
   1.137 +		{
   1.138 +		ISeq s = seq();
   1.139 +		for(int i = 0; s != null; ++i, s = s.next())
   1.140 +			{
   1.141 +			a[i] = s.first();
   1.142 +			}
   1.143 +		if(a.length > count())
   1.144 +			a[count()] = null;
   1.145 +		return a;
   1.146 +		}
   1.147 +	else
   1.148 +		return toArray();
   1.149 +}
   1.150 +
   1.151 +public int size(){
   1.152 +	return count();
   1.153 +}
   1.154 +
   1.155 +public boolean isEmpty(){
   1.156 +	return count() == 0;
   1.157 +}
   1.158 +
   1.159 +public Iterator iterator(){
   1.160 +	return new SeqIterator(seq());
   1.161 +}
   1.162 +
   1.163 +}