Rank queries in classic B-trees #
rankM scans a node's separators and descends into one child, following the search in lookupM.
Each separator passed contributes one plus the size of its preceding child. At the first
separator at least the query, the search descends into the preceding child; a key equal to the
query is not counted. The constructor equations support evaluation and monadic instantiations
without unfolding the well-founded recursion.
Main definitions #
Main results #
Cslib.IsMonadHom.map_rankM: a monad morphism commutes with the query.rank_eq_of_isSortedAssoc,rank_eq: on a tree with sorted entries, and in particular on a well-formed tree,rankisSortedAssoc.rankontoList.
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/Rank.lean.
Rank by a single descent. Scan past separators k' for which le k k' fails, adding
the pure BTree.size of the child before each separator plus one. At the first separator
with le k k', descend into the child before it; otherwise descend into the last child.
When le decides the key order and toList is sorted, the result counts keys strictly below
k, excluding equality. Scanning triples and computing pure subtree sizes are uncharged
client computation in the node-visit and comparison cost models. visit fires once when
the scan chooses a child, including at zero-key nodes, and never at an empty leaf.
Descend before the first separator at least the query; otherwise count the preceding child and separator and continue scanning, without an additional visit.
A monad morphism commutes with the rank search.
No keys are below the query in the empty tree.
A separator at least the query selects the preceding child; a smaller separator contributes its preceding child's size plus one before the scan continues.
Model equation: on a tree with sorted entries, rank counts exactly the entries of
toList whose keys are strictly below the query.
Model equation for rank on well-formed trees.