@@ -6560,7 +6560,164 @@ reduces them without incurring seq initialization"
65606560 (if (identical? k (aget array i))
65616561 i
65626562 (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+
65646721; ; Record Iterator
65656722(deftype RecordIter [^:mutable i record base-count fields ext-map-iter]
65666723 Object
@@ -9034,6 +9191,19 @@ reduces them without incurring seq initialization"
90349191 (.createAsIfByAssoc PersistentArrayMap (to-array s))
90359192 (if (seq s) (first s) (.-EMPTY PersistentArrayMap))))
90369193
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+
90379207(defn sorted-map
90389208 " keyval => key val
90399209 Returns a new sorted map with supplied mappings."
@@ -10685,6 +10855,10 @@ reduces them without incurring seq initialization"
1068510855 MapEntry
1068610856 (-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer " [" " " " ]" opts coll))
1068710857
10858+ ObjMap
10859+ (-pr-writer [coll writer opts]
10860+ (print-map coll pr-writer writer opts))
10861+
1068810862 KeySeq
1068910863 (-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer " (" " " " )" opts coll))
1069010864
0 commit comments