master > master: code-rust - utils für vec -> set conv

This commit is contained in:
RD 2022-04-08 23:35:30 +02:00
parent cc297011b2
commit 0d8583dd0c
1 changed files with 29 additions and 1 deletions

View File

@ -4,6 +4,10 @@
extern crate regex;
use std::hash::Hash;
use std::collections::HashMap;
use std::collections::HashSet;
use self::regex::Regex;
// ----------------------------------------------------------------
@ -57,5 +61,29 @@ pub fn remove_first<T>(x: &Vec<T>) -> Vec<T>
if n == 0 {
return Vec::<T>::new();
}
return restrict::<T>(x, 0, n);
return restrict::<T>(x, 1, n);
}
// ----------------------------------------------------------------
// METHODS Vectors to sets
// ----------------------------------------------------------------
#[allow(dead_code)]
pub fn vec_to_set<T>(x: &Vec<T>) -> HashSet<T>
where T: Eq + Hash + Clone
{
return x.iter()
.map(|u| {u.clone()})
.collect();
}
#[allow(dead_code)]
pub fn vec_to_multiset<T>(x: &Vec<T>) -> HashMap<T, usize>
where T: Eq + Hash + Copy
{
let mut counted = HashMap::<T, usize>::new();
for u in x.iter() {
*counted.entry(u.clone()).or_insert(0) += 1
}
return counted;
}