use std::collections::HashSet;
use rand::Rng as _;
use rostra_core::ShortEventId;
pub(crate) fn representative_head(heads: &HashSet<ShortEventId>) -> Option<ShortEventId> {
heads.iter().copied().min()
}
pub(crate) fn sample_head(heads: &HashSet<ShortEventId>) -> Option<ShortEventId> {
sample_head_with(heads, |len| rand::rng().random_range(0..len))
}
fn sample_head_with(
heads: &HashSet<ShortEventId>,
choose_index: impl FnOnce(usize) -> usize,
) -> Option<ShortEventId> {
if heads.is_empty() {
return None;
}
heads.iter().nth(choose_index(heads.len())).copied()
}
pub(crate) fn sorted_heads(heads: &HashSet<ShortEventId>) -> Vec<ShortEventId> {
let mut heads: Vec<_> = heads.iter().copied().collect();
heads.sort_unstable();
heads
}
#[cfg(test)]
mod tests;