 ;; On creation/reset, points to first key
 ;; On next, either point to next key or set .-end? true. Return (not .-end?)
 ;; On seek, point to first key greater than or equal to seek-key. Return (key= (.key this) seek-key)
-;; On push, push current position onto a stack
-;; On pop, pop last position from the stack
 ;; NOTE iterators are not write-safe unless reset after writing
 
-(deftype Iterator [tree ^:mutable node ^:mutable ix ^:mutable end? ^:mutable old-node ^:mutable old-ix ^:mutable old-end? pushed-nodes pushed-ixes pushed-end?s key-len]
+(deftype Iterator [tree ^:mutable node ^:mutable ix ^:mutable end? key-len]
   Object
   (reset [this]
          (set! node (.-root tree))
          (set! ix 0)
          (while (not (nil? (.-children node)))
            (set! node (aget (.-children node) 0)))
-         (set! end? (<= (alength (.-keys node)) 0))
-         (aclear pushed-nodes)
-         (aclear pushed-ixes)
-         (aclear pushed-end?s))
+         (set! end? (<= (alength (.-keys node)) 0)))
   (key [this]
        (when (false? end?)
          (aget (.-keys node) ix)))

       (when (false? end?)
         (aget (.-vals node) ix)))
  (next [this]
        (set! old-node node)
        (set! old-ix ix)
        (set! old-end? end?)
        (if (true? end?)
          false
          (if (nil? (.-children node))

    iterator))

;; used only inside Join
(deftype JoinIterator [iterator inner->outer inner-key outer-key key-len blocked?]
  Object
  (maintain [this]
            (if (.-end? iterator)
              (set! blocked? true)
              (let [new-key (.key iterator)]
                (set! blocked? false)
                (dotimes [inner-ix (alength inner->outer)]
                  (aset outer-key (aget inner->outer inner-ix) (aget new-key inner-ix))))))
  (reset [this]
         (.reset iterator)
         (.maintain this))
  (key [this]
       outer-key)
  (next [this]
        (.next iterator)
        (.maintain this))
  (seek [this key]
        (dotimes [inner-ix (alength inner->outer)]
          (aset inner-key inner-ix (aget outer-key (aget inner->outer inner-ix))))
        (.seek iterator seek-key)
        (dotimes [outer-ix (alength outer->inner)]
          (aset outer-key outer-ix (aget key outer-ix)))
        (.maintain this)))

(deftype Join [current-key iterators ^:mutable end?]
  Object
  (reset [this]
         (debug)
