pub struct SuffixArray {
sa: Vec<usize>,
}Fields§
§sa: Vec<usize>Implementations§
Source§impl SuffixArray
impl SuffixArray
Sourcepub fn new<T>(text: &[T]) -> Selfwhere
T: Ord,
pub fn new<T>(text: &[T]) -> Selfwhere
T: Ord,
Examples found in repository?
More 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 }Sourcepub fn into_inner(self) -> Vec<usize>
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 }Sourcepub fn lcp_array_with_rank<T>(&self, text: &[T]) -> (Vec<usize>, Vec<usize>)where
T: PartialEq,
pub fn lcp_array_with_rank<T>(&self, text: &[T]) -> (Vec<usize>, Vec<usize>)where
T: PartialEq,
Examples found in repository?
More 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 }pub fn lcp_array<T>(&self, text: &[T]) -> Vec<usize>where
T: PartialEq,
Trait Implementations§
Source§impl Clone for SuffixArray
impl Clone for SuffixArray
Source§fn clone(&self) -> SuffixArray
fn clone(&self) -> SuffixArray
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SuffixArray
impl Debug for SuffixArray
Auto Trait Implementations§
impl Freeze for SuffixArray
impl RefUnwindSafe for SuffixArray
impl Send for SuffixArray
impl Sync for SuffixArray
impl Unpin for SuffixArray
impl UnsafeUnpin for SuffixArray
impl UnwindSafe for SuffixArray
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more