Skip to content
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

remove_until like API, removes all items until a predicate matches #55

Open
Dav1dde opened this issue Jul 23, 2024 · 0 comments
Open

remove_until like API, removes all items until a predicate matches #55

Dav1dde opened this issue Jul 23, 2024 · 0 comments

Comments

@Dav1dde
Copy link

Dav1dde commented Jul 23, 2024

The idea is similar to #47 but instead of being able to skip items, it would remove all items until a predicate matches (excluding that element).

For example in Rust pseudo code:

let mut queue = PriorityQueue::new();
queue.push(1, "a");
queue.push(2, "b");
queue.push(3, "c");

let items = queue.remove_until(|k| k < 3).collect::<Vec<_>>();
assert_eq!(items, vec![(1, "a"), (2, "b")]);

assert_eq!(queue.peek(), Some((3, "c")));

Currently this requires to either peek and pop:

while let Some((k, _)) = queue.peek() {
    if k < 3 { 
        break 
    }

    let Some((k, v)) = queue.pop().unwrap();
} 

Or constant pops() with a push of the removed item which does not match the predicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants