Module similar::algorithms[][src]

Various diff (longest common subsequence) algorithms.

The implementations of the algorithms in this module are relatively low level and expose the most generic bounds possible for the algorithm. To use them you would typically use the higher level API if possible but direct access to these algorithms can be useful in some cases.

All these algorithms provide a diff function which takes two indexable objects (for instance slices) and a DiffHook. As the diff is generated the diff hook is invoked. Note that the diff hook does not get access to the actual values but only the indexes. This is why the diff hook is not used outside of the raw algorithm implementations as for most situations access to the values is useful of required.

The algorithms module really is the most low-level module in similar and generally not the place to start.

Example

This is a simple example that shows how you can calculate the difference between two sequences and capture the ops into a vector.

use similar::algorithms::{Algorithm, Replace, Capture, diff_slices};

let a = vec![1, 2, 3, 4, 5];
let b = vec![1, 2, 3, 4, 7];
let mut d = Replace::new(Capture::new());
diff_slices(Algorithm::Myers, &mut d, &a, &b).unwrap();
let ops = d.into_inner().into_ops();

The above example is equivalent to using capture_diff_slices.

Re-exports

pub use crate::Algorithm;

Modules

myers

Myers' diff algorithm.

patience

Patience diff algorithm.

Structs

Capture

A DiffHook that captures all diff operations.

Replace

A DiffHook that combines deletions and insertions to give blocks of maximal length, and replacements when appropriate.

Traits

DiffHook

A trait for reacting to an edit script from the "old" version to the "new" version.

Functions

diff

Creates a diff between old and new with the given algorithm.

diff_slices

Shortcut for diffing slices with a specific algorithm.