use core::iter::FusedIterator;
use crate::flavor::Flavor;
use crate::links::Links;
use crate::node::{Node, SkipTokens};
use crate::pointer::Pointer;
pub struct Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
tree: &'a [Links<T, F::Index, F::Pointer>],
links: Option<&'a Links<T, F::Index, F::Pointer>>,
}
impl<'a, T, F> Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
pub(crate) const fn new(
tree: &'a [Links<T, F::Index, F::Pointer>],
links: &'a Links<T, F::Index, F::Pointer>,
) -> Self {
Self {
tree,
links: Some(links),
}
}
#[must_use]
pub const fn skip_tokens(self) -> SkipTokens<Self> {
SkipTokens::new(self)
}
#[inline]
pub fn next_node(&mut self) -> Option<Node<'a, T, F>> {
self.find(|n| n.has_children())
}
}
impl<'a, T, F> Iterator for Siblings<'a, T, F>
where
T: Copy,
F: Flavor,
{
type Item = Node<'a, T, F>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let links = self.links.take()?;
self.links = links.next.and_then(|id| self.tree.get(id.get()));
Some(Node::new(links, self.tree))
}
}
impl<T, F> FusedIterator for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
}
impl<T, F> Clone for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn clone(&self) -> Self {
Self {
tree: self.tree,
links: self.links,
}
}
}
impl<T, F> Default for Siblings<'_, T, F>
where
T: Copy,
F: Flavor,
{
#[inline]
fn default() -> Self {
Self {
tree: &[],
links: None,
}
}
}