Skip to content

Commit 8dd9a05

Browse files
committed
Implement missing specializations on the MergeJoinBy Iterator
1 parent 09403b3 commit 8dd9a05

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/merge_join.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,70 @@ impl<I, J, F> Iterator for MergeJoinBy<I, J, F>
8484

8585
(lower, upper)
8686
}
87+
88+
fn count(mut self) -> usize {
89+
let mut count = 0;
90+
loop {
91+
match (self.left.next(), self.right.next()) {
92+
(None, None) => break count,
93+
(Some(_left), None) => break count + 1 + self.left.count(),
94+
(None, Some(_right)) => break count + 1 + self.right.count(),
95+
(Some(left), Some(right)) => {
96+
count += 1;
97+
match (self.cmp_fn)(&left, &right) {
98+
Ordering::Equal => {}
99+
Ordering::Less => self.right.put_back(right),
100+
Ordering::Greater => self.left.put_back(left),
101+
}
102+
}
103+
}
104+
}
105+
}
106+
107+
fn last(mut self) -> Option<Self::Item> {
108+
let mut previous_element = None;
109+
loop {
110+
match (self.left.next(), self.right.next()) {
111+
(None, None) => break previous_element,
112+
(Some(left), None) => {
113+
break Some(EitherOrBoth::Left(self.left.last().unwrap_or(left)))
114+
}
115+
(None, Some(right)) => {
116+
break Some(EitherOrBoth::Right(self.right.last().unwrap_or(right)))
117+
}
118+
(Some(left), Some(right)) => {
119+
previous_element = match (self.cmp_fn)(&left, &right) {
120+
Ordering::Equal => Some(EitherOrBoth::Both(left, right)),
121+
Ordering::Less => {
122+
self.right.put_back(right);
123+
Some(EitherOrBoth::Left(left))
124+
}
125+
Ordering::Greater => {
126+
self.left.put_back(left);
127+
Some(EitherOrBoth::Right(right))
128+
}
129+
}
130+
}
131+
}
132+
}
133+
}
134+
135+
fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
136+
loop {
137+
if n == 0 {
138+
break self.next();
139+
}
140+
n -= 1;
141+
match (self.left.next(), self.right.next()) {
142+
(None, None) => break None,
143+
(Some(_left), None) => break self.left.nth(n).map(EitherOrBoth::Left),
144+
(None, Some(_right)) => break self.right.nth(n).map(EitherOrBoth::Right),
145+
(Some(left), Some(right)) => match (self.cmp_fn)(&left, &right) {
146+
Ordering::Equal => {}
147+
Ordering::Less => self.right.put_back(right),
148+
Ordering::Greater => self.left.put_back(left),
149+
},
150+
}
151+
}
152+
}
87153
}

0 commit comments

Comments
 (0)