All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pure4j.collections.PersistentTreeSet Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
/**
 *   Copyright (c) Rich Hickey. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

/* rich Mar 3, 2008 */

package org.pure4j.collections;

import java.util.Comparator;

import org.pure4j.Pure4J;
import org.pure4j.annotations.pure.Enforcement;
import org.pure4j.annotations.pure.Pure;
import org.pure4j.annotations.pure.PureParameters;

public class PersistentTreeSet extends APersistentSet implements Reversible, Sorted {

	static private final PersistentTreeSet EMPTY = new PersistentTreeSet(PersistentTreeMap.emptyMap());

	public PersistentTreeSet(ISeq items) {
		this(APersistentSet.createMap(items, new TransientTreeMap()));
	}
	
	public PersistentTreeSet(Comparator comp) {
		this(new PersistentTreeMap(comp));
	}
	
	@Pure
	@PureParameters(Enforcement.NOT_PURE)
	public static  PersistentTreeSet create(Comparator comp, K... init) {
		PersistentTreeSet ret = new PersistentTreeSet(new PersistentTreeMap(comp));
		for (int i = 0; i < init.length; i++) {
			ret = ret.cons(init[i]);
		}
		return ret;
	}
	
	@Pure
	@PureParameters(Enforcement.NOT_PURE)
	public static  PersistentTreeSet create(K... init) {
		return create(PersistentTreeMap.DEFAULT_COMPARATOR, init);
	}

	public PersistentTreeSet(Comparator comp, ISeq items) {
		super(APersistentSet.createMap(items, new TransientTreeMap(comp)));
	}

	private PersistentTreeSet(IPersistentMap impl) {
		super(impl);
	}

	public PersistentTreeSet disjoin(Object key) {
		Pure4J.immutable(key);
		if (contains(key))
			return new PersistentTreeSet(impl.without(key));
		return this;
	}

	public PersistentTreeSet cons(K o) {
		Pure4J.immutable(o);
		if (contains(o))
			return this;
		return new PersistentTreeSet(impl.assoc(o, o));
	}

	@SuppressWarnings("unchecked")
	public PersistentTreeSet empty() {
		return (PersistentTreeSet) EMPTY;
	}
	
	@SuppressWarnings("unchecked")
	public static  PersistentTreeSet emptySet() {
		return (PersistentTreeSet) EMPTY;
	}

	public ISeq rseq() {
		return seq(false);
	}


	public Comparator comparator() {
		return ((PersistentTreeMap) impl).comparator();
	}

	@SuppressWarnings("unchecked")
	public K entryKey(Object entry) {
		Pure4J.immutable(entry);
		return (K) entry;
	}

	public ISeq seq(boolean ascending) {
		PersistentTreeMap m = (PersistentTreeMap) impl;
		return APersistentMap.KeySeq.createFromMap(m);
	}

	public ISeq seqFrom(K key, boolean ascending) {
		Pure4J.immutable(key);
		PersistentTreeMap m = (PersistentTreeMap) impl;
		return APersistentMap.KeySeq.create(m.seq());
	}

	@Override
	public ITransientSet asTransient() {
		return new TransientTreeSet(comparator(), this.seq());
	}
}