Skip to content

Commit 98af39f

Browse files
authored
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 <[email protected]>
1 parent c27ed84 commit 98af39f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

timely/src/progress/frontier.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ impl<T: PartialOrder> Antichain<T> {
4343
}
4444
}
4545

46+
/// Updates the `Antichain` if the element is not greater than or equal to some present element.
47+
///
48+
/// Returns true if element is added to the set
49+
///
50+
/// Accepts a reference to an element, which is cloned when inserting.
51+
///
52+
/// # Examples
53+
///
54+
///```
55+
/// use timely::progress::frontier::Antichain;
56+
///
57+
/// let mut frontier = Antichain::new();
58+
/// assert!(frontier.insert_ref(&2));
59+
/// assert!(!frontier.insert(3));
60+
///```
61+
pub fn insert_ref(&mut self, element: &T) -> bool where T: Clone {
62+
if !self.elements.iter().any(|x| x.less_equal(element)) {
63+
self.elements.retain(|x| !element.less_equal(x));
64+
self.elements.push(element.clone());
65+
true
66+
}
67+
else {
68+
false
69+
}
70+
}
71+
4672
/// Reserves capacity for at least additional more elements to be inserted in the given `Antichain`
4773
pub fn reserve(&mut self, additional: usize) {
4874
self.elements.reserve(additional);

0 commit comments

Comments
 (0)