diff src/clojure/lang/PersistentTreeSet.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/PersistentTreeSet.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,90 @@
     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.Comparator;
    1.19 +
    1.20 +public class PersistentTreeSet extends APersistentSet implements IObj, Reversible, Sorted{
    1.21 +static public final PersistentTreeSet EMPTY = new PersistentTreeSet(null, PersistentTreeMap.EMPTY);
    1.22 +final IPersistentMap _meta;
    1.23 +
    1.24 +
    1.25 +static public PersistentTreeSet create(ISeq items){
    1.26 +	PersistentTreeSet ret = EMPTY;
    1.27 +	for(; items != null; items = items.next())
    1.28 +		{
    1.29 +		ret = (PersistentTreeSet) ret.cons(items.first());
    1.30 +		}
    1.31 +	return ret;
    1.32 +}
    1.33 +
    1.34 +static public PersistentTreeSet create(Comparator comp, ISeq items){
    1.35 +	PersistentTreeSet ret = new PersistentTreeSet(null, new PersistentTreeMap(null, comp));
    1.36 +	for(; items != null; items = items.next())
    1.37 +		{
    1.38 +		ret = (PersistentTreeSet) ret.cons(items.first());
    1.39 +		}
    1.40 +	return ret;
    1.41 +}
    1.42 +
    1.43 +PersistentTreeSet(IPersistentMap meta, IPersistentMap impl){
    1.44 +	super(impl);
    1.45 +	this._meta = meta;
    1.46 +}
    1.47 +
    1.48 +public IPersistentSet disjoin(Object key) throws Exception{
    1.49 +	if(contains(key))
    1.50 +		return new PersistentTreeSet(meta(),impl.without(key));
    1.51 +	return this;
    1.52 +}
    1.53 +
    1.54 +public IPersistentSet cons(Object o){
    1.55 +	if(contains(o))
    1.56 +		return this;
    1.57 +	return new PersistentTreeSet(meta(),impl.assoc(o,o));
    1.58 +}
    1.59 +
    1.60 +public IPersistentCollection empty(){
    1.61 +	return new PersistentTreeSet(meta(),(PersistentTreeMap)impl.empty());
    1.62 +}
    1.63 +
    1.64 +public ISeq rseq() throws Exception{
    1.65 +	return APersistentMap.KeySeq.create(((Reversible) impl).rseq());
    1.66 +}
    1.67 +
    1.68 +public PersistentTreeSet withMeta(IPersistentMap meta){
    1.69 +	return new PersistentTreeSet(meta, impl);
    1.70 +}
    1.71 +
    1.72 +public Comparator comparator(){
    1.73 +	return ((Sorted)impl).comparator();
    1.74 +}
    1.75 +
    1.76 +public Object entryKey(Object entry){
    1.77 +	return entry;
    1.78 +}
    1.79 +
    1.80 +public ISeq seq(boolean ascending){
    1.81 +	PersistentTreeMap m = (PersistentTreeMap) impl;
    1.82 +	return RT.keys(m.seq(ascending));
    1.83 +}
    1.84 +
    1.85 +public ISeq seqFrom(Object key, boolean ascending){
    1.86 +	PersistentTreeMap m = (PersistentTreeMap) impl;
    1.87 +	return RT.keys(m.seqFrom(key,ascending));
    1.88 +}
    1.89 +
    1.90 +public IPersistentMap meta(){
    1.91 +	return _meta;
    1.92 +}
    1.93 +}