@@ -12400,3 +12400,345 @@ reduces them without incurring seq initialization"
12400
12400
(identical? " window" *global*) (set! goog/global js/window)
12401
12401
(identical? " self" *global*) (set! goog/global js/self)
12402
12402
(identical? " global" *global*) (set! goog/global js/global)))
12403
+
12404
+ ; ; -----------------------------------------------------------------------------
12405
+ ; ; Original 2011 Copy-on-Write Types
12406
+
12407
+ ; ;; Vector
12408
+
12409
+ (deftype Vector [meta array]
12410
+ IWithMeta
12411
+ (-with-meta [coll meta] (Vector. meta array))
12412
+
12413
+ IMeta
12414
+ (-meta [coll] meta)
12415
+
12416
+ IStack
12417
+ (-peek [coll]
12418
+ (let [count (.-length array)]
12419
+ (when (> count 0 )
12420
+ (aget array (dec count)))))
12421
+ (-pop [coll]
12422
+ (if (> (.-length array) 0 )
12423
+ (let [new-array (aclone array)]
12424
+ (. new-array (pop ))
12425
+ (Vector. meta new-array))
12426
+ (throw (js/Error. " Can't pop empty vector" ))))
12427
+
12428
+ ICollection
12429
+ (-conj [coll o]
12430
+ (let [new-array (aclone array)]
12431
+ (.push new-array o)
12432
+ (Vector. meta new-array)))
12433
+
12434
+ IEmptyableCollection
12435
+ (-empty [coll] (with-meta cljs.core.Vector/EMPTY meta))
12436
+
12437
+ ISequential
12438
+ IEquiv
12439
+ (-equiv [coll other] (equiv-sequential coll other))
12440
+
12441
+ IHash
12442
+ (-hash [coll] (hash-coll coll))
12443
+
12444
+ ISeqable
12445
+ (-seq [coll]
12446
+ (when (> (.-length array) 0 )
12447
+ (let [vector-seq
12448
+ (fn vector-seq [i]
12449
+ (lazy-seq
12450
+ (when (< i (.-length array))
12451
+ (cons (aget array i) (vector-seq (inc i))))))]
12452
+ (vector-seq 0 ))))
12453
+
12454
+ ICounted
12455
+ (-count [coll] (.-length array))
12456
+
12457
+ IIndexed
12458
+ (-nth [coll n]
12459
+ (if (and (<= 0 n) (< n (.-length array)))
12460
+ (aget array n)
12461
+ #_(throw (js/Error. (str " No item " n " in vector of length " (.-length array))))))
12462
+ (-nth [coll n not-found]
12463
+ (if (and (<= 0 n) (< n (.-length array)))
12464
+ (aget array n)
12465
+ not-found))
12466
+
12467
+ ILookup
12468
+ (-lookup [coll k] (-nth coll k nil ))
12469
+ (-lookup [coll k not-found] (-nth coll k not-found))
12470
+
12471
+ IAssociative
12472
+ (-assoc [coll k v]
12473
+ (let [new-array (aclone array)]
12474
+ (aset new-array k v)
12475
+ (Vector. meta new-array)))
12476
+
12477
+ IVector
12478
+ (-assoc-n [coll n val] (-assoc coll n val))
12479
+
12480
+ IReduce
12481
+ (-reduce [v f]
12482
+ (ci-reduce array f))
12483
+ (-reduce [v f start]
12484
+ (ci-reduce array f start))
12485
+
12486
+ IFn
12487
+ (-invoke [coll k]
12488
+ (-lookup coll k))
12489
+ (-invoke [coll k not-found]
12490
+ (-lookup coll k not-found)))
12491
+
12492
+ (set! (. Vector -EMPTY) (Vector. nil (array )))
12493
+
12494
+ (set! (. Vector -fromArray) (fn [xs] (Vector. nil xs)))
12495
+
12496
+ ; The keys field is an array of all keys of this map, in no particular
12497
+ ; order. Any string, keyword, or symbol key is used as a property name
12498
+ ; to store the value in strobj. If a key is assoc'ed when that same
12499
+ ; key already exists in strobj, the old value is overwritten. If a
12500
+ ; non-string key is assoc'ed, return a HashMap object instead.
12501
+
12502
+ (defn- obj-map-contains-key?
12503
+ ([k strobj]
12504
+ (obj-map-contains-key? k strobj true false ))
12505
+ ([k strobj true -val false -val]
12506
+ (if (and (goog/isString k) (.hasOwnProperty strobj k))
12507
+ true -val
12508
+ false -val)))
12509
+
12510
+ (defn- obj-map-compare-keys [a b]
12511
+ (let [a (hash a)
12512
+ b (hash b)]
12513
+ (cond
12514
+ (< a b) -1
12515
+ (> a b) 1
12516
+ :else 0 )))
12517
+
12518
+ (deftype ObjMap [meta keys strobj]
12519
+ IWithMeta
12520
+ (-with-meta [coll meta] (ObjMap. meta keys strobj))
12521
+
12522
+ IMeta
12523
+ (-meta [coll] meta)
12524
+
12525
+ ICollection
12526
+ (-conj [coll entry]
12527
+ (if (vector? entry)
12528
+ (-assoc coll (-nth entry 0 ) (-nth entry 1 ))
12529
+ (reduce -conj
12530
+ coll
12531
+ entry)))
12532
+
12533
+ IEmptyableCollection
12534
+ (-empty [coll] (with-meta cljs.core.ObjMap/EMPTY meta))
12535
+
12536
+ IEquiv
12537
+ (-equiv [coll other] (equiv-map coll other))
12538
+
12539
+ IHash
12540
+ (-hash [coll] (hash-coll coll))
12541
+
12542
+ ISeqable
12543
+ (-seq [coll]
12544
+ (when (pos? (.-length keys))
12545
+ (map #(vector % (aget strobj %))
12546
+ (.sort keys obj-map-compare-keys))))
12547
+
12548
+ ICounted
12549
+ (-count [coll] (.-length keys))
12550
+
12551
+ ILookup
12552
+ (-lookup [coll k] (-lookup coll k nil ))
12553
+ (-lookup [coll k not-found]
12554
+ (obj-map-contains-key? k strobj (aget strobj k) not-found))
12555
+
12556
+ IAssociative
12557
+ (-assoc [coll k v]
12558
+ (if (goog/isString k)
12559
+ (let [new-strobj (goog.object/clone strobj)
12560
+ overwrite? (.hasOwnProperty new-strobj k)]
12561
+ (aset new-strobj k v)
12562
+ (if overwrite?
12563
+ (ObjMap. meta keys new-strobj) ; overwrite
12564
+ (let [new-keys (aclone keys)] ; append
12565
+ (.push new-keys k)
12566
+ (ObjMap. meta new-keys new-strobj))))
12567
+ ; non-string key. game over.
12568
+ (with-meta (into (hash-map k v) (seq coll)) meta)))
12569
+ (-contains-key? [coll k]
12570
+ (obj-map-contains-key? k strobj))
12571
+
12572
+ IMap
12573
+ (-dissoc [coll k]
12574
+ (if (and (goog/isString k) (.hasOwnProperty strobj k))
12575
+ (let [new-keys (aclone keys)
12576
+ new-strobj (goog.object/clone strobj)]
12577
+ (.splice new-keys (scan-array 1 k new-keys) 1 )
12578
+ (js-delete new-strobj k)
12579
+ (ObjMap. meta new-keys new-strobj))
12580
+ coll)) ; key not found, return coll unchanged
12581
+
12582
+ IFn
12583
+ (-invoke [coll k]
12584
+ (-lookup coll k))
12585
+ (-invoke [coll k not-found]
12586
+ (-lookup coll k not-found)))
12587
+
12588
+ (set! (. ObjMap -EMPTY) (ObjMap. nil (array ) (js-obj )))
12589
+
12590
+ (set! (. ObjMap -fromObject) (fn [ks obj] (ObjMap. nil ks obj)))
12591
+
12592
+ ; The keys field is an array of all keys of this map, in no particular
12593
+ ; order. Each key is hashed and the result used as a property name of
12594
+ ; hashobj. Each values in hashobj is actually a bucket in order to handle hash
12595
+ ; collisions. A bucket is an array of alternating keys (not their hashes) and
12596
+ ; vals.
12597
+ (deftype HashMap [meta count hashobj]
12598
+ IWithMeta
12599
+ (-with-meta [coll meta] (HashMap. meta count hashobj))
12600
+
12601
+ IMeta
12602
+ (-meta [coll] meta)
12603
+
12604
+ ICollection
12605
+ (-conj [coll entry]
12606
+ (if (vector? entry)
12607
+ (-assoc coll (-nth entry 0 ) (-nth entry 1 ))
12608
+ (reduce -conj
12609
+ coll
12610
+ entry)))
12611
+
12612
+ IEmptyableCollection
12613
+ (-empty [coll] (with-meta cljs.core.HashMap/EMPTY meta))
12614
+
12615
+ IEquiv
12616
+ (-equiv [coll other] (equiv-map coll other))
12617
+
12618
+ IHash
12619
+ (-hash [coll] (hash-coll coll))
12620
+
12621
+ ISeqable
12622
+ (-seq [coll]
12623
+ (when (pos? count)
12624
+ (let [hashes (.sort (js-keys hashobj))]
12625
+ (mapcat #(map vec (partition 2 (aget hashobj %)))
12626
+ hashes))))
12627
+
12628
+ ICounted
12629
+ (-count [coll] count)
12630
+
12631
+ ILookup
12632
+ (-lookup [coll k] (-lookup coll k nil ))
12633
+ (-lookup [coll k not-found]
12634
+ (let [bucket (aget hashobj (hash k))
12635
+ i (when bucket (scan-array 2 k bucket))]
12636
+ (if i
12637
+ (aget bucket (inc i))
12638
+ not-found)))
12639
+
12640
+ IAssociative
12641
+ (-assoc [coll k v]
12642
+ (let [h (hash k)
12643
+ bucket (aget hashobj h)]
12644
+ (if bucket
12645
+ (let [new-bucket (aclone bucket)
12646
+ new-hashobj (goog.object/clone hashobj)]
12647
+ (aset new-hashobj h new-bucket)
12648
+ (if-let [i (scan-array 2 k new-bucket)]
12649
+ (do ; found key, replace
12650
+ (aset new-bucket (inc i) v)
12651
+ (HashMap. meta count new-hashobj))
12652
+ (do ; did not find key, append
12653
+ (.push new-bucket k v)
12654
+ (HashMap. meta (inc count) new-hashobj))))
12655
+ (let [new-hashobj (goog.object/clone hashobj)] ; did not find bucket
12656
+ (aset new-hashobj h (array k v))
12657
+ (HashMap. meta (inc count) new-hashobj)))))
12658
+ (-contains-key? [coll k]
12659
+ (let [bucket (aget hashobj (hash k))
12660
+ i (when bucket (scan-array 2 k bucket))]
12661
+ (if i
12662
+ true
12663
+ false )))
12664
+
12665
+ IMap
12666
+ (-dissoc [coll k]
12667
+ (let [h (hash k)
12668
+ bucket (aget hashobj h)
12669
+ i (when bucket (scan-array 2 k bucket))]
12670
+ (if (not i)
12671
+ coll ; key not found, return coll unchanged
12672
+ (let [new-hashobj (goog.object/clone hashobj)]
12673
+ (if (> 3 (.-length bucket))
12674
+ (js-delete new-hashobj h)
12675
+ (let [new-bucket (aclone bucket)]
12676
+ (.splice new-bucket i 2 )
12677
+ (aset new-hashobj h new-bucket)))
12678
+ (HashMap. meta (dec count) new-hashobj)))))
12679
+
12680
+ IFn
12681
+ (-invoke [coll k]
12682
+ (-lookup coll k))
12683
+ (-invoke [coll k not-found]
12684
+ (-lookup coll k not-found)))
12685
+
12686
+ (set! (. HashMap -EMPTY) (HashMap. nil 0 (js-obj )))
12687
+
12688
+ (set! cljs.core.HashMap/fromArrays (fn [ks vs]
12689
+ (let [len (.-length ks)]
12690
+ (loop [i 0 , out cljs.core.HashMap/EMPTY]
12691
+ (if (< i len)
12692
+ (recur (inc i) (assoc out (aget ks i) (aget vs i)))
12693
+ out)))))
12694
+
12695
+ (deftype Set [meta hash-map]
12696
+ IWithMeta
12697
+ (-with-meta [coll meta] (Set. meta hash-map))
12698
+
12699
+ IMeta
12700
+ (-meta [coll] meta)
12701
+
12702
+ ICollection
12703
+ (-conj [coll o]
12704
+ (Set. meta (assoc hash-map o nil )))
12705
+
12706
+ IEmptyableCollection
12707
+ (-empty [coll] (with-meta cljs.core.Set/EMPTY meta))
12708
+
12709
+ IEquiv
12710
+ (-equiv [coll other]
12711
+ (and
12712
+ (set? other)
12713
+ (= (count coll) (count other))
12714
+ (every? #(contains? coll %)
12715
+ other)))
12716
+
12717
+ IHash
12718
+ (-hash [coll] (hash-coll coll))
12719
+
12720
+ ISeqable
12721
+ (-seq [coll] (keys hash-map))
12722
+
12723
+ ICounted
12724
+ (-count [coll] (count (seq coll)))
12725
+
12726
+ ILookup
12727
+ (-lookup [coll v]
12728
+ (-lookup coll v nil ))
12729
+ (-lookup [coll v not-found]
12730
+ (if (-contains-key? hash-map v)
12731
+ v
12732
+ not-found))
12733
+
12734
+ ISet
12735
+ (-disjoin [coll v]
12736
+ (Set. meta (dissoc hash-map v)))
12737
+
12738
+ IFn
12739
+ (-invoke [coll k]
12740
+ (-lookup coll k))
12741
+ (-invoke [coll k not-found]
12742
+ (-lookup coll k not-found)))
12743
+
12744
+ (set! (. Set -EMPTY) (Set. nil (hash-map )))
0 commit comments