Skip to content
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

Add PartialOrder impls #322

Merged
merged 6 commits into from
Mar 9, 2020
Merged
Changes from 1 commit
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
98 changes: 59 additions & 39 deletions timely/src/progress/frontier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use crate::order::PartialOrder;
/// This antichain implementation allows you to repeatedly introduce elements to the antichain, and
/// which will evict larger elements to maintain the *minimal* antichain, those incomparable elements
/// no greater than any other element.
#[derive(Clone, Debug, Default, Eq, PartialEq, Abomonation, Serialize, Deserialize)]
///
/// Two antichains are equal if the contain the same set of elements, even if in different orders.
/// This can make equality testing quadratic, though linear in the common case that the sequences
/// are identical.
#[derive(Clone, Debug, Default, Abomonation, Serialize, Deserialize)]
pub struct Antichain<T> {
elements: Vec<T>
}
Expand Down Expand Up @@ -59,44 +63,6 @@ impl<T: PartialOrder> Antichain<T> {
added
}

/// Creates a new empty `Antichain`.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::<u32>::new();
///```
pub fn new() -> Antichain<T> { Antichain { elements: Vec::new() } }

/// Creates a new singleton `Antichain`.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::from_elem(2);
///```
pub fn from_elem(element: T) -> Antichain<T> { Antichain { elements: vec![element] } }

/// Clears the contents of the antichain.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::from_elem(2);
/// frontier.clear();
/// assert!(frontier.elements().is_empty());
///```
pub fn clear(&mut self) { self.elements.clear() }

/// Sorts the elements so that comparisons between antichains can be made.
pub fn sort(&mut self) where T: Ord { self.elements.sort() }

/// Returns true if any item in the antichain is strictly less than the argument.
///
/// # Examples
Expand Down Expand Up @@ -143,6 +109,47 @@ impl<T: PartialOrder> Antichain<T> {
pub fn dominates(&self, other: &Antichain<T>) -> bool {
<Self as PartialOrder>::less_equal(self, other)
}
}

impl<T> Antichain<T> {

/// Creates a new empty `Antichain`.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::<u32>::new();
///```
pub fn new() -> Antichain<T> { Antichain { elements: Vec::new() } }

/// Creates a new singleton `Antichain`.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::from_elem(2);
///```
pub fn from_elem(element: T) -> Antichain<T> { Antichain { elements: vec![element] } }

/// Clears the contents of the antichain.
///
/// # Examples
///
///```
/// use timely::progress::frontier::Antichain;
///
/// let mut frontier = Antichain::from_elem(2);
/// frontier.clear();
/// assert!(frontier.elements().is_empty());
///```
pub fn clear(&mut self) { self.elements.clear() }

/// Sorts the elements so that comparisons between antichains can be made.
pub fn sort(&mut self) where T: Ord { self.elements.sort() }

/// Reveals the elements in the antichain.
///
Expand All @@ -157,6 +164,19 @@ impl<T: PartialOrder> Antichain<T> {
#[inline] pub fn elements(&self) -> &[T] { &self.elements[..] }
}

impl<T: PartialEq> PartialEq for Antichain<T> {
fn eq(&self, other: &Self) -> bool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it works here as I haven't tried, but.. can we just do self.elements() == other.elements() ? slice comparison should do the job.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has the defect that it doesn't equate two slices that have the same elements, but in a different order. I think this was one of the reasons that I shied away from the implementation in the first place, but it's probably worth fixing (as otherwise equality testing could return surprising results anyhow).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed they were sorted, but it's not the case.. also, how can you sort incomparable elements? ahah nvm, dumb comment

// Lengths should be the same, with the option for fast acceptance if identical.
self.elements().len() == other.elements().len() &&
(
self.elements().iter().zip(other.elements().iter()).all(|(t1,t2)| t1 == t2) ||
self.elements().iter().all(|t1| other.elements().iter().any(|t2| t1.eq(t2)))
)
}
}

impl<T: Eq> Eq for Antichain<T> { }

impl<T: PartialOrder> PartialOrder for Antichain<T> {
fn less_equal(&self, other: &Self) -> bool {
other.elements().iter().all(|t2| self.elements().iter().any(|t1| t1.less_equal(t2)))
Expand Down