Skip to content

Commit 51b89ac

Browse files
committed
Refactored Bench to use IPersistentSet. Extracted slices to ISortedSet
1 parent 87c6246 commit 51b89ac

10 files changed

Lines changed: 162 additions & 190 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The intention with DataScript is to be a basic building block in client-side app
2323

2424
## Support us
2525

26-
<a href="https://patreon.com/tonsky" target="_blank"><img src="./datascript_patreon.png"></a>
26+
<a href="https://patreon.com/tonsky" target="_blank"><img src="./extras/datascript_patreon.png"></a>
2727

2828
## Resources
2929

repl.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

src-java/datascript/ASortedSet.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@ public ASortedSet(IPersistentMap meta, Comparator cmp) {
1818
// IMeta
1919
public IPersistentMap meta() { return _meta; }
2020

21-
// IObj
22-
abstract public ASortedSet withMeta(IPersistentMap meta);
23-
24-
// Counted
25-
abstract public int count();
26-
27-
// Seqable
28-
abstract public ISeq seq();
29-
3021
// IPersistentCollection
31-
abstract public ASortedSet empty();
32-
abstract public ASortedSet cons(Object key);
33-
3422
public boolean equiv(Object obj) {
3523
if (!(obj instanceof Set)) return false;
3624
Set s = (Set) obj;
@@ -39,8 +27,6 @@ public boolean equiv(Object obj) {
3927
}
4028

4129
// IPersistentSet
42-
abstract public ASortedSet disjoin(Object key);
43-
abstract public boolean contains(Object o);
4430
public Object get(Object key) { return contains(key) ? key : null; }
4531

4632
// ILookup
@@ -58,9 +44,6 @@ public int hasheq() {
5844
return _hasheq;
5945
}
6046

61-
// Iterable
62-
abstract public Iterator iterator();
63-
6447
// Collection
6548
public boolean containsAll​(Collection c) {
6649
for (Object o: c)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package datascript;
2+
3+
import java.util.*;
4+
import clojure.lang.*;
5+
6+
public interface ISortedSet extends Seqable, Reversible, Sorted {
7+
ISeq slice(Object from, Object to, Comparator cmp);
8+
ISeq rslice(Object from, Object to, Comparator cmp);
9+
10+
default ISeq slice(Object from, Object to) { return slice(from, to, comparator()); }
11+
default ISeq rslice(Object from, Object to) { return rslice(from, to, comparator()); }
12+
13+
// Seqable
14+
default ISeq seq() { return slice(null, null, comparator()); }
15+
16+
// Reversible
17+
default ISeq rseq() { return rslice(null, null, comparator()); }
18+
19+
// Sorted
20+
default ISeq seq(boolean asc) { return asc ? slice(null, null, comparator()) : rslice(null, null, comparator()); }
21+
default ISeq seqFrom(Object key, boolean asc) { return asc ? slice(key, null, comparator()) : rslice(key, null, comparator()); }
22+
}

src-java/datascript/SortedSet.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
*/
2424

2525
@SuppressWarnings("unchecked")
26-
public class SortedSet extends ASortedSet implements IEditableCollection, ITransientCollection, Reversible, Sorted, IReduce {
26+
public class SortedSet extends ASortedSet implements IEditableCollection, ITransientSet, Reversible, Sorted, IReduce, ISortedSet {
2727

2828
static Leaf[] EARLY_EXIT = new Leaf[0],
2929
UNCHANGED = new Leaf[0];
3030

3131
static int MIN_LEN = 32, MAX_LEN = 64, EXTRA_LEN = 8;
3232

33+
public static final SortedSet EMPTY = new SortedSet();
34+
3335
static class Edit {
3436
public volatile boolean _value = false;
3537
Edit(boolean value) { _value = value; }
@@ -62,6 +64,7 @@ public SortedSet(IPersistentMap meta, Comparator cmp) {
6264
_edit = edit;
6365
}
6466

67+
// ISortedSet
6568
public Seq slice(Object from, Object to) { return slice(from, to, _cmp); }
6669
public Seq slice(Object from, Object to, Comparator cmp) {
6770
assert from == null || to == null || cmp.compare(from, to) <= 0 : "From " + from + " after to " + to;
@@ -158,27 +161,19 @@ public SortedSet withMeta(IPersistentMap meta) {
158161
// Counted
159162
public int count() { return _count; }
160163

161-
// Seqable
162-
public Seq seq() { return slice(null, null, _cmp); }
163-
164-
// Reversible
165-
public ISeq rseq() { return rslice(null, null, _cmp); }
166-
167164
// Sorted
168165
public Comparator comparator() { return _cmp; }
169166
public Object entryKey(Object entry) { return entry; }
170-
public ISeq seq(boolean asc) { return asc ? seq() : rseq(); }
171-
public ISeq seqFrom(Object key, boolean asc) { return asc ? slice(key, null) : rslice(key, null); }
172167

173168
// IReduce
174169
public Object reduce(IFn f) {
175-
Seq seq = seq();
170+
Seq seq = (Seq) seq();
176171
return seq == null ? f.invoke() : seq.reduce(f);
177172
}
178173

179174
public Object reduce(IFn f, Object start) {
180-
Seq seq = seq();
181-
return seq == null ? start : seq().reduce(f, start);
175+
Seq seq = (Seq) seq();
176+
return seq == null ? start : seq.reduce(f, start);
182177
}
183178

184179
// IPersistentCollection
@@ -263,7 +258,7 @@ public SortedSet persistent() {
263258

264259
// Iterable
265260
public Iterator iterator() {
266-
return new JavaIter(seq());
261+
return new JavaIter((Seq) seq());
267262
}
268263

269264

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package clojure.lang;
2+
3+
import java.util.*;
4+
import clojure.java.api.Clojure;
5+
6+
@SuppressWarnings("unchecked")
7+
public class ClojureSet extends PersistentTreeSet implements datascript.ISortedSet {
8+
static public final ClojureSet EMPTY = new ClojureSet(null, PersistentTreeMap.EMPTY);
9+
10+
static IFn takeWhile;
11+
static {
12+
takeWhile = (IFn) Clojure.var("clojure.core", "take-while");
13+
}
14+
15+
ClojureSet(IPersistentMap meta, IPersistentMap impl) {
16+
super(meta, impl);
17+
}
18+
19+
public IPersistentSet cons(Object o) {
20+
if(contains(o))
21+
return this;
22+
return new ClojureSet(meta(), impl.assoc(o,o));
23+
}
24+
25+
public ISeq slice(Object from, Object to, Comparator cmp) {
26+
IFn pred = new AFn() {
27+
public Object invoke(Object arg) {
28+
return cmp.compare(arg, to) <= 0;
29+
}
30+
};
31+
return (ISeq) takeWhile.invoke(pred, seqFrom(from, true));
32+
}
33+
34+
public ISeq rslice(Object from, Object to, Comparator cmp) {
35+
IFn pred = new AFn() {
36+
public Object invoke(Object arg) {
37+
return cmp.compare(arg, to) >= 0;
38+
}
39+
};
40+
return (ISeq) takeWhile.invoke(pred, seqFrom(from, false));
41+
}
42+
}

0 commit comments

Comments
 (0)