Contigious tree
Write and read tree graphs to and from contigious blocks of memory. This comes in handy if you have really large trees you want to read and load from a memory mapped file.
About
A useful tree representation for situations there you want to serialize / deserialize a tree, or query it very quickly. Do not use this crate if you need to change your tree frequently. The implementation is generic over the value type associated with their nodes and their binary representation.
Usage
Consider this tree:
(1) root
├── (2)
└── (3)
└── (4)
Writing
We write trees in a depth first manner. With each subrtee written, before the parent node which owns it.
use ;
/// Value type is a singend 32 Bit integer with a little endian representation.
type Node = LeI32;
// Any io::Write, will do for writing
let mut persistence = Vec:: new;
let mut builder = new;
// Build tree depth first. For each node pass a reference to the value and the number of preceding
// direct children.
builder.write_node.unwrap;
builder.write_node.unwrap;
builder.write_node.unwrap;
builder.write_node.unwrap;
builder.finish.unwrap;
Reading
use ;
let persistence: = ;
/// Value type is a singend 32 Bit integer with a little endian representation.
type Node = LeI32;
let tree = new;
// Read value of tree root, and iterate over direct children
let = tree.read_node;
assert_eq!;
let first = branches.next.unwrap;
let second = branches.next.unwrap;
assert!;
// First branch has value 2 and no children
let = first.read_node;
assert_eq!;
assert!;
// Second branch has value 3 and one child with value 4
let = second.read_node;
assert_eq!;
assert_eq!;