diff options
-rw-r--r-- | src/clj/clojure/core.clj | 12 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ATransientSet.java | 54 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ITransientSet.java | 19 | ||||
-rw-r--r-- | src/jvm/clojure/lang/PersistentHashSet.java | 16 |
4 files changed, 100 insertions, 1 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index 8e716cf4..42c359ee 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -4544,6 +4544,18 @@ [#^clojure.lang.ITransientVector coll] (.pop coll)) +(defn disj! + "disj[oin]. Returns a transient set of the same (hashed/sorted) type, that + does not contain key(s)." + ([set] set) + ([#^clojure.lang.ITransientSet set key] + (. set (disjoin key))) + ([set key & ks] + (let [ret (disj set key)] + (if ks + (recur ret (first ks) (next ks)) + ret)))) + ;redef into with batch support (defn into "Returns a new coll consisting of to-coll with all of the items of diff --git a/src/jvm/clojure/lang/ATransientSet.java b/src/jvm/clojure/lang/ATransientSet.java new file mode 100644 index 00000000..6eec807a --- /dev/null +++ b/src/jvm/clojure/lang/ATransientSet.java @@ -0,0 +1,54 @@ +/** + * 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 clojure.lang; + +public abstract class ATransientSet extends AFn implements ITransientSet{ + ITransientMap impl; + + ATransientSet(ITransientMap impl) { + this.impl = impl; + } + + public int count() { + return impl.count(); + } + + public ITransientSet conj(Object val) { + ITransientMap m = impl.assoc(val, val); + if (m != impl) this.impl = m; + return this; + } + + public boolean contains(Object key) { + return this != impl.valAt(key, this); + } + + public ITransientSet disjoin(Object key) throws Exception { + ITransientMap m = impl.without(key); + if (m != impl) this.impl = m; + return this; + } + + public Object get(Object key) { + return impl.valAt(key); + } + + public Object invoke(Object key, Object notFound) throws Exception { + return impl.valAt(key, notFound); + } + + public Object invoke(Object key) throws Exception { + return impl.valAt(key); + } + +} diff --git a/src/jvm/clojure/lang/ITransientSet.java b/src/jvm/clojure/lang/ITransientSet.java new file mode 100644 index 00000000..b7a369cb --- /dev/null +++ b/src/jvm/clojure/lang/ITransientSet.java @@ -0,0 +1,19 @@ +/** + * 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 clojure.lang; + +public interface ITransientSet extends ITransientCollection, Counted{ + public ITransientSet disjoin(Object key) throws Exception; + public boolean contains(Object key); + public Object get(Object key); +} diff --git a/src/jvm/clojure/lang/PersistentHashSet.java b/src/jvm/clojure/lang/PersistentHashSet.java index 35c6fa95..73a5a61f 100644 --- a/src/jvm/clojure/lang/PersistentHashSet.java +++ b/src/jvm/clojure/lang/PersistentHashSet.java @@ -14,7 +14,7 @@ package clojure.lang; import java.util.List; -public class PersistentHashSet extends APersistentSet{ +public class PersistentHashSet extends APersistentSet implements IEditableCollection { static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY); @@ -69,4 +69,18 @@ public PersistentHashSet withMeta(IPersistentMap meta){ return new PersistentHashSet(meta, impl); } +public ITransientCollection asTransient() { + return new TransientHashSet(((PersistentHashMap) impl).asTransient()); +} + +static final class TransientHashSet extends ATransientSet { + TransientHashSet(ITransientMap impl) { + super(impl); + } + + public IPersistentCollection persistent() { + return new PersistentHashSet(null, impl.persistent()); + } +} + } |