use alloc::vec::Vec;
use core::{
ops::{Index, IndexMut},
slice::Iter,
};
pub use self::node_id::NodeId;
use crate::styled_dom::NodeHierarchyItem;
pub type NodeDepths = Vec<(usize, NodeId)>;
pub mod node_id {
use alloc::vec::Vec;
use core::{
fmt,
ops::{Add, AddAssign},
};
#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct NodeId {
inner: usize,
}
impl NodeId {
pub const ZERO: NodeId = NodeId { inner: 0 };
#[inline(always)]
pub const fn new(value: usize) -> Self {
NodeId { inner: value }
}
#[inline]
pub const fn from_usize(value: usize) -> Option<Self> {
match value {
0 => None,
i => Some(NodeId { inner: i - 1 }),
}
}
#[inline]
pub const fn into_raw(val: &Option<Self>) -> usize {
match val {
None => 0,
Some(s) => s.inner + 1,
}
}
#[inline(always)]
pub const fn index(&self) -> usize {
self.inner
}
}
impl From<usize> for NodeId {
fn from(val: usize) -> Self {
NodeId::new(val)
}
}
impl From<NodeId> for usize {
fn from(val: NodeId) -> Self {
val.inner
}
}
impl Add<usize> for NodeId {
type Output = NodeId;
#[inline(always)]
fn add(self, other: usize) -> NodeId {
NodeId::new(self.inner + other)
}
}
impl AddAssign<usize> for NodeId {
#[inline(always)]
fn add_assign(&mut self, other: usize) {
*self = *self + other;
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl fmt::Debug for NodeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NodeId({})", self.inner)
}
}
}
#[derive(Debug, Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Node {
pub parent: Option<NodeId>,
pub previous_sibling: Option<NodeId>,
pub next_sibling: Option<NodeId>,
pub last_child: Option<NodeId>,
}
impl Node {
pub const ROOT: Node = Node {
parent: None,
previous_sibling: None,
next_sibling: None,
last_child: None,
};
#[inline]
pub const fn has_parent(&self) -> bool {
self.parent.is_some()
}
#[inline]
pub const fn has_previous_sibling(&self) -> bool {
self.previous_sibling.is_some()
}
#[inline]
pub const fn has_next_sibling(&self) -> bool {
self.next_sibling.is_some()
}
#[inline]
pub const fn has_first_child(&self) -> bool {
self.last_child.is_some()
}
#[inline]
pub const fn has_last_child(&self) -> bool {
self.last_child.is_some()
}
#[inline]
pub fn get_first_child(&self, current_node_id: NodeId) -> Option<NodeId> {
self.last_child.map(|_| current_node_id + 1)
}
}
#[derive(Debug, Default, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct NodeHierarchy {
pub internal: Vec<Node>,
}
impl NodeHierarchy {
#[inline(always)]
pub const fn new(data: Vec<Node>) -> Self {
Self { internal: data }
}
#[inline(always)]
pub fn as_ref(&self) -> NodeHierarchyRef<'_> {
NodeHierarchyRef {
internal: &self.internal[..],
}
}
}
#[derive(Debug, PartialEq, Hash, Eq)]
pub struct NodeHierarchyRef<'a> {
pub internal: &'a [Node],
}
impl<'a> NodeHierarchyRef<'a> {
#[inline(always)]
pub fn from_slice(data: &'a [Node]) -> NodeHierarchyRef<'a> {
NodeHierarchyRef { internal: data }
}
#[inline(always)]
pub fn len(&self) -> usize {
self.internal.len()
}
#[inline(always)]
pub fn get(&self, id: NodeId) -> Option<&Node> {
self.internal.get(id.index())
}
#[inline(always)]
pub fn linear_iter(&self) -> LinearIterator {
LinearIterator {
arena_len: self.len(),
position: 0,
}
}
pub fn get_parents_sorted_by_depth(&self) -> NodeDepths {
let mut non_leaf_nodes = Vec::new();
let mut current_children = vec![(0, NodeId::new(0))];
let mut next_children = Vec::new();
let mut depth = 1_usize;
loop {
for id in ¤t_children {
for child_id in id.1.children(self).filter(|id| self[*id].has_first_child()) {
next_children.push((depth, child_id));
}
}
non_leaf_nodes.extend(&mut current_children.drain(..));
if next_children.is_empty() {
break;
} else {
current_children.extend(&mut next_children.drain(..));
depth += 1;
}
}
non_leaf_nodes
}
}
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct NodeDataContainer<T> {
pub internal: Vec<T>,
}
impl<T> From<Vec<T>> for NodeDataContainer<T> {
fn from(v: Vec<T>) -> NodeDataContainer<T> {
NodeDataContainer { internal: v }
}
}
#[derive(Debug, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct NodeDataContainerRef<'a, T> {
pub internal: &'a [T],
}
#[derive(Debug, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct NodeDataContainerRefMut<'a, T> {
pub internal: &'a mut [T],
}
impl<T> Default for NodeDataContainer<T> {
fn default() -> Self {
Self {
internal: Vec::new(),
}
}
}
impl<'a> Index<NodeId> for NodeHierarchyRef<'a> {
type Output = Node;
#[inline(always)]
fn index(&self, node_id: NodeId) -> &Node {
&self.internal[node_id.index()]
}
}
impl<T> NodeDataContainer<T> {
#[inline(always)]
pub const fn new(data: Vec<T>) -> Self {
Self { internal: data }
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.internal.is_empty()
}
#[inline(always)]
pub fn as_ref(&self) -> NodeDataContainerRef<'_, T> {
NodeDataContainerRef {
internal: &self.internal[..],
}
}
#[inline(always)]
pub fn as_ref_mut(&mut self) -> NodeDataContainerRefMut<'_, T> {
NodeDataContainerRefMut {
internal: &mut self.internal[..],
}
}
#[inline(always)]
pub fn len(&self) -> usize {
self.internal.len()
}
}
impl<'a, T: 'a> NodeDataContainerRefMut<'a, T> {
#[inline(always)]
pub fn from_slice(data: &'a mut [T]) -> NodeDataContainerRefMut<'a, T> {
NodeDataContainerRefMut { internal: data }
}
}
impl<'a, T: 'a> NodeDataContainerRefMut<'a, T> {
#[inline(always)]
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> {
self.internal.get_mut(id.index())
}
}
impl<'a, T: Send + 'a> NodeDataContainerRef<'a, T> {
pub fn transform_nodeid_optional<U: Send, F: Send + Sync>(
&self,
closure: F,
) -> NodeDataContainer<U>
where
F: Fn(NodeId) -> Option<U>,
{
let len = self.len();
NodeDataContainer {
internal: (0..len)
.filter_map(|node_id| closure(NodeId::new(node_id)))
.collect::<Vec<U>>(),
}
}
}
impl<'a, T: 'a> NodeDataContainerRef<'a, T> {
#[inline(always)]
pub fn from_slice(data: &'a [T]) -> NodeDataContainerRef<'a, T> {
NodeDataContainerRef { internal: data }
}
#[inline(always)]
pub fn len(&self) -> usize {
self.internal.len()
}
#[inline(always)]
pub fn get(&self, id: NodeId) -> Option<&T> {
self.internal.get(id.index())
}
#[inline(always)]
pub fn iter(&self) -> Iter<T> {
self.internal.iter()
}
#[inline(always)]
pub fn linear_iter(&self) -> LinearIterator {
LinearIterator {
arena_len: self.len(),
position: 0,
}
}
}
impl<'a, T> Index<NodeId> for NodeDataContainerRef<'a, T> {
type Output = T;
#[inline(always)]
fn index(&self, node_id: NodeId) -> &T {
&self.internal[node_id.index()]
}
}
impl<'a, T> Index<NodeId> for NodeDataContainerRefMut<'a, T> {
type Output = T;
#[inline(always)]
fn index(&self, node_id: NodeId) -> &T {
&self.internal[node_id.index()]
}
}
impl<'a, T> IndexMut<NodeId> for NodeDataContainerRefMut<'a, T> {
#[inline(always)]
fn index_mut(&mut self, node_id: NodeId) -> &mut T {
&mut self.internal[node_id.index()]
}
}
impl NodeId {
#[inline]
pub const fn preceding_siblings<'a>(
self,
node_hierarchy: &'a NodeHierarchyRef<'a>,
) -> PrecedingSiblings<'a> {
PrecedingSiblings {
node_hierarchy,
node: Some(self),
}
}
#[inline]
pub fn children<'a>(self, node_hierarchy: &'a NodeHierarchyRef<'a>) -> Children<'a> {
Children {
node_hierarchy,
node: node_hierarchy[self].get_first_child(self),
}
}
}
macro_rules! impl_node_iterator {
($name:ident, $next:expr) => {
impl<'a> Iterator for $name<'a> {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
match self.node.take() {
Some(node) => {
self.node = $next(&self.node_hierarchy[node]);
Some(node)
}
None => None,
}
}
}
};
}
#[derive(Debug, Copy, Clone)]
pub struct LinearIterator {
arena_len: usize,
position: usize,
}
impl Iterator for LinearIterator {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
if self.arena_len < 1 || self.position > (self.arena_len - 1) {
None
} else {
let new_id = Some(NodeId::new(self.position));
self.position += 1;
new_id
}
}
}
pub struct PrecedingSiblings<'a> {
node_hierarchy: &'a NodeHierarchyRef<'a>,
node: Option<NodeId>,
}
impl_node_iterator!(PrecedingSiblings, |node: &Node| node.previous_sibling);
pub struct AzChildren<'a> {
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
node: Option<NodeId>,
}
impl<'a> Iterator for AzChildren<'a> {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
match self.node.take() {
Some(node) => {
self.node = self.node_hierarchy[node].next_sibling_id();
Some(node)
}
None => None,
}
}
}
pub struct AzReverseChildren<'a> {
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
node: Option<NodeId>,
}
impl<'a> Iterator for AzReverseChildren<'a> {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
match self.node.take() {
Some(node) => {
self.node = self.node_hierarchy[node].previous_sibling_id();
Some(node)
}
None => None,
}
}
}
impl NodeId {
pub fn get_nearest_matching_parent<'a, F>(
self,
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
predicate: F,
) -> Option<NodeId>
where
F: Fn(NodeId) -> bool,
{
let mut current_node = node_hierarchy[self].parent_id()?;
loop {
match predicate(current_node) {
true => {
return Some(current_node);
}
false => {
current_node = node_hierarchy[current_node].parent_id()?;
}
}
}
}
#[inline]
pub fn az_children_collect<'a>(
self,
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
) -> Vec<NodeId> {
self.az_children(node_hierarchy).collect()
}
#[inline]
pub fn az_children<'a>(
self,
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
) -> AzChildren<'a> {
AzChildren {
node_hierarchy,
node: node_hierarchy[self].first_child_id(self),
}
}
#[inline]
pub fn az_reverse_children<'a>(
self,
node_hierarchy: &'a NodeDataContainerRef<'a, NodeHierarchyItem>,
) -> AzReverseChildren<'a> {
AzReverseChildren {
node_hierarchy,
node: node_hierarchy[self].last_child_id(),
}
}
}
pub struct Children<'a> {
node_hierarchy: &'a NodeHierarchyRef<'a>,
node: Option<NodeId>,
}
impl_node_iterator!(Children, |node: &Node| node.next_sibling);