pub struct LCSubstring {
pub substring_len: u32,
/* private fields */
}Expand description
An objecft that will hold the given two strings and their grid so you can later ask for the longest common substring without having to re-create the 2d grid
§Examples
use rs_algo::compare::LCSubstring;
let lcs = LCSubstring::new_substring("sunny today outside".to_string(), "today is cold".to_string());
println!("length of the lc substring is {}", lcs);
assert_eq!(lcs.get_longest_substring(), Some("today ".to_string()));Fields§
§substring_len: u32the length of the found longest common substring
Implementations§
Source§impl LCSubstring
impl LCSubstring
Sourcepub fn new_substring<T>(left: T, right: T) -> Self
pub fn new_substring<T>(left: T, right: T) -> Self
This will return a new LCSubstring object. The substring grid will be created and stored, the longest substring length will be known at this time.
Examples found in repository?
examples/algo.rs (lines 47-50)
7fn main() {
8 let mut a = vec![
9 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 32, 1, 3,
10 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5,
11 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
12 ];
13 let mut b = vec![
14 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
15 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
16 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
17 ];
18 let mut c = vec![
19 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
20 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
21 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
22 ];
23 let mut d = vec![
24 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
25 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
26 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
27 ];
28
29 // Get a sorted array without changing the original
30 let sorted_bubble = bubble::sort(&a);
31 let sorted_insertion = insertion::sort(&b);
32 let sorted_merge = merge::sort(&c);
33 let sorted_quick = quick::sort(&d);
34
35 // This will sort the vector passed in, changing the original vector order
36 merge::sort_mut(&mut a);
37 quick::sort_mut(&mut b);
38 insertion::sort_mut(&mut c);
39 bubble::sort_mut(&mut d);
40
41 // get a new longest common sequence object
42 let sequence = LCSubsequence::new_subsequence("leighxxxft".to_string(), "right".to_string());
43 assert_eq!(sequence.subsequence_len, 4);
44 assert_eq!(sequence.get_longest_subsequence(), Some("ight".to_string()));
45
46 // get a new longest common substring
47 let substring = LCSubstring::new_substring(
48 "!!!!Hello WorldXXXXX".to_string(),
49 "XXX Hello World@cvcvcvc".to_string(),
50 );
51 assert_eq!(substring.substring_len, 11);
52 assert_eq!(
53 substring.get_longest_substring(),
54 Some("Hello World".to_string())
55 );
56
57 // search our array. Binary search needs the array to already be sorted
58 match binary::search(817, &a) {
59 Some(value) => println!("our array has value {}", value),
60 None => println!("our array dosen't have value 817"),
61 }
62
63 match binary::index_of(817, &a) {
64 Some(index) => println!("index of 817 is {}", index),
65 None => println!("no index of 817, guess it's not in there"),
66 }
67
68 // common math functions
69 let divisor = math::gcd(30, 21);
70 assert_eq!(Ok(3), divisor);
71
72 let factor = math::factors(9124);
73 assert_eq!(Some(vec![2, 4, 2281, 4562]), factor);
74}Sourcepub fn get_longest_substring(&self) -> Option<String>
pub fn get_longest_substring(&self) -> Option<String>
Return the longest substring as an option. If there was no longest common substring found during the new_substring call, this will return None, otherwise the lc substring as a string will be returned.
Examples found in repository?
examples/algo.rs (line 53)
7fn main() {
8 let mut a = vec![
9 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 32, 1, 3,
10 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 32, 1, 3, 99, 10, 7, 2, 11, -5,
11 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
12 ];
13 let mut b = vec![
14 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
15 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
16 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
17 ];
18 let mut c = vec![
19 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
20 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
21 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
22 ];
23 let mut d = vec![
24 117, 1, 3, 99, 10, 7, 7, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 817, 1, 3,
25 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 2, 11, -5, 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5,
26 4, 9, 817, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 87, 1, 3, 99, 10, 7, 2, 11, -5, 4, 9, 8,
27 ];
28
29 // Get a sorted array without changing the original
30 let sorted_bubble = bubble::sort(&a);
31 let sorted_insertion = insertion::sort(&b);
32 let sorted_merge = merge::sort(&c);
33 let sorted_quick = quick::sort(&d);
34
35 // This will sort the vector passed in, changing the original vector order
36 merge::sort_mut(&mut a);
37 quick::sort_mut(&mut b);
38 insertion::sort_mut(&mut c);
39 bubble::sort_mut(&mut d);
40
41 // get a new longest common sequence object
42 let sequence = LCSubsequence::new_subsequence("leighxxxft".to_string(), "right".to_string());
43 assert_eq!(sequence.subsequence_len, 4);
44 assert_eq!(sequence.get_longest_subsequence(), Some("ight".to_string()));
45
46 // get a new longest common substring
47 let substring = LCSubstring::new_substring(
48 "!!!!Hello WorldXXXXX".to_string(),
49 "XXX Hello World@cvcvcvc".to_string(),
50 );
51 assert_eq!(substring.substring_len, 11);
52 assert_eq!(
53 substring.get_longest_substring(),
54 Some("Hello World".to_string())
55 );
56
57 // search our array. Binary search needs the array to already be sorted
58 match binary::search(817, &a) {
59 Some(value) => println!("our array has value {}", value),
60 None => println!("our array dosen't have value 817"),
61 }
62
63 match binary::index_of(817, &a) {
64 Some(index) => println!("index of 817 is {}", index),
65 None => println!("no index of 817, guess it's not in there"),
66 }
67
68 // common math functions
69 let divisor = math::gcd(30, 21);
70 assert_eq!(Ok(3), divisor);
71
72 let factor = math::factors(9124);
73 assert_eq!(Some(vec![2, 4, 2281, 4562]), factor);
74}Trait Implementations§
Source§impl Debug for LCSubstring
impl Debug for LCSubstring
Auto Trait Implementations§
impl Freeze for LCSubstring
impl RefUnwindSafe for LCSubstring
impl Send for LCSubstring
impl Sync for LCSubstring
impl Unpin for LCSubstring
impl UnsafeUnpin for LCSubstring
impl UnwindSafe for LCSubstring
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