RedNode
(RedNode. key val left right __hash)
(deftype RedNode [key val left right ^:mutable __hash]
Object
(add-left [node ins]
(RedNode. key val ins right nil))
(add-right [node ins]
(RedNode. key val left ins nil))
(remove-left [node del]
(RedNode. key val del right nil))
(remove-right [node del]
(RedNode. key val left del nil))
(blacken [node]
(BlackNode. key val left right nil))
(redden [node]
(throw (js/Error. "red-black tree invariant violation")))
(balance-left [node parent]
(cond
(instance? RedNode left)
(RedNode. key val
(.blacken left)
(BlackNode. (.-key parent) (.-val parent) right (.-right parent) nil)
nil)
(instance? RedNode right)
(RedNode. (.-key right) (.-val right)
(BlackNode. key val left (.-left right) nil)
(BlackNode. (.-key parent) (.-val parent)
(.-right right)
(.-right parent)
nil)
nil)
:else
(BlackNode. (.-key parent) (.-val parent) node (.-right parent) nil)))
(balance-right [node parent]
(cond
(instance? RedNode right)
(RedNode. key val
(BlackNode. (.-key parent) (.-val parent)
(.-left parent)
left
nil)
(.blacken right)
nil)
(instance? RedNode left)
(RedNode. (.-key left) (.-val left)
(BlackNode. (.-key parent) (.-val parent)
(.-left parent)
(.-left left)
nil)
(BlackNode. key val (.-right left) right nil)
nil)
:else
(BlackNode. (.-key parent) (.-val parent) (.-left parent) node nil)))
(replace [node key val left right]
(RedNode. key val left right nil))
(kv-reduce [node f init]
(tree-map-kv-reduce node f init))
(indexOf [coll x]
(-indexOf coll x 0))
(indexOf [coll x start]
(-indexOf coll x start))
(lastIndexOf [coll x]
(-lastIndexOf coll x (count coll)))
(lastIndexOf [coll x start]
(-lastIndexOf coll x start))
IMapEntry
(-key [node] key)
(-val [node] val)
IHash
(-hash [coll] (caching-hash coll hash-ordered-coll __hash))
IEquiv
(-equiv [coll other] (equiv-sequential coll other))
IMeta
(-meta [node] nil)
IWithMeta
(-with-meta [node meta]
(-with-meta [key val] meta))
IStack
(-peek [node] val)
(-pop [node] [key])
ICollection
(-conj [node o] [key val o])
IEmptyableCollection
(-empty [node] nil)
ISequential
ISeqable
(-seq [node] (IndexedSeq. #js [key val] 0 nil))
IReversible
(-rseq [node] (IndexedSeq. #js [val key] 0 nil))
ICounted
(-count [node] 2)
IIndexed
(-nth [node n]
(cond (== n 0) key
(== n 1) val
:else (throw (js/Error. "Index out of bounds"))))
(-nth [node n not-found]
(cond (== n 0) key
(== n 1) val
:else not-found))
ILookup
(-lookup [node k] (-nth node k nil))
(-lookup [node k not-found] (-nth node k not-found))
IAssociative
(-assoc [node k v]
(assoc [key val] k v))
(-contains-key? [node k]
(or (== k 0) (== k 1)))
IFind
(-find [node k]
(case k
0 (MapEntry. 0 key nil)
1 (MapEntry. 1 val nil)
nil))
IVector
(-assoc-n [node n v]
(-assoc-n [key val] n v))
IReduce
(-reduce [node f]
(ci-reduce node f))
(-reduce [node f start]
(ci-reduce node f start))
IFn
(-invoke [node k]
(-nth node k))
(-invoke [node k not-found]
(-nth node k not-found)))