Skip to main content

SuffixArray

Struct SuffixArray 

Source
pub struct SuffixArray {
    sa: Vec<usize>,
}

Fields§

§sa: Vec<usize>

Implementations§

Source§

impl SuffixArray

Source

pub fn new<T>(text: &[T]) -> Self
where T: Ord,

Examples found in repository?
crates/library_checker/src/string/suffixarray.rs (line 11)
6pub fn suffixarray(reader: impl Read, mut writer: impl Write) {
7    let s = read_all_unchecked(reader);
8    let mut scanner = Scanner::new(&s);
9    scan!(scanner, s: Chars);
10    let n = s.len();
11    let sa = SuffixArray::new(&s);
12    iter_print!(writer, @it (1..=n).map(|i| sa[i]));
13}
More examples
Hide additional examples
crates/competitive/src/string/string_search.rs (line 71)
70    pub fn new(text: Vec<T>) -> Self {
71        let suffix_array = SuffixArray::new(&text);
72
73        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
74        let rmq = RangeMinimumQuery::new(lcp_array.clone());
75
76        Self {
77            text,
78            suffix_array,
79            lcp_array,
80            rank,
81            rmq,
82        }
83    }
crates/competitive/src/string/suffix_tree.rs (line 203)
201    pub fn new(text: Vec<T>) -> Self {
202        let n = text.len();
203        let suffix_array = SuffixArray::new(&text);
204        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
205        let mut builder = SuffixTreeBuilder::with_capacity(n * 2);
206        builder.push_node(0, 0);
207        let mut leafs = vec![!0; n + 1];
208        leafs[0] = 0;
209        let mut stack = Vec::with_capacity(n);
210        stack.push(0);
211        for i in 1..=n {
212            let lcp = lcp_array[i - 1];
213            let mut last_popped = !0;
214            while builder.nodes[stack.last().cloned().unwrap()].depth > lcp {
215                last_popped = stack.pop().unwrap();
216                builder.nodes[last_popped].sa_range.end = i;
217            }
218
219            // internal node
220            if builder.nodes[stack.last().cloned().unwrap()].depth < lcp {
221                let parent = stack.last().cloned().unwrap();
222                let internal = builder.nodes.len();
223                builder.push_node(lcp, builder.nodes[last_popped].sa_range.start);
224                builder.detach_last_child(parent, last_popped);
225                builder.attach_child(parent, internal);
226                builder.attach_child(internal, last_popped);
227                stack.push(internal);
228            }
229
230            // leaf node
231            let parent = stack.last().cloned().unwrap();
232            let leaf = builder.nodes.len();
233            builder.push_node(n - suffix_array[i], i);
234            builder.attach_child(parent, leaf);
235            stack.push(leaf);
236            leafs[i] = leaf;
237        }
238        while let Some(node) = stack.pop() {
239            builder.nodes[node].sa_range.end = n + 1;
240        }
241
242        builder.reindex(&mut leafs[1..]);
243        let children = builder.build_children();
244        let inv = builder.build_aux(&children);
245        let nodes = builder.nodes;
246        let suffix_array = suffix_array.into_inner();
247
248        Self {
249            text,
250            suffix_array,
251            lcp_array,
252            rank,
253            nodes,
254            children,
255            leafs,
256            inv,
257        }
258    }
Source

pub fn into_inner(self) -> Vec<usize>

Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 246)
201    pub fn new(text: Vec<T>) -> Self {
202        let n = text.len();
203        let suffix_array = SuffixArray::new(&text);
204        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
205        let mut builder = SuffixTreeBuilder::with_capacity(n * 2);
206        builder.push_node(0, 0);
207        let mut leafs = vec![!0; n + 1];
208        leafs[0] = 0;
209        let mut stack = Vec::with_capacity(n);
210        stack.push(0);
211        for i in 1..=n {
212            let lcp = lcp_array[i - 1];
213            let mut last_popped = !0;
214            while builder.nodes[stack.last().cloned().unwrap()].depth > lcp {
215                last_popped = stack.pop().unwrap();
216                builder.nodes[last_popped].sa_range.end = i;
217            }
218
219            // internal node
220            if builder.nodes[stack.last().cloned().unwrap()].depth < lcp {
221                let parent = stack.last().cloned().unwrap();
222                let internal = builder.nodes.len();
223                builder.push_node(lcp, builder.nodes[last_popped].sa_range.start);
224                builder.detach_last_child(parent, last_popped);
225                builder.attach_child(parent, internal);
226                builder.attach_child(internal, last_popped);
227                stack.push(internal);
228            }
229
230            // leaf node
231            let parent = stack.last().cloned().unwrap();
232            let leaf = builder.nodes.len();
233            builder.push_node(n - suffix_array[i], i);
234            builder.attach_child(parent, leaf);
235            stack.push(leaf);
236            leafs[i] = leaf;
237        }
238        while let Some(node) = stack.pop() {
239            builder.nodes[node].sa_range.end = n + 1;
240        }
241
242        builder.reindex(&mut leafs[1..]);
243        let children = builder.build_children();
244        let inv = builder.build_aux(&children);
245        let nodes = builder.nodes;
246        let suffix_array = suffix_array.into_inner();
247
248        Self {
249            text,
250            suffix_array,
251            lcp_array,
252            rank,
253            nodes,
254            children,
255            leafs,
256            inv,
257        }
258    }
Source

pub fn lcp_array_with_rank<T>(&self, text: &[T]) -> (Vec<usize>, Vec<usize>)
where T: PartialEq,

Examples found in repository?
crates/competitive/src/string/suffix_array.rs (line 61)
57    pub fn lcp_array<T>(&self, text: &[T]) -> Vec<usize>
58    where
59        T: PartialEq,
60    {
61        self.lcp_array_with_rank(text).0
62    }
More examples
Hide additional examples
crates/competitive/src/string/string_search.rs (line 73)
70    pub fn new(text: Vec<T>) -> Self {
71        let suffix_array = SuffixArray::new(&text);
72
73        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
74        let rmq = RangeMinimumQuery::new(lcp_array.clone());
75
76        Self {
77            text,
78            suffix_array,
79            lcp_array,
80            rank,
81            rmq,
82        }
83    }
crates/competitive/src/string/suffix_tree.rs (line 204)
201    pub fn new(text: Vec<T>) -> Self {
202        let n = text.len();
203        let suffix_array = SuffixArray::new(&text);
204        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
205        let mut builder = SuffixTreeBuilder::with_capacity(n * 2);
206        builder.push_node(0, 0);
207        let mut leafs = vec![!0; n + 1];
208        leafs[0] = 0;
209        let mut stack = Vec::with_capacity(n);
210        stack.push(0);
211        for i in 1..=n {
212            let lcp = lcp_array[i - 1];
213            let mut last_popped = !0;
214            while builder.nodes[stack.last().cloned().unwrap()].depth > lcp {
215                last_popped = stack.pop().unwrap();
216                builder.nodes[last_popped].sa_range.end = i;
217            }
218
219            // internal node
220            if builder.nodes[stack.last().cloned().unwrap()].depth < lcp {
221                let parent = stack.last().cloned().unwrap();
222                let internal = builder.nodes.len();
223                builder.push_node(lcp, builder.nodes[last_popped].sa_range.start);
224                builder.detach_last_child(parent, last_popped);
225                builder.attach_child(parent, internal);
226                builder.attach_child(internal, last_popped);
227                stack.push(internal);
228            }
229
230            // leaf node
231            let parent = stack.last().cloned().unwrap();
232            let leaf = builder.nodes.len();
233            builder.push_node(n - suffix_array[i], i);
234            builder.attach_child(parent, leaf);
235            stack.push(leaf);
236            leafs[i] = leaf;
237        }
238        while let Some(node) = stack.pop() {
239            builder.nodes[node].sa_range.end = n + 1;
240        }
241
242        builder.reindex(&mut leafs[1..]);
243        let children = builder.build_children();
244        let inv = builder.build_aux(&children);
245        let nodes = builder.nodes;
246        let suffix_array = suffix_array.into_inner();
247
248        Self {
249            text,
250            suffix_array,
251            lcp_array,
252            rank,
253            nodes,
254            children,
255            leafs,
256            inv,
257        }
258    }
Source

pub fn lcp_array<T>(&self, text: &[T]) -> Vec<usize>
where T: PartialEq,

Trait Implementations§

Source§

impl Clone for SuffixArray

Source§

fn clone(&self) -> SuffixArray

Returns a duplicate 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 SuffixArray

Source§

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

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

impl Index<usize> for SuffixArray

Source§

type Output = usize

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. 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> ToArrayVecScalar for T

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.