Skip to content

Implement From Vec for Antichain #480

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions timely/src/progress/frontier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,33 @@ impl<T: Clone> Clone for Antichain<T> {
}

impl<T: PartialOrder> From<Vec<T>> for Antichain<T> {
fn from(vec: Vec<T>) -> Self {
// TODO: We could reuse `vec` with some care.
let mut temp = Antichain::new();
for elem in vec.into_iter() { temp.insert(elem); }
temp
fn from(mut vec: Vec<T>) -> Self {
// We will iteratively insert elements `vec[index]` into `vec[0 .. valid]`,
// by pivoting elements around and maintaining `vec[0 .. valid]` to contain
// those elements that form a minimal antichain.
let mut valid = 0;
for index in 0 .. vec.len() {
// Attempt to insert `vec[index]` into `vec[0 .. valid]`.
if !vec[0 .. valid].iter().any(|x| x.less_equal(&vec[index])) {
// "Retain" elements not greater or equal to `vec[index]`.
let mut prior = 0;
while prior < valid {
if vec[index].less_equal(&vec[prior]) {
vec.swap(prior, valid - 1);
valid -= 1;
} else {
prior += 1;
}
}
// "Push" the element to the end of the valid region.
vec.swap(valid, index);
valid += 1;
}
}
vec.truncate(valid);
vec.shrink_to_fit();

Self { elements: vec.into() }
}
}

Expand Down