Documentation

CslibFrontier.DataStructure.BPlusTree.Range

Range queries in B+-trees #

rangeM lo hi returns the entries whose keys lie in the closed interval [lo, hi], in key order. At an interior node it walks along the separators. The child before a separator s holds keys below s, so it is entered only when s is not at most lo; the children after s hold keys that are at least s, so the walk goes on only while s is at most hi. That is two comparisons per separator reached. In a leaf it runs the scan Cslib.SortedAssoc.rangeM of the entry list. visit fires once at each node entered, the leaves included. Costs are not stated here.

The walk enters children one after another; it does not follow a chain of leaves. What a chained implementation would return is described by range_eq_flatten_run: the output is the restriction of a contiguous run of leaves, every leaf of the run contributes, and every leaf of the run other than its first and last is returned whole.

Main definitions #

Main results #

@[irreducible]
def Cslib.DataStructure.BPlusTree.rangeM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) :
BPlusTree α β → m (List (α × β))

Entries with key in the closed interval [lo, hi]. Walk along the separators of a node: enter the child before a separator unless the separator is at most lo, and go on past the separator only when it is at most hi; in a leaf, scan the entries. visit fires once at each node entered, when the walk along its separators ends.

theorem Cslib.DataStructure.BPlusTree.map_rangeM {α β : 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) (lo hi : α) (tr : BPlusTree α β) :
f (rangeM le visit lo hi tr) = rangeM (fun (a b : α) => f (le a b)) (f visit) lo hi tr

A monad morphism commutes with the range query.

def Cslib.DataStructure.BPlusTree.range {α β : Type} [LinearOrder α] (lo hi : α) (tr : BPlusTree α β) :
List (α × β)

Pure range restriction: the Id instantiation of rangeM with visit := pure ().

theorem Cslib.DataStructure.BPlusTree.range_def {α β : Type} [LinearOrder α] {tr : BPlusTree α β} (lo hi : α) :
range lo hi tr = (rangeM SortedAssoc.leI (pure ()) lo hi tr).run

range is the Id instantiation of rangeM.

theorem Cslib.DataStructure.BPlusTree.range_leaf {α β : Type} [LinearOrder α] (lo hi : α) (entries : List (α × β)) :
range lo hi (leaf entries) = (SortedAssoc.rangeM SortedAssoc.leI lo hi entries).run

Range restriction of a leaf is the scan of its entries.

@[simp]
theorem Cslib.DataStructure.BPlusTree.range_node_nil {α β : Type} [LinearOrder α] (lo hi : α) (first : BPlusTree α β) :
range lo hi (first.node []) = range lo hi first

Range restriction of a node without separators is that of its only child.

theorem Cslib.DataStructure.BPlusTree.range_node_cons {α β : Type} [LinearOrder α] (lo hi s : α) (first c : BPlusTree α β) (rest : List (Branch α β)) :
range lo hi (first.node ((s, c) :: rest)) = (if s ≤ lo then [] else range lo hi first) ++ if s ≤ hi then range lo hi (c.node rest) else []

Range restriction at a node's first separator: the child before it contributes unless the separator is at most lo, and what follows it contributes when the separator is at most hi.

theorem Cslib.DataStructure.BPlusTree.range_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (lo hi : α) :
range lo hi tr = SortedAssoc.range lo hi tr.toList

Model equation of range restriction on well-formed trees.

theorem Cslib.DataStructure.BPlusTree.Occupied.ne_nil_of_mem_leaves {α β : Type} {t : ℕ} {tr : BPlusTree α β} (ht : 2 ≤ t) (ho : Occupied t tr) (l : List (α × β)) :
l ∈ tr.leaves → l ≠ []

An occupied tree has no empty leaf when 2 ≤ t.

theorem Cslib.DataStructure.BPlusTree.range_eq_flatten_run {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (lo hi : α) :
∃ (pre : List (List (α × β))) (run : List (List (α × β))) (post : List (List (α × β))), tr.leaves = pre ++ run ++ post ∧ range lo hi tr = (List.map (SortedAssoc.range lo hi) run).flatten ∧ (∀ l ∈ run, SortedAssoc.range lo hi l ≠ []) ∧ ∀ l ∈ (List.drop 1 run).dropLast, SortedAssoc.range lo hi l = l

On a well-formed tree, the output of a range query is the restriction of a contiguous run of leaves in which every leaf contributes and every leaf other than the first and last is returned whole. Without the last two conjuncts the run could be all of leaves.