Documentation

CslibFrontier.DataStructure.BPlusTree.OfSortedList

Bulk loading of B+-trees #

Bottom-up bulk loading of the B+-trees of Cslib.DataStructure.BPlusTree: a tree is built from a list of entries in one pass, level by level from the bottom.

The bottom level cuts the entries into leaves of t - 1 to 2 * t - 1 entries. chunkSizes chooses the sizes with one look ahead: a leaf of 2 * t - 1 entries unless that would leave fewer than t - 1 for the rest, then one of t - 1. At most 2 * t - 1 entries, the empty list included, form a single root leaf. Each leaf after the first is introduced by a separator, which is a copy of its first key: no entry moves up between leaves.

A level above the leaves is a first tree followed by (separator, tree) branches, all of its trees of one height. A round cuts the branches of a level into groups of t - 1 to 2 * t - 1 branches with one branch between consecutive groups: each group becomes a node one level taller, and the separator of each branch between groups moves up, its child becoming the first child of the next node. This is the round of the classic loader Cslib.DataStructure.BTree.ofSortedList, and the group sizes are its Cslib.DataStructure.BTree.groupSizes. packM cuts a level by a list of sizes, and buildM runs rounds until at most 2 * t - 1 branches are left, which become the root. Below 2 the loader uses minimum degree 2.

Each operation is defined once over an arbitrary monad m, with a node-visit action visit : m Unit that fires once per node built, the leaves included; no comparator is needed, since the input order is kept. The pure operation is the Id instantiation with visit := pure (). All recursion is structural, on a fuel argument or on a list of sizes, so the definitions are @[expose]d and decide evaluates bulk loading on concrete inputs; when the fuel or the sizes run out, what remains becomes a single leaf or node, which keeps the entries. Costs are not stated here.

Main definitions #

Main results #

References #

The statements here are not taken from a source.

The sizes of the leaves when n entries are cut into leaves of t - 1 to 2 * t - 1 entries, with recursion bounded by fuel. A leaf has 2 * t - 1 entries unless that would leave fewer than t - 1, then t - 1; at most 2 * t - 1 entries form the last leaf. Out of fuel, the remaining entries form one leaf. The sizes are in bounds for 2 ≤ t; below that a size can be 0.

def Cslib.DataStructure.BPlusTree.leafBranchesM {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :
List ℕ → List (α × β) → m (List (Branch α β))

Cut entries into leaves by a list of sizes, each leaf under a copy of its first key as its separator. When the sizes run out, the remaining entries form one leaf. A size of 0 gives an empty leaf under the first remaining key. visit fires once per leaf built.

@[simp]
theorem Cslib.DataStructure.BPlusTree.leafBranchesM_nil {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (ss : List ℕ) :

No entries give no leaves.

def Cslib.DataStructure.BPlusTree.leafLevelM {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :
List ℕ → List (α × β) → m (BPlusTree α β × List (Branch α β))

The bottom level: the first leaf, of the first size, and the later leaves with their separators. With no sizes, all entries form one leaf. visit fires once per leaf built.

def Cslib.DataStructure.BPlusTree.packM {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first : BPlusTree α β) (rest : List (Branch α β)) :
List ℕ → m (BPlusTree α β × List (Branch α β))

Cut a level by a list of group sizes: the node over the first group, and the next level's branches, each a separator moved up with the node over the following group. The last group takes the remaining branches. visit fires once per node built.

@[simp]
theorem Cslib.DataStructure.BPlusTree.packM_nil {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first : BPlusTree α β) (rest : List (Branch α β)) :
packM visit first rest [] = do visit pure (first.node rest, [])

With no sizes left, the whole level is one node.

theorem Cslib.DataStructure.BPlusTree.packM_cons {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first : BPlusTree α β) (rest : List (Branch α β)) (s : ℕ) (ss : List ℕ) :
packM visit first rest (s :: ss) = match List.drop s rest with | [] => do visit pure (first.node rest, []) | (k, c) :: rest' => do visit let p ← packM visit c rest' ss pure (first.node (List.take s rest), (k, p.1) :: p.2)

A group of s branches becomes a node, and the separator of the branch after it moves up over the rest.

def Cslib.DataStructure.BPlusTree.buildM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) :
ℕ → BPlusTree α β → List (Branch α β) → m (BPlusTree α β)

Rounds of bulk loading at minimum degree t, at most fuel of them: a level with no branches is its first tree, a level with at most 2 * t - 1 branches becomes the root, and a longer one is cut by Cslib.DataStructure.BTree.groupSizes into the next level. Out of fuel, the level becomes one node.

@[simp]
theorem Cslib.DataStructure.BPlusTree.buildM_nil {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (fuel : ℕ) (first : BPlusTree α β) :
buildM t visit fuel first [] = pure first

A level with no branches is its first tree.

theorem Cslib.DataStructure.BPlusTree.buildM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (fuel : ℕ) (first : BPlusTree α β) (e : Branch α β) (es : List (Branch α β)) :
buildM t visit fuel first (e :: es) = if (e :: es).length ≤ 2 * t - 1 then do visit pure (first.node (e :: es)) else match fuel with | 0 => do visit pure (first.node (e :: es)) | fuel.succ => do let p ← packM visit first (e :: es) (BTree.groupSizes t (e :: es).length (e :: es).length) buildM t visit fuel p.1 p.2

A round of bulk loading on a level with branches.

def Cslib.DataStructure.BPlusTree.ofSortedListM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (l : List (α × β)) :
m (BPlusTree α β)

Bulk loading at minimum degree t, or 2 when t is below 2: the bottom level cut by chunkSizes, then the rounds of buildM, with fuel the number of branches of the bottom level.

theorem Cslib.DataStructure.BPlusTree.ofSortedListM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (l : List (α × β)) :
ofSortedListM t visit l = do let p ← leafLevelM visit (chunkSizes (max t 2) l.length l.length) l buildM (max t 2) visit p.2.length p.1 p.2

Bulk loading cuts the bottom level and runs the rounds of buildM, at minimum degree max t 2.

theorem Cslib.DataStructure.BPlusTree.map_ofSortedListM {α β : 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) (t : ℕ) (visit : m Unit) (l : List (α × β)) :
f (ofSortedListM t visit l) = ofSortedListM t (f visit) l

A monad morphism commutes with bulk loading.

def Cslib.DataStructure.BPlusTree.ofSortedList {α β : Type} (t : ℕ) (l : List (α × β)) :
BPlusTree α β

Pure bulk loading.

ofSortedList is the Id instantiation of ofSortedListM.

Pure forms of the steps #

Entries #

@[simp]
theorem Cslib.DataStructure.BPlusTree.toList_ofSortedList {α β : Type} (t : ℕ) (l : List (α × β)) :

Bulk loading keeps the entries in order, for every list and every t.

@[simp]

Bulk loading keeps the number of entries.

Leaf sizes #

Shape #

theorem Cslib.DataStructure.BPlusTree.height_ofSortedList_le {α β : Type} {t : ℕ} (ht : 2 ≤ t) (l : List (α × β)) :

At a minimum degree of at least 2, bulk loading meets the height bound of well-formed trees, for every list: the shape does not depend on the keys.

Separators #

theorem Cslib.DataStructure.BPlusTree.isWF_ofSortedList {α β : Type} {t : ℕ} [LinearOrder α] {l : List (α × β)} (ht : 2 ≤ t) (h : l.IsSortedAssoc) :

Bulk loading a sorted list builds a well-formed tree at a minimum degree of at least 2.

@[simp]
theorem Cslib.DataStructure.BPlusTree.lookup_ofSortedList {α β : Type} {t : ℕ} [LinearOrder α] {l : List (α × β)} (h : l.IsSortedAssoc) (k : α) :

Lookup in a bulk-loaded sorted list is lookup in the list, for every t.