From 1bcac7dbaa405ce8956c7976a791a8e76a28748c Mon Sep 17 00:00:00 2001 From: Michal Terepeta Date: Sun, 12 Sep 2021 09:05:37 +0200 Subject: [PATCH] Fix `Relation::from_antijoin` (issue #39) Now the `antijoin` helper accepts a `Relation`, which does exactly what we want in `Relation::from_antijoin`, while in `Variable::from_antijoin` we now explicitly pass just the `recent` `Relation`. Signed-off-by: Michal Terepeta --- src/join.rs | 6 +++--- src/test.rs | 11 +++++++++++ src/variable.rs | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/join.rs b/src/join.rs index dbf1f67..2495dd7 100644 --- a/src/join.rs +++ b/src/join.rs @@ -78,15 +78,15 @@ pub(crate) fn join_into_relation<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Or } /// Moves all recent tuples from `input1` that are not present in `input2` into `output`. -pub(crate) fn antijoin<'me, Key: Ord, Val: Ord, Result: Ord>( - input1: impl JoinInput<'me, (Key, Val)>, +pub(crate) fn antijoin( + input1: &Relation<(Key, Val)>, input2: &Relation, mut logic: impl FnMut(&Key, &Val) -> Result, ) -> Relation { let mut tuples2 = &input2[..]; let results = input1 - .recent() + .elements .iter() .filter(|(ref key, _)| { tuples2 = gallop(tuples2, |k| k < key); diff --git a/src/test.rs b/src/test.rs index d76c884..381468b 100644 --- a/src/test.rs +++ b/src/test.rs @@ -219,3 +219,14 @@ fn passthrough_leaper() { expected.extend((20..=40).filter_map(|i| (i%4 == 0).then(|| (i, i)))); assert_eq!(&*variable, &expected); } + +#[test] +fn relation_from_antijoin() { + let lhs: Relation<_> = (0 .. 10).map(|x| (x, x)).collect(); + let rhs: Relation<_> = (0 .. 10).filter(|x| x % 2 == 0).collect(); + let expected: Relation<_> = (0 .. 10).filter(|x| x % 2 == 1).map(|x| (x, x)).collect(); + + let result = Relation::from_antijoin(&lhs, &rhs, |a, b| (*a, *b)); + + assert_eq!(result.elements, expected.elements); +} diff --git a/src/variable.rs b/src/variable.rs index 20cf1b0..755ba02 100644 --- a/src/variable.rs +++ b/src/variable.rs @@ -177,7 +177,7 @@ impl Variable { input2: &Relation, logic: impl FnMut(&K, &V) -> Tuple, ) { - self.insert(join::antijoin(input1, input2, logic)) + self.insert(join::antijoin(&input1.recent.borrow(), input2, logic)) } /// Adds tuples that result from mapping `input`.