#![warn(missing_docs)]
use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;
pub type FxHashMap<K, V> = std::collections::HashMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxHashSet<T> = std::collections::HashSet<T, BuildHasherDefault<FxHasher>>;
pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>;
pub use indexmap::{IndexMap, IndexSet};
pub use parking_lot::{Mutex, RwLock};
pub use smallvec::SmallVec;
pub type TinyVec<T> = SmallVec<[T; 4]>;
pub type SmallVec8<T> = SmallVec<[T; 8]>;
pub trait FxHashMapExt<K, V> {
fn new() -> Self;
fn with_capacity(capacity: usize) -> Self;
}
impl<K, V> FxHashMapExt<K, V> for FxHashMap<K, V> {
fn new() -> Self {
Self::default()
}
fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, BuildHasherDefault::default())
}
}
pub trait FxHashSetExt<T> {
fn new() -> Self;
fn with_capacity(capacity: usize) -> Self;
}
impl<T> FxHashSetExt<T> for FxHashSet<T> {
fn new() -> Self {
Self::default()
}
fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, BuildHasherDefault::default())
}
}
#[derive(Debug, Clone)]
pub struct WorkQueue<T> {
queue: std::collections::VecDeque<T>,
seen: FxHashSet<T>,
}
impl<T: std::hash::Hash + Eq + Clone> WorkQueue<T> {
#[must_use]
pub fn new() -> Self {
Self {
queue: std::collections::VecDeque::new(),
seen: FxHashSet::default(),
}
}
pub fn push(&mut self, item: T) -> bool {
if self.seen.insert(item.clone()) {
self.queue.push_back(item);
true
} else {
false
}
}
pub fn pop(&mut self) -> Option<T> {
self.queue.pop_front()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
}
impl<T: std::hash::Hash + Eq + Clone> Default for WorkQueue<T> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct FrozenMap<K, V> {
map: FxHashMap<K, V>,
}
impl<K: std::hash::Hash + Eq, V> FrozenMap<K, V> {
#[must_use]
pub fn new(map: FxHashMap<K, V>) -> Self {
Self { map }
}
#[must_use]
pub fn get(&self, key: &K) -> Option<&V> {
self.map.get(key)
}
#[must_use]
pub fn contains_key(&self, key: &K) -> bool {
self.map.contains_key(key)
}
#[must_use]
pub fn len(&self) -> usize {
self.map.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
self.map.iter()
}
}
impl<K: std::hash::Hash + Eq, V> FromIterator<(K, V)> for FrozenMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
Self::new(iter.into_iter().collect())
}
}
#[derive(Debug, Clone)]
pub struct UnionFind {
parent: Vec<usize>,
rank: Vec<usize>,
}
impl UnionFind {
#[must_use]
pub fn new(n: usize) -> Self {
Self {
parent: (0..n).collect(),
rank: vec![0; n],
}
}
pub fn find(&mut self, x: usize) -> usize {
if self.parent[x] != x {
self.parent[x] = self.find(self.parent[x]);
}
self.parent[x]
}
pub fn union(&mut self, x: usize, y: usize) {
let rx = self.find(x);
let ry = self.find(y);
if rx == ry {
return;
}
match self.rank[rx].cmp(&self.rank[ry]) {
std::cmp::Ordering::Less => self.parent[rx] = ry,
std::cmp::Ordering::Greater => self.parent[ry] = rx,
std::cmp::Ordering::Equal => {
self.parent[ry] = rx;
self.rank[rx] += 1;
}
}
}
pub fn same_set(&mut self, x: usize, y: usize) -> bool {
self.find(x) == self.find(y)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_work_queue() {
let mut wq: WorkQueue<i32> = WorkQueue::new();
assert!(wq.push(1));
assert!(wq.push(2));
assert!(!wq.push(1));
assert_eq!(wq.pop(), Some(1));
assert_eq!(wq.pop(), Some(2));
assert_eq!(wq.pop(), None);
}
#[test]
fn test_union_find() {
let mut uf = UnionFind::new(5);
uf.union(0, 1);
uf.union(2, 3);
uf.union(1, 3);
assert!(uf.same_set(0, 3));
assert!(!uf.same_set(0, 4));
}
}