Skip to content

Commit 6455759

Browse files
authored
Merge pull request #47 from jafingerhut/fix-crrbv-29
Fix crrbv 29
2 parents 07f017c + a33fd68 commit 6455759

File tree

5 files changed

+57
-37
lines changed

5 files changed

+57
-37
lines changed

doc/rrb-tree-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Published papers and theses:
7575
to Make Things Simple and Fast", January 2013, given at Clojure
7676
conj conference
7777
* Jean Niklas L'orange, "Improving RRB-Tree Performance through
78-
Transience", MAster Thesis, 2014,
78+
Transience", Master Thesis, 2014,
7979
[[PDF]](https://hypirion.com/thesis.pdf)
8080
* "RRB Vector: A Practical General Purpose Immutable Sequence",
8181
Nicolas Stucki, Tiark, Rompf, Vlad Ureche, Phil Bagwell, Proc. of

src/main/cljs/clojure/core/rrb_vector/rrbt.cljs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -488,24 +488,26 @@
488488

489489
IKVReduce
490490
(-kv-reduce [this f init]
491-
(loop [i 0
492-
j 0
493-
init init
494-
arr (-array-for this i)
495-
lim (dec (alength arr))
496-
step (inc lim)]
497-
(let [init (f init (+ i j) (aget arr j))]
498-
(if (reduced? init)
499-
@init
500-
(if (< j lim)
501-
(recur i (inc j) init arr lim step)
502-
(let [i (+ i step)]
503-
(if (< i cnt)
504-
(let [arr (-array-for this i)
505-
len (alength arr)
506-
lim (dec len)]
507-
(recur i 0 init arr lim len))
508-
init)))))))
491+
(if (zero? cnt)
492+
init
493+
(loop [i 0
494+
j 0
495+
init init
496+
arr (-array-for this i)
497+
lim (dec (alength arr))
498+
step (inc lim)]
499+
(let [init (f init (+ i j) (aget arr j))]
500+
(if (reduced? init)
501+
@init
502+
(if (< j lim)
503+
(recur i (inc j) init arr lim step)
504+
(let [i (+ i step)]
505+
(if (< i cnt)
506+
(let [arr (-array-for this i)
507+
len (alength arr)
508+
lim (dec len)]
509+
(recur i 0 init arr lim len))
510+
init))))))))
509511

510512
IComparable
511513
(-compare [this that]

src/main/clojure/clojure/core/rrb_vector/rrbt.clj

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,24 +1026,26 @@
10261026

10271027
IKVReduce
10281028
(kv-reduce [this f init]
1029-
(loop [i (int 0)
1030-
j (int 0)
1031-
init init
1032-
arr (.arrayFor this i)
1033-
lim (unchecked-dec-int (.alength am arr))
1034-
step (unchecked-inc-int lim)]
1035-
(let [init (f init (unchecked-add-int i j) (.aget am arr j))]
1036-
(if (reduced? init)
1037-
@init
1038-
(if (< j lim)
1039-
(recur i (unchecked-inc-int j) init arr lim step)
1040-
(let [i (unchecked-add-int i step)]
1041-
(if (< i cnt)
1042-
(let [arr (.arrayFor this i)
1043-
len (.alength am arr)
1044-
lim (unchecked-dec-int len)]
1045-
(recur i (int 0) init arr lim len))
1046-
init)))))))
1029+
(if (zero? cnt)
1030+
init
1031+
(loop [i (int 0)
1032+
j (int 0)
1033+
init init
1034+
arr (.arrayFor this i)
1035+
lim (unchecked-dec-int (.alength am arr))
1036+
step (unchecked-inc-int lim)]
1037+
(let [init (f init (unchecked-add-int i j) (.aget am arr j))]
1038+
(if (reduced? init)
1039+
@init
1040+
(if (< j lim)
1041+
(recur i (unchecked-inc-int j) init arr lim step)
1042+
(let [i (unchecked-add-int i step)]
1043+
(if (< i cnt)
1044+
(let [arr (.arrayFor this i)
1045+
len (.alength am arr)
1046+
lim (unchecked-dec-int len)]
1047+
(recur i (int 0) init arr lim len))
1048+
init))))))))
10471049

10481050
CollFold
10491051
;; adapted from #'clojure.core.reducers/foldvec

src/test/cljs/clojure/core/rrb_vector/test_common.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@
8383
(reduce + 0 (r/map identity (seq v1)))
8484
(reduce + 0 (r/map identity (seq v2)))))))
8585

86+
(deftest test-reduce-3
87+
(let [v0 (vec [])
88+
rv0 (fv/vec [])]
89+
(testing "reduce"
90+
(is (= (reduce + v0) (reduce + rv0))))
91+
(testing "reduce-kv"
92+
(is (= (reduce-kv + 0 v0) (reduce-kv + 0 rv0))))))
93+
8694
(deftest test-seq
8795
(let [v (fv/vec (range 128))
8896
s (seq v)]

src/test/clojure/clojure/core/rrb_vector/test_common.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@
8383
(reduce + 0 (r/map identity (seq v1)))
8484
(reduce + 0 (r/map identity (seq v2)))))))
8585

86+
(deftest test-reduce-3
87+
(let [v0 (vec [])
88+
rv0 (fv/vec [])]
89+
(testing "reduce"
90+
(is (= (reduce + v0) (reduce + rv0))))
91+
(testing "reduce-kv"
92+
(is (= (reduce-kv + 0 v0) (reduce-kv + 0 rv0))))))
93+
8694
(deftest test-seq
8795
(let [v (fv/vec (range 128))
8896
s (seq v)]

0 commit comments

Comments
 (0)