use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::iter::once;
use std::usize;
fn get_and_redirect(table: &mut [usize], mut idx: usize) -> usize {
while idx != table[idx] {
table[idx] = table[table[idx]];
idx = table[idx];
}
idx
}
#[must_use]
pub fn separate_components<N>(groups: &[Vec<N>]) -> (HashMap<&N, usize>, Vec<usize>)
where
N: Hash + Eq,
{
let mut table = (0..groups.len()).collect::<Vec<_>>();
let mut indices = HashMap::new();
for (mut group_index, group) in groups.iter().enumerate() {
if group.is_empty() {
table[group_index] = usize::max_value();
}
for element in group {
match indices.entry(element) {
Occupied(e) => {
table[group_index] = get_and_redirect(&mut table, *e.get());
group_index = table[group_index];
}
Vacant(e) => {
e.insert(group_index);
}
}
}
}
for group_index in indices.values_mut() {
*group_index = get_and_redirect(&mut table, *group_index);
}
for group_index in 0..groups.len() {
if table[group_index] != usize::max_value() {
let target = get_and_redirect(&mut table, group_index);
table[group_index] = target;
}
}
(indices, table)
}
#[must_use]
pub fn components<N>(groups: &[Vec<N>]) -> Vec<HashSet<N>>
where
N: Clone + Hash + Eq,
{
let (_, gindices) = separate_components(groups);
let mut gb = gindices
.into_iter()
.enumerate()
.filter(|&(_, n)| n != usize::max_value())
.collect::<Vec<_>>();
gb.sort_unstable_by(|&(_, n1), &(_, n2)| Ord::cmp(&n1, &n2));
let mut key = None;
let mut res = vec![];
for (group_index, k) in gb {
if key != Some(k) {
res.push(HashSet::default());
key = Some(k);
}
if let Some(set) = res.last_mut() {
for item in &groups[group_index] {
set.insert(item.clone());
}
}
}
res
}
pub fn connected_components<N, FN, IN>(starts: &[N], mut neighbours: FN) -> Vec<HashSet<N>>
where
N: Clone + Hash + Eq,
FN: FnMut(&N) -> IN,
IN: IntoIterator<Item = N>,
{
components(
&starts
.iter()
.map(|s| neighbours(s).into_iter().chain(once(s.clone())).collect())
.collect::<Vec<_>>(),
)
}
#[allow(clippy::implicit_hasher)]
#[must_use]
pub fn component_index<N>(components: &[HashSet<N>]) -> HashMap<N, usize>
where
N: Clone + Hash + Eq,
{
let mut assoc = HashMap::with_capacity(components.len());
for (i, c) in components.iter().enumerate() {
for n in c {
assoc.insert(n.clone(), i);
}
}
assoc
}