       (array-copy arr (+ 2 idx-old) dst idx-old (- idx-new idx-old))
       (aset dst idx-new node)
       (array-copy arr (+ idx-new 2) dst (inc idx-new) (- (alength arr) idx-new 2))
-      (BitmapIndexedNode. e (bit-or datamap bit) (bit-xor nodemap bit) dst)))
+      (BitmapIndexedNode. e (bit-xor datamap bit) (bit-or nodemap bit) dst)))
+
+  (merge-two-kv-pairs [inode medit shift key1 val1 key2hash key2 val2]
+    (let [key1hash (hash key1)]
+      (if (and (< shift 32) (== key1hash key2hash))
+        (HashCollisionNode. medit key1hash 2 (array key1 val1 key2 val2))
+        (let [mask1 (mask key1hash shift)
+              mask2 (mask key2hash shift)]
+          (if (== mask1 mask2)
+            (let [new-node (.merge-two-kv-pairs inode medit (+ shift 5) key1 val1 key2hash key2 val2)]
+              (BitmapIndexedNode. medit 0 (bitpos key1hash shift) (array new-node)))
+            (let [new-datamap (bit-or (bitpos key1hash shift) (bitpos key2hash shift))]
+              (if (< mask1 mask2)
+                (BitmapIndexedNode. medit new-datamap 0 (array key1 val1 key2 val2))
+                (BitmapIndexedNode. medit new-datamap 0 (array key2 val2 key1 val1)))))))))
 
   (inode-seq [inode]
     (NodeSeq. nil arr (* 2 (bit-count datamap)) (bit-count nodemap) 0 nil nil))

  (-reduce [coll f start] (seq-reduce f start coll)))

(extend-protocol IPrintWithWriter
  PersistentHashMap
  (-pr-writer [coll writer opts]
    (print-map coll pr-writer writer opts)))

(comment
  (let [em (.-EMPTY PersistentHashMap)
        hm1 (loop [m em i 0]
              (if (< i 50)
                (recur (assoc m (str "key" i) i) (inc i))
                m))]
    (println hm1)
    [(= (into #{} (vals hm1)) (into #{} (map #(get hm1 (str "key" %)) (range 50))))
     (= (keys hm1) (map (fn [[k v]] k) hm1))]))
            (.copy-and-set-node inode aedit bit sub-node-new)
            inode))
        :else
        (let [n (alength arr)
              idx (* 2 (bitmap-indexed-node-index datamap bit))
              new-arr (make-array (+ 2 n))]
          (array-copy arr 0 new-arr 0 idx )
          (aset new-arr idx key)
          (aset new-arr (inc idx) val)
          (array-copy arr idx new-arr (+ 2 idx) (- n idx))
          (set! (.-added changed?) true)
          (BitmapIndexedNode. aedit (bit-or datamap bit) nodemap new-arr)))))

