intrex 0.2.0

Intrusive collections with items addressed by indices
Documentation
use core::cmp::Ordering;

/// Read access for binary tree link fields.
pub trait NodesLink<Index> {
    /// Get the parent of a node.
    fn node_parent(&self, node: Index) -> Option<Index>;

    /// Get the left child of a node.
    fn node_left(&self, node: Index) -> Option<Index>;

    /// Get the right child of a node.
    fn node_right(&self, node: Index) -> Option<Index>;
}

/// Forwarding implementation
impl<T, Index> NodesLink<Index> for &T
where
    T: ?Sized + NodesLink<Index>,
{
    #[inline]
    #[track_caller]
    fn node_parent(&self, node: Index) -> Option<Index> {
        (**self).node_parent(node)
    }

    #[inline]
    #[track_caller]
    fn node_left(&self, node: Index) -> Option<Index> {
        (**self).node_left(node)
    }

    #[inline]
    #[track_caller]
    fn node_right(&self, node: Index) -> Option<Index> {
        (**self).node_right(node)
    }
}

/// Forwarding implementation
impl<T, Index> NodesLink<Index> for &mut T
where
    T: ?Sized + NodesLink<Index>,
{
    #[inline]
    #[track_caller]
    fn node_parent(&self, node: Index) -> Option<Index> {
        (**self).node_parent(node)
    }

    #[inline]
    #[track_caller]
    fn node_left(&self, node: Index) -> Option<Index> {
        (**self).node_left(node)
    }

    #[inline]
    #[track_caller]
    fn node_right(&self, node: Index) -> Option<Index> {
        (**self).node_right(node)
    }
}

/// Write access for binary tree link fields.
pub trait NodesLinkMut<Index>: NodesLink<Index> {
    /// Set the parent of a node.
    fn set_node_parent(&mut self, node: Index, parent: Option<Index>);

    /// Set the left child of a node.
    fn set_node_left(&mut self, node: Index, left: Option<Index>);

    /// Set the right child of a node.
    fn set_node_right(&mut self, node: Index, right: Option<Index>);
}

/// Forwarding implementation
impl<T, Index> NodesLinkMut<Index> for &mut T
where
    T: ?Sized + NodesLinkMut<Index>,
{
    #[inline]
    #[track_caller]
    fn set_node_parent(&mut self, node: Index, parent: Option<Index>) {
        (**self).set_node_parent(node, parent)
    }

    #[inline]
    #[track_caller]
    fn set_node_left(&mut self, node: Index, left: Option<Index>) {
        (**self).set_node_left(node, left)
    }

    #[inline]
    #[track_caller]
    fn set_node_right(&mut self, node: Index, right: Option<Index>) {
        (**self).set_node_right(node, right)
    }
}

/// Compare two nodes by their stored keys.
pub trait NodesCmp<Index> {
    /// Compare two nodes by their stored keys
    /// ([`Ord::cmp`]`(lhs.key, rhs.key)`).
    fn cmp_nodes(&self, lhs: Index, rhs: Index) -> Ordering;
}

/// Forwarding implementation
impl<T, Index> NodesCmp<Index> for &T
where
    T: ?Sized + NodesCmp<Index>,
{
    #[inline]
    #[track_caller]
    fn cmp_nodes(&self, lhs: Index, rhs: Index) -> Ordering {
        (**self).cmp_nodes(lhs, rhs)
    }
}

/// Forwarding implementation
impl<T, Index> NodesCmp<Index> for &mut T
where
    T: ?Sized + NodesCmp<Index>,
{
    #[inline]
    #[track_caller]
    fn cmp_nodes(&self, lhs: Index, rhs: Index) -> Ordering {
        (**self).cmp_nodes(lhs, rhs)
    }
}

/// Compare a node against a given search key.
pub trait NodesCmpKey<Index, Key> {
    /// Compare a node's key against a given search key
    /// ([`Ord::cmp`]`(node.key, key)`).
    fn cmp_node_key(&self, node: Index, key: &Key) -> Ordering;
}

/// Forwarding implementation
impl<T, Index, Key> NodesCmpKey<Index, Key> for &T
where
    T: ?Sized + NodesCmpKey<Index, Key>,
{
    #[inline]
    #[track_caller]
    fn cmp_node_key(&self, node: Index, key: &Key) -> Ordering {
        (**self).cmp_node_key(node, key)
    }
}

/// Forwarding implementation
impl<T, Index, Key> NodesCmpKey<Index, Key> for &mut T
where
    T: ?Sized + NodesCmpKey<Index, Key>,
{
    #[inline]
    #[track_caller]
    fn cmp_node_key(&self, node: Index, key: &Key) -> Ordering {
        (**self).cmp_node_key(node, key)
    }
}