Documentation

CslibFrontier.DataStructure.BTree.NodeAccess.Range

External-memory closed-interval queries on classic B-trees #

Closed-interval range queries (rangeR) over the PFunctor.FreeM node-access oracle of Cslib.DataStructure.BTree.NodeAccess. rangeR takes only a descent bound fuel : ℕ, interval endpoints lo hi : α, and an optional root handle root : Option H: empty leaves (none) return [] with 0 page reads, and each entered node (some h) issues one readNode query before rangeScan traverses its separator triples in memory, pruning any child excluded by its adjacent separators.

When Denotes s root tr and tr.height ≤ fuel:

  1. eval_rangeR proves eval s (rangeR fuel lo hi root) = BTree.range lo hi tr.
  2. countQueries_rangeR_eq proves countQueries s (rangeR fuel lo hi root) = rangeVisits lo hi tr, the structural page-read count on tr.
  3. countQueries_rangeR_of_occupied, countQueries_rangeR, and countQueries_rangeR_le_log bound the page reads by 2 * tr.height + (BTree.range lo hi tr).length (and 2 * (Nat.log t ((tr.size + 1) / 2) + 1) + (BTree.range lo hi tr).length on IsWF t tr): a subtree wholly inside [lo, hi] reads at most one page per returned entry because every occupied node holds at least one key when 2 ≤ t, while at each depth at most one node straddles lo and at most one straddles hi.

Main definitions #

Main results #

References #

Adapted from cslib-community/EpsilonEridani at commit 1966ab476 (EpsilonEridani/Algorithms/Lean/BTree/Cost/Range.lean), recast from monadic node-visit annotations to structural PFunctor.FreeM page-read queries.

def Cslib.DataStructure.BTree.NodeAccess.rangeScan {α β H : Type} [LinearOrder α] (rec : Option H → NodeM α β H (List (α × β))) (lo hi : α) :
Option H → List (PageEntry α β H) → NodeM α β H (List (α × β))

Within-page scan for rangeR: traverses an already-read page's entries without issuing extra page reads for the current node, recursing into children permitted by [lo, hi].

def Cslib.DataStructure.BTree.NodeAccess.rangeR {α β H : Type} [LinearOrder α] :
ℕ → α → α → Option H → NodeM α β H (List (α × β))

External-memory closed-interval query [lo, hi] over nodeQuery.

@[irreducible]

Structural page-read count of a pruned closed-interval traversal on BTree α β.

@[simp]
theorem Cslib.DataStructure.BTree.NodeAccess.rangeVisits_node_nil {α β : Type} [LinearOrder α] (lo hi : α) (first : BTree α β) :
rangeVisits lo hi (first.node []) = rangeVisits lo hi first + 1
@[simp]
theorem Cslib.DataStructure.BTree.NodeAccess.rangeVisits_node_cons {α β : Type} [LinearOrder α] (lo hi k : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
rangeVisits lo hi (first.node ((k, v, c) :: rest)) = (if lo ≤ k then rangeVisits lo hi first else 0) + if k ≤ hi then rangeVisits lo hi (c.node rest) else 1

Exact agreement with BTree.range and rangeVisits #

theorem Cslib.DataStructure.BTree.NodeAccess.eval_rangeR {α β H : Type} [LinearOrder α] {s : Store α β H} {fuel : ℕ} {root : Option H} {tr : BTree α β} (hd : Denotes s root tr) (hf : tr.height ≤ fuel) (lo hi : α) :
PFunctor.FreeM.eval s (rangeR fuel lo hi root) = range lo hi tr

rangeR evaluates to BTree.range whenever root denotes tr and tr.height ≤ fuel.

theorem Cslib.DataStructure.BTree.NodeAccess.countQueries_rangeR_eq {α β H : Type} [LinearOrder α] {s : Store α β H} {fuel : ℕ} {root : Option H} {tr : BTree α β} (hd : Denotes s root tr) (hf : tr.height ≤ fuel) (lo hi : α) :
PFunctor.FreeM.countQueries s (rangeR fuel lo hi root) = rangeVisits lo hi tr

rangeR issues rangeVisits lo hi tr page reads when root denotes tr and tr.height ≤ fuel.

Output-sensitive 2 * height + k page-read bounds #

theorem Cslib.DataStructure.BTree.NodeAccess.rangeVisits_le_of_occupied {α β : Type} [LinearOrder α] {t : ℕ} {lo hi : α} {tr : BTree α β} (ht : 2 ≤ t) (hc : ∀ c ∈ tr.children, Occupied t c) (hs : tr.toList.IsSortedAssoc) :
rangeVisits lo hi tr ≤ 2 * tr.height + (range lo hi tr).length

Pure page-visit bound rangeVisits lo hi tr ≤ 2 * tr.height + (BTree.range lo hi tr).length on any sorted tree whose children satisfy Occupied t for 2 ≤ t.

theorem Cslib.DataStructure.BTree.NodeAccess.countQueries_rangeR_of_occupied {α β H : Type} [LinearOrder α] {t : ℕ} {s : Store α β H} {fuel : ℕ} {root : Option H} {tr : BTree α β} (ht : 2 ≤ t) (hc : ∀ c ∈ tr.children, Occupied t c) (hs : tr.toList.IsSortedAssoc) (hd : Denotes s root tr) (hf : tr.height ≤ fuel) (lo hi : α) :
PFunctor.FreeM.countQueries s (rangeR fuel lo hi root) ≤ 2 * tr.height + (range lo hi tr).length

rangeR issues at most 2 * tr.height + (BTree.range lo hi tr).length page reads on any sorted tree with Occupied t children (2 ≤ t) when Denotes s root tr and tr.height ≤ fuel.

theorem Cslib.DataStructure.BTree.NodeAccess.countQueries_rangeR {α β H : Type} [LinearOrder α] {t : ℕ} {s : Store α β H} {fuel : ℕ} {root : Option H} {tr : BTree α β} (hwf : IsWF t tr) (hd : Denotes s root tr) (hf : tr.height ≤ fuel) (lo hi : α) :
PFunctor.FreeM.countQueries s (rangeR fuel lo hi root) ≤ 2 * tr.height + (range lo hi tr).length

rangeR issues at most 2 * tr.height + (BTree.range lo hi tr).length page reads on a well-formed B-tree when Denotes s root tr and tr.height ≤ fuel.

theorem Cslib.DataStructure.BTree.NodeAccess.countQueries_rangeR_le_log {α β H : Type} [LinearOrder α] {t : ℕ} {s : Store α β H} {fuel : ℕ} {root : Option H} {tr : BTree α β} (hwf : IsWF t tr) (hd : Denotes s root tr) (hf : tr.height ≤ fuel) (lo hi : α) :
PFunctor.FreeM.countQueries s (rangeR fuel lo hi root) ≤ 2 * (Nat.log t ((tr.size + 1) / 2) + 1) + (range lo hi tr).length

Logarithmic O(log_t n + k) page-read bound for rangeR on a well-formed B-tree.