From 501ca2fd5fe3cb755bc2ae8294a477a90ba05d04 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 17 Jan 2021 23:19:16 +0100 Subject: [PATCH] Rearrange code to be more consistent --- src/algorithms/myers.rs | 8 +-- src/algorithms/patience.rs | 100 ++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/algorithms/myers.rs b/src/algorithms/myers.rs index 4c72b39..7834f85 100644 --- a/src/algorithms/myers.rs +++ b/src/algorithms/myers.rs @@ -11,10 +11,6 @@ use std::ops::{Index, Range}; use crate::algorithms::DiffHook; -fn modulo(a: isize, b: usize) -> usize { - a.rem_euclid(b as isize) as usize -} - /// Myers' diff algorithm. /// /// 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()) } +fn modulo(a: isize, b: usize) -> usize { + a.rem_euclid(b as isize) as usize +} + pub(crate) fn diff_offsets( diff: &mut D, old: &Old, diff --git a/src/algorithms/patience.rs b/src/algorithms/patience.rs index 2cc5fd8..983013b 100644 --- a/src/algorithms/patience.rs +++ b/src/algorithms/patience.rs @@ -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( + d: &mut D, + old: &Old, + old_range: Range, + new: &New, + new_range: Range, +) -> Result<(), D::Error> +where + Old: Index + ?Sized, + New: Index + ?Sized, + Old::Output: Hash + Eq, + New::Output: PartialEq + 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: &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( - d: &mut D, - old: &Old, - old_range: Range, - new: &New, - new_range: Range, -) -> Result<(), D::Error> -where - Old: Index + ?Sized, - New: Index + ?Sized, - Old::Output: Hash + Eq, - New::Output: PartialEq + 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: &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];