lcs

Struct LcsTable

Source
pub struct LcsTable<'a, T: 'a> { /* private fields */ }

Implementations§

Source§

impl<'a, T> LcsTable<'a, T>
where T: Eq,

Finding longest common subsequences (“LCS”) between two sequences requires constructing a n x m table (where the two sequences are of lengths n and m). This is expensive to construct and there’s a lot of stuff you can calculate using it, so LcsTable holds onto this data.

Source

pub fn new(a: &'a [T], b: &'a [T]) -> LcsTable<'a, T>

Constructs a LcsTable for matching between two sequences a and b.

Source

pub fn longest_common_subsequence(&self) -> Vec<(&T, &T)>

Gets the longest common subsequence between a and b. Returned elements are in the form (elem_a, elem_b), where elem_a is a reference to an element in a, elem_b is a reference to an element in b, and elem_a == elem_b.

Example:

use lcs::LcsTable;

let a: Vec<_> = "a--b---c".chars().collect();
let b: Vec<_> = "abc".chars().collect();

let table = LcsTable::new(&a, &b);
let lcs = table.longest_common_subsequence();

assert_eq!(vec![(&'a', &'a'), (&'b', &'b'), (&'c', &'c')], lcs);
Source

pub fn longest_common_subsequences(&self) -> HashSet<Vec<(&T, &T)>>
where T: Hash,

Gets all longest common subsequences between a and b. Returned elements are in the form (elem_a, elem_b), where elem_a is a reference to an element in a, elem_b is a reference to an element in b, and elem_a == elem_b.

Example:

use lcs::LcsTable;

let a: Vec<_> = "gac".chars().collect();
let b: Vec<_> = "agcat".chars().collect();

let table = LcsTable::new(&a, &b);
let subsequences = table.longest_common_subsequences();
assert_eq!(3, subsequences.len());
assert!(subsequences.contains(&vec![(&'a', &'a'), (&'c', &'c')]));
assert!(subsequences.contains(&vec![(&'g', &'g'), (&'a', &'a')]));
assert!(subsequences.contains(&vec![(&'g', &'g'), (&'c', &'c')]));
Source

pub fn diff(&self) -> Vec<DiffComponent<&T>>

Computes a diff from a to b.

§Example
use lcs::{DiffComponent, LcsTable};

let a: Vec<_> = "axb".chars().collect();
let b: Vec<_> = "abc".chars().collect();

let table = LcsTable::new(&a, &b);
let diff = table.diff();
assert_eq!(diff, vec![
    DiffComponent::Unchanged(&'a', &'a'),
    DiffComponent::Deletion(&'x'),
    DiffComponent::Unchanged(&'b', &'b'),
    DiffComponent::Insertion(&'c')
]);

Trait Implementations§

Source§

impl<'a, T: Debug + 'a> Debug for LcsTable<'a, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for LcsTable<'a, T>

§

impl<'a, T> RefUnwindSafe for LcsTable<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for LcsTable<'a, T>
where T: Sync,

§

impl<'a, T> Sync for LcsTable<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for LcsTable<'a, T>

§

impl<'a, T> UnwindSafe for LcsTable<'a, T>
where T: RefUnwindSafe,

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, 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.