Skip to content

Commit 1447ee0

Browse files
committed
add a Variable::extend method that takes an iterator directly
1 parent 402f415 commit 1447ee0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl<Tuple: Ord> Variable<Tuple> {
285285
///
286286
/// let mut iteration = Iteration::new();
287287
/// let variable = iteration.variable::<(usize, usize)>("source");
288-
/// variable.insert((0 .. 10).map(|x| (x, x + 1)).collect());
289-
/// variable.insert((0 .. 10).map(|x| (x + 1, x)).collect());
288+
/// variable.extend((0 .. 10).map(|x| (x, x + 1)));
289+
/// variable.extend((0 .. 10).map(|x| (x + 1, x)));
290290
///
291291
/// while iteration.changed() {
292292
/// variable.from_join(&variable, &variable, |&key, &val1, &val2| (val1, val2));
@@ -322,7 +322,7 @@ impl<Tuple: Ord> Variable<Tuple> {
322322
///
323323
/// let mut iteration = Iteration::new();
324324
/// let variable = iteration.variable::<(usize, usize)>("source");
325-
/// variable.insert((0 .. 10).map(|x| (x, x + 1)).collect());
325+
/// variable.extend((0 .. 10).map(|x| (x, x + 1)));
326326
///
327327
/// let relation: Relation<_> = (0 .. 10).filter(|x| x % 3 == 0).collect();
328328
///
@@ -356,7 +356,7 @@ impl<Tuple: Ord> Variable<Tuple> {
356356
///
357357
/// let mut iteration = Iteration::new();
358358
/// let variable = iteration.variable::<(usize, usize)>("source");
359-
/// variable.insert((0 .. 10).map(|x| (x, x)).collect());
359+
/// variable.extend((0 .. 10).map(|x| (x, x)));
360360
///
361361
/// while iteration.changed() {
362362
/// variable.from_map(&variable, |&(key, val)|
@@ -434,6 +434,7 @@ impl<Tuple: Ord> Variable<Tuple> {
434434
to_add: Rc::new(RefCell::new(Vec::new())),
435435
}
436436
}
437+
437438
/// Inserts a relation into the variable.
438439
///
439440
/// This is most commonly used to load initial values into a variable.
@@ -444,6 +445,18 @@ impl<Tuple: Ord> Variable<Tuple> {
444445
self.to_add.borrow_mut().push(relation);
445446
}
446447
}
448+
449+
/// Extend the variable with values from the iterator.
450+
///
451+
/// This is most commonly used to load initial values into a variable.
452+
/// it is not obvious that it should be commonly used otherwise, but
453+
/// it should not be harmful.
454+
pub fn extend<T>(&self, iterator: impl IntoIterator<Item = T>)
455+
where Relation<Tuple>: FromIterator<T>
456+
{
457+
self.insert(iterator.into_iter().collect());
458+
}
459+
447460
/// Consumes the variable and returns a relation.
448461
///
449462
/// This method removes the ability for the variable to develop, and

src/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn reachable_with_var_join(edges: &[(u32, u32)]) -> Relation<(u32, u32)> {
1616
let mut iteration = Iteration::new();
1717

1818
let edges_by_successor = iteration.variable::<(u32, u32)>("edges_invert");
19-
edges_by_successor.insert(edges.iter().map(|&(n1, n2)| (n2, n1)).collect());
19+
edges_by_successor.extend(edges.iter().map(|&(n1, n2)| (n2, n1)));
2020

2121
let reachable = iteration.variable::<(u32, u32)>("reachable");
2222
reachable.insert(edges);
@@ -78,10 +78,10 @@ fn sum_join_via_var(
7878
let mut iteration = Iteration::new();
7979

8080
let input1 = iteration.variable::<(u32, u32)>("input1");
81-
input1.insert(input1_slice.iter().collect());
81+
input1.extend(input1_slice);
8282

8383
let input2 = iteration.variable::<(u32, u32)>("input1");
84-
input2.insert(input2_slice.iter().collect());
84+
input2.extend(input2_slice);
8585

8686
let output = iteration.variable::<(u32, u32)>("output");
8787

0 commit comments

Comments
 (0)