ArrayNode
(ArrayNode. edit cnt arr)
(deftype ArrayNode [edit ^:mutable cnt ^:mutable arr]
Object
(inode-assoc [inode shift hash key val added-leaf?]
(let [idx (mask hash shift)
node (aget arr idx)]
(if (nil? node)
(ArrayNode. nil (inc cnt) (clone-and-set arr idx (.inode-assoc (.-EMPTY BitmapIndexedNode) (+ shift 5) hash key val added-leaf?)))
(let [n (.inode-assoc node (+ shift 5) hash key val added-leaf?)]
(if (identical? n node)
inode
(ArrayNode. nil cnt (clone-and-set arr idx n)))))))
(inode-without [inode shift hash key]
(let [idx (mask hash shift)
node (aget arr idx)]
(if-not (nil? node)
(let [n (.inode-without node (+ shift 5) hash key)]
(cond
(identical? n node)
inode
(nil? n)
(if (<= cnt 8)
(pack-array-node inode nil idx)
(ArrayNode. nil (dec cnt) (clone-and-set arr idx n)))
:else
(ArrayNode. nil cnt (clone-and-set arr idx n))))
inode)))
(inode-lookup [inode shift hash key not-found]
(let [idx (mask hash shift)
node (aget arr idx)]
(if-not (nil? node)
(.inode-lookup node (+ shift 5) hash key not-found)
not-found)))
(inode-find [inode shift hash key not-found]
(let [idx (mask hash shift)
node (aget arr idx)]
(if-not (nil? node)
(.inode-find node (+ shift 5) hash key not-found)
not-found)))
(inode-seq [inode]
(create-array-node-seq arr))
(ensure-editable [inode e]
(if (identical? e edit)
inode
(ArrayNode. e cnt (aclone arr))))
(inode-assoc! [inode edit shift hash key val added-leaf?]
(let [idx (mask hash shift)
node (aget arr idx)]
(if (nil? node)
(let [editable (edit-and-set inode edit idx (.inode-assoc! (.-EMPTY BitmapIndexedNode) edit (+ shift 5) hash key val added-leaf?))]
(set! (.-cnt editable) (inc (.-cnt editable)))
editable)
(let [n (.inode-assoc! node edit (+ shift 5) hash key val added-leaf?)]
(if (identical? n node)
inode
(edit-and-set inode edit idx n))))))
(inode-without! [inode edit shift hash key removed-leaf?]
(let [idx (mask hash shift)
node (aget arr idx)]
(if (nil? node)
inode
(let [n (.inode-without! node edit (+ shift 5) hash key removed-leaf?)]
(cond
(identical? n node)
inode
(nil? n)
(if (<= cnt 8)
(pack-array-node inode edit idx)
(let [editable (edit-and-set inode edit idx n)]
(set! (.-cnt editable) (dec (.-cnt editable)))
editable))
:else
(edit-and-set inode edit idx n))))))
(kv-reduce [inode f init]
(let [len (alength arr)] (loop [i 0 init init]
(if (< i len)
(let [node (aget arr i)]
(if-not (nil? node)
(let [init (.kv-reduce node f init)]
(if (reduced? init)
init
(recur (inc i) init)))
(recur (inc i) init)))
init))))
IIterable
(-iterator [coll]
(ArrayNodeIterator. arr 0 nil)))