Skip to content

Commit 359f92f

Browse files
committed
enable antijoin from a relation too
1 parent dc3f60a commit 359f92f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/join.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ pub(crate) fn join_into_relation<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Or
4949
}
5050

5151
/// Moves all recent tuples from `input1` that are not present in `input2` into `output`.
52-
pub(crate) fn antijoin_into<Key: Ord, Val: Ord, Result: Ord>(
53-
input1: &Variable<(Key, Val)>,
52+
pub(crate) fn antijoin<'me, Key: Ord, Val: Ord, Result: Ord>(
53+
input1: impl JoinInput<'me, (Key, Val)>,
5454
input2: &Relation<Key>,
55-
output: &Variable<Result>,
5655
mut logic: impl FnMut(&Key, &Val) -> Result,
57-
) {
56+
) -> Relation<Result> {
5857
let mut tuples2 = &input2[..];
5958

6059
let results = input1
61-
.recent
62-
.borrow()
60+
.recent()
6361
.iter()
6462
.filter(|(ref key, _)| {
6563
tuples2 = gallop(tuples2, |k| k < key);
@@ -68,7 +66,7 @@ pub(crate) fn antijoin_into<Key: Ord, Val: Ord, Result: Ord>(
6866
.map(|(ref key, ref val)| logic(key, val))
6967
.collect::<Vec<_>>();
7068

71-
output.insert(Relation::from_vec(results));
69+
Relation::from_vec(results)
7270
}
7371

7472
fn join_helper<K: Ord, V1, V2>(

src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ impl<Tuple: Ord> Relation<Tuple> {
111111
join::join_into_relation(input1, input2, logic)
112112
}
113113

114+
/// Creates a `Relation` by removing all values from `input1` that
115+
/// share a key with `input2`, and then transforming the reuslting
116+
/// tuples with the `logic` closure. Like
117+
/// [`Variable::from_antijoin`] except for use where the inputs
118+
/// are not varying across iterations.
119+
pub fn from_antijoin<Key: Ord, Val1: Ord>(
120+
input1: &Relation<(Key, Val1)>,
121+
input2: &Relation<Key>,
122+
logic: impl FnMut(&Key, &Val1) -> Tuple,
123+
) -> Self {
124+
join::antijoin(input1, input2, logic)
125+
}
126+
114127
/// Creates a `Relation` from a vector of tuples.
115128
pub fn from_vec(mut elements: Vec<Tuple>) -> Self {
116129
elements.sort();
@@ -264,6 +277,11 @@ impl<Tuple: Ord> Variable<Tuple> {
264277

265278
/// Adds tuples from `input1` whose key is not present in `input2`.
266279
///
280+
/// Note that `input1` must be a variable: if you have a relation
281+
/// instead, you can use `Relation::from_antijoin` and then
282+
/// `Variable::insert`. Note that the result will not vary during
283+
/// the iteration.
284+
///
267285
/// # Examples
268286
///
269287
/// This example starts a collection with the pairs (x, x+1) for x in 0 .. 10. It then
@@ -292,7 +310,7 @@ impl<Tuple: Ord> Variable<Tuple> {
292310
input2: &Relation<K>,
293311
logic: impl FnMut(&K, &V) -> Tuple,
294312
) {
295-
join::antijoin_into(input1, input2, self, logic)
313+
self.insert(join::antijoin(input1, input2, logic))
296314
}
297315

298316
/// Adds tuples that result from mapping `input`.

0 commit comments

Comments
 (0)