Predecessor and successor search in red-black trees on the CentralStrQ word RAM
DefinitionCentralStrQ_RBSearchSearch programs for the red-black trees stored in word-RAM memory by Def_CentralStrQ_RBTrees (a node at holds its key at , the left and right child pointers at and , and its color at ; RBAt m lo hi p t says that Batteries' tree t : RBTree.RBNode ℕ is stored at ).
Answers in memory. OptAt m f a o stores an optional key in two cells: none is the flag , and some y is the flag together with .
Programs. Both programs use the register block of rbInsertStmt g: the root pointer in , the key in , the current node in , a stop flag in , scratch in , and the answer in (flag) and (key). Each is one root-to-leaf descent that keeps the best key found so far in :
rbLowerStmt g(loop bodyrbLowerBody g) computes Batteries'RBNode.lowerBound? (compare k), the largest key (predecessor search). At a node with key : if it records and stops; if it moves to the left child; if it records and moves to the right child. It stops at an empty subtree.rbUpperStmt g(loop bodyrbUpperBody g) computesRBNode.upperBound? (compare k), the smallest key (successor search), symmetrically.
RBSearchHyp g lo hi k Rb t m₀ bundles their hypotheses: the registers lie below the node area , , the memory stores at within , , and bounds the space.
import Mathlib
import Definitions.Def_CentralStrQ_Machine
import Definitions.Def_CentralStrQ_Hoare
import Definitions.Def_CentralStrQ_Structured
import Definitions.Def_CentralStrQ_RBTrees
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Predecessor and successor search in red-black trees on the `CentralStrQ` word RAM: definitions
Search programs for the pointer-based red-black trees of `Def_CentralStrQ_RBTrees` (node at `q`:
key `q`, left pointer `q + 1`, right pointer `q + 2`, color `q + 3`; `RBAt m lo hi p t` says that
Batteries' `RBTree.RBNode` tree `t` is stored at `p`). The programs compute Batteries'
`RBNode.lowerBound?` (the largest key `≤ k`, i.e. predecessor search) and `RBNode.upperBound?`
(the smallest key `≥ k`, successor search) by a single root-to-leaf descent.
* `OptAt m f a o`: an optional key in two cells, `none` as the flag `[f] = 0`, `some y` as the
flag `[f] = 1` with `[a] = y`.
* `rbLowerStmt g` / `rbUpperStmt g`: registers in the cells `g, …, g + 9` (root pointer in
`g + 6`, key in `g + 2`, current node `g + 1`, stop flag `g + 3`, scratch `g + 4`, answer in
`g + 8`, `g + 9`; the same register block as `rbInsertStmt g`), with the loop bodies
`rbLowerBody g` / `rbUpperBody g`. `RBSearchHyp` bundles their hypotheses.
-/
variable {w : Width}
/-- An optional key in two cells: `none` is the flag `[f] = 0`; `some y` is the flag `[f] = 1`
together with `[a] = y`. -/
def OptAt (m : Memory w) (f a : ℕ) : Option ℕ → Prop
| none => (m f).toNat = 0
| some y => (m f).toNat = 1 ∧ (m a).toNat = y
/-- One step of predecessor search (registers from `g`: `g + 1` the current node, `g + 2` the
key, `g + 3` the stop flag, `g + 4` scratch, `g + 8`/`g + 9` the best key so far as `OptAt`):
stop at an empty subtree; at a node holding the key record it and stop; at a larger key go
left; at a smaller key record it and go right. -/
def rbLowerBody (g : ℕ) : Stmt :=
.ite .eq (.memory (.direct (g + 1))) (.immediate 0)
(.copy (.direct (g + 3)) (.immediate 1))
(.ite .eq (.memory (.indirect (g + 1))) (.memory (.direct (g + 2)))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 1))))
(.seq (.copy (.direct (g + 8)) (.immediate 1))
(.copy (.direct (g + 3)) (.immediate 1))))
(.ite .lt (.memory (.direct (g + 2))) (.memory (.indirect (g + 1)))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 1))))
(.seq (.copy (.direct (g + 8)) (.immediate 1))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))))))
/-- **Predecessor search as a word-RAM program**: the largest key `≤ [g + 2]` in the tree whose
root pointer is in `g + 6`, reported in the cells `g + 8`, `g + 9` (`OptAt`). -/
def rbLowerStmt (g : ℕ) : Stmt :=
.seq (.copy (.direct (g + 1)) (.memory (.direct (g + 6))))
(.seq (.copy (.direct (g + 8)) (.immediate 0))
(.seq (.copy (.direct (g + 3)) (.immediate 0))
(.loop .lt (.memory (.direct (g + 3))) (.immediate 1) (rbLowerBody g))))
/-- One step of successor search (registers as in `rbLowerBody`): stop at an empty subtree; at a
node holding the key record it and stop; at a larger key record it and go left; at a smaller
key go right. -/
def rbUpperBody (g : ℕ) : Stmt :=
.ite .eq (.memory (.direct (g + 1))) (.immediate 0)
(.copy (.direct (g + 3)) (.immediate 1))
(.ite .eq (.memory (.indirect (g + 1))) (.memory (.direct (g + 2)))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 1))))
(.seq (.copy (.direct (g + 8)) (.immediate 1))
(.copy (.direct (g + 3)) (.immediate 1))))
(.ite .lt (.memory (.direct (g + 2))) (.memory (.indirect (g + 1)))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 1))))
(.seq (.copy (.direct (g + 8)) (.immediate 1))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))))
/-- **Successor search as a word-RAM program**: the smallest key `≥ [g + 2]` in the tree whose
root pointer is in `g + 6`, reported in the cells `g + 8`, `g + 9` (`OptAt`). -/
def rbUpperStmt (g : ℕ) : Stmt :=
.seq (.copy (.direct (g + 1)) (.memory (.direct (g + 6))))
(.seq (.copy (.direct (g + 8)) (.immediate 0))
(.seq (.copy (.direct (g + 3)) (.immediate 0))
(.loop .lt (.memory (.direct (g + 3))) (.immediate 1) (rbUpperBody g))))
section rbsearch
variable (g lo hi k Rb : ℕ) (t : RBTree.RBNode ℕ) (m₀ : Memory w)
/-- Hypotheses of the searches: registers in the cells `g, …, g + 9` below the node area
`[lo, hi)` (root pointer in `g + 6`, key in `g + 2`), no overflow, the tree `t` stored at the
root, and `Rb ≥ hi` bounds the space. -/
structure RBSearchHyp : Prop where
below : g + 10 ≤ lo
lohi : lo ≤ hi
fit : hi < 2 ^ w.bits
tree : RBAt m₀ lo hi (m₀ (g + 6)).toNat t
key : (m₀ (g + 2)).toNat = k
bound : hi ≤ Rb
end rbsearch
end CentralStrQ