Skip to content

Commit a780e1d

Browse files
cgrandChouser
authored andcommitted
implemented TransientHashSet Fixes #173
Signed-off-by: Chouser <chouser@n01se.net>
1 parent 2f3d7d3 commit a780e1d

4 files changed

Lines changed: 100 additions & 1 deletion

File tree

src/clj/clojure/core.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,6 +4544,18 @@
45444544
[#^clojure.lang.ITransientVector coll]
45454545
(.pop coll))
45464546

4547+
(defn disj!
4548+
"disj[oin]. Returns a transient set of the same (hashed/sorted) type, that
4549+
does not contain key(s)."
4550+
([set] set)
4551+
([#^clojure.lang.ITransientSet set key]
4552+
(. set (disjoin key)))
4553+
([set key & ks]
4554+
(let [ret (disj set key)]
4555+
(if ks
4556+
(recur ret (first ks) (next ks))
4557+
ret))))
4558+
45474559
;redef into with batch support
45484560
(defn into
45494561
"Returns a new coll consisting of to-coll with all of the items of
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) Rich Hickey. All rights reserved.
3+
* The use and distribution terms for this software are covered by the
4+
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5+
* which can be found in the file epl-v10.html at the root of this distribution.
6+
* By using this software in any fashion, you are agreeing to be bound by
7+
* the terms of this license.
8+
* You must not remove this notice, or any other, from this software.
9+
**/
10+
11+
/* rich Mar 3, 2008 */
12+
13+
package clojure.lang;
14+
15+
public abstract class ATransientSet extends AFn implements ITransientSet{
16+
ITransientMap impl;
17+
18+
ATransientSet(ITransientMap impl) {
19+
this.impl = impl;
20+
}
21+
22+
public int count() {
23+
return impl.count();
24+
}
25+
26+
public ITransientSet conj(Object val) {
27+
ITransientMap m = impl.assoc(val, val);
28+
if (m != impl) this.impl = m;
29+
return this;
30+
}
31+
32+
public boolean contains(Object key) {
33+
return this != impl.valAt(key, this);
34+
}
35+
36+
public ITransientSet disjoin(Object key) throws Exception {
37+
ITransientMap m = impl.without(key);
38+
if (m != impl) this.impl = m;
39+
return this;
40+
}
41+
42+
public Object get(Object key) {
43+
return impl.valAt(key);
44+
}
45+
46+
public Object invoke(Object key, Object notFound) throws Exception {
47+
return impl.valAt(key, notFound);
48+
}
49+
50+
public Object invoke(Object key) throws Exception {
51+
return impl.valAt(key);
52+
}
53+
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Rich Hickey. All rights reserved.
3+
* The use and distribution terms for this software are covered by the
4+
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5+
* which can be found in the file epl-v10.html at the root of this distribution.
6+
* By using this software in any fashion, you are agreeing to be bound by
7+
* the terms of this license.
8+
* You must not remove this notice, or any other, from this software.
9+
**/
10+
11+
/* rich Mar 3, 2008 */
12+
13+
package clojure.lang;
14+
15+
public interface ITransientSet extends ITransientCollection, Counted{
16+
public ITransientSet disjoin(Object key) throws Exception;
17+
public boolean contains(Object key);
18+
public Object get(Object key);
19+
}

src/jvm/clojure/lang/PersistentHashSet.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import java.util.List;
1616

17-
public class PersistentHashSet extends APersistentSet{
17+
public class PersistentHashSet extends APersistentSet implements IEditableCollection {
1818

1919
static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);
2020

@@ -69,4 +69,18 @@ public PersistentHashSet withMeta(IPersistentMap meta){
6969
return new PersistentHashSet(meta, impl);
7070
}
7171

72+
public ITransientCollection asTransient() {
73+
return new TransientHashSet(((PersistentHashMap) impl).asTransient());
74+
}
75+
76+
static final class TransientHashSet extends ATransientSet {
77+
TransientHashSet(ITransientMap impl) {
78+
super(impl);
79+
}
80+
81+
public IPersistentCollection persistent() {
82+
return new PersistentHashSet(null, impl.persistent());
83+
}
84+
}
85+
7286
}

0 commit comments

Comments
 (0)