use std::{
ops::{Index, IndexMut},
slice::{Iter, IterMut},
};
pub use self::node_id::NodeId;
pub type NodeDepths = Vec<(usize, NodeId)>;
mod node_id {
use std::{
fmt,
num::NonZeroUsize,
ops::{Add, AddAssign},
};
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct NodeId {
index: NonZeroUsize,
}
impl NodeId {
pub const ZERO: NodeId = NodeId { index: unsafe { NonZeroUsize::new_unchecked(1) } };
#[inline(always)]
pub fn new(value: usize) -> Self {
NodeId { index: unsafe { NonZeroUsize::new_unchecked(value + 1) } }
}
#[inline(always)]
pub fn index(&self) -> usize {
self.index.get() - 1
}
#[inline]
pub fn range(start: Self, end: Self) -> Vec<NodeId> {
(start.index()..end.index()).map(NodeId::new).collect()
}
}
impl Add<usize> for NodeId {
type Output = NodeId;
#[inline(always)]
fn add(self, other: usize) -> NodeId {
NodeId::new(self.index() + 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.index())
}
}
impl fmt::Debug for NodeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NodeId({})", self.index())
}
}
}
#[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 first_child: Option<NodeId>,
pub last_child: Option<NodeId>,
}
pub const ROOT_NODE: Node = Node {
parent: None,
previous_sibling: None,
next_sibling: None,
first_child: None,
last_child: None,
};
impl Node {
pub const ROOT: Node = ROOT_NODE;
#[inline]
pub fn has_parent(&self) -> bool { self.parent.is_some() }
#[inline]
pub fn has_previous_sibling(&self) -> bool { self.previous_sibling.is_some() }
#[inline]
pub fn has_next_sibling(&self) -> bool { self.next_sibling.is_some() }
#[inline]
pub fn has_first_child(&self) -> bool { self.first_child.is_some() }
#[inline]
pub fn has_last_child(&self) -> bool { self.last_child.is_some() }
}
#[derive(Debug, Default, Clone, PartialEq, Hash, Eq)]
pub struct Arena<T> {
pub node_hierarchy: NodeHierarchy,
pub node_data: NodeDataContainer<T>,
}
#[derive(Debug, Default, Clone, PartialEq, Hash, Eq)]
pub struct NodeHierarchy {
pub internal: Vec<Node>,
}
impl NodeHierarchy {
#[inline]
pub const fn new(data: Vec<Node>) -> Self {
Self {
internal: data,
}
}
#[inline]
pub fn len(&self) -> usize {
self.internal.len()
}
#[inline]
pub fn get(&self, id: NodeId) -> Option<&Node> {
self.internal.get(id.index())
}
#[inline]
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;
loop {
for id in ¤t_children {
for child_id in id.1.children(self).filter(|id| self[*id].first_child.is_some()) {
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
}
#[inline]
pub fn subtree_len(&self, parent_id: NodeId) -> usize {
let self_item_index = parent_id.index();
let next_item_index = match self[parent_id].next_sibling {
None => self.len(),
Some(s) => s.index(),
};
next_item_index - self_item_index - 1
}
#[inline]
pub fn get_index_in_parent(&self, node_id: NodeId) -> usize {
node_id.preceding_siblings(&self).count() - 1
}
}
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct NodeDataContainer<T> {
pub internal: Vec<T>,
}
impl<T> Default for NodeDataContainer<T> {
fn default() -> Self {
Self { internal: Vec::new() }
}
}
impl Index<NodeId> for NodeHierarchy {
type Output = Node;
#[inline]
fn index(&self, node_id: NodeId) -> &Node {
unsafe { self.internal.get_unchecked(node_id.index()) }
}
}
impl IndexMut<NodeId> for NodeHierarchy {
#[inline]
fn index_mut(&mut self, node_id: NodeId) -> &mut Node {
unsafe { self.internal.get_unchecked_mut(node_id.index()) }
}
}
impl<T> NodeDataContainer<T> {
#[inline]
pub const fn new(data: Vec<T>) -> Self {
Self { internal: data }
}
pub fn len(&self) -> usize { self.internal.len() }
pub fn transform<U, F>(&self, mut closure: F) -> NodeDataContainer<U> where F: FnMut(&T, NodeId) -> U {
NodeDataContainer {
internal: self.internal.iter().enumerate().map(|(node_id, node)| closure(node, NodeId::new(node_id))).collect(),
}
}
pub fn get(&self, id: NodeId) -> Option<&T> {
self.internal.get(id.index())
}
pub fn iter(&self) -> Iter<T> {
self.internal.iter()
}
pub fn iter_mut(&mut self) -> IterMut<T> {
self.internal.iter_mut()
}
pub fn linear_iter(&self) -> LinearIterator {
LinearIterator {
arena_len: self.len(),
position: 0,
}
}
}
impl<T> Index<NodeId> for NodeDataContainer<T> {
type Output = T;
#[inline]
fn index(&self, node_id: NodeId) -> &T {
unsafe { self.internal.get_unchecked(node_id.index()) }
}
}
impl<T> IndexMut<NodeId> for NodeDataContainer<T> {
#[inline]
fn index_mut(&mut self, node_id: NodeId) -> &mut T {
unsafe { self.internal.get_unchecked_mut(node_id.index()) }
}
}
impl<T> Arena<T> {
#[inline]
pub fn new() -> Arena<T> {
Arena {
node_hierarchy: NodeHierarchy { internal: Vec::new() },
node_data: NodeDataContainer { internal: Vec::<T>::new() },
}
}
#[inline]
pub fn with_capacity(cap: usize) -> Arena<T> {
Arena {
node_hierarchy: NodeHierarchy { internal: Vec::with_capacity(cap) },
node_data: NodeDataContainer { internal: Vec::<T>::with_capacity(cap) },
}
}
#[inline]
pub fn new_node(&mut self, data: T) -> NodeId {
let next_index = self.node_hierarchy.len();
self.node_hierarchy.internal.push(Node {
parent: None,
first_child: None,
last_child: None,
previous_sibling: None,
next_sibling: None,
});
self.node_data.internal.push(data);
NodeId::new(next_index)
}
#[inline]
pub fn len(&self) -> usize {
self.node_hierarchy.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn linear_iter(&self) -> LinearIterator {
LinearIterator {
arena_len: self.node_hierarchy.len(),
position: 0,
}
}
#[inline]
pub fn append_arena(&mut self, other: &mut Arena<T>) {
self.node_hierarchy.internal.append(&mut other.node_hierarchy.internal);
self.node_data.internal.append(&mut other.node_data.internal);
}
#[inline]
pub fn transform<U, F>(&self, closure: F) -> Arena<U> where F: Fn(&T, NodeId) -> U {
Arena {
node_hierarchy: self.node_hierarchy.clone(),
node_data: self.node_data.transform(closure),
}
}
}
impl NodeId {
#[inline]
pub const fn ancestors(self, node_hierarchy: &NodeHierarchy) -> Ancestors {
Ancestors {
node_hierarchy,
node: Some(self),
}
}
#[inline]
pub const fn preceding_siblings(self, node_hierarchy: &NodeHierarchy) -> PrecedingSiblings {
PrecedingSiblings {
node_hierarchy,
node: Some(self),
}
}
#[inline]
pub const fn following_siblings(self, node_hierarchy: &NodeHierarchy) -> FollowingSiblings {
FollowingSiblings {
node_hierarchy,
node: Some(self),
}
}
#[inline]
pub fn children(self, node_hierarchy: &NodeHierarchy) -> Children {
Children {
node_hierarchy,
node: node_hierarchy[self].first_child,
}
}
#[inline]
pub fn reverse_children(self, node_hierarchy: &NodeHierarchy) -> ReverseChildren {
ReverseChildren {
node_hierarchy,
node: node_hierarchy[self].last_child,
}
}
#[inline]
pub const fn descendants(self, node_hierarchy: &NodeHierarchy) -> Descendants {
Descendants(self.traverse(node_hierarchy))
}
#[inline]
pub const fn traverse(self, node_hierarchy: &NodeHierarchy) -> Traverse {
Traverse {
node_hierarchy,
root: self,
next: Some(NodeEdge::Start(self)),
}
}
#[inline]
pub const fn reverse_traverse(self, node_hierarchy: &NodeHierarchy) -> ReverseTraverse {
ReverseTraverse {
node_hierarchy,
root: self,
next: Some(NodeEdge::End(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 Ancestors<'a> {
node_hierarchy: &'a NodeHierarchy,
node: Option<NodeId>,
}
impl_node_iterator!(Ancestors, |node: &Node| node.parent);
pub struct PrecedingSiblings<'a> {
node_hierarchy: &'a NodeHierarchy,
node: Option<NodeId>,
}
impl_node_iterator!(PrecedingSiblings, |node: &Node| node.previous_sibling);
pub struct FollowingSiblings<'a> {
node_hierarchy: &'a NodeHierarchy,
node: Option<NodeId>,
}
impl_node_iterator!(FollowingSiblings, |node: &Node| node.next_sibling);
pub struct Children<'a> {
node_hierarchy: &'a NodeHierarchy,
node: Option<NodeId>,
}
impl_node_iterator!(Children, |node: &Node| node.next_sibling);
pub struct ReverseChildren<'a> {
node_hierarchy: &'a NodeHierarchy,
node: Option<NodeId>,
}
impl_node_iterator!(ReverseChildren, |node: &Node| node.previous_sibling);
pub struct Descendants<'a>(Traverse<'a>);
impl<'a> Iterator for Descendants<'a> {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
loop {
match self.0.next() {
Some(NodeEdge::Start(node)) => return Some(node),
Some(NodeEdge::End(_)) => {}
None => return None
}
}
}
}
#[derive(Debug, Clone)]
pub enum NodeEdge<T> {
Start(T),
End(T),
}
impl<T> NodeEdge<T> {
pub fn inner_value(self) -> T {
use self::NodeEdge::*;
match self {
Start(t) => t,
End(t) => t,
}
}
}
pub struct Traverse<'a> {
node_hierarchy: &'a NodeHierarchy,
root: NodeId,
next: Option<NodeEdge<NodeId>>,
}
impl<'a> Iterator for Traverse<'a> {
type Item = NodeEdge<NodeId>;
fn next(&mut self) -> Option<NodeEdge<NodeId>> {
match self.next.take() {
Some(item) => {
self.next = match item {
NodeEdge::Start(node) => {
match self.node_hierarchy[node].first_child {
Some(first_child) => Some(NodeEdge::Start(first_child)),
None => Some(NodeEdge::End(node.clone()))
}
}
NodeEdge::End(node) => {
if node == self.root {
None
} else {
match self.node_hierarchy[node].next_sibling {
Some(next_sibling) => Some(NodeEdge::Start(next_sibling)),
None => match self.node_hierarchy[node].parent {
Some(parent) => Some(NodeEdge::End(parent)),
None => None
}
}
}
}
};
Some(item)
}
None => None
}
}
}
pub struct ReverseTraverse<'a> {
node_hierarchy: &'a NodeHierarchy,
root: NodeId,
next: Option<NodeEdge<NodeId>>,
}
impl<'a> Iterator for ReverseTraverse<'a> {
type Item = NodeEdge<NodeId>;
fn next(&mut self) -> Option<NodeEdge<NodeId>> {
match self.next.take() {
Some(item) => {
self.next = match item {
NodeEdge::End(node) => {
match self.node_hierarchy[node].last_child {
Some(last_child) => Some(NodeEdge::End(last_child)),
None => Some(NodeEdge::Start(node.clone()))
}
}
NodeEdge::Start(node) => {
if node == self.root {
None
} else {
match self.node_hierarchy[node].previous_sibling {
Some(previous_sibling) => Some(NodeEdge::End(previous_sibling)),
None => match self.node_hierarchy[node].parent {
Some(parent) => Some(NodeEdge::Start(parent)),
None => None
}
}
}
}
};
Some(item)
}
None => None
}
}
}