Skip to main content

SuffixAutomaton

Struct SuffixAutomaton 

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

A suffix automaton (DAWG) built online from a byte string.

Construct it with SuffixAutomaton::new; the build is O(n log |Σ|) for the BTreeMap transition tables (O(n |Σ|) with array tables). The automaton stores the length of the source string so that occurrence counts and the distinct-substring total can be reported directly.

§Examples

use oxicuda_seq::string::SuffixAutomaton;

let sam = SuffixAutomaton::new(b"abcbc");
assert!(sam.contains(b"bcb"));
assert!(!sam.contains(b"acb"));
// "abcbc" has 12 distinct non-empty substrings.
assert_eq!(sam.distinct_substring_count(), 12);

Implementations§

Source§

impl SuffixAutomaton

Source

pub fn new(s: &[u8]) -> Self

Build the suffix automaton of s.

An empty s yields an automaton with only the initial state; all queries then behave consistently (no non-empty substring exists, the distinct count is zero).

Source

pub fn contains(&self, pattern: &[u8]) -> bool

Return true iff pattern is a substring of the source string.

The empty pattern is a substring of every string (including the empty string), so this returns true for an empty pattern.

Source

pub fn distinct_substring_count(&self) -> usize

Number of distinct non-empty substrings of the source string.

Equals Σ_v (len[v] − len[link[v]]) over all non-initial states v, because each state recognises exactly len[v] − len[link[v]] distinct substrings (the suffixes of its longest string down to, but excluding, its suffix-link length).

Source

pub fn occurrences(&mut self, pattern: &[u8]) -> usize

Number of (possibly overlapping) occurrences of pattern in the source.

Returns 0 when pattern is not a substring. The empty pattern is reported as occurring source_len + 1 times (once at each gap), matching the usual convention.

This finalises the endpos counters on first use (and caches them), hence the &mut self receiver.

Source

pub fn state_count(&self) -> usize

Number of states in the automaton, including the initial state.

Source

pub fn clone_count(&self) -> usize

Number of clone states produced during construction.

Exposed so tests can confirm that an input which forces a split actually triggered the clone path.

Source

pub fn source_len(&self) -> usize

Length of the source string the automaton was built from.

Trait Implementations§

Source§

impl Clone for SuffixAutomaton

Source§

fn clone(&self) -> SuffixAutomaton

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 SuffixAutomaton

Source§

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

Formats the value using the given formatter. Read more

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.