Struct Tree

Source
pub struct Tree<T> { /* private fields */ }
Expand description

A tree structure containing Nodes.

Implementations§

Source§

impl<T> Tree<T>

Source

pub fn new() -> Tree<T>

Creates a new Tree with a capacity of 0.

use slab_tree::tree::Tree;

let tree: Tree<i32> = Tree::new();
Source

pub fn set_root(&mut self, root: T) -> NodeId

Sets the “root” of the Tree to be root.

If there is already a “root” node in the Tree, that node is shifted down and the new one takes its place.

use slab_tree::tree::Tree;

let mut tree = Tree::new();

let root_id = tree.set_root(1);

assert_eq!(tree.root_id().unwrap(), root_id);
Source

pub fn capacity(&self) -> usize

Returns the Tree’s current capacity. Capacity is defined as the number of times new Nodes can be added to the Tree before it must allocate more memory.

use slab_tree::tree::Tree;

let tree: Tree<i32> = Tree::new();

assert_eq!(tree.capacity(), 0);
Source

pub fn root_id(&self) -> Option<NodeId>

Returns the NodeId of the root node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let root_id = tree.root_id().expect("root doesn't exist?");

assert_eq!(tree.get(root_id).unwrap().data(), &1);
Examples found in repository?
examples/basic.rs (line 15)
5fn main() {
6    //      "hello"
7    //        / \
8    // "world"   "trees"
9    //              |
10    //            "are"
11    //              |
12    //            "cool"
13
14    let mut tree = TreeBuilder::new().with_root("hello").build();
15    let root_id = tree.root_id().expect("root doesn't exist?");
16    let mut hello = tree.get_mut(root_id).unwrap();
17
18    hello.append("world");
19    hello.append("trees").append("are").append("cool");
20}
Source

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

Returns a NodeRef pointing to the root Node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let root = tree.root().expect("root doesn't exist?");

assert_eq!(root.data(), &1);
Source

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

Returns a NodeMut pointing to the root Node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let mut root = tree.root_mut().expect("root doesn't exist?");
assert_eq!(root.data(), &mut 1);

*root.data() = 2;
assert_eq!(root.data(), &mut 2);
Examples found in repository?
examples/formatted.rs (line 7)
5fn main() {
6    let mut tree = TreeBuilder::new().with_root(0).build();
7    let mut root = tree.root_mut().unwrap();
8    {
9        let mut one = root.append(1);
10        let mut two = one.append(2);
11        two.append(3);
12        two.append(4);
13    }
14    {
15        let mut five = root.append(5);
16        five.append(6).append(7);
17        five.append(8);
18    }
19    root.append(9);
20
21    let mut s = String::new();
22    // 0
23    // ├── 1
24    // │   └── 2
25    // │       ├── 3
26    // │       └── 4
27    // ├── 5
28    // │   ├── 6
29    // │   │   └── 7
30    // │   └── 8
31    // └── 9
32    tree.write_formatted(&mut s).unwrap();
33    print!("{}", s);
34}
Source

pub fn get(&self, node_id: NodeId) -> Option<NodeRef<'_, T>>

Returns the NodeRef pointing to the Node that the given NodeId identifies. If the NodeId in question points to nothing (or belongs to a different Tree) a None-value will be returned; otherwise, a Some-value will be returned.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");

let root = tree.get(root_id);
assert!(root.is_some());

let root = root.unwrap();
assert_eq!(root.data(), &1);
Source

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

Returns the NodeMut pointing to the Node that the given NodeId identifies. If the NodeId in question points to nothing (or belongs to a different Tree) a None-value will be returned; otherwise, a Some-value will be returned.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");

let root = tree.get_mut(root_id);
assert!(root.is_some());

let mut root = root.unwrap();

*root.data() = 2;
assert_eq!(root.data(), &mut 2);
Examples found in repository?
examples/basic.rs (line 16)
5fn main() {
6    //      "hello"
7    //        / \
8    // "world"   "trees"
9    //              |
10    //            "are"
11    //              |
12    //            "cool"
13
14    let mut tree = TreeBuilder::new().with_root("hello").build();
15    let root_id = tree.root_id().expect("root doesn't exist?");
16    let mut hello = tree.get_mut(root_id).unwrap();
17
18    hello.append("world");
19    hello.append("trees").append("are").append("cool");
20}
Source

pub fn remove(&mut self, node_id: NodeId, behavior: RemoveBehavior) -> Option<T>

Remove a Node by its NodeId and return the data that it contained. Returns a Some-value if the Node exists; returns a None-value otherwise.

Children of the removed Node can either be dropped with DropChildren or orphaned with OrphanChildren.

use slab_tree::tree::TreeBuilder;
use slab_tree::behaviors::RemoveBehavior::*;

let mut tree = TreeBuilder::new().with_root(1).build();
let two_id = {
    let mut root = tree.root_mut().expect("root doesn't exist?");
    let two_id = root.append(2).node_id();
    root.append(3);
    two_id
};

let two = tree.remove(two_id, DropChildren);

assert!(two.is_some());
assert_eq!(two.unwrap(), 2);

let root = tree.root().expect("root doesn't exist?");
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 3);

assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 3);
Source§

impl<T: Debug> Tree<T>

Source

pub fn write_formatted<W: Write>(&self, w: &mut W) -> Result

Write formatted tree representation and nodes with debug formatting.

Example:

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(0).build();
let mut root = tree.root_mut().unwrap();
root.append(1)
    .append(2);
root.append(3);
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "\
0
├── 1
│   └── 2
└── 3
");

Writes nothing if the tree is empty.

use slab_tree::tree::TreeBuilder;

let tree = TreeBuilder::<i32>::new().build();
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "");
Examples found in repository?
examples/formatted.rs (line 32)
5fn main() {
6    let mut tree = TreeBuilder::new().with_root(0).build();
7    let mut root = tree.root_mut().unwrap();
8    {
9        let mut one = root.append(1);
10        let mut two = one.append(2);
11        two.append(3);
12        two.append(4);
13    }
14    {
15        let mut five = root.append(5);
16        five.append(6).append(7);
17        five.append(8);
18    }
19    root.append(9);
20
21    let mut s = String::new();
22    // 0
23    // ├── 1
24    // │   └── 2
25    // │       ├── 3
26    // │       └── 4
27    // ├── 5
28    // │   ├── 6
29    // │   │   └── 7
30    // │   └── 8
31    // └── 9
32    tree.write_formatted(&mut s).unwrap();
33    print!("{}", s);
34}

Trait Implementations§

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> Default for Tree<T>

Source§

fn default() -> Self

Returns the “default value” for a type. 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§

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> 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> 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, 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.