You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
766 B
31 lines
766 B
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()
|
|
}
|
|
}
|
|
|