Skip to content

Commit

Permalink
Implement MergeBy::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
kinto-b committed Apr 23, 2024
1 parent ed695af commit e8b0daa
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/merge_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,54 @@ where
}
}

fn fold<B, G>(mut self, init: B, mut f: G) -> B
where
Self: Sized,
G: FnMut(B, Self::Item) -> B,
{
let mut acc = init;
let mut left = self.left.next();
let mut right = self.right.next();

loop {
match (left, right) {
(Some(l), Some(r)) => match self.cmp_fn.merge(l, r) {
(None, Some(r), x) => {
acc = f(acc, x);
left = self.left.next();
right = Some(r);
}
(Some(l), None, x) => {
acc = f(acc, x);
left = Some(l);
right = self.right.next();
}
(None, None, x) => {
acc = f(acc, x);
left = self.left.next();
right = self.right.next();
}
(Some(_), Some(_), _) => unreachable!(),

Check warning on line 286 in src/merge_join.rs

View check run for this annotation

Codecov / codecov/patch

src/merge_join.rs#L286

Added line #L286 was not covered by tests
},
(Some(l), None) => {
self.left.put_back(l);
acc = self.left.fold(acc, |acc, x| f(acc, F::left(x)));
break;
}
(None, Some(r)) => {
self.right.put_back(r);
acc = self.right.fold(acc, |acc, x| f(acc, F::right(x)));
break;
}
(None, None) => {
break;

Check warning on line 299 in src/merge_join.rs

View check run for this annotation

Codecov / codecov/patch

src/merge_join.rs#L299

Added line #L299 was not covered by tests
}
}
}

acc
}

fn size_hint(&self) -> SizeHint {
F::size_hint(self.left.size_hint(), self.right.size_hint())
}
Expand Down

0 comments on commit e8b0daa

Please sign in to comment.