pub struct Tree<T> { /* private fields */ }
Expand description
Vec-backed, flattened in pre-order, Tree.
Always contains at least a root node.
Implementations§
Source§impl<T: Debug> Tree<T>
impl<T: Debug> Tree<T>
Sourcepub fn with_capacity(root: T, capacity: usize) -> Self
pub fn with_capacity(root: T, capacity: usize) -> Self
Create a new Tree with the specified value & set the capacity of the internal vectors
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of elements the tree can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the given Tree<T>
. The collection may reserve more space to
speculatively avoid frequent reallocations. After calling reserve
,
capacity will be greater than or equal to self.len() + additional
.
Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for at least additional
more elements to
be inserted in the given Tree<T>
. Unlike reserve
, this will not
deliberately over-allocate to speculatively avoid frequent allocations.
After calling reserve_exact
, capacity will be greater than or equal to
self.len() + additional
. Does nothing if the capacity is already
sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer reserve
if future insertions are expected.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional
more elements to be inserted
in the given Tree<T>
. The collection may reserve more space to speculatively avoid
frequent reallocations. After calling try_reserve
, capacity will be
greater than or equal to self.len() + additional
if it returns
Ok(())
. Does nothing if capacity is already sufficient. This method
preserves the contents even if an error occurs.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Tries to reserve the minimum capacity for at least additional
elements to be inserted in the given Tree<T>
. Unlike try_reserve
,
this will not deliberately over-allocate to speculatively avoid frequent
allocations. After calling try_reserve_exact
, capacity will be greater
than or equal to self.len() + additional
if it returns Ok(())
.
Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore, capacity can not be relied upon to be precisely
minimal. Prefer try_reserve
if future insertions are expected.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the tree as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the tree that there is space for a few more elements.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the tree with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the tree, keeping the first len
elements and dropping
the rest.
If len
is greater than the tree’s current length, this has no
effect.
The drain
method can emulate truncate
, but causes the excess
elements to be returned instead of dropped.
Note that this method has no effect on the allocated capacity of the tree.
Sourcepub fn push_with_level(
&mut self,
data: T,
level: usize,
parent: NodeId,
) -> NodeId
pub fn push_with_level( &mut self, data: T, level: usize, parent: NodeId, ) -> NodeId
Push a node into the tree
#WARNING
This assumes you are pushing in pre-order!
Sourcepub fn pop(&mut self) -> Option<(T, usize, NodeId)>
pub fn pop(&mut self) -> Option<(T, usize, NodeId)>
Removes the last element from a tree and returns it as a triple
(data: T, level: usize, parent: NodeId)
, or None
if it
is empty.
Sourcepub fn drain<R>(
&mut self,
range: R,
) -> impl Iterator<Item = (T, usize, NodeId)> + '_
pub fn drain<R>( &mut self, range: R, ) -> impl Iterator<Item = (T, usize, NodeId)> + '_
Removes the specified range from the tree in bulk, returning all removed elements as an iterator. If the iterator is dropped before being fully consumed, it drops the remaining removed elements.
The returned iterator keeps a mutable borrow on the tree to optimize its implementation.
§Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
§Leaking
If the returned iterator goes out of scope without being dropped (due to
[mem::forget
], for example), the tree may have lost and leaked
elements arbitrarily, including elements outside the range.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the tree, removing all values.
Note that this method has no effect on the allocated capacity of the tree.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the tree, also referred to as its ‘length’.
Sourcepub fn tree_root_mut(&mut self) -> TreeMut<'_, T>
pub fn tree_root_mut(&mut self) -> TreeMut<'_, T>
Get a mutable TreeMut
This always success
Sourcepub fn tree_node_mut(&mut self, id: NodeId) -> Option<TreeMut<'_, T>>
pub fn tree_node_mut(&mut self, id: NodeId) -> Option<TreeMut<'_, T>>
Sourcepub fn root_mut(&mut self) -> NodeMut<'_, T>
pub fn root_mut(&mut self) -> NodeMut<'_, T>
Get a mutable NodeMut
This always success
pub fn iter(&self) -> TreeIter<'_, T> ⓘ
pub fn into_iter(&self) -> IntoIter<'_, T>
Sourcepub fn as_data_mut(&mut self) -> &mut [T]
pub fn as_data_mut(&mut self) -> &mut [T]
A slice view of the internal data
Sourcepub fn as_parents(&self) -> &[usize]
pub fn as_parents(&self) -> &[usize]
A slice view of the internal parents