Skip to main content

BinaryAxis

Struct BinaryAxis 

Source
pub struct BinaryAxis<const N: usize> { /* private fields */ }
Expand description

An axis of N stored knots located by binary search.

This is the strategy a BilinearSurface selects when its type does not say otherwise, and it is the right answer for almost every axis: it accepts arbitrary spacing, stores nothing beyond the knots themselves, and its search cost grows logarithmically.

§Cost

2*N stored bytes, no index, and exactly ceil(log2(N)) strategy-specific knot comparisons after the endpoint checks — for a 1024-knot axis, ten comparisons where a scan would take up to 1024.

The count is exact rather than a bound, and it depends only on N: the same number of comparisons for an endpoint, for an exact interior knot, and for a point strictly inside a segment. There is no early exit on an exact match, deliberately, because it would trade a uniform and auditable step count for a data-dependent one.

§Examples

use ph_surfaces::{BilinearSurface, BinaryAxis};

static X: [u16; 4] = [0, 10, 90, 500];
static Y: [u16; 2] = [0, 100];
static VALUES: [[i32; 4]; 2] = [[0, 10, 90, 500], [100, 110, 190, 600]];

// Naming the strategy and letting it default are the same surface.
static NAMED: BilinearSurface<4, 2, BinaryAxis<4>, BinaryAxis<2>> =
    BilinearSurface::from_axes(BinaryAxis::new(&X), BinaryAxis::new(&Y), &VALUES);
static DEFAULTED: BilinearSurface<4, 2> = BilinearSurface::new(&X, &Y, &VALUES);

assert_eq!(NAMED.evaluate(50, 50), DEFAULTED.evaluate(50, 50));
assert_eq!(NAMED, DEFAULTED);

An axis of fewer than two knots does not compile:

use ph_surfaces::BinaryAxis;

static X: [u16; 1] = [7];
static AXIS: BinaryAxis<1> = BinaryAxis::new(&X);

Nor does one whose knots are not strictly increasing:

use ph_surfaces::BinaryAxis;

static X: [u16; 3] = [0, 100, 50];
static AXIS: BinaryAxis<3> = BinaryAxis::new(&X);

Implementations§

Source§

impl<const N: usize> BinaryAxis<N>

Source

pub const fn new(knots: &'static [u16; N]) -> Self

Declares a binary-searched axis over static knots.

§Panics

Panics unless the axis declares at least two strictly increasing knots. In a constant or static definition that panic is a compile error, so an invalid axis cannot be declared.

Source

pub const fn knots(&self) -> &'static [u16; N]

Returns the declared knots.

The same array as KnotArray::knots, available in a constant context.

Trait Implementations§

Source§

impl<const N: usize> AxisLookup<N> for BinaryAxis<N>

Source§

const KNOT_BYTES: usize

Bytes of stored knots or descriptor this strategy references. Read more
Source§

const INDEX_BYTES: usize = 0

Bytes of auxiliary index this strategy references. Read more
Source§

const MAX_SEARCH_COMPARISONS: u32

The most strategy-specific knot comparisons needed to locate an in-domain coordinate, after the inclusive endpoint checks. Read more
Source§

fn first(&self) -> u16

Returns the first knot: the inclusive lower bound of the axis domain.
Source§

fn last(&self) -> u16

Returns the last knot: the inclusive upper bound of the axis domain.
Source§

fn knot(&self, index: usize) -> u16

Returns the knot at index. Read more
Source§

fn search(&self, coordinate: u16) -> (usize, u32)

Returns the greatest index whose knot is at or below coordinate, together with the number of knot comparisons it took. Read more
Source§

impl<const N: usize> Clone for BinaryAxis<N>

Source§

fn clone(&self) -> BinaryAxis<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Copy for BinaryAxis<N>

Source§

impl<const N: usize> Debug for BinaryAxis<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Eq for BinaryAxis<N>

Source§

impl<const N: usize> Hash for BinaryAxis<N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> KnotArray<N> for BinaryAxis<N>

Source§

fn knots(&self) -> &'static [u16; N]

Returns the declared knots, strictly increasing and at least two long.
Source§

impl<const N: usize> PartialEq for BinaryAxis<N>

Source§

fn eq(&self, other: &BinaryAxis<N>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<const N: usize> StructuralPartialEq for BinaryAxis<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for BinaryAxis<N>
where &'static [u16; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for BinaryAxis<N>
where &'static [u16; N]: RefUnwindSafe,

§

impl<const N: usize> Send for BinaryAxis<N>
where &'static [u16; N]: Send,

§

impl<const N: usize> Sync for BinaryAxis<N>
where &'static [u16; N]: Sync,

§

impl<const N: usize> Unpin for BinaryAxis<N>
where &'static [u16; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for BinaryAxis<N>
where &'static [u16; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for BinaryAxis<N>
where &'static [u16; N]: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.