Skip to content

Commit 6a0f391

Browse files
Merge pull request #13 from ljedrz/minor_improvements
A few readability and performance improvements
2 parents ed3ace5 + 2b8aa93 commit 6a0f391

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/join.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ pub fn antijoin_into<Key: Ord, Val: Ord, Result: Ord>(
3838
output: &Variable<Result>,
3939
mut logic: impl FnMut(&Key, &Val)->Result) {
4040

41-
let mut results = Vec::new();
4241
let mut tuples2 = &input2[..];
4342

44-
for &(ref key, ref val) in input1.recent.borrow().iter() {
43+
let results = input1.recent.borrow().iter().filter(|(ref key, _)| {
4544
tuples2 = gallop(tuples2, |k| k < key);
46-
if tuples2.first() != Some(key) {
47-
results.push(logic(key, val));
48-
}
49-
}
45+
tuples2.first() != Some(key)
46+
}).map(|(ref key, ref val)| logic(key, val)).collect::<Vec<_>>();
5047

5148
output.insert(Relation::from_vec(results));
5249
}
@@ -91,7 +88,7 @@ fn join_helper<K: Ord, V1, V2>(
9188

9289
pub fn gallop<T>(mut slice: &[T], mut cmp: impl FnMut(&T)->bool) -> &[T] {
9390
// if empty slice, or already >= element, return
94-
if slice.len() > 0 && cmp(&slice[0]) {
91+
if !slice.is_empty() && cmp(&slice[0]) {
9592
let mut step = 1;
9693
while step < slice.len() && cmp(&slice[step]) {
9794
slice = &slice[step..];
@@ -110,4 +107,4 @@ pub fn gallop<T>(mut slice: &[T], mut cmp: impl FnMut(&T)->bool) -> &[T] {
110107
}
111108

112109
return slice;
113-
}
110+
}

src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ impl<Tuple: Ord> Relation<Tuple> {
3636
let mut elements2 = other.elements;
3737

3838
// If one of the element lists is zero-length, we don't need to do any work
39-
if elements1.len() == 0 {
39+
if elements1.is_empty() {
4040
return Relation { elements: elements2 };
4141
}
42-
if elements2.len() == 0 {
42+
43+
if elements2.is_empty() {
4344
return Relation { elements: elements1 };
4445
}
4546

@@ -61,7 +62,7 @@ impl<Tuple: Ord> Relation<Tuple> {
6162
let mut elements2 = elements2.drain(..).peekable();
6263

6364
elements.push(elements1.next().unwrap());
64-
if &elements[0] == elements2.peek().unwrap() {
65+
if elements.first() == elements2.peek() {
6566
elements2.next();
6667
}
6768

@@ -76,9 +77,7 @@ impl<Tuple: Ord> Relation<Tuple> {
7677
}
7778

7879
// Finish draining second list
79-
for elem in elements2 {
80-
elements.push(elem);
81-
}
80+
elements.extend(elements2);
8281

8382
Relation { elements }
8483
}

0 commit comments

Comments
 (0)