Skip to content

Minor improvements #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
/// A relation represents a fixed set of key-value pairs. In many places in a
/// Datalog computation we want to be sure that certain relations are not able
/// to vary (for example, in antijoins).
#[derive(Clone)]
pub struct Relation<Tuple: Ord> {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Relation<Tuple> {
/// Sorted list of distinct tuples.
pub elements: Vec<Tuple>,
}
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<Tuple: Ord> FromIterator<Tuple> for Relation<Tuple> {
}
}

impl<'tuple, Tuple: 'tuple + Copy + Ord> FromIterator<&'tuple Tuple> for Relation<Tuple> {
impl<'tuple, Tuple: 'tuple + Clone + Ord> FromIterator<&'tuple Tuple> for Relation<Tuple> {
fn from_iter<I>(iterator: I) -> Self
where
I: IntoIterator<Item = &'tuple Tuple>,
Expand All @@ -108,7 +108,7 @@ impl<'tuple, Tuple: 'tuple + Copy + Ord> FromIterator<&'tuple Tuple> for Relatio
}
}

impl<Tuple: Ord> std::ops::Deref for Relation<Tuple> {
impl<Tuple> std::ops::Deref for Relation<Tuple> {
type Target = [Tuple];
fn deref(&self) -> &Self::Target {
&self.elements[..]
Expand Down
27 changes: 23 additions & 4 deletions src/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) trait VariableTrait {
/// of performance. Such a variable cannot be relied on to terminate iterative computation,
/// and it is important that any cycle of derivations have at least one de-duplicating
/// variable on it.
pub struct Variable<Tuple: Ord> {
pub struct Variable<Tuple> {
/// Should the variable be maintained distinctly.
pub(crate) distinct: bool,
/// A useful name for the variable.
Expand All @@ -49,6 +49,26 @@ pub struct Variable<Tuple: Ord> {
pub(crate) to_add: Rc<RefCell<Vec<Relation<Tuple>>>>,
}

impl<Tuple> Variable<Tuple> {
/// Returns the name used to create this variable.
pub fn name(&self) -> &str {
self.name.as_str()
}

/// Returns the total number of "stable" tuples in this variable.
pub fn num_stable(&self) -> usize {
self.stable.borrow().iter().map(|x| x.len()).sum()
}

/// Returns `true` if this variable contains only "stable" tuples.
///
/// Calling `Iteration::changed()` on such `Variables` will not change them unless new tuples
/// are added.
pub fn is_stable(&self) -> bool {
self.recent.borrow().is_empty() && self.to_add.borrow().is_empty()
}
}

// Operator implementations.
impl<Tuple: Ord> Variable<Tuple> {
/// Adds tuples that result from joining `input1` and `input2` --
Expand Down Expand Up @@ -251,7 +271,7 @@ impl<Tuple: Ord> Variable<Tuple> {
}
}

impl<Tuple: Ord> Clone for Variable<Tuple> {
impl<Tuple> Clone for Variable<Tuple> {
fn clone(&self) -> Self {
Variable {
distinct: self.distinct,
Expand Down Expand Up @@ -304,8 +324,7 @@ impl<Tuple: Ord> Variable<Tuple> {
/// asserts that iteration has completed, in that `self.recent` and
/// `self.to_add` should both be empty.
pub fn complete(self) -> Relation<Tuple> {
assert!(self.recent.borrow().is_empty());
assert!(self.to_add.borrow().is_empty());
assert!(self.is_stable());
let mut result: Relation<Tuple> = Vec::new().into();
while let Some(batch) = self.stable.borrow_mut().pop() {
result = result.merge(batch);
Expand Down