Maintain state in slices instead of indices

This commit is contained in:
Magnus Hoff 2017-11-16 16:31:24 +01:00
parent ed5db344fb
commit f6818f5a7f

View file

@ -46,9 +46,6 @@ where
{
left: &'a [diff::Result<Item>],
right: &'a [diff::Result<Item>],
li: usize,
ri: usize,
}
impl<'a, Item> MergeIterator<'a, Item>
@ -56,12 +53,7 @@ where
Item: 'a + Debug + PartialEq + Eq
{
fn new(left: &'a [diff::Result<Item>], right: &'a [diff::Result<Item>]) -> MergeIterator<'a, Item> {
MergeIterator {
left,
right,
li: 0,
ri: 0,
}
MergeIterator { left, right }
}
}
@ -73,18 +65,18 @@ where
fn next(&mut self) -> Option<Self::Item> {
let mut i = 0;
while let (Some(&Both(..)), Some(&Both(..))) = (self.left.get(self.li+i), self.right.get(self.ri+i)) {
while let (Some(&Both(..)), Some(&Both(..))) = (self.left.get(i), self.right.get(i)) {
i += 1;
}
if i > 0 {
let chunk = ChunkItem::stable(Chunk(&self.left[self.li..self.li+i], &self.right[self.ri..self.ri+i]));
self.li += i;
self.ri += i;
let chunk = ChunkItem::stable(Chunk(&self.left[..i], &self.right[..i]));
self.left = &self.left[i..];
self.right = &self.right[i..];
return Some(chunk);
}
let mut li = self.li;
let mut ri = self.ri;
let mut li = 0;
let mut ri = 0;
loop {
match (self.left.get(li), self.right.get(ri)) {
(Some(&Right(_)), _) => {
@ -102,16 +94,16 @@ where
ri += 1;
},
(Some(&Both(..)), Some(&Both(..))) => {
let chunk = ChunkItem::unstable(Chunk(&self.left[self.li..li], &self.right[self.ri..ri]));
self.li = li;
self.ri = ri;
let chunk = ChunkItem::unstable(Chunk(&self.left[..li], &self.right[..ri]));
self.left = &self.left[li..];
self.right = &self.right[ri..];
return Some(chunk);
}
_ => {
if self.li < self.left.len() || self.ri < self.right.len() {
let chunk = ChunkItem::unstable(Chunk(&self.left[self.li..self.left.len()], &self.right[self.ri..self.right.len()]));
self.li = self.left.len();
self.ri = self.right.len();
if self.left.len() > 0 || self.right.len() > 0 {
let chunk = ChunkItem::unstable(Chunk(self.left, self.right));
self.left = &self.left[self.left.len()..];
self.right = &self.right[self.right.len()..];
return Some(chunk);
}
return None;