struct SuffixTreeBuilder {
nodes: Vec<STNode>,
links: Vec<Link>,
}Fields§
§nodes: Vec<STNode>§links: Vec<Link>Implementations§
Source§impl SuffixTreeBuilder
impl SuffixTreeBuilder
Sourcefn with_capacity(capacity: usize) -> Self
fn with_capacity(capacity: usize) -> Self
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 205)
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 }Sourcefn push_node(&mut self, depth: usize, start: usize) -> usize
fn push_node(&mut self, depth: usize, start: usize) -> usize
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 206)
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 }Sourcefn attach_child(&mut self, parent: usize, child: usize)
fn attach_child(&mut self, parent: usize, child: usize)
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 225)
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 }Sourcefn detach_last_child(&mut self, parent: usize, child: usize)
fn detach_last_child(&mut self, parent: usize, child: usize)
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 224)
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 }Sourcefn reindex(&mut self, leafs: &mut [usize])
fn reindex(&mut self, leafs: &mut [usize])
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 242)
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 }Sourcefn build_aux(&mut self, children: &[usize]) -> Vec<usize>
fn build_aux(&mut self, children: &[usize]) -> Vec<usize>
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 244)
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 }Sourcefn build_children(&mut self) -> Vec<usize>
fn build_children(&mut self) -> Vec<usize>
Examples found in repository?
crates/competitive/src/string/suffix_tree.rs (line 243)
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 }Auto Trait Implementations§
impl Freeze for SuffixTreeBuilder
impl RefUnwindSafe for SuffixTreeBuilder
impl Send for SuffixTreeBuilder
impl Sync for SuffixTreeBuilder
impl Unpin for SuffixTreeBuilder
impl UnsafeUnpin for SuffixTreeBuilder
impl UnwindSafe for SuffixTreeBuilder
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