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 option conversions for totally ordered antichains #458

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

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

impl<T: TotalOrder> Antichain<T> {
/// Convert to the at most one element the antichain contains.
pub fn into_option(mut self) -> Option<T> {
debug_assert!(self.len() <= 1);
self.elements.pop()
}
/// Return a reference to the at most one element the antichain contains.
pub fn as_option(&self) -> Option<&T> {
debug_assert!(self.len() <= 1);
self.elements.last()
}
}

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<_>>();
Expand Down Expand Up @@ -711,6 +724,16 @@ impl<'a, T: PartialOrder> PartialOrder for AntichainRef<'a, T> {
}
}

impl<'a, T: TotalOrder> TotalOrder for AntichainRef<'a, T> { }

impl<'a, T: TotalOrder> AntichainRef<'a, T> {
/// Return a reference to the at most one element the antichain contains.
pub fn as_option(&self) -> Option<&T> {
debug_assert!(self.len() <= 1);
self.frontier.last()
}
}

impl<'a, T> ::std::ops::Deref for AntichainRef<'a, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
Expand Down