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
Add external event notifications in transactions. #3020
Changes from 1 commit
File filter...
Jump to…
Add util::drain_filter.
- Loading branch information
| @@ -508,3 +508,63 @@ pub fn world_rect_to_device_pixels( | ||
| let device_rect = rect * device_pixel_scale; | ||
| device_rect.round_out() | ||
| } | ||
|
|
||
| /// Run the first callback over all elements in the array. If the callback returns true, | ||
| /// the element is removed from the array and moved to a second callback. | ||
| /// | ||
| /// This is a simple implementation waiting for Vec::drain_filter to be stable. | ||
| /// When that happens, code like: | ||
| /// | ||
| /// let filter = |op| { | ||
| /// match *op { | ||
| /// Enum::Foo | Enum::Bar => true, | ||
| /// Enum::Baz => false, | ||
| /// } | ||
| /// }; | ||
| /// drain_filter( | ||
| /// &mut ops, | ||
| /// filter, | ||
| /// |op| { | ||
| /// match op { | ||
| /// Enum::Foo => { foo(); } | ||
| /// Enum::Bar => { bar(); } | ||
| /// Enum::Baz => { unreachable!(); } | ||
| /// } | ||
| /// }, | ||
| /// ); | ||
| /// | ||
| /// Can be rewritten as: | ||
| /// | ||
| /// let filter = |op| { | ||
| /// match *op { | ||
| /// Enum::Foo | Enum::Bar => true, | ||
| /// Enum::Baz => false, | ||
| /// } | ||
| /// }; | ||
| /// for op in ops.drain_filter(filter) { | ||
| /// match op { | ||
| /// Enum::Foo => { foo(); } | ||
| /// Enum::Bar => { bar(); } | ||
| /// Enum::Baz => { unreachable!(); } | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// See https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain_filter | ||
| pub fn drain_filter<T, Filter, Action>( | ||
| vec: &mut Vec<T>, | ||
| mut filter: Filter, | ||
| mut action: Action, | ||
| ) | ||
| where | ||
| Filter: FnMut(&mut T) -> bool, | ||
| Action: FnMut(T) | ||
| { | ||
| let mut i = 0; | ||
| while i != vec.len() { | ||
| if filter(&mut vec[i]) { | ||
| action(vec.remove(i)); | ||
nical
Author
Collaborator
|
||
| } else { | ||
| i += 1; | ||
| } | ||
| } | ||
| } | ||
This is quadratic asymptotic complexity, or rather O(N*M) where M is the number of filter matches. Can we do better?