Skip to content
Merged
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
42 changes: 42 additions & 0 deletions timely/src/progress/frontier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ impl<T: Clone> Clone for Antichain<T> {

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

impl<T: Ord+std::hash::Hash> std::hash::Hash for Antichain<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let mut temp = self.elements.iter().collect::<Vec<_>>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this temp Vec is unfortunate, should I be doing something different? maybe impl Hash for MutableAntichain (which requires that T be Ord) instead?

Copy link
Member

Choose a reason for hiding this comment

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

This seems fine to me, and is in-line with how we do equality. It just means that Hash is not super cheap, but that isn't the end of the world imo. A different take is that we should maintain antichains in normal form (sorted, say), which is a different amount of disruptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay then it works for me, too! It's still much faster than the alternative (repeatedly iterating through a list to call Eq). Just wanted to make sure I wasn't missing something.

temp.sort();
for element in temp {
element.hash(state);
}
}
}

impl<T: PartialOrder> From<Vec<T>> for Antichain<T> {
fn from(vec: Vec<T>) -> Self {
// TODO: We could reuse `vec` with some care.
Expand Down Expand Up @@ -715,3 +725,35 @@ impl<'a, T: 'a> ::std::iter::IntoIterator for &'a AntichainRef<'a, T> {
self.iter()
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use super::*;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Elem(char, usize);

impl PartialOrder for Elem {
fn less_equal(&self, other: &Self) -> bool {
self.0 <= other.0 && self.1 <= other.1
}
}

#[test]
fn antichain_hash() {
let mut hashed = HashSet::new();
hashed.insert(Antichain::from(vec![Elem('a', 2), Elem('b', 1)]));

assert!(hashed.contains(&Antichain::from(vec![Elem('a', 2), Elem('b', 1)])));
assert!(hashed.contains(&Antichain::from(vec![Elem('b', 1), Elem('a', 2)])));

assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 2)])));
assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 1)])));
assert!(!hashed.contains(&Antichain::from(vec![Elem('b', 2)])));
assert!(!hashed.contains(&Antichain::from(vec![Elem('a', 1), Elem('b', 2)])));
assert!(!hashed.contains(&Antichain::from(vec![Elem('c', 3)])));
assert!(!hashed.contains(&Antichain::from(vec![])));
}
}