Skip to main content

LCSubstring

Struct LCSubstring 

Source
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: u32

the length of the found longest common substring

Implementations§

Source§

impl LCSubstring

Source

pub fn new_substring<T>(left: T, right: T) -> Self
where T: AsRef<str>,

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}
Source

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

Source§

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

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

impl Display for LCSubstring

Source§

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

Print the size of the longest substring

§Examples
§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);

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.