implement NONE removal for ALL and MAP-VALS on PersistentArrayMap

This commit is contained in:
nathanmarz 2017-01-08 14:01:58 -05:00
parent 9617aa1931
commit 6b500a6aef

View file

@ -100,19 +100,29 @@
(all-transform [structure next-fn] (all-transform [structure next-fn]
(let [k-it (.keyIterator structure) (let [k-it (.keyIterator structure)
v-it (.valIterator structure) v-it (.valIterator structure)
array (i/fast-object-array (* 2 (.count structure)))] none-cell (i/mutable-cell 0)
;;TODO: how to handle NONE here...? len (.count structure)
;;needs to size the array appropriately... array (i/fast-object-array (* 2 len))]
;;need to use the same strategy for MAP-VALS (loop [i 0
(loop [i 0] j 0]
(if (.hasNext k-it) (if (.hasNext k-it)
(let [k (.next k-it) (let [k (.next k-it)
v (.next v-it) v (.next v-it)
[newk newv] (next-fn [k v])] newkv (next-fn [k v])]
(aset array i newk) (if (identical? newkv i/NONE)
(aset array (inc i) newv) (do
(recur (+ i 2))))) (i/update-cell! none-cell inc)
(clojure.lang.PersistentArrayMap/createAsIfByAssoc array)))) (recur (+ i 2) j))
(do
(aset array j (nth newkv 0))
(aset array (inc j) (nth newkv 1))
(recur (+ i 2) (+ j 2)))))))
(let [none-count (i/get-cell none-cell)
array (if (not= 0 none-count)
(java.util.Arrays/copyOf array (* 2 (- len none-count)))
array
)]
(clojure.lang.PersistentArrayMap/createAsIfByAssoc array)))))
#?(:cljs cljs.core/PersistentArrayMap) #?(:cljs cljs.core/PersistentArrayMap)
@ -208,17 +218,29 @@
(map-vals-transform [structure next-fn] (map-vals-transform [structure next-fn]
(let [k-it (.keyIterator structure) (let [k-it (.keyIterator structure)
v-it (.valIterator structure) v-it (.valIterator structure)
array (i/fast-object-array (* 2 (.count structure)))] none-cell (i/mutable-cell 0)
;;TODO: Need to handle NONE here just like it's handled in all-transform len (.count structure)
(loop [i 0] array (i/fast-object-array (* 2 len))]
(loop [i 0
j 0]
(if (.hasNext k-it) (if (.hasNext k-it)
(let [k (.next k-it) (let [k (.next k-it)
v (.next v-it) v (.next v-it)
newv (next-fn v)] newv (next-fn v)]
(aset array i k) (if (identical? newv i/NONE)
(aset array (inc i) newv) (do
(recur (+ i 2))))) (i/update-cell! none-cell inc)
(clojure.lang.PersistentArrayMap. array)))) (recur (+ i 2) j))
(do
(aset array j k)
(aset array (inc j) newv)
(recur (+ i 2) (+ j 2)))))))
(let [none-count (i/get-cell none-cell)
array (if (not= 0 none-count)
(java.util.Arrays/copyOf array (* 2 (- len none-count)))
array
)]
(clojure.lang.PersistentArrayMap. array)))))
#?(:cljs cljs.core/PersistentArrayMap) #?(:cljs cljs.core/PersistentArrayMap)