Skip to content

Commit 092f902

Browse files
Clean up control flow in Relation::merge
This might also be a perf improvement
1 parent 084911a commit 092f902

File tree

1 file changed

+9
-34
lines changed

1 file changed

+9
-34
lines changed

src/lib.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use std::rc::Rc;
1515
use std::cell::RefCell;
16+
use std::cmp::Ordering;
1617

1718
mod map;
1819
mod join;
@@ -58,50 +59,24 @@ impl<Tuple: Ord> Relation<Tuple> {
5859
let mut elements = Vec::with_capacity(elements1.len() + elements2.len());
5960
let mut elements1 = elements1.drain(..);
6061
let mut elements2 = elements2.drain(..).peekable();
61-
// let mut from_1 = 0;
62-
// let mut from_2 = 0;
6362

6463
elements.push(elements1.next().unwrap());
65-
// from_1 += 1;
64+
if &elements[0] == elements2.peek().unwrap() {
65+
elements2.next();
66+
}
6667

6768
for elem in elements1 {
68-
loop {
69-
// Pull everything out of elements2 that is less than the thing
70-
// we're about to insert into elements from elements1
71-
use std::cmp::Ordering;
72-
let cmpres = match elements2.peek() {
73-
None => Ordering::Greater,
74-
Some(e2) => e2.cmp(&elem)
75-
};
76-
77-
match cmpres {
78-
Ordering::Greater => break,
79-
Ordering::Equal => {
80-
// consume without pushing
81-
elements2.next();
82-
},
83-
Ordering::Less => {
84-
let tp = elements2.next().unwrap();
85-
if elements[elements.len() - 1] == tp {
86-
// Don't push duplicates
87-
continue;
88-
}
89-
elements.push(tp);
90-
}
91-
}
69+
while elements2.peek().map(|x| x.cmp(&elem)) == Some(Ordering::Less) {
70+
elements.push(elements2.next().unwrap());
9271
}
93-
if elements[elements.len() - 1] == elem {
94-
// Don't push duplicates
95-
continue;
72+
if elements2.peek().map(|x| x.cmp(&elem)) == Some(Ordering::Equal) {
73+
elements2.next();
9674
}
9775
elements.push(elem);
9876
}
77+
9978
// Finish draining second list
10079
for elem in elements2 {
101-
if elements[elements.len() - 1] == elem {
102-
continue;
103-
}
104-
// from_2 += 1;
10580
elements.push(elem);
10681
}
10782

0 commit comments

Comments
 (0)