Skip to main content

Crate fastanim_diff

Crate fastanim_diff 

Source
Expand description

A small, dependency-free, generic sequence diff library (see docs/SPEC.md §5).

It knows nothing about animation: it turns two ordered sequences into an edit script of Ops. Items are compared by a caller-supplied key rather than PartialEq, so the caller decides what “the same” means.

The pipeline is:

  1. Keys are interned to dense integers and the common prefix/suffix is trimmed.
  2. A shortest edit script is computed with Myers’ greedy algorithm, its linear-space variant (chosen automatically for large inputs), or patience diff. For inputs of moderate size, ties between equally short scripts are broken by TieBreak::Stable.
  3. Hunks are normalized so deletions come before insertions (determinism).
  4. Optional post-passes: move detection (§5.4), semantic cleanup, and replacement pairing, both adjacent and cross-hunk (§5.5).
use fastanim_diff::{diff, DiffOptions, Op};

let a: Vec<char> = "a+b=c".chars().collect();
let b: Vec<char> = "b+a=c".chars().collect();
let ops = diff(&a, &b, |c| *c, &DiffOptions::default());
assert!(ops.iter().any(|op| matches!(op, Op::Move { .. })));

Structs§

DiffOptions
Options for diff and Differ.
Differ
A configurable diff, for when diff isn’t enough.

Enums§

Algorithm
The core shortest-edit-script algorithm.
Cleanup
Folding of short equal runs into the surrounding edits.
Op
One step of an edit script.
ScriptError
Why an edit script is not a valid transformation of a into b.
TieBreak
How to choose between edit scripts of equal (minimal) cost.

Constants§

LINEAR_SPACE_THRESHOLD
Above this many items (N + M, after trimming), Algorithm::Myers switches to the linear-space variant.
STABLE_LIMIT
TieBreak::Stable applies while N · M (after trimming, if needed) is at most this.

Functions§

apply
Applies a script to a, producing the new sequence. Kept and moved items are taken from a; inserted and replacement items from b. Fails if the script is invalid or pairs items whose keys differ, so on success the result’s keys equal b’s.
diff
Diffs a against b, comparing items by key.
edit_cost
Number of items not kept in place: every a and b item outside an Equal counts once. For a core script (Equal/Delete/Insert only) this is the edit distance D.
expand
Expands a script over groups (e.g. lines) into one over their items (e.g. tokens), for coarse-to-fine diffing (SPEC §5.6). a and b give each group’s contiguous range of items.
validate
Checks the structure of a script for inputs of length n and m: every index of each input is used exactly once, b indices appear in increasing order, and Equal ops keep a order.