extern crate alloc;
use crate::metis::{Dot, DotFun, DotMap, DotSet, DotStore, Locus, Rhapsody};
mod kind;
mod reads;
mod store;
pub use kind::{Kind, KindPlurality, Kinds};
pub use reads::NodeContent;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Node<K, V> {
tag: DotFun<Kind>,
register: DotFun<V>,
children: DotMap<K, Self>,
sequence: Rhapsody,
}
impl<K, V> Default for Node<K, V> {
fn default() -> Self {
Self {
tag: DotFun::default(),
register: DotFun::default(),
children: DotMap::default(),
sequence: Rhapsody::new(),
}
}
}
impl<K: Ord + Clone, V: Clone> Node<K, V> {
#[must_use]
pub const fn new() -> Self {
Self {
tag: DotFun::new(),
register: DotFun::new(),
children: DotMap::new(),
sequence: Rhapsody::new(),
}
}
#[must_use = "the write is refused (returns false) for a straddling or duplicate dot, which the caller must handle to keep support exactness"]
pub fn write_tag(&mut self, dot: Dot, kind: Kind) -> bool {
if self.register_holds(dot) || self.children_hold(dot) || self.sequence_holds(dot) {
return false;
}
self.tag.insert(dot, kind)
}
#[must_use = "the write is refused (returns false) for a straddling or duplicate dot, which the caller must handle to keep support exactness"]
pub fn write_register(&mut self, dot: Dot, value: V) -> bool {
if self.tag_holds(dot) || self.children_hold(dot) || self.sequence_holds(dot) {
return false;
}
self.register.insert(dot, value)
}
#[must_use = "the insertion is refused (returns false) on a bottom child or a straddling dot, which the caller must handle to keep support exactness"]
pub fn insert_child(&mut self, key: K, child: Self) -> bool {
let straddles = child
.dots()
.any(|dot| self.tag_holds(dot) || self.register_holds(dot) || self.sequence_holds(dot));
if straddles {
return false;
}
self.children.insert(key, child)
}
#[must_use = "the weave is refused (returns false) for a straddling or already-woven dot, which the caller must handle to keep support exactness"]
pub fn weave(&mut self, dot: Dot, locus: Locus) -> bool {
if self.tag_holds(dot) || self.register_holds(dot) || self.children_hold(dot) {
return false;
}
self.sequence.weave(dot, locus)
}
#[must_use]
pub const fn tag(&self) -> &DotFun<Kind> {
&self.tag
}
#[must_use]
pub const fn register(&self) -> &DotFun<V> {
&self.register
}
#[must_use]
pub const fn children(&self) -> &DotMap<K, Self> {
&self.children
}
#[must_use]
pub const fn sequence(&self) -> &Rhapsody {
&self.sequence
}
#[must_use]
pub fn observed(&self) -> DotSet {
let mut set = DotSet::new();
for dot in self.dots() {
let _ = set.insert(dot);
}
set
}
fn tag_holds(&self, dot: Dot) -> bool {
self.tag.get(dot).is_some()
}
fn register_holds(&self, dot: Dot) -> bool {
self.register.get(dot).is_some()
}
fn children_hold(&self, dot: Dot) -> bool {
self.children.dots().any(|held| held == dot)
}
fn sequence_holds(&self, dot: Dot) -> bool {
self.sequence.is_visible(dot)
}
}