Class std.tree
Tree container.
Derived from std.container, and inherits Container's metamethods.
Note that Functions listed below are only available from the Tree prototype return by requiring this module, because Container objects cannot have object methods.
Functions
std.tree.clone (t, nometa) | Make a deep copy of a tree, including any metatables. |
std.tree.ileaves (tr) | Tree iterator which returns just numbered leaves, in order. |
std.tree.inodes (tr) | Tree iterator over numbered nodes, in order. |
std.tree.leaves (tr) | Tree iterator which returns just leaves. |
std.tree.merge (t, u) | Destructively deep-merge one tree into another. |
std.tree.nodes (tr) | Tree iterator over all nodes. |
Tables
std.tree | Tree prototype object. |
Metamethods
std.tree:__index (i) | Tree __index metamethod. |
std.tree:__newindex (i, v) | Tree __newindex metamethod. |
Functions
- std.tree.clone (t, nometa)
-
Make a deep copy of a tree, including any metatables.
To make fast shallow copies, use std.table.clone.
Parameters:
Returns:
- std.tree.ileaves (tr)
-
Tree iterator which returns just numbered leaves, in order.
Parameters:
Returns:
- std.tree.inodes (tr)
-
Tree iterator over numbered nodes, in order.
The iterator function behaves like nodes, but only traverses the array part of the nodes of
tr
, ignoring any others.Parameters:
Returns:
See also:
- std.tree.leaves (tr)
-
Tree iterator which returns just leaves.
Parameters:
Returns:
- std.tree.merge (t, u)
-
Destructively deep-merge one tree into another.
Parameters:
Returns:
See also:
- std.tree.nodes (tr)
-
Tree iterator over all nodes.
The returned iterator function performs a depth-first traversal of
tr
, and at each node it returns{node-type, tree-path, tree-node}
wherenode-type
isbranch
,join
orleaf
;tree-path
is a list of keys used to reach this node, andtree-node
is the current node.Given a tree to represent:
+ root +-- node1 | +-- leaf1 | '-- leaf2 '-- leaf 3 tree = std.tree { std.tree { "leaf1", "leaf2"}, "leaf3" }
A series of calls to
tree.nodes
will return:"branch", {}, {{"leaf1", "leaf2"}, "leaf3"} "branch", {1}, {"leaf1", "leaf"2") "leaf", {1,1}, "leaf1" "leaf", {1,2}, "leaf2" "join", {1}, {"leaf1", "leaf2"} "leaf", {2}, "leaf3" "join", {}, {{"leaf1", "leaf2"}, "leaf3"}
Note that the
tree-path
reuses the same table on each iteration, so you musttable.clone
a copy if you want to take a snap-shot of the current state of thetree-path
list before the next iteration changes it.Parameters:
Returns:
See also:
Tables
- std.tree
-
Tree prototype object.
Fields:
- _init table or function a table of field names, or initialisation function, see std.object.__call (default {})
- _functions nil or table a table of module functions not copied by std.object.__call
- _type string type of Tree, returned by std.object.prototype (default "Tree")
Metamethods
- std.tree:__index (i)
-
Tree __index metamethod.
Parameters:
- i
non-table, or list of keys
{i_1 ... i_n}
Returns:
self[i]...[i_n]
if i is a table, orself[i]
otherwise - i
non-table, or list of keys
- std.tree:__newindex (i, v)
-
Tree __newindex metamethod.
Sets
self[i_1]...[i_n] = v
if i is a table, orself[i] = v
otherwiseParameters:
- i
non-table, or list of keys
{i_1 ... i_n}
- v value
- i
non-table, or list of keys