@@ -105,6 +105,29 @@ void run() {
105105 }
106106}
107107
108+ class BoundSliceBench extends ABench {
109+ ISortedSet set ;
110+
111+ BoundSliceBench (Class <? extends ISortedSet > setClass , Collection source , boolean asTransient , int maxLen ) {
112+ super (setClass , source , asTransient , maxLen );
113+ set = newSet (source );
114+ }
115+
116+ void run () {
117+ int from = 1 , to = 999999 , expected = from ;
118+ ISeq seq = set .slice (from , to );
119+ Integer value = -1 ;
120+ while (seq != null ) {
121+ value = (Integer ) seq .first ();
122+ assert value .intValue () == expected : "Expected " + expected + ", got " + value ;
123+ ++expected ;
124+ seq = seq .next ();
125+ }
126+ assert value .intValue () == to : "Expected " + to + ", got " + value ;
127+ }
128+ }
129+
130+
108131class RemoveBench extends ABench {
109132 ISortedSet set ;
110133 List <Integer > removes ;
@@ -135,15 +158,15 @@ abstract class APersistentCollection<T> implements ISortedSet {
135158
136159 abstract APersistentCollection <T > create (T _impl );
137160
138- public ISortedSet with (Object o ) { return create ((T ) ((IPersistentCollection ) _impl ).cons (o )); }
139- public ISortedSet without (Object o ) { return create ((T ) ((IPersistentSet ) _impl ).disjoin (o )); }
140- public int size () { return ((IPersistentCollection ) _impl ).count (); }
141- public boolean contains (Object o ) { return ((IPersistentSet ) _impl ).contains (o ); }
161+ public ISortedSet with (Object o ) { return create ((T ) ((IPersistentCollection ) _impl ).cons (o )); }
162+ public ISortedSet without (Object o ) { return create ((T ) ((IPersistentSet ) _impl ).disjoin (o )); }
163+ public int size () { return ((IPersistentCollection ) _impl ).count (); }
164+ public boolean contains (Object o ) { return ((IPersistentSet ) _impl ).contains (o ); }
142165
143- public Iterator iterator () { return ((Iterable ) _impl ).iterator (); }
144- public ISortedSet toTransient () { return create ((T ) ((IEditableCollection ) _impl ).asTransient ()); }
145- public ISortedSet toPersistent () { return create ((T ) ((ITransientCollection ) _impl ).persistent ()); }
146- public ISeq seq () { return ((Seqable ) _impl ).seq (); }
166+ public Iterator iterator () { return ((Iterable ) _impl ).iterator (); }
167+ public ISortedSet toTransient () { return create ((T ) ((IEditableCollection ) _impl ).asTransient ()); }
168+ public ISortedSet toPersistent () { return create ((T ) ((ITransientCollection ) _impl ).persistent ()); }
169+ public ISeq seq () { return ((Seqable ) _impl ).seq (); }
147170}
148171
149172class DatomicSet extends APersistentCollection <BTSet > {
@@ -166,12 +189,27 @@ class DatascriptSet extends APersistentCollection<SortedSet> {
166189 _impl = new SortedSet ();
167190 }
168191 DatascriptSet create (SortedSet impl ) { return new DatascriptSet (impl ); }
192+ public ISeq slice (Object from , Object to ) { return _impl .slice (from , to ); }
169193}
170194
195+ @ SuppressWarnings ("unchecked" )
171196class ClojureSet extends APersistentCollection <PersistentTreeSet > {
197+ static IFn takeWhile ;
198+ static {
199+ takeWhile = (IFn ) Clojure .var ("clojure.core" , "take-while" );
200+ }
201+
172202 ClojureSet (PersistentTreeSet impl ) { _impl = impl ; }
173203 ClojureSet (int ml ) { _impl = PersistentTreeSet .create (RT .DEFAULT_COMPARATOR , null ); }
174204 ClojureSet create (PersistentTreeSet impl ) { return new ClojureSet (impl ); }
205+ public ISeq slice (Object from , Object to ) {
206+ IFn pred = new AFn () {
207+ public Object invoke (Object arg ) {
208+ return _impl .comparator ().compare (arg , to ) <= 0 ;
209+ }
210+ };
211+ return (ISeq ) takeWhile .invoke (pred , _impl .seqFrom (from , true ));
212+ }
175213}
176214
177215public class Bench {
@@ -250,6 +288,10 @@ public static void bench() throws Exception {
250288 runBench (SeqIterateBench .class , bigSource , ClojureSet .class );
251289 runBench (SeqIterateBench .class , bigSource , DatomicSet .class );
252290 runBench (SeqIterateBench .class , bigSource , DatascriptSet .class );
291+
292+ System .out .println ("\n === Bound slice over 1M ===" );
293+ runBench (BoundSliceBench .class , bigSource , ClojureSet .class );
294+ runBench (BoundSliceBench .class , bigSource , DatascriptSet .class );
253295
254296 System .out .println ("\n === 100K REMOVEs ===" );
255297 runBench (RemoveBench .class , source , ClojureSet .class , false );
@@ -331,6 +373,6 @@ public static void test() throws Exception {
331373
332374 public static void main (String args []) throws Exception {
333375 bench ();
334- // test();
376+ test ();
335377 }
336378}
0 commit comments