Enum memchr::arch::all::twoway::Shift

source ·
enum Shift {
    Small {
        period: usize,
    },
    Large {
        shift: usize,
    },
}
Expand description

A representation of the amount we’re allowed to shift by during Two-Way search.

When computing a critical factorization of the needle, we find the position of the critical factorization by finding the needle’s maximal (or minimal) suffix, along with the period of that suffix. It turns out that the period of that suffix is a lower bound on the period of the needle itself.

This lower bound is equivalent to the actual period of the needle in some cases. To describe that case, we denote the needle as x where x = uv and v is the lexicographic maximal suffix of v. The lower bound given here is always the period of v, which is <= period(x). The case where period(v) == period(x) occurs when len(u) < (len(x) / 2) and where u is a suffix of v[0..period(v)].

This case is important because the search algorithm for when the periods are equivalent is slightly different than the search algorithm for when the periods are not equivalent. In particular, when they aren’t equivalent, we know that the period of the needle is no less than half its length. In this case, we shift by an amount less than or equal to the period of the needle (determined by the maximum length of the components of the critical factorization of x, i.e., max(len(u), len(v)))..

The above two cases are represented by the variants below. Each entails a different instantiation of the Two-Way search algorithm.

N.B. If we could find a way to compute the exact period in all cases, then we could collapse this case analysis and simplify the algorithm. The Two-Way paper suggests this is possible, but more reading is required to grok why the authors didn’t pursue that path.

Variants§

§

Small

Fields

§period: usize
§

Large

Fields

§shift: usize

Implementations§

source§

impl Shift

source

fn forward( needle: &[u8], period_lower_bound: usize, critical_pos: usize, ) -> Shift

Compute the shift for a given needle in the forward direction.

This requires a lower bound on the period and a critical position. These can be computed by extracting both the minimal and maximal lexicographic suffixes, and choosing the right-most starting position. The lower bound on the period is then the period of the chosen suffix.

source

fn reverse( needle: &[u8], period_lower_bound: usize, critical_pos: usize, ) -> Shift

Compute the shift for a given needle in the reverse direction.

This requires a lower bound on the period and a critical position. These can be computed by extracting both the minimal and maximal lexicographic suffixes, and choosing the left-most starting position. The lower bound on the period is then the period of the chosen suffix.

Trait Implementations§

source§

impl Clone for Shift

source§

fn clone(&self) -> Shift

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Shift

source§

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

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

impl Copy for Shift

Auto Trait Implementations§

§

impl Freeze for Shift

§

impl RefUnwindSafe for Shift

§

impl Send for Shift

§

impl Sync for Shift

§

impl Unpin for Shift

§

impl UnwindSafe for Shift

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, dst: *mut T)

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