Skip to main content

SuffixArray

Struct SuffixArray 

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

A suffix array together with its source length and (lazily-built) LCP array.

Construct with SuffixArray::new. The stored sa is a permutation of 0..n (n = source length); SuffixArray::lcp returns the Kasai LCP array, and SuffixArray::search performs SA binary search.

§Examples

use oxicuda_seq::string::SuffixArray;

let sa = SuffixArray::new(b"banana").expect("non-empty");
// Suffixes of "banana" sorted: a, ana, anana, banana, na, nana.
assert_eq!(sa.sa(), &[5, 3, 1, 0, 4, 2]);
assert_eq!(sa.search(b"ana"), vec![1, 3]);

Implementations§

Source§

impl SuffixArray

Source

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

Build the suffix array of s using SA-IS in linear time.

§Errors

Returns SeqError::EmptyInput for an empty s: the suffix array of the empty string is empty and is rejected to keep the contract explicit and consistent with the sibling string modules.

Source

pub fn sa(&self) -> &[usize]

Borrow the suffix array (a permutation of 0..n).

Source

pub fn text(&self) -> &[u8]

Borrow the source text.

Source

pub fn rank(&self) -> Vec<usize>

The rank (inverse suffix) array: rank[sa[i]] == i.

Source

pub fn lcp(&self) -> Vec<usize>

Compute the Kasai LCP array in O(n).

The returned vector has length n; lcp[0] = 0 and, for 1 ≤ i < n, lcp[i] is the length of the longest common prefix of the suffixes text[sa[i-1]..] and text[sa[i]..].

Source

pub fn distinct_substring_count(&self) -> usize

Number of distinct non-empty substrings of the source, n(n+1)/2 − Σ lcp.

Every substring is a prefix of exactly one suffix; summing suffix lengths counts all substrings with multiplicity, and the LCP between adjacent suffixes is precisely the number of duplicate prefixes to subtract.

Source

pub fn search(&self, pattern: &[u8]) -> Vec<usize>

Find all occurrences of pattern via two binary searches on the suffix array, returning the matching text positions in ascending order.

Returns an empty vector for an empty pattern or when there is no match. Overlapping occurrences are all reported.

Trait Implementations§

Source§

impl Clone for SuffixArray

Source§

fn clone(&self) -> SuffixArray

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 SuffixArray

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.