Skip to content

Commit 1cc53d1

Browse files
authored
Merge pull request #17 from nikomatsakis/from_leapjoin
add `.from_leapjoin` method to public API
2 parents 58a7cb3 + 5bba0b2 commit 1cc53d1

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

examples/borrow_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() {
6161
subset_r1p.complete()
6262
};
6363

64-
let requires = {
64+
let _requires = {
6565
// Create a new iteration context, ...
6666
let mut iteration2 = Iteration::new();
6767

src/lib.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ use std::rc::Rc;
1717

1818
mod join;
1919
mod map;
20-
pub mod treefrog;
20+
mod treefrog;
21+
pub use treefrog::{
22+
extend_anti::ExtendAnti, extend_with::ExtendWith, filter_anti::FilterAnti,
23+
filter_with::FilterWith, Leaper, RelationLeaper,
24+
};
2125

2226
/// A static, ordered list of key-value pairs.
2327
///
2428
/// A relation represents a fixed set of key-value pairs. In many places in a
2529
/// Datalog computation we want to be sure that certain relations are not able
2630
/// to vary (for example, in antijoins).
31+
#[derive(Clone)]
2732
pub struct Relation<Tuple: Ord> {
2833
/// Sorted list of distinct tuples.
2934
pub elements: Vec<Tuple>,
@@ -114,7 +119,7 @@ impl<Tuple: Ord> std::ops::Deref for Relation<Tuple> {
114119
/// It can inform the user if they have ceased changing, at which point the
115120
/// computation should be done.
116121
pub struct Iteration {
117-
variables: Vec<Box<VariableTrait>>,
122+
variables: Vec<Box<dyn VariableTrait>>,
118123
}
119124

120125
impl Iteration {
@@ -222,6 +227,7 @@ impl<Tuple: Ord> Variable<Tuple> {
222227
) {
223228
join::join_into(input1, input2, self, logic)
224229
}
230+
225231
/// Adds tuples from `input1` whose key is not present in `input2`.
226232
///
227233
/// # Examples
@@ -254,6 +260,7 @@ impl<Tuple: Ord> Variable<Tuple> {
254260
) {
255261
join::antijoin_into(input1, input2, self, logic)
256262
}
263+
257264
/// Adds tuples that result from mapping `input`.
258265
///
259266
/// # Examples
@@ -286,6 +293,21 @@ impl<Tuple: Ord> Variable<Tuple> {
286293
pub fn from_map<T2: Ord>(&self, input: &Variable<T2>, logic: impl FnMut(&T2) -> Tuple) {
287294
map::map_into(input, self, logic)
288295
}
296+
297+
/// Adds tuples that result from combining `source` with the
298+
/// relations given in `leapers`. This operation is very flexible
299+
/// and can be used to do a combination of joins and anti-joins.
300+
/// The main limitation is that the things being combined must
301+
/// consist of one dynamic variable (`source`) and then several
302+
/// fixed relations (`leapers`).
303+
pub fn from_leapjoin<'a, SourceTuple: Ord, Val: Ord + 'a>(
304+
&self,
305+
source: &Variable<SourceTuple>,
306+
leapers: &mut [&mut dyn Leaper<'a, SourceTuple, Val>],
307+
logic: impl FnMut(&SourceTuple, &Val) -> Tuple,
308+
) {
309+
treefrog::leapjoin_into(source, leapers, self, logic)
310+
}
289311
}
290312

291313
impl<Tuple: Ord> Clone for Variable<Tuple> {

src/treefrog.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{Relation, Variable};
55
/// Performs treefrog leapjoin using a list of leapers.
66
pub fn leapjoin_into<'a, Tuple: Ord, Val: Ord + 'a, Result: Ord>(
77
source: &Variable<Tuple>,
8-
leapers: &mut [&mut Leaper<'a, Tuple, Val>],
8+
leapers: &mut [&mut dyn Leaper<'a, Tuple, Val>],
99
output: &Variable<Result>,
1010
mut logic: impl FnMut(&Tuple, &Val) -> Result,
1111
) {
@@ -138,7 +138,7 @@ impl<Key: Ord, Val: Ord> RelationLeaper<Key, Val> for Relation<(Key, Val)> {
138138
}
139139
}
140140

141-
mod extend_with {
141+
pub(crate) mod extend_with {
142142
use super::{binary_search, Leaper, Relation};
143143
use join::gallop;
144144

@@ -205,7 +205,7 @@ mod extend_with {
205205
}
206206
}
207207

208-
mod extend_anti {
208+
pub(crate) mod extend_anti {
209209
use super::{binary_search, Leaper, Relation};
210210
use join::gallop;
211211

@@ -269,7 +269,7 @@ mod extend_anti {
269269
}
270270
}
271271

272-
mod filter_with {
272+
pub(crate) mod filter_with {
273273

274274
use super::{Leaper, Relation};
275275

@@ -328,7 +328,7 @@ mod filter_with {
328328
}
329329
}
330330

331-
mod filter_anti {
331+
pub(crate) mod filter_anti {
332332

333333
use super::{Leaper, Relation};
334334

0 commit comments

Comments
 (0)