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>
impl<const N: usize> BinaryAxis<N>
Sourcepub const fn new(knots: &'static [u16; N]) -> Self
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.
Sourcepub const fn knots(&self) -> &'static [u16; N]
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>
impl<const N: usize> AxisLookup<N> for BinaryAxis<N>
Source§const KNOT_BYTES: usize
const KNOT_BYTES: usize
Source§const INDEX_BYTES: usize = 0
const INDEX_BYTES: usize = 0
Source§const MAX_SEARCH_COMPARISONS: u32
const MAX_SEARCH_COMPARISONS: u32
Source§impl<const N: usize> Clone for BinaryAxis<N>
impl<const N: usize> Clone for BinaryAxis<N>
Source§fn clone(&self) -> BinaryAxis<N>
fn clone(&self) -> BinaryAxis<N>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more