Rearrange code to be more consistent

This commit is contained in:
Armin Ronacher 2021-01-17 23:19:16 +01:00
parent 71e0498c49
commit 501ca2fd5f
2 changed files with 54 additions and 54 deletions

View file

@ -12,6 +12,56 @@ use std::ops::{Index, Range};
use crate::algorithms::{myers, DiffHook, Replace};
/// Patience diff algorithm.
///
/// Diff `old`, between indices `old_range` and `new` between indices `new_range`.
pub fn diff<Old, New, D>(
d: &mut D,
old: &Old,
old_range: Range<usize>,
new: &New,
new_range: Range<usize>,
) -> Result<(), D::Error>
where
Old: Index<usize> + ?Sized,
New: Index<usize> + ?Sized,
Old::Output: Hash + Eq,
New::Output: PartialEq<Old::Output> + Hash + Eq,
D: DiffHook,
{
let old_indexes = unique(old, old_range.start, old_range.end);
let new_indexes = unique(new, old_range.start, old_range.end);
let mut d = Replace::new(Patience {
d,
old,
old_current: old_range.start,
old_end: old_range.end,
old_indexes: &old_indexes,
new,
new_current: new_range.start,
new_end: new_range.end,
new_indexes: &new_indexes,
});
myers::diff(
&mut d,
&old_indexes,
0..old_indexes.len(),
&new_indexes,
0..new_indexes.len(),
)?;
Ok(())
}
/// Shortcut for diffing slices.
pub fn diff_slices<D, T>(d: &mut D, old: &[T], new: &[T]) -> Result<(), D::Error>
where
D: DiffHook,
T: Eq + Hash,
{
diff(d, old, 0..old.len(), new, 0..new.len())
}
struct Indexable<'a, T: ?Sized> {
value: &'a T,
index: usize,
@ -125,56 +175,6 @@ where
}
}
/// Patience diff algorithm.
///
/// Diff `old`, between indices `old_range` and `new` between indices `new_range`.
pub fn diff<Old, New, D>(
d: &mut D,
old: &Old,
old_range: Range<usize>,
new: &New,
new_range: Range<usize>,
) -> Result<(), D::Error>
where
Old: Index<usize> + ?Sized,
New: Index<usize> + ?Sized,
Old::Output: Hash + Eq,
New::Output: PartialEq<Old::Output> + Hash + Eq,
D: DiffHook,
{
let old_indexes = unique(old, old_range.start, old_range.end);
let new_indexes = unique(new, old_range.start, old_range.end);
let mut d = Replace::new(Patience {
d,
old,
old_current: old_range.start,
old_end: old_range.end,
old_indexes: &old_indexes,
new,
new_current: new_range.start,
new_end: new_range.end,
new_indexes: &new_indexes,
});
myers::diff(
&mut d,
&old_indexes,
0..old_indexes.len(),
&new_indexes,
0..new_indexes.len(),
)?;
Ok(())
}
/// Shortcut for diffing slices.
pub fn diff_slices<D, T>(d: &mut D, old: &[T], new: &[T]) -> Result<(), D::Error>
where
D: DiffHook,
T: Eq + Hash,
{
diff(d, old, 0..old.len(), new, 0..new.len())
}
#[test]
fn test_patience() {
let a: &[usize] = &[11, 1, 2, 2, 3, 4, 4, 4, 5, 47, 19];