pub struct Node<T: Debug + Clone> {
pub inner: Rc<HedelCell<NodeInner<T>>>,
}
Expand description
Wraps the inner value with an Rc<HedelCell<_>> pointer.
allowing for multiple owners and a mutable NodeInner
Fields§
§inner: Rc<HedelCell<NodeInner<T>>>
Implementations§
Source§impl<T: Debug + Clone> Node<T>
impl<T: Debug + Clone> Node<T>
Sourcepub fn new(content: T) -> Self
pub fn new(content: T) -> Self
Default constructor. Notice how it builds a stand-alone node, not pointing to any parent, any sibling and any child, but owning the content
Sourcepub fn downgrade(&self) -> WeakNode<T>
pub fn downgrade(&self) -> WeakNode<T>
A WeakNode
has to be built by downgrading Node
following the same logic to get a Weak
from a Rc
Sourcepub fn try_get(&self) -> Result<RefHedel<'_, NodeInner<T>>, HedelError>
pub fn try_get(&self) -> Result<RefHedel<'_, NodeInner<T>>, HedelError>
Get access to NodeInner
or return HedelError
in case
the runtime borrow checker in HedelCell
doesn’t allow to get a shared reference.
Sourcepub fn get(&self) -> RefHedel<'_, NodeInner<T>>
pub fn get(&self) -> RefHedel<'_, NodeInner<T>>
Get access to NodeInner
or panic! in case
the runtime borrow checker in HedelCell
doesn’t allow to get a shared reference.
Sourcepub fn try_get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>
pub fn try_get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>
Get mutable access to NodeInner
or return HedelError
in case
the runtime borrow checker in HedelCell
doesn’t allow to get a mutable reference.
Sourcepub fn get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>
pub fn get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>
Get mutable access to NodeInner
or panic! in case
the runtime borrow checker in HedelCell
doesn’t allow to get a mutable reference.
Sourcepub fn prev(&self) -> Option<Node<T>>
pub fn prev(&self) -> Option<Node<T>>
Get the previous Node
in horizontal direction by upgrading it.
Sourcepub fn parent(&self) -> Option<Node<T>>
pub fn parent(&self) -> Option<Node<T>>
Get the parent Node
in vertical direction by upgrading it.
pub fn to_content(self) -> T
Sourcepub fn free(&self)
pub fn free(&self)
Re-set the parent
, next
and prev
fields on the Node
.
WARNING: this is meant to be used by NodeCollection::free
after
the HedelDetach::detach_preserve
function. Refer to it’s documentation
for an usage example.
If you want to detach a single Node while iterating, most of the times
you can simply break the loop and use HedelDetach::detach
.
WARNING: using this function instead of HedelDetach::detach
might break the linked list.
Trait Implementations§
Source§impl<T: Debug + Clone> AppendNode<T> for Node<T>
impl<T: Debug + Clone> AppendNode<T> for Node<T>
Source§fn append_next(&self, node: Node<T>)
fn append_next(&self, node: Node<T>)
Inserts a new node right after &self
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
fn main() {
let node = node!(1, node!(2));
let two = node.child().unwrap();
two.append_next(node!(3));
assert_eq!(node.get_last_child().unwrap().to_content(), 3);
}
Source§fn append_prev(&self, node: Node<T>)
fn append_prev(&self, node: Node<T>)
Inserts a new node right before &self
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
fn main() {
let node = node!(1, node!(2));
let two = node.child().unwrap();
two.append_prev(node!(3));
assert_eq!(node.child().unwrap().to_content(), 3);
}
Source§fn append_child(&self, node: Node<T>)
fn append_child(&self, node: Node<T>)
Inserts a new node right after the last child of &self
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
fn main() {
let node = node!(1, node!(2));
node.append_child(node!(3));
println!("{}", node.get_last_child().unwrap().to_content());
}
Source§impl<T: Debug + Clone, I: CompareNode<T>> CollectNode<T, I> for Node<T>
impl<T: Debug + Clone, I: CompareNode<T>> CollectNode<T, I> for Node<T>
Source§fn collect_siblings(&self, ident: &I) -> NodeCollection<T>
fn collect_siblings(&self, ident: &I) -> NodeCollection<T>
Given an identifier of type implementing CompareNode
this iterates over all the nodes
in the linked list horizontally ( iterates over the siblings, previous and next ),
and compare every node. The nodes satisfying the identifier get collected into a NodeCollection
.
Source§fn collect_children(&self, ident: &I) -> NodeCollection<T>
fn collect_children(&self, ident: &I) -> NodeCollection<T>
Given an identifier of type implementing CompareNode
this iterates over all the nodes that stand
lower and deeper in the linked list. Every child satysfying the identifier get collected into a NodeCollection
Source§fn collect_linked_list(&self, ident: &I) -> NodeCollection<T>
fn collect_linked_list(&self, ident: &I) -> NodeCollection<T>
Given an identifier of type implementing CompareNode
this iterates over all the nodes in the
linked list both horizontally and vertically ( iterates horizontally in each hierarchical level,
up to the top parent and down to the deepest child also
iterating vertically and horizontally for each layer of the children ).
The nodes satisfying the identifier get collected into a NodeCollection
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
pub enum NumIdent {
Equal(i32),
BiggerThan(i32),
SmallerThan(i32)
}
impl CompareNode<i32> for NumIdent {
fn compare(&self, node: &Node<i32>) -> bool {
match &self {
NumIdent::Equal(n) => {
as_content!(node, |content| {
content == *n
})
},
NumIdent::BiggerThan(n) => {
as_content!(node, |content| {
content > *n
})
},
NumIdent::SmallerThan(n) => {
as_content!(node, |content| {
content < *n
})
}
}
}
}
fn main() {
let node = node!(1,
node!(8),
node!(3),
node!(7),
node!(6, node!(3))
);
// retrive any child
let a = node.get_last_child().unwrap();
let collection = a.collect_linked_list(&NumIdent::SmallerThan(5));
for node in collection.into_iter() {
println!("{}", node.to_content());
}
}
Source§impl<T: Debug + Clone> DetachNode<T> for Node<T>
impl<T: Debug + Clone> DetachNode<T> for Node<T>
Source§fn detach(&self)
fn detach(&self)
Detaches a single node from the linked list by fixing the pointers between the
parent, the previous and next siblings. This also detaches all the children of the Node
,
which will only remain linked with the node itself.
WARNING: This also re-sets the pointers in the node itself to None.
So when you are detecting nodes in a linked-list and detaching them, you cant iterate over them using this method
as it would break the loop. Use detach_preserve
instead.
Source§fn detach_preserve(&self, vec: &mut NodeCollection<T>)
fn detach_preserve(&self, vec: &mut NodeCollection<T>)
Detaches a single node from the linked list like detach
, but doesn’t re-set the pointers inside the Node.
This should only be used when you have to iterate over a linked list and detach some Node
s.
You should create a vector to store the detached nodes, and iterate over them only when the while loop is
compleated, re-setting the parent
, prev
, next
fields to None
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
pub enum NumIdent {
Equal(i32),
BiggerThan(i32),
SmallerThan(i32)
}
impl CompareNode<i32> for NumIdent {
fn compare(&self, node: &Node<i32>) -> bool {
match &self {
NumIdent::Equal(n) => {
as_content!(node, |content| {
content == *n
})
},
NumIdent::BiggerThan(n) => {
as_content!(node, |content| {
content > *n
})
},
NumIdent::SmallerThan(n) => {
as_content!(node, |content| {
content < *n
})
}
}
}
}
fn main() {
let list = list!(
node!(1),
node!(2),
node!(3),
node!(4),
node!(5),
node!(6)
);
let ident = NumIdent::SmallerThan(4);
let mut detached_nodes = NodeCollection::<i32>::new();
// possible algorithm to detach all the nodes smaller than 4 in a linked list.
let mut next: Node<i32> = list.first().unwrap();
/* do */ {
if ident.compare(&next) {
next.detach_preserve(&mut detached_nodes);
}
} while let Some(n) = next.next() {
next = n;
if ident.compare(&next) {
next.detach_preserve(&mut detached_nodes);
}
}
// this will finally re-set to None every pointer in the collected
// nodes.
detached_nodes.free();
}
Source§impl<T: Debug + Clone, I: CompareNode<T>> FindNode<T, I> for Node<T>
impl<T: Debug + Clone, I: CompareNode<T>> FindNode<T, I> for Node<T>
Source§fn find_next(&self, ident: &I) -> Option<Node<T>>
fn find_next(&self, ident: &I) -> Option<Node<T>>
Get the first Node
in the linked list, at the same depth-level of &self
and coming after it,
matching the identifier.
This guarantees to actually retrive the closest Node
.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
pub enum NumIdent {
Equal(i32),
BiggerThan(i32),
SmallerThan(i32)
}
impl CompareNode<i32> for NumIdent {
fn compare(&self, node: &Node<i32>) -> bool {
match &self {
NumIdent::Equal(n) => {
as_content!(node, |content| {
content == *n
})
},
NumIdent::BiggerThan(n) => {
as_content!(node, |content| {
content > *n
})
},
NumIdent::SmallerThan(n) => {
as_content!(node, |content| {
content < *n
})
}
}
}
}
fn main() {
let node = node!(33,
node!(1),
node!(34),
node!(66)
);
let one = node.child().unwrap();
assert_eq!(
one.find_next(&NumIdent::BiggerThan(50)).unwrap().to_content(),
66
);
}
Source§fn find_prev(&self, ident: &I) -> Option<Node<T>>
fn find_prev(&self, ident: &I) -> Option<Node<T>>
Get the first Node
in the linked list, at the same depth-level of &self
and coming before it,
matching the identifier.
This guarantees to actually retrive the closest Node
.
Source§fn find_linked_list(&self, ident: &I) -> Option<Node<T>>
fn find_linked_list(&self, ident: &I) -> Option<Node<T>>
Get a Node
somewhere in the linked list matching the identifier.
WARNING: it’s not guaranteed to retrive the closest Node
. Only use when you don’t
care about which node is retrived as long as it matches the identifier or when you are 100% sure
that there isn’t more than one Node
satisfying the identifier in the linked list.
Source§fn find_child(&self, ident: &I) -> Option<Node<T>>
fn find_child(&self, ident: &I) -> Option<Node<T>>
Get the first child Node
of &self
in the linked list matching the identifier.
WARNING: it’s not guaranteed to retrive the closest Node
. Only use when you don’t
care about which node is retrived as long as it matches the identifier or when you are 100% sure
that there isn’t more than one Node
satisfying the identifier in the children.
Source§fn find_sibling(&self, ident: &I) -> Option<Node<T>>
fn find_sibling(&self, ident: &I) -> Option<Node<T>>
In the case you can’t know if the Node
you are looking for comes before or after, here’s a combination of the two previous methods.
Always prefer using HedelFind::find_next
and HedelFind::find_prev
when you know the position of the Node
,
as they might be faster.
Source§impl<T: Debug + Clone> GetNode<T> for Node<T>
impl<T: Debug + Clone> GetNode<T> for Node<T>
Source§fn get_first_sibling(&self) -> Option<Node<T>>
fn get_first_sibling(&self) -> Option<Node<T>>
Get the first Node
in the linked list at the same depth level of &self
.
If None is returned, &self
is the first Node
at that depth level.
Source§fn get_last_sibling(&self) -> Option<Node<T>>
fn get_last_sibling(&self) -> Option<Node<T>>
Get the last Node
in the linked list at the same depth level of &self
.
If None is returned, &self
is the last Node
at that depth level.
Source§fn get_last_child(&self) -> Option<Node<T>>
fn get_last_child(&self) -> Option<Node<T>>
Get the last child Node
of &self
If None is returned, &self
doesn’t have any children.
NOTE: use &self.child() to get the first Node
.
Source§impl<T: Debug + Clone> InsertNode<T> for Node<T>
impl<T: Debug + Clone> InsertNode<T> for Node<T>
Source§fn insert_sibling(&self, position: usize, node: Node<T>)
fn insert_sibling(&self, position: usize, node: Node<T>)
Inserts a new node at the same depth-level of &self
and at the given position.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
fn main() {
let mut node = node!(1, node!(2), node!(4));
let two = node.child().unwrap();
two.insert_sibling(23, node!(3));
// if the position is bigger than the length, the node gets placed at the end
let three = node.get_last_child().unwrap();
println!("{}", three.to_content()); // prints 3
}
Source§fn insert_child(&self, position: usize, node: Node<T>)
fn insert_child(&self, position: usize, node: Node<T>)
Inserts a new node to the childrenl of &self
and at the given position.
§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
fn main() {
let mut node = node!(1, node!(2), node!(4));
node.insert_child(2, node!(3));
let three = node.get_last_child().unwrap();
println!("{}", three.to_content()); // prints 3
}