Skip to content

Commit 815dde6

Browse files
author
dnolen
committed
CLJS-1189: array-map will return PersistentHashMap if applied to more than (.-HASHMAP-THRESHOLD PersistentArrayMap) pairs
make array helpers for PAM easier to reuse in other contexts. Fix array-map and the PersistentArrayMap.fromArray helper to preserve the type instead of converting to PHM.
1 parent 343573f commit 815dde6

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/cljs/cljs/core.cljs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,15 +5484,15 @@ reduces them without incurring seq initialization"
54845484

54855485
;;; PersistentArrayMap
54865486

5487-
(defn- array-map-index-of-nil? [arr m k]
5487+
(defn- array-index-of-nil? [arr]
54885488
(let [len (alength arr)]
54895489
(loop [i 0]
54905490
(cond
54915491
(<= len i) -1
54925492
(nil? (aget arr i)) i
54935493
:else (recur (+ i 2))))))
54945494

5495-
(defn- array-map-index-of-keyword? [arr m k]
5495+
(defn- array-index-of-keyword? [arr k]
54965496
(let [len (alength arr)
54975497
kstr (.-fqn k)]
54985498
(loop [i 0]
@@ -5503,7 +5503,7 @@ reduces them without incurring seq initialization"
55035503
(identical? kstr (.-fqn k')))) i
55045504
:else (recur (+ i 2))))))
55055505

5506-
(defn- array-map-index-of-symbol? [arr m k]
5506+
(defn- array-index-of-symbol? [arr k]
55075507
(let [len (alength arr)
55085508
kstr (.-str k)]
55095509
(loop [i 0]
@@ -5514,40 +5514,41 @@ reduces them without incurring seq initialization"
55145514
(identical? kstr (.-str k')))) i
55155515
:else (recur (+ i 2))))))
55165516

5517-
(defn- array-map-index-of-identical? [arr m k]
5517+
(defn- array-index-of-identical? [arr k]
55185518
(let [len (alength arr)]
55195519
(loop [i 0]
55205520
(cond
55215521
(<= len i) -1
55225522
(identical? k (aget arr i)) i
55235523
:else (recur (+ i 2))))))
55245524

5525-
(defn- array-map-index-of-equiv? [arr m k]
5525+
(defn- array-index-of-equiv? [arr k]
55265526
(let [len (alength arr)]
55275527
(loop [i 0]
55285528
(cond
55295529
(<= len i) -1
55305530
(= k (aget arr i)) i
55315531
:else (recur (+ i 2))))))
55325532

5533-
(defn- array-map-index-of [m k]
5534-
(let [arr (.-arr m)]
5535-
(cond
5536-
(keyword? k) (array-map-index-of-keyword? arr m k)
5533+
(defn array-index-of [arr k]
5534+
(cond
5535+
(keyword? k) (array-index-of-keyword? arr k)
55375536

5538-
(or ^boolean (goog/isString k) (number? k))
5539-
(array-map-index-of-identical? arr m k)
5537+
(or ^boolean (goog/isString k) (number? k))
5538+
(array-index-of-identical? arr k)
55405539

5541-
(symbol? k) (array-map-index-of-symbol? arr m k)
5540+
(symbol? k) (array-index-of-symbol? arr k)
55425541

5543-
(nil? k)
5544-
(array-map-index-of-nil? arr m k)
5542+
(nil? k)
5543+
(array-index-of-nil? arr)
55455544

5546-
:else (array-map-index-of-equiv? arr m k))))
5545+
:else (array-index-of-equiv? arr k)))
55475546

5548-
(defn- array-map-extend-kv [m k v]
5549-
(let [arr (.-arr m)
5550-
l (alength arr)
5547+
(defn- array-map-index-of [m k]
5548+
(array-index-of (.-arr m) k))
5549+
5550+
(defn- array-extend-kv [arr k v]
5551+
(let [l (alength arr)
55515552
narr (make-array (+ l 2))]
55525553
(loop [i 0]
55535554
(when (< i l)
@@ -5557,6 +5558,9 @@ reduces them without incurring seq initialization"
55575558
(aset narr (inc l) v)
55585559
narr))
55595560

5561+
(defn- array-map-extend-kv [m k v]
5562+
(array-extend-kv (.-arr m) k v))
5563+
55605564
(declare TransientArrayMap)
55615565

55625566
(deftype PersistentArrayMapSeq [arr i _meta]
@@ -5793,17 +5797,22 @@ reduces them without incurring seq initialization"
57935797

57945798
(set! (.-fromArray PersistentArrayMap)
57955799
(fn [arr ^boolean no-clone ^boolean no-check]
5796-
(let [arr (if no-clone arr (aclone arr))]
5800+
(as-> (if no-clone arr (aclone arr)) arr
57975801
(if no-check
5798-
(let [cnt (/ (alength arr) 2)]
5799-
(PersistentArrayMap. nil cnt arr nil))
5800-
(let [len (alength arr)]
5801-
(loop [i 0
5802-
ret (transient (.-EMPTY PersistentArrayMap))]
5803-
(if (< i len)
5804-
(recur (+ i 2)
5805-
(-assoc! ret (aget arr i) (aget arr (inc i))))
5806-
(-persistent! ret))))))))
5802+
arr
5803+
(let [ret (array)]
5804+
(loop [i 0]
5805+
(when (< i (alength arr))
5806+
(let [k (aget arr i)
5807+
v (aget arr (inc i))
5808+
idx (array-index-of ret k)]
5809+
(when (== idx -1)
5810+
(.push ret k)
5811+
(.push ret v)))
5812+
(recur (+ i 2))))
5813+
ret))
5814+
(let [cnt (/ (alength arr) 2)]
5815+
(PersistentArrayMap. nil cnt arr nil)))))
58075816

58085817
(es6-iterable PersistentArrayMap)
58095818

@@ -7439,7 +7448,10 @@ reduces them without incurring seq initialization"
74397448
"keyval => key val
74407449
Returns a new array map with supplied mappings."
74417450
[& keyvals]
7442-
(.fromArray cljs.core/PersistentArrayMap (apply array keyvals) true false))
7451+
(let [arr (if (instance? IndexedSeq keyvals)
7452+
(.-arr keyvals)
7453+
(into-array keyvals))]
7454+
(.fromArray cljs.core/PersistentArrayMap arr true false)))
74437455

74447456
(defn obj-map
74457457
"keyval => key val

test/cljs/cljs/core_test.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,12 @@
27362736
(is (= (with-out-str (print-foo-1187 :foo))
27372737
"foo!"))))
27382738

2739+
(deftest test-cljs-1189 []
2740+
(testing "array-map should always return array maps"
2741+
(let [am (apply array-map (range 100))]
2742+
(is (== (count am) 50))
2743+
(is (instance? PersistentArrayMap am)))))
2744+
27392745
(comment
27402746
;; ObjMap
27412747
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)