Rearrange code to be more consistent
This commit is contained in:
parent
71e0498c49
commit
501ca2fd5f
2 changed files with 54 additions and 54 deletions
|
|
@ -11,10 +11,6 @@ use std::ops::{Index, Range};
|
||||||
|
|
||||||
use crate::algorithms::DiffHook;
|
use crate::algorithms::DiffHook;
|
||||||
|
|
||||||
fn modulo(a: isize, b: usize) -> usize {
|
|
||||||
a.rem_euclid(b as isize) as usize
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Myers' diff algorithm.
|
/// Myers' diff algorithm.
|
||||||
///
|
///
|
||||||
/// Diff `old`, between indices `old_range` and `new` between indices `new_range`.
|
/// Diff `old`, between indices `old_range` and `new` between indices `new_range`.
|
||||||
|
|
@ -52,6 +48,10 @@ where
|
||||||
diff(d, old, 0..old.len(), new, 0..new.len())
|
diff(d, old, 0..old.len(), new, 0..new.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn modulo(a: isize, b: usize) -> usize {
|
||||||
|
a.rem_euclid(b as isize) as usize
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn diff_offsets<D, Old, New>(
|
pub(crate) fn diff_offsets<D, Old, New>(
|
||||||
diff: &mut D,
|
diff: &mut D,
|
||||||
old: &Old,
|
old: &Old,
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,56 @@ use std::ops::{Index, Range};
|
||||||
|
|
||||||
use crate::algorithms::{myers, DiffHook, Replace};
|
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> {
|
struct Indexable<'a, T: ?Sized> {
|
||||||
value: &'a T,
|
value: &'a T,
|
||||||
index: usize,
|
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]
|
#[test]
|
||||||
fn test_patience() {
|
fn test_patience() {
|
||||||
let a: &[usize] = &[11, 1, 2, 2, 3, 4, 4, 4, 5, 47, 19];
|
let a: &[usize] = &[11, 1, 2, 2, 3, 4, 4, 4, 5, 47, 19];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue