Documentation

CslibFrontier.DataStructure.BPlusTree.Rank

Rank queries in B+-trees #

rankM counts the entries with key strictly below the query by one descent, following the search of lookupM. Each separator passed contributes the size of the child before it, and the leaf at the end of the path contributes the rank of the query in its entry list, by the scan Cslib.SortedAssoc.rankM. The sizes of the children passed are pure subtree sizes: computing them is uncharged client computation, as in the classic tree. visit fires once at each node on the path, the leaf included.

Main definitions #

Main results #

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

Rank by a single descent. Pass the separators that are at most k, adding the pure size of the child before each; descend into the child introduced by the last of them; in the leaf, scan the entries. visit fires once at each node on the path.

theorem Cslib.DataStructure.BPlusTree.map_rankM {α β : 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 (rankM le visit k tr) = rankM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the rank query.

def Cslib.DataStructure.BPlusTree.rank {α β : Type} [LinearOrder α] (k : α) (tr : BPlusTree α β) :

Pure rank: the Id instantiation of rankM with visit := pure ().

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

rank is the Id instantiation of rankM.

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

Rank in a leaf is the scan of its entries.

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

Rank in a node without separators is rank in its only child.

@[simp]
theorem Cslib.DataStructure.BPlusTree.rank_node_cons {α β : Type} [LinearOrder α] (k s : α) (first c : BPlusTree α β) (rest : List (Branch α β)) :
rank k (first.node ((s, c) :: rest)) = if s ≤ k then first.size + rank k (c.node rest) else rank k first

Rank at a node's first separator: when it is at most the query, count the child before it and pass it; otherwise descend into that child.

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

Model equation of rank on well-formed trees.