                 (do
                   (.split this)
                   (recur 0)))))))
-  (keys [this]
-       (let [results #js []]
-         (loop []
-           (let [result (.next this)]
-             (when-not (nil? result)
-               (apush results result)
-               (recur))))
+  (val [this]
+       (let [result 1]
+         (dotimes [constraint (alength constraints)]
+           (set!! result (* result (.val (aget constraints constraint) solver constraint))))
+         result))
+  (elems [this]
+         (let [results #js []]
+           (loop []
+             (let [result (.next this)]
+               (when-not (nil? result)
+                 (apush results result)
+                 (apush results (.val this))
+                 (recur))))
          results))
   ISeqable
   (-seq [this]
-        (seq (.keys this))))
+        (seq (map vec (partition 2 (.elems this))))))
 
 (defn solver [num-vars constraints]
   (let [los (least-key num-vars)


(defn apply-to-tree [tree updates]
  [tree
   (for [update updates]
     (.update tree (nth update 1) (nth update 2)))])

(defn apply-to-sorted-map [map updates]
  (let [map (atom map)
        results (for [update updates]
                  (let [key (nth update 1)
                        delta (nth update 2)
                        old-val (get @map key 0)
                        new-val (+ old-val delta)]
                    (if (== new-val 0)
                      (swap! map dissoc key)
                      (swap! map assoc key new-val))
                    (cond
                     (and (== old-val 0) (not (== new-val 0))) added
                     (and (not (== old-val 0)) (not (== new-val 0))) changed
                     (and (not (== old-val 0)) (== new-val 0)) deled)))]
    [@map results]))

(defn run-building-prop [min-keys key-len updates]
  (let [[tree tree-results] (apply-to-tree (tree min-keys key-len) updates)
        [sorted-map sorted-map-results] (apply-to-sorted-map (sorted-map-by key-compare) updates)]
    (and (= (seq (map vec tree)) (seq sorted-map))
         (= tree-results sorted-map-results)
         (.valid! tree))))


  (reset [this solver constraint]
         (dotimes [i (alength vars)]
           (.set-watch solver (aget vars i) constraint true)))
  (val [this solver constraint]
       1)
  (split-left [this solver constraint]
              false)
  (split-right [this solver constraint])
