use hashbrown::HashMap;
use hashbrown::HashSet;
use std::hash::Hash;
use std::iter::Iterator;
pub trait HttmIter: Iterator {
#[allow(dead_code)]
fn into_group_map<K, V>(self) -> HashMap<K, Vec<V>>
where
Self: Iterator<Item = (K, V)> + Sized,
K: Hash + Eq,
{
group_map::into_group_map(self)
}
fn into_group_map_by<K, V, F>(self, f: F) -> HashMap<K, Vec<V>>
where
Self: Iterator<Item = V> + Sized,
K: Hash + Eq,
F: Fn(&V) -> K,
{
group_map::into_group_map_by(self, f)
}
#[allow(dead_code)]
fn collect_map_no_update<K, V>(self) -> HashMap<K, V>
where
Self: Iterator<Item = (K, V)> + Sized,
K: Hash + Eq,
{
collect_no_update::collect_map_no_update(self)
}
#[allow(dead_code)]
fn collect_set_no_update<K>(self) -> HashSet<K>
where
Self: Iterator<Item = K> + Sized,
K: Hash + Eq,
{
collect_no_update::collect_set_no_update(self)
}
#[allow(dead_code)]
unsafe fn collect_map_unique<K, V>(self) -> HashMap<K, V>
where
Self: Iterator<Item = (K, V)> + Sized,
K: Hash + Eq,
{
unsafe { collect_no_update::collect_map_unique(self) }
}
#[allow(dead_code)]
unsafe fn collect_set_unique<K>(self) -> HashSet<K>
where
Self: Iterator<Item = K> + Sized,
K: Hash + Eq,
{
unsafe { collect_no_update::collect_set_unique(self) }
}
}
impl<T: ?Sized> HttmIter for T where T: Iterator {}
pub mod group_map {
use hashbrown::HashMap;
use std::hash::Hash;
use std::iter::Iterator;
pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>>
where
I: Iterator<Item = (K, V)>,
K: Hash + Eq,
{
let mut lookup: HashMap<K, Vec<V>> = HashMap::with_capacity(iter.size_hint().0);
iter.for_each(|(key, val)| match lookup.get_mut(&key) {
Some(vec_val) => {
vec_val.push(val);
}
None => {
unsafe {
lookup.insert_unique_unchecked(key, [val].into());
};
}
});
lookup
}
pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) -> HashMap<K, Vec<V>>
where
I: Iterator<Item = V>,
K: Hash + Eq,
{
into_group_map(iter.map(|v| (f(&v), v)))
}
}
pub mod collect_no_update {
use hashbrown::HashMap;
use hashbrown::HashSet;
use std::hash::Hash;
use std::iter::Iterator;
#[allow(dead_code)]
pub fn collect_map_no_update<I, K, V>(iter: I) -> HashMap<K, V>
where
I: Iterator<Item = (K, V)>,
K: Hash + Eq,
{
let mut lookup: HashMap<K, V> = HashMap::with_capacity(iter.size_hint().0);
iter.for_each(|(key, val)| {
if !lookup.contains_key(&key) {
unsafe {
lookup.insert_unique_unchecked(key, val);
};
}
});
lookup
}
#[allow(dead_code)]
pub fn collect_set_no_update<I, K>(iter: I) -> HashSet<K>
where
I: Iterator<Item = K>,
K: Hash + Eq,
{
let mut lookup: HashSet<K> = HashSet::with_capacity(iter.size_hint().0);
iter.for_each(|key| {
if !lookup.contains(&key) {
unsafe {
lookup.insert_unique_unchecked(key);
};
}
});
lookup
}
#[allow(dead_code)]
pub unsafe fn collect_map_unique<I, K, V>(iter: I) -> HashMap<K, V>
where
I: Iterator<Item = (K, V)>,
K: Hash + Eq,
{
let mut lookup: HashMap<K, V> = HashMap::with_capacity(iter.size_hint().0);
iter.for_each(|(key, value)| {
unsafe {
lookup.insert_unique_unchecked(key, value);
};
});
lookup
}
#[allow(dead_code)]
pub unsafe fn collect_set_unique<I, K>(iter: I) -> HashSet<K>
where
I: Iterator<Item = K>,
K: Hash + Eq,
{
let mut lookup: HashSet<K> = HashSet::with_capacity(iter.size_hint().0);
iter.for_each(|key| {
unsafe {
lookup.insert_unique_unchecked(key);
};
});
lookup
}
}