From cdd1d99ef83c5ab5887b957c1210ca72ec426f8f Mon Sep 17 00:00:00 2001 From: Frank McSherry Date: Fri, 18 Mar 2022 09:38:24 -0400 Subject: [PATCH 1/2] Add option conversions for totally ordered antichains --- timely/src/progress/frontier.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/timely/src/progress/frontier.rs b/timely/src/progress/frontier.rs index 77fd5a120..6a707ccc5 100644 --- a/timely/src/progress/frontier.rs +++ b/timely/src/progress/frontier.rs @@ -229,6 +229,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::>(); @@ -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 { From d3f0ea6670dc331e60d3ea51744a595e26bc7b8c Mon Sep 17 00:00:00 2001 From: Frank McSherry Date: Fri, 18 Mar 2022 10:28:56 -0400 Subject: [PATCH 2/2] Add FromIterator implementations --- timely/src/progress/frontier.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/timely/src/progress/frontier.rs b/timely/src/progress/frontier.rs index 6a707ccc5..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`. @@ -623,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> {