Skip to content

Commit 07bf407

Browse files
authored
Merge pull request #50 from ecstatic-morse/var-helpers
Minor improvements
2 parents a0e9666 + f03e231 commit 07bf407

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/relation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
/// A relation represents a fixed set of key-value pairs. In many places in a
1212
/// Datalog computation we want to be sure that certain relations are not able
1313
/// to vary (for example, in antijoins).
14-
#[derive(Clone)]
15-
pub struct Relation<Tuple: Ord> {
14+
#[derive(Clone, Debug, PartialEq, Eq)]
15+
pub struct Relation<Tuple> {
1616
/// Sorted list of distinct tuples.
1717
pub elements: Vec<Tuple>,
1818
}
@@ -97,7 +97,7 @@ impl<Tuple: Ord> FromIterator<Tuple> for Relation<Tuple> {
9797
}
9898
}
9999

100-
impl<'tuple, Tuple: 'tuple + Copy + Ord> FromIterator<&'tuple Tuple> for Relation<Tuple> {
100+
impl<'tuple, Tuple: 'tuple + Clone + Ord> FromIterator<&'tuple Tuple> for Relation<Tuple> {
101101
fn from_iter<I>(iterator: I) -> Self
102102
where
103103
I: IntoIterator<Item = &'tuple Tuple>,
@@ -106,7 +106,7 @@ impl<'tuple, Tuple: 'tuple + Copy + Ord> FromIterator<&'tuple Tuple> for Relatio
106106
}
107107
}
108108

109-
impl<Tuple: Ord> std::ops::Deref for Relation<Tuple> {
109+
impl<Tuple> std::ops::Deref for Relation<Tuple> {
110110
type Target = [Tuple];
111111
fn deref(&self) -> &Self::Target {
112112
&self.elements[..]

src/variable.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) trait VariableTrait {
3636
/// of performance. Such a variable cannot be relied on to terminate iterative computation,
3737
/// and it is important that any cycle of derivations have at least one de-duplicating
3838
/// variable on it.
39-
pub struct Variable<Tuple: Ord> {
39+
pub struct Variable<Tuple> {
4040
/// Should the variable be maintained distinctly.
4141
pub(crate) distinct: bool,
4242
/// A useful name for the variable.
@@ -49,6 +49,26 @@ pub struct Variable<Tuple: Ord> {
4949
pub(crate) to_add: Rc<RefCell<Vec<Relation<Tuple>>>>,
5050
}
5151

52+
impl<Tuple> Variable<Tuple> {
53+
/// Returns the name used to create this variable.
54+
pub fn name(&self) -> &str {
55+
self.name.as_str()
56+
}
57+
58+
/// Returns the total number of "stable" tuples in this variable.
59+
pub fn num_stable(&self) -> usize {
60+
self.stable.borrow().iter().map(|x| x.len()).sum()
61+
}
62+
63+
/// Returns `true` if this variable contains only "stable" tuples.
64+
///
65+
/// Calling `Iteration::changed()` on such `Variables` will not change them unless new tuples
66+
/// are added.
67+
pub fn is_stable(&self) -> bool {
68+
self.recent.borrow().is_empty() && self.to_add.borrow().is_empty()
69+
}
70+
}
71+
5272
// Operator implementations.
5373
impl<Tuple: Ord> Variable<Tuple> {
5474
/// Adds tuples that result from joining `input1` and `input2` --
@@ -251,7 +271,7 @@ impl<Tuple: Ord> Variable<Tuple> {
251271
}
252272
}
253273

254-
impl<Tuple: Ord> Clone for Variable<Tuple> {
274+
impl<Tuple> Clone for Variable<Tuple> {
255275
fn clone(&self) -> Self {
256276
Variable {
257277
distinct: self.distinct,
@@ -304,8 +324,7 @@ impl<Tuple: Ord> Variable<Tuple> {
304324
/// asserts that iteration has completed, in that `self.recent` and
305325
/// `self.to_add` should both be empty.
306326
pub fn complete(self) -> Relation<Tuple> {
307-
assert!(self.recent.borrow().is_empty());
308-
assert!(self.to_add.borrow().is_empty());
327+
assert!(self.is_stable());
309328
let mut result: Relation<Tuple> = Vec::new().into();
310329
while let Some(batch) = self.stable.borrow_mut().pop() {
311330
result = result.merge(batch);

0 commit comments

Comments
 (0)