Browse files

CLJ-1082 - add generic ranged iterator for use by SubVectors and a fa…

…llback for cases where it cannot be applied

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information...
1 parent 43873e4 commit ef2e762e21a817305314ed10bc33505bed6874a5 @mikera mikera committed with Stuart Halloway Nov 24, 2013
View
25 src/jvm/clojure/lang/APersistentVector.java
@@ -241,6 +241,24 @@ public void add(Object o){
};
}
+Iterator rangedIterator(final int start, final int end){
+ return new Iterator(){
+ int i = start;
+
+ public boolean hasNext(){
+ return i < end;
+ }
+
+ public Object next(){
+ return nth(i++);
+ }
+
+ public void remove(){
+ throw new UnsupportedOperationException();
+ }
+ };
+}
+
public List subList(int fromIndex, int toIndex){
return (List) RT.subvec(this, fromIndex, toIndex);
}
@@ -524,7 +542,12 @@ public SubVector(IPersistentMap meta, IPersistentVector v, int start, int end){
this.end = end;
}
- public Iterator iterator(){return ((PersistentVector)v).rangedIterator(start,end);}
+ public Iterator iterator(){
+ if (v instanceof APersistentVector) {
+ return ((APersistentVector)v).rangedIterator(start,end);
+ }
+ return super.iterator();
+ }
public Object nth(int i){
if((start + i >= end) || (i < 0))
View
1 src/jvm/clojure/lang/PersistentVector.java
@@ -233,6 +233,7 @@ public ISeq seq(){
return chunkedSeq();
}
+@Override
Iterator rangedIterator(final int start, final int end){
return new Iterator(){
int i = start;
View
5 test/clojure/test_clojure/vectors.clj
@@ -79,6 +79,11 @@
vs-32 vs
vs nil))))
+(deftest test-primitive-subvector-reduce
+ ;; regression test for CLJ-1082
+ (is (== 60 (let [prim-vec (into (vector-of :long) (range 1000))]
+ (reduce + (subvec prim-vec 10 15))))))
+
(deftest test-vec-compare
(let [nums (range 1 100)
; randomly replaces a single item with the given value

0 comments on commit ef2e762

Please sign in to comment.