Struct Tree

Source
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>

Source

pub fn new(root: T) -> Self

Create a new Tree with the specified value

Source

pub fn with_capacity(root: T, capacity: usize) -> Self

Create a new Tree with the specified value & set the capacity of the internal vectors

Source

pub fn capacity(&self) -> usize

Returns the total number of elements the tree can hold without reallocating.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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!

Source

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.

Source

pub fn drain<R>( &mut self, range: R, ) -> impl Iterator<Item = (T, usize, NodeId)> + '_
where R: RangeBounds<usize> + Clone,

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.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of elements in the tree, also referred to as its ‘length’.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn tree_root_mut(&mut self) -> TreeMut<'_, T>

Get a mutable TreeMut handle of the root, so you can push children

This always success

Source

pub fn tree_node_mut(&mut self, id: NodeId) -> Option<TreeMut<'_, T>>

Get a mutable TreeMut from his NodeId, so you can push children

Source

pub fn node(&self, id: NodeId) -> Option<Node<'_, T>>

Get the Node from his NodeId

Source

pub fn root(&self) -> Node<'_, T>

Get the root Node

Source

pub fn node_mut(&mut self, id: NodeId) -> Option<NodeMut<'_, T>>

Get a mutable NodeMut from his NodeId.

Source

pub fn root_mut(&mut self) -> NodeMut<'_, T>

Get a mutable NodeMut handle of the root.

This always success

Source

pub fn iter(&self) -> TreeIter<'_, T>

Source

pub fn into_iter(&self) -> IntoIter<'_, T>

Source

pub fn as_data(&self) -> &[T]

A slice view of the internal data

Source

pub fn as_data_mut(&mut self) -> &mut [T]

A slice view of the internal data

Source

pub fn as_level(&self) -> &[usize]

A slice view of the internal level

Source

pub fn get_level(&self, of: NodeId) -> usize

Get the level from a NodeId

Source

pub fn as_parents(&self) -> &[usize]

A slice view of the internal parents

Source

pub fn to_data(self) -> Vec<T>

Consume tree and move-out the data

Source

pub fn print(&self, f: &mut Formatter<'_>) -> Result
where T: Display,

Pretty-print the tree

Trait Implementations§

Source§

impl<T: Clone> Clone for Tree<T>

Source§

fn clone(&self) -> Tree<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Tree<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Debug + Display> Display for Tree<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Debug> IntoIterator for &'a Tree<T>

Source§

type Item = Node<'a, T>

The type of the elements being iterated over.
Source§

type IntoIter = TreeIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for Tree<T>

Source§

fn cmp(&self, other: &Tree<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for Tree<T>

Source§

fn eq(&self, other: &Tree<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for Tree<T>

Source§

fn partial_cmp(&self, other: &Tree<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for Tree<T>

Source§

impl<T> StructuralPartialEq for Tree<T>

Auto Trait Implementations§

§

impl<T> Freeze for Tree<T>

§

impl<T> RefUnwindSafe for Tree<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tree<T>
where T: Send,

§

impl<T> Sync for Tree<T>
where T: Sync,

§

impl<T> Unpin for Tree<T>
where T: Unpin,

§

impl<T> UnwindSafe for Tree<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.