diff --git a/timely/src/progress/frontier.rs b/timely/src/progress/frontier.rs index 77fd5a120..ea52ba63e 100644 --- a/timely/src/progress/frontier.rs +++ b/timely/src/progress/frontier.rs @@ -116,6 +116,17 @@ impl Antichain { } } +impl std::iter::FromIterator for Antichain { + fn from_iter(iterator: I) -> Self + where + I: IntoIterator + { + let mut result = Self::new(); + result.extend(iterator); + result + } +} + impl Antichain { /// Creates a new empty `Antichain`. @@ -229,6 +240,19 @@ impl Clone for Antichain { impl TotalOrder for Antichain { } +impl Antichain { + /// Convert to the at most one element the antichain contains. + pub fn into_option(mut self) -> Option { + 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 std::hash::Hash for Antichain { fn hash(&self, state: &mut H) { let mut temp = self.elements.iter().collect::>(); @@ -610,6 +634,20 @@ impl<'a, T: PartialOrder+Ord+Clone> From> for MutableAnticha } } +impl std::iter::FromIterator<(T, i64)> for MutableAntichain +where + T: Clone + PartialOrder + Ord, +{ + fn from_iter(iterator: I) -> Self + where + I: IntoIterator, + { + let mut result = Self::new(); + result.update_iter(iterator); + result + } +} + /// A wrapper for elements of an antichain. #[derive(Debug)] pub struct AntichainRef<'a, T: 'a> { @@ -711,6 +749,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 {