use sp_std::{cell::RefCell, fmt, prelude::*, rc::Rc};
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
pub(crate) enum NodeRole {
Voter,
Target,
}
pub(crate) type RefCellOf<T> = Rc<RefCell<T>>;
pub(crate) type NodeRef<A> = RefCellOf<Node<A>>;
#[derive(PartialOrd, Ord, Clone, PartialEq, Eq)]
pub(crate) struct NodeId<A> {
pub who: A,
pub role: NodeRole,
}
impl<A> NodeId<A> {
pub fn from(who: A, role: NodeRole) -> Self {
Self { who, role }
}
}
#[cfg(feature = "std")]
impl<A: fmt::Debug> sp_std::fmt::Debug for NodeId<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> sp_std::fmt::Result {
write!(
f,
"Node({:?}, {:?})",
self.who,
if self.role == NodeRole::Voter { "V" } else { "T" }
)
}
}
#[derive(Clone)]
pub(crate) struct Node<A> {
pub(crate) id: NodeId<A>,
pub(crate) parent: Option<NodeRef<A>>,
}
impl<A: PartialEq> PartialEq for Node<A> {
fn eq(&self, other: &Node<A>) -> bool {
self.id == other.id
}
}
impl<A: PartialEq> Eq for Node<A> {}
#[cfg(feature = "std")]
impl<A: fmt::Debug + Clone> fmt::Debug for Node<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?} --> {:?})", self.id, self.parent.as_ref().map(|p| p.borrow().id.clone()))
}
}
impl<A: PartialEq + Eq + Clone + fmt::Debug> Node<A> {
pub fn new(id: NodeId<A>) -> Node<A> {
Self { id, parent: None }
}
pub fn is_parent_of(who: &NodeRef<A>, other: &NodeRef<A>) -> bool {
if who.borrow().parent.is_none() {
return false
}
who.borrow().parent.as_ref() == Some(other)
}
pub fn remove_parent(who: &NodeRef<A>) {
who.borrow_mut().parent = None;
}
pub fn set_parent_of(who: &NodeRef<A>, parent: &NodeRef<A>) {
who.borrow_mut().parent = Some(parent.clone());
}
pub fn root(start: &NodeRef<A>) -> (NodeRef<A>, Vec<NodeRef<A>>) {
let mut parent_path: Vec<NodeRef<A>> = Vec::new();
let mut visited: Vec<NodeRef<A>> = Vec::new();
parent_path.push(start.clone());
visited.push(start.clone());
let mut current = start.clone();
while let Some(ref next_parent) = current.clone().borrow().parent {
if visited.contains(next_parent) {
break
}
parent_path.push(next_parent.clone());
current = next_parent.clone();
visited.push(current.clone());
}
(current, parent_path)
}
pub fn into_ref(self) -> NodeRef<A> {
Rc::from(RefCell::from(self))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn id(i: u32) -> NodeId<u32> {
NodeId::from(i, NodeRole::Target)
}
#[test]
fn basic_create_works() {
let node = Node::new(id(10));
assert_eq!(node, Node { id: NodeId { who: 10, role: NodeRole::Target }, parent: None });
}
#[test]
fn set_parent_works() {
let a = Node::new(id(10)).into_ref();
let b = Node::new(id(20)).into_ref();
assert_eq!(a.borrow().parent, None);
Node::set_parent_of(&a, &b);
assert_eq!(*a.borrow().parent.as_ref().unwrap(), b);
}
#[test]
fn get_root_singular() {
let a = Node::new(id(1)).into_ref();
assert_eq!(Node::root(&a), (a.clone(), vec![a.clone()]));
}
#[test]
fn get_root_works() {
let a = Node::new(id(1)).into_ref();
let b = Node::new(id(2)).into_ref();
let c = Node::new(id(3)).into_ref();
let d = Node::new(id(4)).into_ref();
let e = Node::new(id(5)).into_ref();
let f = Node::new(id(6)).into_ref();
Node::set_parent_of(&c, &b);
Node::set_parent_of(&b, &a);
Node::set_parent_of(&e, &a);
Node::set_parent_of(&a, &d);
assert_eq!(Node::root(&e), (d.clone(), vec![e.clone(), a.clone(), d.clone()]));
assert_eq!(Node::root(&a), (d.clone(), vec![a.clone(), d.clone()]));
assert_eq!(Node::root(&c), (d.clone(), vec![c.clone(), b.clone(), a.clone(), d.clone()]));
Node::set_parent_of(&a, &f);
assert_eq!(Node::root(&a), (f.clone(), vec![a.clone(), f.clone()]));
assert_eq!(Node::root(&c), (f.clone(), vec![c.clone(), b.clone(), a.clone(), f.clone()]));
}
#[test]
fn get_root_on_cycle() {
let a = Node::new(id(1)).into_ref();
let b = Node::new(id(2)).into_ref();
let c = Node::new(id(3)).into_ref();
Node::set_parent_of(&a, &b);
Node::set_parent_of(&b, &c);
Node::set_parent_of(&c, &a);
let (root, path) = Node::root(&a);
assert_eq!(root, c);
assert_eq!(path.clone(), vec![a.clone(), b.clone(), c.clone()]);
}
#[test]
fn get_root_on_cycle_2() {
let a = Node::new(id(1)).into_ref();
let b = Node::new(id(2)).into_ref();
let c = Node::new(id(3)).into_ref();
Node::set_parent_of(&a, &b);
Node::set_parent_of(&b, &c);
Node::set_parent_of(&c, &b);
let (root, path) = Node::root(&a);
assert_eq!(root, c);
assert_eq!(path.clone(), vec![a.clone(), b.clone(), c.clone()]);
}
#[test]
fn node_cmp_stack_overflows_on_non_unique_elements() {
let a = Node::new(id(1)).into_ref();
let b = Node::new(id(2)).into_ref();
let c = Node::new(id(3)).into_ref();
Node::set_parent_of(&a, &b);
Node::set_parent_of(&b, &c);
Node::set_parent_of(&c, &a);
Node::root(&a);
}
}