@@ -5330,6 +5330,21 @@ reduces them without incurring seq initialization"
5330
5330
5331
5331
; ;; PersistentQueue ;;;
5332
5332
5333
+ (deftype PersistentQueueIter [^:mutable fseq riter]
5334
+ Object
5335
+ (hasNext [_]
5336
+ (or (and (some? fseq) (seq fseq)) (and (some? riter) (.hasNext riter))))
5337
+ (next [_]
5338
+ (cond
5339
+ (some? fseq)
5340
+ (let [ret (first fseq)]
5341
+ (set! fseq (next fseq))
5342
+ ret)
5343
+ (and (some? riter) ^boolean (.hasNext riter))
5344
+ (.next riter)
5345
+ :else (throw (js/Error. " No such element" ))))
5346
+ (remove [_] (js/Error. " Unsupported operation" )))
5347
+
5333
5348
(deftype PersistentQueueSeq [meta front rear ^:mutable __hash]
5334
5349
Object
5335
5350
(toString [coll]
@@ -5380,6 +5395,10 @@ reduces them without incurring seq initialization"
5380
5395
ICloneable
5381
5396
(-clone [coll] (PersistentQueue. meta count front rear __hash))
5382
5397
5398
+ IIterable
5399
+ (-iterator [coll]
5400
+ (PersistentQueueIter. front (-iterator rear)))
5401
+
5383
5402
IWithMeta
5384
5403
(-with-meta [coll meta] (PersistentQueue. meta count front rear __hash))
5385
5404
@@ -5607,6 +5626,19 @@ reduces them without incurring seq initialization"
5607
5626
5608
5627
(set! (.-fromObject ObjMap) (fn [ks obj] (ObjMap. nil ks obj 0 nil )))
5609
5628
5629
+ ; ; Record Iterator
5630
+ (deftype RecordIter [^:mutable i record base-count fields ext-map-iter]
5631
+ Object
5632
+ (hasNext [_]
5633
+ (or (< i base-count) (.hasNext ext-map-iter)))
5634
+ (next [_]
5635
+ (if (< i base-count)
5636
+ (let [k (nth fields i)]
5637
+ (set! i (inc i))
5638
+ [k (-lookup record k)])
5639
+ (.next ext-map-iter)))
5640
+ (remove [_] (js/Error. " Unsupported operation" )))
5641
+
5610
5642
; ; EXPERIMENTAL: subject to change
5611
5643
(deftype ES6EntriesIterator [^:mutable s]
5612
5644
Object
@@ -6109,6 +6141,44 @@ reduces them without incurring seq initialization"
6109
6141
6110
6142
(declare ArrayNode )
6111
6143
6144
+ (deftype NodeIterator [arr ^:mutable i ^:mutable next-entry ^:mutable next-iter]
6145
+ Object
6146
+ (advance [this]
6147
+ (let [len (alength arr)]
6148
+ (loop []
6149
+ (if (< i len)
6150
+ (let [key (aget arr i)
6151
+ node-or-val (aget arr (inc i))
6152
+ ^boolean found
6153
+ (cond (some? key)
6154
+ (set! next-entry [key node-or-val])
6155
+ (some? node-or-val)
6156
+ (let [new-iter (-iterator node-or-val)]
6157
+ (if ^boolean (.hasNext new-iter)
6158
+ (set! next-iter new-iter)
6159
+ false ))
6160
+ :else false )]
6161
+ (set! i (+ i 2 ))
6162
+ (if found true (recur )))
6163
+ false ))))
6164
+ (hasNext [this]
6165
+ (or (some? next-entry) (some? next-iter) (.advance this)))
6166
+ (next [this]
6167
+ (cond
6168
+ (some? next-entry)
6169
+ (let [ret next-entry]
6170
+ (set! next-entry nil )
6171
+ ret)
6172
+ (some? next-iter)
6173
+ (let [ret (.next next-iter)]
6174
+ (when-not ^boolean (.hasNext next-iter)
6175
+ (set! next-iter nil ))
6176
+ ret)
6177
+ ^boolean (.advance this)
6178
+ (.next this)
6179
+ :else (throw (js/Error. " No such element" ))))
6180
+ (remove [_] (js/Error. " Unsupported operation" )))
6181
+
6112
6182
(deftype BitmapIndexedNode [edit ^:mutable bitmap ^:mutable arr]
6113
6183
Object
6114
6184
(inode-assoc [inode shift hash key val added-leaf?]
@@ -6303,7 +6373,11 @@ reduces them without incurring seq initialization"
6303
6373
:else inode)))))
6304
6374
6305
6375
(kv-reduce [inode f init]
6306
- (inode-kv-reduce arr f init)))
6376
+ (inode-kv-reduce arr f init))
6377
+
6378
+ IIterable
6379
+ (-iterator [coll]
6380
+ (NodeIterator. arr 0 nil nil )))
6307
6381
6308
6382
(set! (.-EMPTY BitmapIndexedNode) (BitmapIndexedNode. nil 0 (make-array 0 )))
6309
6383
@@ -6320,6 +6394,26 @@ reduces them without incurring seq initialization"
6320
6394
(recur (inc i) j bitmap))
6321
6395
(BitmapIndexedNode. edit bitmap new-arr)))))
6322
6396
6397
+ (deftype ArrayNodeIterator [arr ^:mutable i ^:mutable next-iter]
6398
+ Object
6399
+ (hasNext [this]
6400
+ (let [len (alength arr)]
6401
+ (loop []
6402
+ (if-not (and (some? next-iter) ^boolean (.hasNext next-iter))
6403
+ (if (< i len)
6404
+ (let [node (aget arr i)]
6405
+ (set! i (inc i))
6406
+ (when (some? node)
6407
+ (set! next-iter (-iterator node)))
6408
+ (recur ))
6409
+ false )
6410
+ true ))))
6411
+ (next [this]
6412
+ (if ^boolean (.hasNext this)
6413
+ (.next next-iter)
6414
+ (throw (js/Error. " No such element" ))))
6415
+ (remove [_] (js/Error. " Unsupported operation" )))
6416
+
6323
6417
(deftype ArrayNode [edit ^:mutable cnt ^:mutable arr]
6324
6418
Object
6325
6419
(inode-assoc [inode shift hash key val added-leaf?]
@@ -6415,7 +6509,11 @@ reduces them without incurring seq initialization"
6415
6509
@init
6416
6510
(recur (inc i) init)))
6417
6511
(recur (inc i) init)))
6418
- init)))))
6512
+ init))))
6513
+
6514
+ IIterable
6515
+ (-iterator [coll]
6516
+ (ArrayNodeIterator. arr 0 nil )))
6419
6517
6420
6518
(defn- hash-collision-node-find-index [arr cnt key]
6421
6519
(let [lim (* 2 cnt)]
@@ -6522,7 +6620,11 @@ reduces them without incurring seq initialization"
6522
6620
editable))))))
6523
6621
6524
6622
(kv-reduce [inode f init]
6525
- (inode-kv-reduce arr f init)))
6623
+ (inode-kv-reduce arr f init))
6624
+
6625
+ IIterable
6626
+ (-iterator [coll]
6627
+ (NodeIterator. arr 0 nil nil )))
6526
6628
6527
6629
(defn- create-node
6528
6630
([shift key1 val1 key2hash key2 val2]
@@ -6660,6 +6762,18 @@ reduces them without incurring seq initialization"
6660
6762
6661
6763
(declare TransientHashMap )
6662
6764
6765
+ (deftype HashMapIter [nil-val root-iter ^:mutable seen]
6766
+ Object
6767
+ (hasNext [_]
6768
+ (and ^boolean seen ^boolean (.hasNext root-iter)))
6769
+ (next [_]
6770
+ (if-not ^boolean seen
6771
+ (do
6772
+ (set! seen true )
6773
+ nil-val)
6774
+ (.next root-iter)))
6775
+ (remove [_] (js/Error. " Unsupported operation" )))
6776
+
6663
6777
(deftype PersistentHashMap [meta cnt root ^boolean has-nil? nil-val ^:mutable __hash]
6664
6778
Object
6665
6779
(toString [coll]
@@ -6685,6 +6799,13 @@ reduces them without incurring seq initialization"
6685
6799
ICloneable
6686
6800
(-clone [_] (PersistentHashMap. meta cnt root has-nil? nil-val __hash))
6687
6801
6802
+ IIterable
6803
+ (-iterator [coll]
6804
+ (let [root-iter (if ^boolean root (-iterator root) nil-iter)]
6805
+ (if has-nil?
6806
+ (HashMapIter. nil-val root-iter false )
6807
+ root-iter)))
6808
+
6688
6809
IWithMeta
6689
6810
(-with-meta [coll meta] (PersistentHashMap. meta cnt root has-nil? nil-val __hash))
6690
6811
@@ -7812,6 +7933,16 @@ reduces them without incurring seq initialization"
7812
7933
7813
7934
(declare TransientHashSet )
7814
7935
7936
+ (deftype HashSetIter [iter]
7937
+ Object
7938
+ (hasNext [_]
7939
+ (.hasNext iter))
7940
+ (next [_]
7941
+ (if ^boolean (.hasNext iter)
7942
+ (aget (.-tail (.next iter)) 0 )
7943
+ (throw (js/Error. " No such element" ))))
7944
+ (remove [_] (js/Error. " Unsupported operation" )))
7945
+
7815
7946
(deftype PersistentHashSet [meta hash-map ^:mutable __hash]
7816
7947
Object
7817
7948
(toString [coll]
@@ -7835,6 +7966,10 @@ reduces them without incurring seq initialization"
7835
7966
ICloneable
7836
7967
(-clone [_] (PersistentHashSet. meta hash-map __hash))
7837
7968
7969
+ IIterable
7970
+ (-iterator [coll]
7971
+ (HashSetIter. (-iterator hash-map)))
7972
+
7838
7973
IWithMeta
7839
7974
(-with-meta [coll meta] (PersistentHashSet. meta hash-map __hash))
7840
7975
0 commit comments