Trait flange_flat_tree::Tree
source · [−]pub trait Tree<'a>: Sizedwhere
Self: 'a,
&'a Self: TreeData<Node = Self::Node>,{
type Node;
type SubtreeType: Subtree<Node = Self::Node>;
fn at_pos(&'a self, index: usize) -> Self::SubtreeType;
fn get_nav(&self) -> &Navigator;
fn root(&'a self) -> Self::SubtreeType { ... }
fn node_count(&'a self) -> usize { ... }
fn map<B, M>(&'a self, m: M) -> MappedTree<Self::Node, B, M, &'a Self>
where
M: Fn(usize, Self::Node) -> B,
{ ... }
fn flange<B>(&'a self, data: Vec<B>) -> FlangedTree<&'a Self, B> { ... }
fn flange_map<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>
where
B: 'a,
B: Clone,
F: Fn(Self::Node) -> B,
{ ... }
fn for_each<F>(&'a self, f: F)
where
F: FnMut(Self::SubtreeType),
{ ... }
fn depth_first_flange<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>
where
B: 'a,
B: Default,
B: Clone,
F: Fn(Self::Node, Vec<&B>) -> B,
{ ... }
}
Expand description
Trait for everything that can be handled as a tree.
Required Associated Types
sourcetype SubtreeType: Subtree<Node = Self::Node>
type SubtreeType: Subtree<Node = Self::Node>
The type of the subtree, returned by root()
Required Methods
sourcefn at_pos(&'a self, index: usize) -> Self::SubtreeType
fn at_pos(&'a self, index: usize) -> Self::SubtreeType
Direct access to a node in the tree via its position in the flat map
Direct access to the Navigator
storing the neighboring information of the tree
Provided Methods
sourcefn root(&'a self) -> Self::SubtreeType
fn root(&'a self) -> Self::SubtreeType
The root node of the tree.
sourcefn node_count(&'a self) -> usize
fn node_count(&'a self) -> usize
The number of nodes in the tree.
sourcefn map<B, M>(&'a self, m: M) -> MappedTree<Self::Node, B, M, &'a Self>where
M: Fn(usize, Self::Node) -> B,
fn map<B, M>(&'a self, m: M) -> MappedTree<Self::Node, B, M, &'a Self>where
M: Fn(usize, Self::Node) -> B,
Create a new tree using a function to map from the nodes of this tree. The map function can also include external data sources.
Example
use flange_flat_tree::{Builder, Tree, Subtree};
let mut builder = Builder::with_capacity(2);
builder.start_element("one");
builder.start_end_element("two");
builder.end_element();
let tree = builder.build();
struct FlangedNode<'a> {
pub name: &'a str,
pub value: u32, // we don't use a reference for u32, because copying the value itself is fine
}
// The data we are going to flange
let data: Vec<u32> = vec![1,2];
let tree_with_values = tree.map(
|i,v| FlangedNode {
name: v,
value: data[i],
}
);
assert_eq!(tree_with_values.root().value().name, "one");
assert_eq!(tree_with_values.root().value().value, 1);
sourcefn flange<B>(&'a self, data: Vec<B>) -> FlangedTree<&'a Self, B>
fn flange<B>(&'a self, data: Vec<B>) -> FlangedTree<&'a Self, B>
Flange data to the nodes.
A new tree is created, that references the old tree and whos
Node
type is a typle with a reference to the old data
and a reference to the new data from the inserted data
vector.
sourcefn flange_map<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>where
B: 'a,
B: Clone,
F: Fn(Self::Node) -> B,
fn flange_map<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>where
B: 'a,
B: Clone,
F: Fn(Self::Node) -> B,
Use flange
, but create the data using the mapf
function
fn for_each<F>(&'a self, f: F)where
F: FnMut(Self::SubtreeType),
sourcefn depth_first_flange<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>where
B: 'a,
B: Default,
B: Clone,
F: Fn(Self::Node, Vec<&B>) -> B,
fn depth_first_flange<B, F>(&'a self, mapf: F) -> FlangedTree<&'a Self, B>where
B: 'a,
B: Default,
B: Clone,
F: Fn(Self::Node, Vec<&B>) -> B,
Flange data to the nodes using a map function in a depth first order.
This means the children are available (already created) when a node is created and can be used to calculate the nodes value.
Example
use flange_flat_tree::{Builder, Tree, Subtree};
let mut builder = Builder::with_capacity(2);
builder.start_element("one");
builder.start_element("two");
builder.start_end_element("three");
builder.start_end_element("four");
builder.end_element();
builder.end_element();
let tree = builder.build();
// The data we are going to flange
let data: Vec<u32> = vec![1,2];
let tree_with_values = tree.depth_first_flange(
|v, childs| {
// Calculate the total number of children
childs.iter().fold(childs.len(), |l,c| l+*c )
}
);
assert_eq!(tree_with_values.root().value().1, &3);
assert_eq!(tree_with_values.root().children()[0].value(), (&"two", &2));