Remove Chunk categorization from MergeIterator

This commit is contained in:
Magnus Hoff 2017-11-17 12:41:09 +01:00
parent 1985677474
commit 34ca3e424d

View file

@ -4,10 +4,7 @@ use diff;
use diff::Result::*; use diff::Result::*;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
enum Chunk<'a, Item: 'a + Debug + PartialEq + Eq>{ struct Chunk<'a, Item: 'a + Debug + PartialEq + Eq>(&'a [diff::Result<Item>], &'a [diff::Result<Item>]);
Stable(&'a [diff::Result<Item>], &'a [diff::Result<Item>]),
Unstable(&'a [diff::Result<Item>], &'a [diff::Result<Item>]),
}
struct MergeIterator<'a, Item> struct MergeIterator<'a, Item>
where where
@ -38,7 +35,7 @@ where
i += 1; i += 1;
} }
if i > 0 { if i > 0 {
let chunk = Chunk::Stable(&self.left[..i], &self.right[..i]); let chunk = Chunk(&self.left[..i], &self.right[..i]);
self.left = &self.left[i..]; self.left = &self.left[i..];
self.right = &self.right[i..]; self.right = &self.right[i..];
return Some(chunk); return Some(chunk);
@ -63,14 +60,14 @@ where
ri += 1; ri += 1;
}, },
(Some(&Both(..)), Some(&Both(..))) => { (Some(&Both(..)), Some(&Both(..))) => {
let chunk = Chunk::Unstable(&self.left[..li], &self.right[..ri]); let chunk = Chunk(&self.left[..li], &self.right[..ri]);
self.left = &self.left[li..]; self.left = &self.left[li..];
self.right = &self.right[ri..]; self.right = &self.right[ri..];
return Some(chunk); return Some(chunk);
} }
_ => { _ => {
if self.left.len() > 0 || self.right.len() > 0 { if self.left.len() > 0 || self.right.len() > 0 {
let chunk = Chunk::Unstable(self.left, self.right); let chunk = Chunk(self.left, self.right);
self.left = &self.left[self.left.len()..]; self.left = &self.left[self.left.len()..];
self.right = &self.right[self.right.len()..]; self.right = &self.right[self.right.len()..];
return Some(chunk); return Some(chunk);
@ -99,11 +96,11 @@ mod test {
let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>(); let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>();
assert_eq!(vec![ assert_eq!(vec![
Chunk::Stable (&oa[0.. 3], &ob[0.. 3]), Chunk(&oa[0.. 3], &ob[0.. 3]),
Chunk::Unstable(&oa[3.. 6], &ob[3.. 3]), Chunk(&oa[3.. 6], &ob[3.. 3]),
Chunk::Stable (&oa[6.. 9], &ob[3.. 6]), Chunk(&oa[6.. 9], &ob[3.. 6]),
Chunk::Unstable(&oa[9.. 9], &ob[6.. 9]), Chunk(&oa[9.. 9], &ob[6.. 9]),
Chunk::Stable (&oa[9..12], &ob[9..12]), Chunk(&oa[9..12], &ob[9..12]),
], merge); ], merge);
} }
@ -118,9 +115,9 @@ mod test {
let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>(); let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>();
assert_eq!(vec![ assert_eq!(vec![
Chunk::Stable (&oa[0.. 3], &ob[0.. 3]), Chunk(&oa[0.. 3], &ob[0.. 3]),
Chunk::Unstable(&oa[3.. 9], &ob[3.. 9]), Chunk(&oa[3.. 9], &ob[3.. 9]),
Chunk::Stable (&oa[9..12], &ob[9..12]), Chunk(&oa[9..12], &ob[9..12]),
], merge); ], merge);
} }
@ -135,8 +132,8 @@ mod test {
let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>(); let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>();
assert_eq!(vec![ assert_eq!(vec![
Chunk::Stable (&oa[0..9], &ob[0.. 9]), Chunk(&oa[0..9], &ob[0.. 9]),
Chunk::Unstable(&oa[9..9], &ob[9..12]), Chunk(&oa[9..9], &ob[9..12]),
], merge); ], merge);
} }
@ -151,8 +148,8 @@ mod test {
let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>(); let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>();
assert_eq!(vec![ assert_eq!(vec![
Chunk::Stable (&oa[0..6], &ob[0.. 6]), Chunk(&oa[0..6], &ob[0.. 6]),
Chunk::Unstable(&oa[6..9], &ob[6..12]), Chunk(&oa[6..9], &ob[6..12]),
], merge); ], merge);
} }
@ -167,7 +164,7 @@ mod test {
let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>(); let merge = MergeIterator::new(&oa, &ob).collect::<Vec<_>>();
assert_eq!(vec![ assert_eq!(vec![
Chunk::Unstable(&oa[0..6], &ob[0..6]), Chunk(&oa[0..6], &ob[0..6]),
], merge); ], merge);
} }
} }