Skip to main content

SortedVecCursor

Struct SortedVecCursor 

Source
pub struct SortedVecCursor<'a, K>
where K: DenseId,
{ /* private fields */ }
Expand description

Cursor into a bucket slice: the verified galloping cursor from containers-verus, re-exported so this module’s public surface is unchanged.

The seek is proven, not just tested: it lands on the first key >= target and skips no present key, for every slice and every target, with every slice index in bounds and no arithmetic overflow. The proof is stated against the same seek_target_idx spec function the verified B+tree cursor uses, which is what makes the two substitutable at the SortedCursor boundary.

See containers-verus/doc/design/12-sorted-vec-cursor.md. The algorithm and the #[inline] attributes match what this module previously defined inline; the erased build compiles the same doubling ladder and bounded bisection. Cursor over a sorted, duplicate-free slice of ids.

pos is both the executable position and the model index — unlike BPlusCursor, whose (node, pos) pair needs a ghost gidx to name its place in the model, an array-backed cursor’s position is its model index. That is the whole reason this cursor’s proofs are short: there is no arena-to-model refinement to carry, only the seek arithmetic.

pos == data.len() marks “exhausted”. Production parity: egraph/src/index.rs’s SortedVecCursor { data, pos }, field for field.

Implementations§

Source§

impl<'a, K> SortedVecCursor<'a, K>
where K: DenseId,

Source

pub fn new(data: &'a [K]) -> SortedVecCursor<'a, K>

Position at the start of data. Requires-free: any slice is accepted, and the cursor’s method contracts state their characterizations under the hypothesis that the data’s model is strictly sorted (cursor_wf). A caller that proves sortedness gets the full contracts; every caller gets the in-bounds guarantees.

Source

pub fn pos(&self) -> usize

The cursor’s position, as a usize — the executable read of Self::idx, which is a spec function and so invisible to a plain build.

Exists so a consumer (and the erased-build test harness) can observe the position without reaching into the private field. Nothing in the proofs needs it; the ensures is what makes it usable as an oracle.

Source

pub fn is_valid(&self) -> bool

Is the cursor positioned on a key (rather than exhausted)?

Source

pub fn key(&self) -> K

The key at the cursor. Requires-free: exhaustion is refused at runtime, and the ensures needs no sortedness — it only ties the returned key to the model at the current position.

Source

pub fn seek(&mut self, target: K)

Advance to the first key >= target, or exhaust. Forward-only: a cursor already at or past the target does not move.

Galloping doubles an offset from the cursor until it lands on or past the target, then bisects the window that offset just proved brackets the answer. O(log d) in the distance advanced rather than O(log rem) in the remainder.

The postcondition is the whole soundness story in one line, and every theorem below is a corollary of it: the new position is seek_target_idx(model, target) — the count of model keys strictly below the target, the same spec function BPlusCursor::seek lands on — except where the cursor was already beyond it, in which case it stays.

#[inline] because this is the join layer’s hot path – called once per leapfrog step through a generic cursor, where inlining also permits devirtualization. Its machine effect is covered by the Criterion cursor and saturation benchmarks.

The bisection below is written as an explicit loop rather than data[lo+1..hi].partition_point(..) (which is what egraph called before it adopted this cursor) because there is no way to state a loop invariant through std’s partition_point. The explicit loop also gives the compiler the code shape used by the maintained implementation; changing it requires a Criterion comparison as well as the contract tests. Requires-free: the sortedness hypothesis lives in the ensures as an implication. On any input the gallop and the bisection stay in bounds and terminate; the position never decreases and never leaves [0, len] once inside it. Under cursor_wf the full forward-only split-point characterization holds.

Trait Implementations§

Source§

impl<'a, K> SortedCursor for SortedVecCursor<'a, K>
where K: DenseId + Copy + Ord,

Source§

fn key(&self) -> Option<K>

The verified key refuses at runtime on an exhausted cursor rather than returning Option, because production’s panics there (data[pos] past the end). The is_valid guard here is what keeps that refusal unreachable, and it is the only thing these three bodies add over the BPlusCursor ones.

Source§

type Key = K

Source§

fn step(&mut self)

Advance one key. No-op on an exhausted cursor.
Source§

fn seek(&mut self, target: K)

Advance to the first key ≥ target. Exhausts the cursor if no such key exists.

Auto Trait Implementations§

§

impl<'a, K> Freeze for SortedVecCursor<'a, K>
where &'a [K]: Freeze,

§

impl<'a, K> RefUnwindSafe for SortedVecCursor<'a, K>
where &'a [K]: RefUnwindSafe,

§

impl<'a, K> Send for SortedVecCursor<'a, K>
where &'a [K]: Send,

§

impl<'a, K> Sync for SortedVecCursor<'a, K>
where &'a [K]: Sync,

§

impl<'a, K> Unpin for SortedVecCursor<'a, K>
where &'a [K]: Unpin,

§

impl<'a, K> UnsafeUnpin for SortedVecCursor<'a, K>
where &'a [K]: UnsafeUnpin,

§

impl<'a, K> UnwindSafe for SortedVecCursor<'a, K>
where &'a [K]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.