aph_disjoint_set 0.1.0

Disjoint set implementation with optimized memory usage and ability to detach elements.
Documentation
use crate::macros::{bits_enum, for_current_bitness};
use crate::tag_type::TagType;
use crate::Root;

type SliceRef<'a, T> = &'a [T];

/// Readonly view to the content of [`DisjointSet`](crate::DisjointSet).
/// Useful for quering same disjoint set from many threads
/// using [rayon](https://crates.io/crates/rayon) or [`std::thread::scope`].
/// See [`DisjointSet::make_ro_view`](crate::DisjointSet::make_ro_view) for examples
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy)]
pub struct DisjointSetRoView<'a>(pub(crate) bits_enum!(SliceRef, 'a));

#[allow(clippy::len_without_is_empty)]
impl DisjointSetRoView<'_> {
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        for_current_bitness!(self.0;arr;arr.len())
    }

    /// Similar to [`DisjointSet::get_root`](crate::DisjointSet::get_root).
    /// Takes *O(1)* time.
    /// # Panics
    /// If index is out of bounds.
    #[must_use]
    #[inline]
    pub fn get_root(&self, idx: usize) -> Root {
        // Because readonly views made from compressed paths,
        // we can just return value by index.
        let root = for_current_bitness!(self.0;a;a[idx].as_u());
        Root(root)
    }

    /// Similar to [`DisjointSet::is_united`](crate::DisjointSet::is_united).
    /// Takes *O(1)* time.
    /// # Panics
    /// If any index is out of bounds.
    #[must_use]
    #[inline]
    pub fn is_united(&self, idx0: usize, idx1: usize) -> bool {
        idx0 == idx1 || self.get_root(idx0) == self.get_root(idx1)
    }
}