Skip to main content

CostModel

Struct CostModel 

Source
pub struct CostModel { /* private fields */ }
Expand description

Costs of the four edit operations.

Built for a keyboard Layout with CostModel::for_layout (CostModel::qwerty is the default). The layout only decides which substitutions are cheap; the minimum costs the search bounds use (c_min, c_indel_min, c_transpose_min) do not depend on it.

Implementations§

Source§

impl CostModel

Source

pub fn qwerty() -> Self

The default model with a QWERTY keyboard. Same as CostModel::for_layout(Layout::Qwerty).

Source

pub fn for_layout(layout: Layout) -> Self

A model whose cheap substitutions are the neighbouring keys of layout (Layout::are_neighbours); see Layout for what the geometry leaves out. Every other pair of characters costs an ordinary substitution. Does not panic.

§Examples
use keyhammer::cost::{CostModel, Layout};

let qwerty = CostModel::for_layout(Layout::Qwerty);
let azerty = CostModel::for_layout(Layout::Azerty);
// q and z touch on AZERTY, not on QWERTY.
assert_eq!(azerty.sub_cost(b'q', b'z', 1), 8);
assert_eq!(qwerty.sub_cost(b'q', b'z', 1), 16);
// m is next to n on QWERTY only.
assert_eq!(qwerty.sub_cost(b'm', b'n', 1), 8);
assert_eq!(azerty.sub_cost(b'm', b'n', 1), 16);
// ABNT2 has the QWERTY letters, and ç next to l and p.
let abnt2 = CostModel::for_layout(Layout::Abnt2);
assert_eq!(abnt2.sub_cost('ç', 'l', 1), 8);
assert_eq!(qwerty.sub_cost('ç', 'l', 1), 16);
assert_eq!(abnt2.sub_cost('a', 's', 1), qwerty.sub_cost('a', 's', 1));
Source

pub fn sub_cost<S: Into<u32>>(&self, q: S, t: S, qpos: usize) -> Cost

Cost of typing q where the term has t; qpos is the index of q in the query.

Symbols are Unicode scalar values: a u8 is read as U+0000 to U+00FF, a char or a u32 as itself.

Source

pub fn del_cost<S: PartialEq>(&self, q: &[S], qpos: usize) -> Cost

Cost of deleting the query symbol at qpos (the user typed an extra one).

Source

pub fn ins_cost<S: PartialEq>( &self, t: S, prev_t: Option<S>, qpos: usize, ) -> Cost

Cost of inserting term symbol t (the user skipped it) when qpos query symbols are consumed.

Source

pub fn transpose_cost(&self, qpos: usize) -> Cost

Cost of swapping two adjacent symbols, the first at qpos in the query.

Source

pub fn c_min(&self) -> Cost

Smallest cost of any single edit.

Source

pub fn c_indel_min(&self) -> Cost

Smallest cost of an insertion or deletion.

Source

pub fn c_transpose_min(&self) -> Cost

Smallest cost of a transposition.

Trait Implementations§

Source§

impl Clone for CostModel

Source§

fn clone(&self) -> CostModel

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 Debug for CostModel

Source§

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

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

impl Eq for CostModel

Source§

impl PartialEq for CostModel

Source§

fn eq(&self, other: &CostModel) -> bool

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CostModel

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.