flange-flat-tree
A Rust implementation of Tree Data-Structure storing nodes in a Flat Array (Vec
to be exact)
and allowing to "flange" more data to the nodes.
Examples
Building a tree.
A tree is build using the Builder
. Afterwards a tree can not be changed anymore.
use ;
let mut b = new;
b.start_element; // The root node
b.start_end_element; // This will be a child of "one"
b.start_element; // This will be a child of "one"
b.start_end_element; // This will be a child of "three"
b.end_element; // End "three"
b.end_element; // End "one" (the root)
let tree = b.build;
assert_eq!;
assert_eq!;
But you can change elements of the tree still in the builder by using the index:
use ;
let mut b = new;
let root_id = b.start_element; // The root node
b.start_end_element; // This will be a child of "one"
// Change root afterwards
b.get_mut.unwrap.name = "root".to_string;
b.end_element; // End "one" (the root)
let tree = b.build;
assert_eq!;
See the builder
for more detail.
Flanging data
After the tree is build, additional data can be "flanged" to the nodes of the tree. This means data is added without modifying the original tree or copying its data.
"Flanging" can happen in 2 ways:
Flanging using "flange"
You can flange using tree.flange
.
This crates a new tree which references the old tree and adds new data
from the vector passed to tree.flange
. The Vector is consumed, so it is
owned by the new tree.
The nodes of the Vec
are associated the nodes with the same index in the
original tree. When you access the tree you get a tuple of references to the values:
use ;
let mut builder = with_capacity;
builder.start_element;
builder.start_end_element;
builder.end_element;
let tree = builder.build;
// flanged values
let values: = .collect;
let tree_with_values = tree.flange;
assert_eq!;
You can also create the flange by directly mapping from the original tree:
use ;
let mut builder = with_capacity;
builder.start_element;
builder.start_end_element;
builder.end_element;
let tree = builder.build;
let tree_with_values = tree.flange_map;
assert_eq!;
This can also be done "depth-first", in which case the children are available before the node is created:
use ;
let mut builder = with_capacity;
builder.start_element;
builder.start_end_element;
builder.end_element;
let tree = builder.build;
let tree_with_values = tree.depth_first_flange;
assert_eq!;
assert_eq!;
Flange using map
If you want to access the tree and its flange using named properties, you have to create
a type for it. The type should reference the values it exposes. Than
you can use tree.map
with a closure to return your type.
You can also combine the existing tree with external data that way:
use ;
let mut builder = with_capacity;
builder.start_element;
builder.start_end_element;
builder.end_element;
let tree = builder.build;
// The data we are going to flange
let data: = vec!;
let tree_with_values = tree.map;
assert_eq!;
assert_eq!;
Installation
See https://crates.io/crates/flange-flat-tree
and add
= "0.2.1"
Current version: 0.2.1
License: MIT