Documentation

CslibFrontier.DataStructure.BPlusTree.Comparisons.Query

Comparison costs of B+-tree point queries #

The point queries of Cslib.DataStructure.BPlusTree are defined once over a monad, with a comparator and a node-visit action. This file instantiates them in CSLib's query-complexity framework, at FreeM (LEQuery α) with the comparison query LEQuery.ask and the trivial visit := pure (), so that every key comparison is a query and node visits are free: lookupQ, containsQ, minQ, maxQ, predQ, succQ and rankQ. Counting is structural: a program parametric in the oracle cannot compare keys without asking.

Applied to FreeM.isMonadHom_pure_eval, the transfer lemmas map_lookupM and its siblings give eval_lookupQ and its siblings: under BTree.orderOracle, the oracle of the order on the keys, each query evaluates to the pure query. The minimum and maximum take no comparator, so they evaluate to min? and max? under every oracle.

The constants #

A leaf has height 1, so a descent in a tree of height h passes h - 1 interior nodes and ends in one leaf.

The root and every other node of a well-formed tree have at most 2 * t - 1 separators or entries. le_mul_height_add_of_scan turns the per-node counts into (2 * t - 1) * height + 1 for lookup and the membership test and (2 * t - 1) * height for rank, predecessor and successor. The bounds hold under every oracle, since the shape of the tree does not depend on the answers; only the values returned depend on the oracle being that of the order.

The lookup and rank bounds are attained: the test module CslibFrontierTests.BPlusTreeComparisons has a tree of height 2 at t = 2, full along its rightmost path, on which looking up the greatest key asks 7 comparisons and the rank of a key above all others asks 6.

The classic B-tree's bound is 2 * t * height for all five queries (BTree.countQueries_lookupQ_le): there a node holds entries, lookup tests for equality at every node, and the empty leaf is free. Here the equality test happens once, in the leaf, which is a level of its own.

Main definitions #

Main results #

References #

def Cslib.DataStructure.BPlusTree.lookupQ {α β : Type} (k : α) (tr : BPlusTree α β) :

Lookup, issuing one query per key comparison.

lookupQ is lookupM with the comparison query and free visits.

Membership test, issuing one query per key comparison.

containsQ is containsM with the comparison query and free visits.

def Cslib.DataStructure.BPlusTree.minQ {α β : Type} (tr : BPlusTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Least entry in the comparison model, where it asks nothing.

theorem Cslib.DataStructure.BPlusTree.minQ_def {α β : Type} {tr : BPlusTree α β} :
tr.minQ = minM (pure ()) tr

minQ is minM with free visits.

def Cslib.DataStructure.BPlusTree.maxQ {α β : Type} (tr : BPlusTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Greatest entry in the comparison model, where it asks nothing.

theorem Cslib.DataStructure.BPlusTree.maxQ_def {α β : Type} {tr : BPlusTree α β} :
tr.maxQ = maxM (pure ()) tr

maxQ is maxM with free visits.

def Cslib.DataStructure.BPlusTree.predQ {α β : Type} (k : α) (tr : BPlusTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Strict predecessor, issuing one query per key comparison.

theorem Cslib.DataStructure.BPlusTree.predQ_def {α β : Type} {tr : BPlusTree α β} (k : α) :

predQ is predM with the comparison query and free visits.

def Cslib.DataStructure.BPlusTree.succQ {α β : Type} (k : α) (tr : BPlusTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Strict successor, issuing one query per key comparison.

theorem Cslib.DataStructure.BPlusTree.succQ_def {α β : Type} {tr : BPlusTree α β} (k : α) :

succQ is succM with the comparison query and free visits.

def Cslib.DataStructure.BPlusTree.rankQ {α β : Type} (k : α) (tr : BPlusTree α β) :

Rank, issuing one query per key comparison.

theorem Cslib.DataStructure.BPlusTree.rankQ_def {α β : Type} {tr : BPlusTree α β} (k : α) :

rankQ is rankM with the comparison query and free visits.

Transfer: eval at the order's oracle is the pure operation #

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_minQ {α β : Type} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
FreeM.eval (fun {ι : Type} => oracle) tr.minQ = tr.min?

Under any oracle, minQ evaluates to min?.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_maxQ {α β : Type} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
FreeM.eval (fun {ι : Type} => oracle) tr.maxQ = tr.max?

Under any oracle, maxQ evaluates to max?.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_lookupQ {α β : Type} {tr : BPlusTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => BTree.orderOracle) (lookupQ k tr) = lookup k tr

Under the order's oracle, lookupQ evaluates to lookup.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_containsQ {α β : Type} {tr : BPlusTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => BTree.orderOracle) (containsQ k tr) = contains k tr

Under the order's oracle, containsQ evaluates to contains.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_predQ {α β : Type} {tr : BPlusTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => BTree.orderOracle) (predQ k tr) = pred? k tr

Under the order's oracle, predQ evaluates to pred?.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_succQ {α β : Type} {tr : BPlusTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => BTree.orderOracle) (succQ k tr) = succ? k tr

Under the order's oracle, succQ evaluates to succ?.

@[simp]
theorem Cslib.DataStructure.BPlusTree.eval_rankQ {α β : Type} {tr : BPlusTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => BTree.orderOracle) (rankQ k tr) = rank k tr

Under the order's oracle, rankQ evaluates to rank.

Comparison counts #

theorem Cslib.DataStructure.BPlusTree.countQueries_slotM_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (rest : List (Branch α β)) :
FreeM.countQueries (fun {ι : Type} => oracle) (slotM Query.LEQuery.ask k rest) ≤ rest.length

The within-node separator search makes at most one comparison per separator.

@[simp]
theorem Cslib.DataStructure.BPlusTree.countQueries_minQ {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (tr : BPlusTree α β) :
FreeM.countQueries (fun {ι : Type} => oracle) tr.minQ = 0

The minimum search makes no comparison.

@[simp]
theorem Cslib.DataStructure.BPlusTree.countQueries_maxQ {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (tr : BPlusTree α β) :
FreeM.countQueries (fun {ι : Type} => oracle) tr.maxQ = 0

The maximum search makes no comparison.

@[simp]
theorem Cslib.DataStructure.BPlusTree.countQueries_containsQ {α β : Type} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (containsQ k tr) = FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k tr)

The membership test makes as many comparisons as lookup.

Comparison bounds #

theorem Cslib.DataStructure.BPlusTree.countQueries_lookupQ_le {α β : Type} {t : ℕ} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k tr) ≤ (2 * t - 1) * tr.height + 1

Lookup in a well-formed tree makes at most 2 * t - 1 comparisons per level and one more, under every oracle.

theorem Cslib.DataStructure.BPlusTree.countQueries_containsQ_le {α β : Type} {t : ℕ} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (containsQ k tr) ≤ (2 * t - 1) * tr.height + 1

The membership test in a well-formed tree makes at most 2 * t - 1 comparisons per level and one more, under every oracle.

theorem Cslib.DataStructure.BPlusTree.countQueries_predQ_le {α β : Type} {t : ℕ} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (predQ k tr) ≤ (2 * t - 1) * tr.height

Predecessor search in a well-formed tree makes at most 2 * t - 1 comparisons per level, under every oracle.

theorem Cslib.DataStructure.BPlusTree.countQueries_succQ_le {α β : Type} {t : ℕ} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (succQ k tr) ≤ (2 * t - 1) * tr.height

Successor search in a well-formed tree makes at most 2 * t - 1 comparisons per level, under every oracle.

theorem Cslib.DataStructure.BPlusTree.countQueries_rankQ_le {α β : Type} {t : ℕ} {tr : BPlusTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankQ k tr) ≤ (2 * t - 1) * tr.height

Rank in a well-formed tree makes at most 2 * t - 1 comparisons per level, under every oracle.

Upper bounds over all oracles #

A well-formed B+-tree of minimum degree t and a query key: the inputs of the bounded point queries.

  • tree : BPlusTree α β

    The B+-tree being queried.

  • key : α

    The query key.

  • isWF : IsWF t self.tree

    Well-formedness of tree at minimum degree t.

theorem Cslib.DataStructure.BPlusTree.lookupQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => lookupQ p.key p.tree) (fun (p : WFInput α β t) => p.tree.height) fun (h : ℕ) => (2 * t - 1) * h + 1

Lookup makes at most 2 * t - 1 comparisons at each of height levels, and one more.

theorem Cslib.DataStructure.BPlusTree.containsQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => containsQ p.key p.tree) (fun (p : WFInput α β t) => p.tree.height) fun (h : ℕ) => (2 * t - 1) * h + 1

The membership test makes at most 2 * t - 1 comparisons at each of height levels, and one more.

theorem Cslib.DataStructure.BPlusTree.predQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => predQ p.key p.tree) (fun (p : WFInput α β t) => p.tree.height) fun (h : ℕ) => (2 * t - 1) * h

Predecessor search makes at most 2 * t - 1 comparisons at each of height levels.

theorem Cslib.DataStructure.BPlusTree.succQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => succQ p.key p.tree) (fun (p : WFInput α β t) => p.tree.height) fun (h : ℕ) => (2 * t - 1) * h

Successor search makes at most 2 * t - 1 comparisons at each of height levels.

theorem Cslib.DataStructure.BPlusTree.rankQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => rankQ p.key p.tree) (fun (p : WFInput α β t) => p.tree.height) fun (h : ℕ) => (2 * t - 1) * h

Rank makes at most 2 * t - 1 comparisons at each of height levels.