use std::ops::{Deref, DerefMut};
use crate::DisjointSet;
#[allow(clippy::missing_inline_in_public_items)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisjointSetVec<T> {
data: Vec<T>,
indices: DisjointSet,
}
impl<T> Default for DisjointSetVec<T> {
#[inline]
#[must_use]
fn default() -> Self {
Self::new()
}
}
impl<IntoVec, T> From<IntoVec> for DisjointSetVec<T>
where
Vec<T>: From<IntoVec>,
{
#[must_use]
#[inline]
fn from(value: IntoVec) -> Self {
let data = Vec::from(value);
let len = data.len();
Self {
data,
indices: DisjointSet::with_len(len),
}
}
}
impl<T> DisjointSetVec<T> {
#[must_use]
#[inline]
pub const fn values(&self) -> &Vec<T> {
&self.data
}
#[must_use]
#[inline]
pub const fn indices(&self) -> &DisjointSet {
&self.indices
}
#[inline]
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
data: Vec::with_capacity(capacity),
indices: DisjointSet::with_capacity(capacity),
}
}
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
data: Vec::new(),
indices: DisjointSet::new(),
}
}
#[inline]
pub fn clear(&mut self) {
self.data.clear();
self.indices.clear();
}
#[inline]
pub fn push(&mut self, value: T) -> usize {
self.data.push(value);
self.indices.add_singleton()
}
#[must_use]
#[inline]
pub fn root_of(&self, child_index: usize) -> usize {
self.indices.root_of(child_index)
}
#[must_use]
#[inline]
pub fn is_joined(&self, first_index: usize, second_index: usize) -> bool {
self.indices.is_joined(first_index, second_index)
}
#[inline]
pub fn join(&mut self, first_index: usize, second_index: usize) -> bool {
self.indices.join(first_index, second_index)
}
}
impl<T> IntoIterator for DisjointSetVec<T> {
type Item = <Vec<T> as IntoIterator>::Item;
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
#[inline]
#[must_use]
fn into_iter(self) -> Self::IntoIter {
IntoIterator::into_iter(self.data)
}
}
impl<'a, T> IntoIterator for &'a DisjointSetVec<T> {
type Item = <&'a Vec<T> as IntoIterator>::Item;
type IntoIter = <&'a Vec<T> as IntoIterator>::IntoIter;
#[inline]
#[must_use]
fn into_iter(self) -> Self::IntoIter {
IntoIterator::into_iter(&self.data)
}
}
impl<'a, T> IntoIterator for &'a mut DisjointSetVec<T> {
type Item = <&'a mut Vec<T> as IntoIterator>::Item;
type IntoIter = <&'a mut Vec<T> as IntoIterator>::IntoIter;
#[inline]
#[must_use]
fn into_iter(self) -> Self::IntoIter {
IntoIterator::into_iter(&mut self.data)
}
}
impl<T> Deref for DisjointSetVec<T> {
type Target = [T];
#[inline]
fn deref(&self) -> &[T] {
&self.data
}
}
impl<T> DerefMut for DisjointSetVec<T> {
#[inline]
fn deref_mut(&mut self) -> &mut [T] {
&mut self.data
}
}