mirror of
https://github.com/martinvonz/jj.git
synced 2024-12-24 12:48:55 +00:00
merge: rewrite iter()
using itertools::interleave()
`itertools::interleave()` does exactly what we want for `Merge::iter()`. I had just not thought to look for it before. Hopefully it's not noticeably slow.
This commit is contained in:
parent
f877610792
commit
d0be24ac62
1 changed files with 2 additions and 32 deletions
|
@ -218,8 +218,8 @@ impl<T> Merge<T> {
|
|||
/// Returns an iterator over the terms. The items will alternate between
|
||||
/// positive and negative terms, starting with positive (since there's one
|
||||
/// more of those).
|
||||
pub fn iter(&self) -> Iter<'_, T> {
|
||||
Iter::new(self)
|
||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
itertools::interleave(&self.adds, &self.removes)
|
||||
}
|
||||
|
||||
/// Creates a new merge by applying `f` to each remove and add.
|
||||
|
@ -246,36 +246,6 @@ impl<T> Merge<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Iterator over the terms in a merge. See `Merge::iter()`.
|
||||
pub struct Iter<'a, T> {
|
||||
merge: &'a Merge<T>,
|
||||
i: usize,
|
||||
}
|
||||
|
||||
impl<'a, T> Iter<'a, T> {
|
||||
fn new(merge: &'a Merge<T>) -> Self {
|
||||
Self { merge, i: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for Iter<'a, T> {
|
||||
type Item = &'a T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.i > 2 * self.merge.removes.len() {
|
||||
None
|
||||
} else {
|
||||
let item = if self.i % 2 == 0 {
|
||||
&self.merge.adds[self.i / 2]
|
||||
} else {
|
||||
&self.merge.removes[self.i / 2]
|
||||
};
|
||||
self.i += 1;
|
||||
Some(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper for consuming items from an iterator and then creating a `Merge`.
|
||||
///
|
||||
/// By not collecting directly into `Merge`, we can avoid creating invalid
|
||||
|
|
Loading…
Reference in a new issue