From 98af39f61e4d278e6f33c1ca667a4db5a8eeee40 Mon Sep 17 00:00:00 2001 From: Moritz Hoffmann Date: Sat, 25 Nov 2023 14:08:55 -0500 Subject: [PATCH] Insert an element by reference into an antichain (#536) This is useful if all the caller has is a reference. Signed-off-by: Moritz Hoffmann --- timely/src/progress/frontier.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/timely/src/progress/frontier.rs b/timely/src/progress/frontier.rs index 3ad8e90f5..46012bcce 100644 --- a/timely/src/progress/frontier.rs +++ b/timely/src/progress/frontier.rs @@ -43,6 +43,32 @@ impl Antichain { } } + /// Updates the `Antichain` if the element is not greater than or equal to some present element. + /// + /// Returns true if element is added to the set + /// + /// Accepts a reference to an element, which is cloned when inserting. + /// + /// # Examples + /// + ///``` + /// use timely::progress::frontier::Antichain; + /// + /// let mut frontier = Antichain::new(); + /// assert!(frontier.insert_ref(&2)); + /// assert!(!frontier.insert(3)); + ///``` + pub fn insert_ref(&mut self, element: &T) -> bool where T: Clone { + if !self.elements.iter().any(|x| x.less_equal(element)) { + self.elements.retain(|x| !element.less_equal(x)); + self.elements.push(element.clone()); + true + } + else { + false + } + } + /// Reserves capacity for at least additional more elements to be inserted in the given `Antichain` pub fn reserve(&mut self, additional: usize) { self.elements.reserve(additional);