Documentation

CslibFrontier.DataStructure.BTree.OfSortedList

Bulk loading of classic B-trees #

Bottom-up bulk loading of the B-trees of [BMcC72]: a tree is built from a list of entries in one pass, level by level from the bottom. The shape invariants are those of [CLRS09], Chapter 18.

A level is a first tree followed by (key, value, tree) triples, all of its trees of one height; the bottom level is the empty leaf and one triple per entry, each with an empty child. A round cuts the triples of a level into groups of t - 1 to 2 * t - 1 triples with one triple between consecutive groups: each group becomes a node one level taller, and each triple between groups moves up, its child becoming the first child of the next node. groupSizes chooses the sizes of all groups but the last, with one look ahead: a group of 2 * t - 1 unless that would leave fewer than t - 1 triples for the rest, then one of t - 1. packM cuts a level by a list of sizes, and buildM runs rounds until at most 2 * t - 1 triples are left, which become the root. Below 2 the loader uses minimum degree 2, so that every node built holds a key.

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

Main definitions #

Main results #

References #

Ported from cslib-community/EpsilonEridani at commit 1966ab476: EpsilonEridani/Algorithms/Lean/BTree/OfSortedList.lean.

The sizes of all groups but the last when the r triples of a level are cut into groups of t - 1 to 2 * t - 1 triples with one triple between groups, with recursion bounded by fuel. A group has 2 * t - 1 triples unless that would leave fewer than t - 1, then t - 1.

@[simp]

Out of fuel, no group is cut.

@[simp]
theorem Cslib.DataStructure.BTree.groupSizes_of_le {t fuel r : ℕ} (h : r ≤ 2 * t - 1) :
groupSizes t fuel r = []

At most 2 * t - 1 triples form the last group, and no group is cut.

theorem Cslib.DataStructure.BTree.groupSizes_succ_of_lt {t fuel r : ℕ} (h : 2 * t - 1 < r) :
groupSizes t (fuel + 1) r = (if r < 3 * t - 1 then t - 1 else 2 * t - 1) :: groupSizes t fuel ((r - if r < 3 * t - 1 then t - 1 else 2 * t - 1) - 1)

More than 2 * t - 1 triples: cut a group of t - 1 when one of 2 * t - 1 would leave fewer than t - 1 triples, and otherwise one of 2 * t - 1.

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

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

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

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

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

A group of s triples becomes a node, and the triple after it moves up over the rest.

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

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

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

A level with no triples is its first tree.

theorem Cslib.DataStructure.BTree.buildM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (fuel : ℕ) (first : BTree α β) (e : Triple α β) (es : List (Triple α β)) :
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) (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 triples.

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

Bulk loading at minimum degree t, or 2 when t is below 2: the rounds of buildM from the bottom level of empty children, with fuel the number of entries.

theorem Cslib.DataStructure.BTree.ofSortedListM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (l : List (α × β)) :
ofSortedListM t visit l = buildM (max t 2) visit l.length leaf (List.map (fun (p : α × β) => (p.1, p.2, leaf)) l)

Bulk loading runs the rounds of buildM at minimum degree max t 2.

@[simp]
theorem Cslib.DataStructure.BTree.ofSortedListM_nil {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) :

Bulk loading the empty list builds the empty tree, without a visit.

theorem Cslib.IsMonadHom.map_packM {α β : 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) (visit : m Unit) (first : DataStructure.BTree α β) (rest : List (DataStructure.BTree.Triple α β)) (ss : List ℕ) :
f (DataStructure.BTree.packM visit first rest ss) = DataStructure.BTree.packM (f visit) first rest ss

A monad morphism commutes with cutting a level.

theorem Cslib.IsMonadHom.map_buildM {α β : 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) (fuel : ℕ) (first : DataStructure.BTree α β) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.buildM t visit fuel first rest) = DataStructure.BTree.buildM t (f visit) fuel first rest

A monad morphism commutes with the rounds of bulk loading.

theorem Cslib.IsMonadHom.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 (α × β)) :

A monad morphism commutes with bulk loading.

def Cslib.DataStructure.BTree.pack {α β : Type} (first : BTree α β) (rest : List (Triple α β)) (ss : List ℕ) :
BTree α β × List (Triple α β)

Pure cutting of a level by a list of group sizes.

def Cslib.DataStructure.BTree.build {α β : Type} (t fuel : ℕ) (first : BTree α β) (rest : List (Triple α β)) :
BTree α β

Pure rounds of bulk loading.

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

Pure bulk loading.

theorem Cslib.DataStructure.BTree.pack_def {α β : Type} (first : BTree α β) (rest : List (Triple α β)) (ss : List ℕ) :
first.pack rest ss = (packM (pure ()) first rest ss).run

pack is the Id instantiation of packM.

theorem Cslib.DataStructure.BTree.build_def {α β : Type} {t : ℕ} (fuel : ℕ) (first : BTree α β) (rest : List (Triple α β)) :
build t fuel first rest = (buildM t (pure ()) fuel first rest).run

build is the Id instantiation of buildM.

theorem Cslib.DataStructure.BTree.ofSortedList_def {α β : Type} {t : ℕ} (l : List (α × β)) :

ofSortedList is the Id instantiation of ofSortedListM.

@[simp]
theorem Cslib.DataStructure.BTree.pack_nil {α β : Type} (first : BTree α β) (rest : List (Triple α β)) :
first.pack rest [] = (first.node rest, [])

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

theorem Cslib.DataStructure.BTree.pack_cons_of_drop_eq_nil {α β : Type} {first : BTree α β} {rest : List (Triple α β)} {s : ℕ} (ss : List ℕ) (h : List.drop s rest = []) :
first.pack rest (s :: ss) = (first.node rest, [])

Cutting at a size beyond the level leaves the whole level as one node.

theorem Cslib.DataStructure.BTree.pack_cons_of_drop_eq_cons {α β : Type} {first : BTree α β} {rest : List (Triple α β)} {s : ℕ} {k : α} {v : β} {c : BTree α β} {rest' : List (Triple α β)} (ss : List ℕ) (h : List.drop s rest = (k, v, c) :: rest') :
first.pack rest (s :: ss) = (first.node (List.take s rest), (k, v, (c.pack rest' ss).1) :: (c.pack rest' ss).2)

A group of s triples becomes a node, and the triple after it moves up over the rest.

@[simp]
theorem Cslib.DataStructure.BTree.build_nil {α β : Type} {t : ℕ} (fuel : ℕ) (first : BTree α β) :
build t fuel first [] = first

A level with no triples is its first tree.

theorem Cslib.DataStructure.BTree.build_of_length_le {α β : Type} {t fuel : ℕ} {first : BTree α β} {rest : List (Triple α β)} (hne : rest ≠ []) (h : rest.length ≤ 2 * t - 1) :
build t fuel first rest = first.node rest

A level of at most 2 * t - 1 triples becomes the root.

theorem Cslib.DataStructure.BTree.build_zero_of_lt {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} (h : 2 * t - 1 < rest.length) :
build t 0 first rest = first.node rest

Out of fuel, a level of more than 2 * t - 1 triples becomes one node.

theorem Cslib.DataStructure.BTree.build_succ_of_lt {α β : Type} {t fuel : ℕ} {first : BTree α β} {rest : List (Triple α β)} (h : 2 * t - 1 < rest.length) :
build t (fuel + 1) first rest = build t fuel (first.pack rest (groupSizes t rest.length rest.length)).1 (first.pack rest (groupSizes t rest.length rest.length)).2

A level of more than 2 * t - 1 triples is cut by groupSizes into the next level.

theorem Cslib.DataStructure.BTree.ofSortedList_eq_build {α β : Type} {t : ℕ} (l : List (α × β)) :
ofSortedList t l = build (max t 2) l.length leaf (List.map (fun (p : α × β) => (p.1, p.2, leaf)) l)

Bulk loading runs the rounds of build at minimum degree max t 2.

@[simp]

Bulk loading the empty list builds the empty tree.

Entries #

theorem Cslib.DataStructure.BTree.toList_pack {α β : Type} (first : BTree α β) (rest : List (Triple α β)) (ss : List ℕ) :
(first.pack rest ss).1.toList ++ toListRest (first.pack rest ss).2 = first.toList ++ toListRest rest

Cutting a level keeps its entries in order.

theorem Cslib.DataStructure.BTree.toList_build {α β : Type} {t : ℕ} (fuel : ℕ) (first : BTree α β) (rest : List (Triple α β)) :
(build t fuel first rest).toList = first.toList ++ toListRest rest

The rounds of bulk loading keep the entries of a level in order.

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

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

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

Bulk loading keeps the number of entries.

theorem Cslib.DataStructure.BTree.mem_ofSortedList {α β : Type} {t : ℕ} {l : List (α × β)} {p : α × β} :

An entry belongs to a bulk-loaded tree exactly when it belongs to the list. Not @[simp]: simp proves it from mem_def and toList_ofSortedList.

Group sizes #

IsGrouping t r ss: the r triples of a level are cut into groups of t - 1 to 2 * t - 1 triples with one triple between consecutive groups, the groups but the last having the sizes ss.

theorem Cslib.DataStructure.BTree.isGrouping_groupSizes {t fuel r : ℕ} (hr : t - 1 ≤ r) (hf : r ≤ fuel) :
IsGrouping t r (groupSizes t fuel r)

groupSizes cuts r triples, at least t - 1 of them, into a grouping, given fuel at least r.

theorem Cslib.DataStructure.BTree.IsGrouping.two_mul_length_lt {t r : ℕ} {ss : List ℕ} (ht : 2 ≤ t) (hg : IsGrouping t r ss) :
2 * ss.length < r

At minimum degree at least 2, a grouping has fewer than half as many sizes as triples: every group but the last takes at least one triple and the one after it, and the last at least one.

Shape #

theorem Cslib.DataStructure.BTree.length_pack {α β : Type} {t r : ℕ} {ss : List ℕ} {first : BTree α β} {rest : List (Triple α β)} (hg : IsGrouping t r ss) (hr : rest.length = r) :
(first.pack rest ss).2.length = ss.length

Cutting a level by a grouping gives one triple per size.

theorem Cslib.DataStructure.BTree.height_ofSortedList_le {α β : Type} {t : ℕ} (ht : 2 ≤ t) (l : List (α × β)) :
(ofSortedList t l).height ≤ Nat.log t ((l.length + 1) / 2) + 1

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.

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

At a minimum degree of at least 2, the root of a bulk-loaded tree holds at most 2 * t - 1 keys, for every list.

theorem Cslib.DataStructure.BTree.occupied_of_mem_children_ofSortedList {α β : Type} {t : ℕ} (ht : 2 ≤ t) (l : List (α × β)) {c : BTree α β} (hc : c ∈ (ofSortedList t l).children) :

At a minimum degree of at least 2, the children of the root of a bulk-loaded tree are occupied, for every list.

theorem Cslib.DataStructure.BTree.isWF_ofSortedList {α β : Type} [LinearOrder α] {t : ℕ} {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.

theorem Cslib.DataStructure.BTree.exists_isWF_toList_map_snd_eq {α β : Type} [LinearOrder α] [Infinite α] {t : ℕ} (ht : 2 ≤ t) (vs : List β) :
∃ (tree : BTree α β), IsWF t tree ∧ tree.size = vs.length ∧ (List.map Prod.fst tree.toList).Nodup ∧ List.map Prod.snd tree.toList = vs

Every list of values vs is the value list of a well-formed B-tree of minimum degree t ≥ 2 with vs.length entries and distinct keys, over an infinite linearly ordered key type: bulk load vs under any vs.length keys in increasing order.

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

Lookup in a bulk-loaded sorted list is lookup in the list.