Skip to content

Commit e85fe0b

Browse files
authored
Merge pull request #50 from jafingerhut/fix-crrbv-30
Test and fix for CRRBV-30
2 parents 2b8e748 + 47a15af commit e85fe0b

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

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

+53-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
IIndexed
317317
(-nth [this i]
318318
(if (and (<= 0 i) (< i cnt))
319-
(let [tail-off (- cnt (alength tail))]
319+
(let [tail-off (-tail-offset this)]
320320
(if (<= tail-off i)
321321
(aget tail (- i tail-off))
322322
(loop [i i node root shift shift]
@@ -1192,6 +1192,57 @@
11921192
^:mutable root
11931193
^:mutable tail
11941194
^:mutable tidx]
1195+
1196+
;; The Clojure/Java deftype Transient implements the Java interface
1197+
;; clojure.lang.ILookup. The corresponding ClojureScript protocol
1198+
;; named ILookup is implemented by the deftype Vector in this file.
1199+
;; TBD: Should ILookup be implemented by type Transient, too?
1200+
1201+
IIndexed
1202+
(-nth [this i]
1203+
(if (and (<= 0 i) (< i cnt))
1204+
(let [tail-off (-tail-offset this)]
1205+
(if (<= tail-off i)
1206+
(aget tail (- i tail-off))
1207+
(loop [i i node root shift shift]
1208+
(if (zero? shift)
1209+
(let [arr (.-arr node)]
1210+
(aget arr (bit-and (bit-shift-right i shift) 0x1f)))
1211+
(if (regular? node)
1212+
(let [arr (.-arr node)
1213+
idx (bit-and (bit-shift-right i shift) 0x1f)]
1214+
(loop [i i
1215+
node (aget arr idx)
1216+
shift (- shift 5)]
1217+
(let [arr (.-arr node)
1218+
idx (bit-and (bit-shift-right i shift) 0x1f)]
1219+
(if (zero? shift)
1220+
(aget arr idx)
1221+
(recur i (aget arr idx) (- shift 5))))))
1222+
(let [arr (.-arr node)
1223+
rngs (node-ranges node)
1224+
idx (loop [j (bit-and (bit-shift-right i shift) 0x1f)]
1225+
(if (< i (aget rngs j))
1226+
j
1227+
(recur (inc j))))
1228+
i (if (zero? idx)
1229+
i
1230+
(- i (aget rngs (dec idx))))]
1231+
(recur i (aget arr idx) (- shift 5))))))))
1232+
(vector-index-out-of-bounds i cnt)))
1233+
1234+
(-nth [this i not-found]
1235+
(if (and (>= i 0) (< i cnt))
1236+
(-nth this i)
1237+
not-found))
1238+
1239+
IFn
1240+
(-invoke [this k]
1241+
(-nth this k))
1242+
1243+
(-invoke [this k not-found]
1244+
(-nth this k not-found))
1245+
11951246
ITransientCollection
11961247
(-conj! [this o]
11971248
(if ^boolean (.-edit root)
@@ -1254,7 +1305,7 @@
12541305
(if ^boolean (.-edit root)
12551306
(cond
12561307
(and (<= 0 i) (< i cnt))
1257-
(let [tail-off (- cnt tidx)]
1308+
(let [tail-off (-tail-offset this)]
12581309
(if (<= tail-off i)
12591310
(aset tail (- i tail-off) val)
12601311
(set! root (do-assoc! shift (.-edit root) root i val)))

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@
548548
clojure.lang.Indexed
549549
(nth [this i]
550550
(if (and (<= (int 0) i) (< i cnt))
551-
(let [tail-off (unchecked-subtract-int cnt (.alength am tail))]
551+
(let [tail-off (.tailoff this)]
552552
(if (<= tail-off i)
553553
(.aget am tail (unchecked-subtract-int i tail-off))
554554
(loop [i i node root shift shift]
@@ -1865,7 +1865,7 @@
18651865
(nth [this i]
18661866
(.ensureEditable transient-helper nm root)
18671867
(if (and (<= (int 0) i) (< i cnt))
1868-
(let [tail-off (unchecked-subtract-int cnt (.alength am tail))]
1868+
(let [tail-off (.tailoff this)]
18691869
(if (<= tail-off i)
18701870
(.aget am tail (unchecked-subtract-int i tail-off))
18711871
(loop [i i node root shift shift]
@@ -1998,7 +1998,7 @@
19981998
(.ensureEditable transient-helper nm root)
19991999
(cond
20002000
(and (<= 0 i) (< i cnt))
2001-
(let [tail-off (unchecked-subtract-int cnt tidx)]
2001+
(let [tail-off (.tailoff this)]
20022002
(if (<= tail-off i)
20032003
(.aset am tail (unchecked-subtract-int i tail-off) val)
20042004
(set! root (.doAssoc transient-helper nm am

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

+17
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,20 @@
552552
(u/reset-optimizer-counts!)
553553
(is (integer? (puzzle-b-rrbv 978)))
554554
(u/print-optimizer-counts))
555+
556+
(deftest test-crrbv-30
557+
(let [v1 [1 2 3]
558+
tv1 (transient [1 2 3])
559+
fv1 (fv/vector 1 2 3)
560+
tfv1 (transient (fv/vector 1 2 3))]
561+
(doseq [[v msg] [[v1 ""]
562+
[fv1 ""]
563+
[(map #(nth v1 %) [0 1 2]) "#(nth v1 %)"]
564+
[(map #(v1 %) [0 1 2]) "#(v1 %)"]
565+
[(map #(nth tv1 %) [0 1 2]) "#(nth tv1 %)"]
566+
[(map #(tv1 %) [0 1 2]) "#(tv1 %)"]
567+
[(map #(nth fv1 %) [0 1 2]) "#(nth fv1 %)"]
568+
[(map #(fv1 %) [0 1 2]) "#(fv1 %)"]
569+
[(map #(nth tfv1 %) [0 1 2]) "#(nth tfv1 %)"]
570+
[(map #(tfv1 %) [0 1 2]) "#(tfv1 %)"]]]
571+
(is (= '(1 2 3) v) (str "Failing case: " msg)))))

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

+17
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,20 @@
553553
(u/reset-optimizer-counts!)
554554
(is (integer? (puzzle-b-rrbv 978)))
555555
(u/print-optimizer-counts))
556+
557+
(deftest test-crrbv-30
558+
(let [v1 [1 2 3]
559+
tv1 (transient [1 2 3])
560+
fv1 (fv/vector 1 2 3)
561+
tfv1 (transient (fv/vector 1 2 3))]
562+
(doseq [[v msg] [[v1 "v1"]
563+
[fv1 "fv1"]
564+
[(map #(nth v1 %) [0 1 2]) "#(nth v1 %)"]
565+
[(map #(v1 %) [0 1 2]) "#(v1 %)"]
566+
[(map #(nth tv1 %) [0 1 2]) "#(nth tv1 %)"]
567+
[(map #(tv1 %) [0 1 2]) "#(tv1 %)"]
568+
[(map #(nth fv1 %) [0 1 2]) "#(nth fv1 %)"]
569+
[(map #(fv1 %) [0 1 2]) "%(fv1 %)"]
570+
[(map #(nth tfv1 %) [0 1 2]) "#(nth tfv1 %)"]
571+
[(map #(tfv1 %) [0 1 2]) "#(tfv1 %)"]]]
572+
(is (= '(1 2 3) v) (str "Failing case: " msg)))))

0 commit comments

Comments
 (0)