pub struct ConsBranch<T> { /* private fields */ }collections and cons only.Expand description
A references counted, linked list implemented similar to a cons list. This type is useful as an list of immutable items with cheap, shallow cloning, that can share nodes with other instances.
When cloned, only the ‘head’ of the tree is cloned (just an Rc), with all of the elements
included in both list. As a result, the data structure is only mutable from the head, where
elements can be pushed or popped. This cheap cloning is
helpful for implementing procedures such that include rollbacks or branching.
Implementations§
Source§impl<T> ConsBranch<T>
impl<T> ConsBranch<T>
Sourcepub const fn new() -> ConsBranch<T>
pub const fn new() -> ConsBranch<T>
Creates a new, empty ConsTree.
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Pushes a new element onto the start of this ConsTree, updating this list’s head without
affecting any overlapping lists (shallow clones).
Sourcepub fn get_head(&self) -> Option<&T>
pub fn get_head(&self) -> Option<&T>
Returns a reference to the element contained within the head node.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Produces a borrowed Iterator<Item = &T> over all elements in this list, both
unique and shared.
Sourcepub fn into_iter_rc(&self) -> RcIter<T> ⓘ
pub fn into_iter_rc(&self) -> RcIter<T> ⓘ
Produces an Iterator<Item = Rc<ConsTreeNode<T>>> over all of the underlying
Rc instances in this list.
Each referenced element is considered ‘shared’ for the lifetime of the Rc produced by
this iterator. To return a ConsTree to being unique, this iterator and all produced
Rcs need to be dropped.
Sourcepub fn is_unique(&self) -> bool
pub fn is_unique(&self) -> bool
Returns true if this entire list is unique (doesn’t share any items with another list).
If this method returns true, into_iter_unique will produce every
item in the list.
Sourcepub fn is_head_unique(&self) -> bool
pub fn is_head_unique(&self) -> bool
Returns true if the head element of this list is unique.
Sourcepub fn pop_if_unique(&mut self) -> Option<T>
pub fn pop_if_unique(&mut self) -> Option<T>
Pops the head element of this list, if it is unique. Otherwise, self remains unchanged.
Sourcepub fn split_off_unique(&mut self) -> ConsBranch<T>
pub fn split_off_unique(&mut self) -> ConsBranch<T>
Removes all unique items from this list and returns them as another ConsTree.
Sourcepub const fn into_iter_unique(self) -> UniqueIter<T> ⓘ
pub const fn into_iter_unique(self) -> UniqueIter<T> ⓘ
Produces an Iterator<Item = T> over the elements in this list, producing
owned values until a shared element is found.
This iterator does no cloning and produces owned items by completely ignoring any elements that are shared by other lists.
If called on every clone of a single initial ConsTree, every element of the tree will be
returned by an iterator only once.
Source§impl<T: Clone> ConsBranch<T>
impl<T: Clone> ConsBranch<T>
Sourcepub fn pop_to_owned(&mut self) -> Option<T>
pub fn pop_to_owned(&mut self) -> Option<T>
Pops the head element from this list, cloning if it is shared by another ConsTree.
Regardless of if a clone is required, the head of this list will be updated.
Sourcepub const fn into_iter_owned(self) -> OwnedIter<T> ⓘ
pub const fn into_iter_owned(self) -> OwnedIter<T> ⓘ
Produces an Iterator<Item = T> over all elements in this list, returning
owned items by cloning any shared elements.
Sourcepub fn deep_clone(&self) -> ConsBranch<T>
pub fn deep_clone(&self) -> ConsBranch<T>
Produces a deep clone of this ConsTree. The result has a clone of every element in this
list, without sharing any. The result is unique.
Trait Implementations§
Source§impl<T> Clone for ConsBranch<T>
impl<T> Clone for ConsBranch<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates a cheap (shallow) clone of this ConsTree, with all the same underlying elements.
After cloning, all elements of the list are considered ‘shared’ between the original list and
the clone.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more