@@ -6561,163 +6561,6 @@ reduces them without incurring seq initialization"
6561
6561
i
6562
6562
(recur (+ i incr)))))))
6563
6563
6564
- ; The keys field is an array of all keys of this map, in no particular
6565
- ; order. Any string, keyword, or symbol key is used as a property name
6566
- ; to store the value in strobj. If a key is assoc'ed when that same
6567
- ; key already exists in strobj, the old value is overwritten. If a
6568
- ; non-string key is assoc'ed, return a HashMap object instead.
6569
-
6570
- (defn- obj-map-compare-keys [a b]
6571
- (let [a (hash a)
6572
- b (hash b)]
6573
- (cond
6574
- (< a b) -1
6575
- (> a b) 1
6576
- :else 0 )))
6577
-
6578
- (defn- obj-map->hash-map [m k v]
6579
- (let [ks (.-keys m)
6580
- len (alength ks)
6581
- so (.-strobj m)
6582
- mm (meta m)]
6583
- (loop [i 0
6584
- out (transient (.-EMPTY PersistentHashMap))]
6585
- (if (< i len)
6586
- (let [k (aget ks i)]
6587
- (recur (inc i) (assoc! out k (gobject/get so k))))
6588
- (-with-meta (persistent! (assoc! out k v)) mm)))))
6589
-
6590
- ; ;; ObjMap - DEPRECATED
6591
-
6592
- (defn- obj-clone [obj ks]
6593
- (let [new-obj (js-obj )
6594
- l (alength ks)]
6595
- (loop [i 0 ]
6596
- (when (< i l)
6597
- (let [k (aget ks i)]
6598
- (gobject/set new-obj k (gobject/get obj k))
6599
- (recur (inc i)))))
6600
- new-obj))
6601
-
6602
- (deftype ObjMap [meta keys strobj update-count ^:mutable __hash]
6603
- Object
6604
- (toString [coll]
6605
- (pr-str* coll))
6606
- (equiv [this other]
6607
- (-equiv this other))
6608
-
6609
- IWithMeta
6610
- (-with-meta [coll new-meta]
6611
- (if (identical? new-meta meta)
6612
- coll
6613
- (ObjMap. new-meta keys strobj update-count __hash)))
6614
-
6615
- IMeta
6616
- (-meta [coll] meta)
6617
-
6618
- ICollection
6619
- (-conj [coll entry]
6620
- (if (vector? entry)
6621
- (-assoc coll (-nth entry 0 ) (-nth entry 1 ))
6622
- (reduce -conj
6623
- coll
6624
- entry)))
6625
-
6626
- IEmptyableCollection
6627
- (-empty [coll] (-with-meta (.-EMPTY ObjMap) meta))
6628
-
6629
- IEquiv
6630
- (-equiv [coll other] (equiv-map coll other))
6631
-
6632
- IHash
6633
- (-hash [coll] (caching-hash coll hash-unordered-coll __hash))
6634
-
6635
- ISeqable
6636
- (-seq [coll]
6637
- (when (pos? (alength keys))
6638
- (map #(vector % (unchecked-get strobj %))
6639
- (.sort keys obj-map-compare-keys))))
6640
-
6641
- ICounted
6642
- (-count [coll] (alength keys))
6643
-
6644
- ILookup
6645
- (-lookup [coll k] (-lookup coll k nil ))
6646
- (-lookup [coll k not-found]
6647
- (if (and (string? k)
6648
- (not (nil? (scan-array 1 k keys))))
6649
- (unchecked-get strobj k)
6650
- not-found))
6651
-
6652
- IAssociative
6653
- (-assoc [coll k v]
6654
- (if (string? k)
6655
- (if (or (> update-count (.-HASHMAP_THRESHOLD ObjMap))
6656
- (>= (alength keys) (.-HASHMAP_THRESHOLD ObjMap)))
6657
- (obj-map->hash-map coll k v)
6658
- (if-not (nil? (scan-array 1 k keys))
6659
- (let [new-strobj (obj-clone strobj keys)]
6660
- (gobject/set new-strobj k v)
6661
- (ObjMap. meta keys new-strobj (inc update-count) nil )) ; overwrite
6662
- (let [new-strobj (obj-clone strobj keys) ; append
6663
- new-keys (aclone keys)]
6664
- (gobject/set new-strobj k v)
6665
- (.push new-keys k)
6666
- (ObjMap. meta new-keys new-strobj (inc update-count) nil ))))
6667
- ; ; non-string key. game over.
6668
- (obj-map->hash-map coll k v)))
6669
- (-contains-key? [coll k]
6670
- (if (and (string? k)
6671
- (not (nil? (scan-array 1 k keys))))
6672
- true
6673
- false ))
6674
-
6675
- IFind
6676
- (-find [coll k]
6677
- (when (and (string? k)
6678
- (not (nil? (scan-array 1 k keys))))
6679
- (MapEntry. k (unchecked-get strobj k) nil )))
6680
-
6681
- IKVReduce
6682
- (-kv-reduce [coll f init]
6683
- (let [len (alength keys)]
6684
- (loop [keys (.sort keys obj-map-compare-keys)
6685
- init init]
6686
- (if (seq keys)
6687
- (let [k (first keys)
6688
- init (f init k (unchecked-get strobj k))]
6689
- (if (reduced? init)
6690
- @init
6691
- (recur (rest keys) init)))
6692
- init))))
6693
-
6694
- IMap
6695
- (-dissoc [coll k]
6696
- (if (and (string? k)
6697
- (not (nil? (scan-array 1 k keys))))
6698
- (let [new-keys (aclone keys)
6699
- new-strobj (obj-clone strobj keys)]
6700
- (.splice new-keys (scan-array 1 k new-keys) 1 )
6701
- (js-delete new-strobj k)
6702
- (ObjMap. meta new-keys new-strobj (inc update-count) nil ))
6703
- coll)) ; key not found, return coll unchanged
6704
-
6705
- IFn
6706
- (-invoke [coll k]
6707
- (-lookup coll k))
6708
- (-invoke [coll k not-found]
6709
- (-lookup coll k not-found))
6710
-
6711
- IEditableCollection
6712
- (-as-transient [coll]
6713
- (transient (into (hash-map ) coll))))
6714
-
6715
- (set! (.-EMPTY ObjMap) (ObjMap. nil (array ) (js-obj ) 0 empty-unordered-hash))
6716
-
6717
- (set! (.-HASHMAP_THRESHOLD ObjMap) 8 )
6718
-
6719
- (set! (.-fromObject ObjMap) (fn [ks obj] (ObjMap. nil ks obj 0 nil )))
6720
-
6721
6564
; ; Record Iterator
6722
6565
(deftype RecordIter [^:mutable i record base-count fields ext-map-iter]
6723
6566
Object
@@ -9191,19 +9034,6 @@ reduces them without incurring seq initialization"
9191
9034
(.createAsIfByAssoc PersistentArrayMap (to-array s))
9192
9035
(if (seq s) (first s) (.-EMPTY PersistentArrayMap))))
9193
9036
9194
- (defn obj-map
9195
- " keyval => key val
9196
- Returns a new object map with supplied mappings."
9197
- [& keyvals]
9198
- (let [ks (array )
9199
- obj (js-obj )]
9200
- (loop [kvs (seq keyvals)]
9201
- (if kvs
9202
- (do (.push ks (first kvs))
9203
- (gobject/set obj (first kvs) (second kvs))
9204
- (recur (nnext kvs)))
9205
- (.fromObject ObjMap ks obj)))))
9206
-
9207
9037
(defn sorted-map
9208
9038
" keyval => key val
9209
9039
Returns a new sorted map with supplied mappings."
@@ -10855,10 +10685,6 @@ reduces them without incurring seq initialization"
10855
10685
MapEntry
10856
10686
(-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer " [" " " " ]" opts coll))
10857
10687
10858
- ObjMap
10859
- (-pr-writer [coll writer opts]
10860
- (print-map coll pr-writer writer opts))
10861
-
10862
10688
KeySeq
10863
10689
(-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer " (" " " " )" opts coll))
10864
10690
@@ -12502,14 +12328,6 @@ reduces them without incurring seq initialization"
12502
12328
; key already exists in strobj, the old value is overwritten. If a
12503
12329
; non-string key is assoc'ed, return a HashMap object instead.
12504
12330
12505
- (defn- obj-map-contains-key?
12506
- ([k strobj]
12507
- (obj-map-contains-key? k strobj true false ))
12508
- ([k strobj true -val false -val]
12509
- (if (and (goog/isString k) (.hasOwnProperty strobj k))
12510
- true -val
12511
- false -val)))
12512
-
12513
12331
(defn- obj-map-compare-keys [a b]
12514
12332
(let [a (hash a)
12515
12333
b (hash b)]
@@ -12518,7 +12336,25 @@ reduces them without incurring seq initialization"
12518
12336
(> a b) 1
12519
12337
:else 0 )))
12520
12338
12521
- (deftype ObjMap [meta keys strobj]
12339
+ (defn- obj-clone [obj ks]
12340
+ (let [new-obj (js-obj )
12341
+ l (alength ks)]
12342
+ (loop [i 0 ]
12343
+ (when (< i l)
12344
+ (let [k (aget ks i)]
12345
+ (gobject/set new-obj k (gobject/get obj k))
12346
+ (recur (inc i)))))
12347
+ new-obj))
12348
+
12349
+ (declare simple-hash-map )
12350
+
12351
+ (deftype ObjMap [meta keys strobj ^:mutable __hash]
12352
+ Object
12353
+ (toString [coll]
12354
+ (pr-str* coll))
12355
+ (equiv [this other]
12356
+ (-equiv this other))
12357
+
12522
12358
IWithMeta
12523
12359
(-with-meta [coll meta] (ObjMap. meta keys strobj))
12524
12360
@@ -12540,46 +12376,73 @@ reduces them without incurring seq initialization"
12540
12376
(-equiv [coll other] (equiv-map coll other))
12541
12377
12542
12378
IHash
12543
- (-hash [coll] (hash- coll coll))
12379
+ (-hash [coll] (caching-hash coll hash-unordered- coll __hash ))
12544
12380
12545
12381
ISeqable
12546
12382
(-seq [coll]
12547
- (when (pos? (.-length keys))
12548
- (map #(vector % (aget strobj %))
12383
+ (when (pos? (alength keys))
12384
+ (map #(vector % (unchecked-get strobj %))
12549
12385
(.sort keys obj-map-compare-keys))))
12550
12386
12551
12387
ICounted
12552
- (-count [coll] (.-length keys))
12388
+ (-count [coll] (alength keys))
12553
12389
12554
12390
ILookup
12555
12391
(-lookup [coll k] (-lookup coll k nil ))
12556
12392
(-lookup [coll k not-found]
12557
- (obj-map-contains-key? k strobj (aget strobj k) not-found))
12393
+ (if (and (string? k)
12394
+ (not (nil? (scan-array 1 k keys))))
12395
+ (unchecked-get strobj k)
12396
+ not-found))
12558
12397
12559
12398
IAssociative
12560
12399
(-assoc [coll k v]
12561
- (if (goog/isString k)
12562
- (let [new-strobj (goog.object/clone strobj)
12563
- overwrite? (.hasOwnProperty new-strobj k)]
12564
- (aset new-strobj k v)
12565
- (if overwrite?
12566
- (ObjMap. meta keys new-strobj) ; overwrite
12567
- (let [new-keys (aclone keys)] ; append
12568
- (.push new-keys k)
12569
- (ObjMap. meta new-keys new-strobj))))
12400
+ (if (string? k)
12401
+ (if-not (nil? (scan-array 1 k keys))
12402
+ (let [new-strobj (obj-clone strobj keys)]
12403
+ (gobject/set new-strobj k v)
12404
+ (ObjMap. meta keys new-strobj nil )) ; overwrite
12405
+ (let [new-strobj (obj-clone strobj keys) ; append
12406
+ new-keys (aclone keys)]
12407
+ (gobject/set new-strobj k v)
12408
+ (.push new-keys k)
12409
+ (ObjMap. meta new-keys new-strobj nil )))
12570
12410
; non-string key. game over.
12571
- (with-meta (into (hash-map k v) (seq coll)) meta)))
12411
+ (with-meta (into (simple- hash-map k v) (seq coll)) meta)))
12572
12412
(-contains-key? [coll k]
12573
- (obj-map-contains-key? k strobj))
12413
+ (if (and (string? k)
12414
+ (not (nil? (scan-array 1 k keys))))
12415
+ true
12416
+ false ))
12417
+
12418
+ IFind
12419
+ (-find [coll k]
12420
+ (when (and (string? k)
12421
+ (not (nil? (scan-array 1 k keys))))
12422
+ (MapEntry. k (unchecked-get strobj k) nil )))
12423
+
12424
+ IKVReduce
12425
+ (-kv-reduce [coll f init]
12426
+ (let [len (alength keys)]
12427
+ (loop [keys (.sort keys obj-map-compare-keys)
12428
+ init init]
12429
+ (if (seq keys)
12430
+ (let [k (first keys)
12431
+ init (f init k (unchecked-get strobj k))]
12432
+ (if (reduced? init)
12433
+ @init
12434
+ (recur (rest keys) init)))
12435
+ init))))
12574
12436
12575
12437
IMap
12576
12438
(-dissoc [coll k]
12577
- (if (and (goog/isString k) (.hasOwnProperty strobj k))
12439
+ (if (and (string? k)
12440
+ (not (nil? (scan-array 1 k keys))))
12578
12441
(let [new-keys (aclone keys)
12579
- new-strobj (goog.object/ clone strobj)]
12442
+ new-strobj (obj- clone strobj keys )]
12580
12443
(.splice new-keys (scan-array 1 k new-keys) 1 )
12581
12444
(js-delete new-strobj k)
12582
- (ObjMap. meta new-keys new-strobj))
12445
+ (ObjMap. meta new-keys new-strobj nil ))
12583
12446
coll)) ; key not found, return coll unchanged
12584
12447
12585
12448
IFn
@@ -12588,6 +12451,10 @@ reduces them without incurring seq initialization"
12588
12451
(-invoke [coll k not-found]
12589
12452
(-lookup coll k not-found))
12590
12453
12454
+ ; IEditableCollection
12455
+ ; (-as-transient [coll]
12456
+ ; (transient (into (simple-hash-map) coll)))
12457
+
12591
12458
IPrintWithWriter
12592
12459
(-pr-writer [coll writer opts]
12593
12460
(print-map coll pr-writer writer opts)))
@@ -12716,6 +12583,15 @@ reduces them without incurring seq initialization"
12716
12583
(recur (inc i) (assoc out (aget ks i) (aget vs i)))
12717
12584
out)))))
12718
12585
12586
+ (defn simple-hash-map
12587
+ " keyval => key val
12588
+ Returns a new hash map with supplied mappings."
12589
+ [& keyvals]
12590
+ (loop [in (seq keyvals), out (. HashMap -EMPTY)]
12591
+ (if in
12592
+ (recur (nnext in) (assoc out (first in) (second in)))
12593
+ out)))
12594
+
12719
12595
(deftype Set [meta hash-map]
12720
12596
IWithMeta
12721
12597
(-with-meta [coll meta] (Set. meta hash-map))
0 commit comments