parent
b4dbb94e27
commit
f8020a01f9
@ -0,0 +1,31 @@ |
||||
use std::hash::Hash; |
||||
use std::collections::HashMap; |
||||
|
||||
pub fn map_drain_filter<K : Eq + Hash, V>(map : &mut HashMap<K, V>, filter : impl Fn(&K, &V) -> bool) -> Vec<(K, V)> { |
||||
let mut removed = vec![]; |
||||
let mut retain = vec![]; |
||||
for (k, v) in map.drain() { |
||||
if filter(&k, &v) { |
||||
removed.push((k, v)); |
||||
} else { |
||||
retain.push((k, v)); |
||||
} |
||||
} |
||||
map.extend(retain); |
||||
removed |
||||
} |
||||
|
||||
pub trait KVVecToKeysOrValues<K, V> { |
||||
fn keys(self) -> Vec<K>; |
||||
fn values(self) -> Vec<V>; |
||||
} |
||||
|
||||
impl<K, V> KVVecToKeysOrValues<K, V> for Vec<(K,V)> { |
||||
fn keys(self) -> Vec<K> { |
||||
self.into_iter().map(|(k, _v)| k).collect() |
||||
} |
||||
|
||||
fn values(self) -> Vec<V> { |
||||
self.into_iter().map(|(_k, v)| v).collect() |
||||
} |
||||
} |
Loading…
Reference in new issue