Documentation

CslibFrontier.DataStructure.BPlusTree.PredSucc

Predecessor and successor queries in B+-trees #

predM and succM return the entry with the greatest key strictly below the query, respectively the least key strictly above it. Each follows the search of lookupM to a leaf and scans it with Cslib.SortedAssoc.predM, respectively Cslib.SortedAssoc.succM.

The leaf that the search reaches need not hold the answer. Every key of that leaf may be at least the query, and then the predecessor is the last entry of the leaf before it; every key may be at most the query, and then the successor is the first entry of the leaf after it. The operations here handle this by a sibling fallback: at the node where the descent into a child yields nothing, they take the greatest entry of the child before it, respectively the least entry of the child after it, with maxM and minM. In a well-formed tree that sibling is not empty, so the fallback runs at most once, and the walk it adds visits at most one node on each level below that node.

An implementation with chained leaves would instead step from the leaf to its neighbour. Modelling that, by passing the neighbouring leaf down the descent and charging one visit when it is consulted, is left as future work.

Main definitions #

Main results #

@[irreducible]
def Cslib.DataStructure.BPlusTree.predM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
BPlusTree α β → m (Option (α × β))

Strict predecessor. Pass the separators that are at most k and descend into the child introduced by the last of them; in the leaf, scan the entries. Where the part of a node after a separator yields nothing, return the greatest entry of the child before that separator. visit fires once at each node on the path, and once at each node of the fallback walk.

@[irreducible]
def Cslib.DataStructure.BPlusTree.succM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
BPlusTree α β → m (Option (α × β))

Strict successor. Pass the separators that are at most k and descend into the child introduced by the last of them; in the leaf, scan the entries. Where the child before a separator yields nothing, return the least entry of the child that the separator introduces. visit fires once at each node on the path, and once at each node of the fallback walk.

theorem Cslib.DataStructure.BPlusTree.map_predM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : BPlusTree α β) :
f (predM le visit k tr) = predM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the predecessor query.

theorem Cslib.DataStructure.BPlusTree.map_succM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : BPlusTree α β) :
f (succM le visit k tr) = succM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the successor query.

def Cslib.DataStructure.BPlusTree.pred? {α β : Type} [LinearOrder α] (k : α) (tr : BPlusTree α β) :
Option (α × β)

Pure strict predecessor: the Id instantiation of predM with visit := pure ().

def Cslib.DataStructure.BPlusTree.succ? {α β : Type} [LinearOrder α] (k : α) (tr : BPlusTree α β) :
Option (α × β)

Pure strict successor: the Id instantiation of succM with visit := pure ().

theorem Cslib.DataStructure.BPlusTree.pred?_def {α β : Type} [LinearOrder α] {tr : BPlusTree α β} (k : α) :

pred? is the Id instantiation of predM.

theorem Cslib.DataStructure.BPlusTree.succ?_def {α β : Type} [LinearOrder α] {tr : BPlusTree α β} (k : α) :

succ? is the Id instantiation of succM.

theorem Cslib.DataStructure.BPlusTree.pred?_leaf {α β : Type} [LinearOrder α] (k : α) (entries : List (α × β)) :

The predecessor in a leaf is the scan of its entries.

@[simp]
theorem Cslib.DataStructure.BPlusTree.pred?_node_nil {α β : Type} [LinearOrder α] (k : α) (first : BPlusTree α β) :
pred? k (first.node []) = pred? k first

The predecessor in a node without separators is the predecessor in its only child.

@[simp]
theorem Cslib.DataStructure.BPlusTree.pred?_node_cons {α β : Type} [LinearOrder α] (k s : α) (first c : BPlusTree α β) (rest : List (Branch α β)) :
pred? k (first.node ((s, c) :: rest)) = if s ≤ k then (pred? k (c.node rest)).or first.max? else pred? k first

The predecessor at a node's first separator: when it is at most the query, look after it and fall back on the greatest entry of the child before it; otherwise descend into that child.

theorem Cslib.DataStructure.BPlusTree.succ?_leaf {α β : Type} [LinearOrder α] (k : α) (entries : List (α × β)) :

The successor in a leaf is the scan of its entries.

@[simp]
theorem Cslib.DataStructure.BPlusTree.succ?_node_nil {α β : Type} [LinearOrder α] (k : α) (first : BPlusTree α β) :
succ? k (first.node []) = succ? k first

The successor in a node without separators is the successor in its only child.

@[simp]
theorem Cslib.DataStructure.BPlusTree.succ?_node_cons {α β : Type} [LinearOrder α] (k s : α) (first c : BPlusTree α β) (rest : List (Branch α β)) :
succ? k (first.node ((s, c) :: rest)) = if s ≤ k then succ? k (c.node rest) else (succ? k first).or c.min?

The successor at a node's first separator: when it is at most the query, look after it; otherwise descend into the child before it and fall back on the least entry of the child that the separator introduces.

theorem Cslib.DataStructure.BPlusTree.pred?_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) :

Model equation of the predecessor on well-formed trees.

theorem Cslib.DataStructure.BPlusTree.succ?_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) :

Model equation of the successor on well-formed trees.