bsearch.bend checks
raw source on the hub · import bend-osdev-algo@0.1.0.0/bsearch.bend as Bsearch
bend-osdev-algo, binary search: the lower bound of a key in a sorted sequence of U32 (the index of the first element not below the key, or the length if every element is below it), and an index holding the key.
Two implementations, as CONVENTIONS asks where speed matters:
bsearch.lower_bound, bsearch.find the reference, on a List: a scan, proved correct bsearch.array_lower_bound, bsearch.array_find the fast one, on an Array<U32>: a binary search, O(log n) reads
LAWS.bend holds the two together for every perfect array whose searched slots are sorted (proved through bend-osdev-array's laws), beside the vectors and the differential test in ref/.
1 import
import Base
Types
type BsearchState source · line 81 · raw
Type
the search state: the array, and the range [lo, hi) still in question
BsearchState@a:Array<U32> -> @lo:U32 -> @hi:U32 -> BsearchState
Definitions
def bsearch.sorted.pair source · line 19 · raw
@xs:List<&2, U32> -> @x:U32 -> Bool
Sorted: each element is at most the next
def bsearch.sorted source · line 26 · raw
@xs:List<&2, U32> -> Bool
def bsearch.below.step source · line 35 · raw
@c:Bool -> @k:Nat -> Nat
def bsearch.lower_bound source · line 44 · raw
@xs:List<&2, U32> -> @+key:U32 -> Nat
the number of leading elements below key: on a sorted list, the index of the first element not below key (the length if there is none)
def bsearch.find.hit source · line 51 · raw
@c:Bool -> @i:Nat -> Maybe<&2, Nat>
def bsearch.find.check source · line 58 · raw
@+i:Nat -> @m:Maybe<&2, U32> -> @key:U32 -> Maybe<&2, Nat>
def bsearch.find source · line 66 · raw
@+xs:List<&2, U32> -> @+key:U32 -> Maybe<&2, Nat>
an index of a sorted list that holds key (the first), or None
def bsearch.array.fin source · line 72 · raw
@r:Pair(Array<U32>, U32) -> Array<U32>
def bsearch.array.val source · line 76 · raw
@r:Pair(Array<U32>, U32) -> U32
def bsearch.array.pick source · line 85 · raw
@below:Bool -> @a:Array<U32> -> @+lo:U32 -> @+hi:U32 -> @+mid:U32 -> BsearchState
one halving of [lo, hi): the element at mid decides which half stays
def bsearch.array.step source · line 92 · raw
@active:Bool -> @r:Pair(Array<U32>, U32) -> @+key:U32 -> @+lo:U32 -> @+hi:U32 -> @+mid:U32 -> BsearchState
def bsearch.array.advance source · line 101 · raw
@st:BsearchState -> @+key:U32 -> BsearchState
def bsearch.array.result source · line 107 · raw
@st:BsearchState -> Pair(Array<U32>, U32)
def bsearch.array.go source · line 114 · raw
@fuel:Nat -> @st:BsearchState -> @+key:U32 -> Pair(Array<U32>, U32)
fuel bounds the halvings: 33 is enough for any [lo, hi) of U32; once the range is empty the remaining steps change nothing
def bsearch.array_lower_bound source · line 122 · raw
@a:Array<U32> -> @n:U32 -> @key:U32 -> Pair(Array<U32>, U32)
the index of the first of the first n elements not below key (n if none)
def bsearch.array.found source · line 125 · raw
@inside:Bool -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @+key:U32 -> Pair(Array<U32>, Maybe<&2, U32>)
def bsearch.array.find source · line 134 · raw
@r:Pair(Array<U32>, U32) -> @+n:U32 -> @+key:U32 -> Pair(Array<U32>, Maybe<&2, U32>)
def bsearch.array_find source · line 139 · raw
@a:Array<U32> -> @+n:U32 -> @+key:U32 -> Pair(Array<U32>, Maybe<&2, U32>)
an index of the first n elements of a sorted array that holds key, or None