diff src/clojure/lang/PersistentHashSet.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/PersistentHashSet.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,128 @@
     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.util.List;
    1.19 +
    1.20 +public class PersistentHashSet extends APersistentSet implements IObj, IEditableCollection {
    1.21 +
    1.22 +static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);
    1.23 +
    1.24 +final IPersistentMap _meta;
    1.25 +
    1.26 +public static PersistentHashSet create(Object... init){
    1.27 +	PersistentHashSet ret = EMPTY;
    1.28 +	for(int i = 0; i < init.length; i++)
    1.29 +		{
    1.30 +		ret = (PersistentHashSet) ret.cons(init[i]);
    1.31 +		}
    1.32 +	return ret;
    1.33 +}
    1.34 +
    1.35 +public static PersistentHashSet create(List init){
    1.36 +	PersistentHashSet ret = EMPTY;
    1.37 +	for(Object key : init)
    1.38 +		{
    1.39 +		ret = (PersistentHashSet) ret.cons(key);
    1.40 +		}
    1.41 +	return ret;
    1.42 +}
    1.43 +
    1.44 +static public PersistentHashSet create(ISeq items){
    1.45 +	PersistentHashSet ret = EMPTY;
    1.46 +	for(; items != null; items = items.next())
    1.47 +		{
    1.48 +		ret = (PersistentHashSet) ret.cons(items.first());
    1.49 +		}
    1.50 +	return ret;
    1.51 +}
    1.52 +
    1.53 +public static PersistentHashSet createWithCheck(Object... init){
    1.54 +	PersistentHashSet ret = EMPTY;
    1.55 +	for(int i = 0; i < init.length; i++)
    1.56 +		{
    1.57 +		ret = (PersistentHashSet) ret.cons(init[i]);
    1.58 +		if(ret.count() != i + 1)
    1.59 +			throw new IllegalArgumentException("Duplicate key: " + init[i]);
    1.60 +		}
    1.61 +	return ret;
    1.62 +}
    1.63 +
    1.64 +public static PersistentHashSet createWithCheck(List init){
    1.65 +	PersistentHashSet ret = EMPTY;
    1.66 +	int i=0;
    1.67 +	for(Object key : init)
    1.68 +		{
    1.69 +		ret = (PersistentHashSet) ret.cons(key);
    1.70 +		if(ret.count() != i + 1)
    1.71 +			throw new IllegalArgumentException("Duplicate key: " + key);		
    1.72 +		++i;
    1.73 +		}
    1.74 +	return ret;
    1.75 +}
    1.76 +
    1.77 +static public PersistentHashSet createWithCheck(ISeq items){
    1.78 +	PersistentHashSet ret = EMPTY;
    1.79 +	for(int i=0; items != null; items = items.next(), ++i)
    1.80 +		{
    1.81 +		ret = (PersistentHashSet) ret.cons(items.first());
    1.82 +		if(ret.count() != i + 1)
    1.83 +			throw new IllegalArgumentException("Duplicate key: " + items.first());
    1.84 +		}
    1.85 +	return ret;
    1.86 +}
    1.87 +
    1.88 +PersistentHashSet(IPersistentMap meta, IPersistentMap impl){
    1.89 +	super(impl);
    1.90 +	this._meta = meta;
    1.91 +}
    1.92 +
    1.93 +public IPersistentSet disjoin(Object key) throws Exception{
    1.94 +	if(contains(key))
    1.95 +		return new PersistentHashSet(meta(),impl.without(key));
    1.96 +	return this;
    1.97 +}
    1.98 +
    1.99 +public IPersistentSet cons(Object o){
   1.100 +	if(contains(o))
   1.101 +		return this;
   1.102 +	return new PersistentHashSet(meta(),impl.assoc(o,o));
   1.103 +}
   1.104 +
   1.105 +public IPersistentCollection empty(){
   1.106 +	return EMPTY.withMeta(meta());	
   1.107 +}
   1.108 +
   1.109 +public PersistentHashSet withMeta(IPersistentMap meta){
   1.110 +	return new PersistentHashSet(meta, impl);
   1.111 +}
   1.112 +
   1.113 +public ITransientCollection asTransient() {
   1.114 +	return new TransientHashSet(((PersistentHashMap) impl).asTransient());
   1.115 +}
   1.116 +
   1.117 +public IPersistentMap meta(){
   1.118 +	return _meta;
   1.119 +}
   1.120 +
   1.121 +static final class TransientHashSet extends ATransientSet {
   1.122 +	TransientHashSet(ITransientMap impl) {
   1.123 +		super(impl);
   1.124 +	}
   1.125 +
   1.126 +	public IPersistentCollection persistent() {
   1.127 +		return new PersistentHashSet(null, impl.persistent());
   1.128 +	}
   1.129 +}
   1.130 +
   1.131 +}