Reorder fields in patience diff

This commit is contained in:
Armin Ronacher 2021-01-17 22:41:45 +01:00
parent 5ba816486e
commit e1c6cb8a42

View file

@ -66,15 +66,15 @@ where
} }
struct Patience<'old, 'new, 'd, Old: ?Sized, New: ?Sized, D> { struct Patience<'old, 'new, 'd, Old: ?Sized, New: ?Sized, D> {
current_old: usize, d: &'d mut D,
current_new: usize,
end_old: usize,
end_new: usize,
old: &'old Old, old: &'old Old,
new: &'new New, new: &'new New,
d: &'d mut D, current_old: usize,
unique_old: &'old [Indexable<'old, Old>], end_old: usize,
unique_new: &'new [Indexable<'new, New>], current_new: usize,
end_new: usize,
indexes_old: &'old [Indexable<'old, Old>],
indexes_new: &'new [Indexable<'new, New>],
} }
impl<'old, 'new, 'd, Old, New, D> DiffHook for Patience<'old, 'new, 'd, Old, New, D> impl<'old, 'new, 'd, Old, New, D> DiffHook for Patience<'old, 'new, 'd, Old, New, D>
@ -89,8 +89,8 @@ where
for (old, new) in (old..old + len).zip(new..new + len) { for (old, new) in (old..old + len).zip(new..new + len) {
let a0 = self.current_old; let a0 = self.current_old;
let b0 = self.current_new; let b0 = self.current_new;
while self.current_old < self.unique_old[old].index while self.current_old < self.indexes_old[old].index
&& self.current_new < self.unique_new[new].index && self.current_new < self.indexes_new[new].index
&& self.new[self.current_new] == self.old[self.current_old] && self.new[self.current_new] == self.old[self.current_old]
{ {
self.current_old += 1; self.current_old += 1;
@ -103,13 +103,13 @@ where
self.d, self.d,
self.old, self.old,
self.current_old, self.current_old,
self.unique_old[old].index, self.indexes_old[old].index,
self.new, self.new,
self.current_new, self.current_new,
self.unique_new[new].index, self.indexes_new[new].index,
)?; )?;
self.current_old = self.unique_old[old].index; self.current_old = self.indexes_old[old].index;
self.current_new = self.unique_new[new].index; self.current_new = self.indexes_new[new].index;
} }
Ok(()) Ok(())
} }
@ -142,21 +142,27 @@ where
New::Output: PartialEq<Old::Output> + Hash + Eq, New::Output: PartialEq<Old::Output> + Hash + Eq,
D: DiffHook, D: DiffHook,
{ {
let au = unique(old, old_range.start, old_range.end); let indexes_old = unique(old, old_range.start, old_range.end);
let bu = unique(new, old_range.start, old_range.end); let indexes_new = unique(new, old_range.start, old_range.end);
let mut d = Replace::new(Patience { let mut d = Replace::new(Patience {
current_old: old_range.start,
current_new: new_range.start,
old,
end_old: old_range.end,
new,
end_new: new_range.end,
d, d,
unique_old: &au, old,
unique_new: &bu, new,
current_old: old_range.start,
end_old: old_range.end,
current_new: new_range.start,
end_new: new_range.end,
indexes_old: &indexes_old,
indexes_new: &indexes_new,
}); });
myers::diff(&mut d, &au, 0..au.len(), &bu, 0..bu.len())?; myers::diff(
&mut d,
&indexes_old,
0..indexes_old.len(),
&indexes_new,
0..indexes_new.len(),
)?;
Ok(()) Ok(())
} }