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:

  • t table or tree table or tree to be cloned
  • nometa boolean if non-nil don't copy metatables

Returns:

    table or tree a deep copy of t
std.tree.ileaves (tr)
Tree iterator which returns just numbered leaves, in order.

Parameters:

Returns:

  1. function iterator function
  2. tree or table the tree tr
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:

  1. function iterator function
  2. tree or table the tree, tr

See also:

std.tree.leaves (tr)
Tree iterator which returns just leaves.

Parameters:

Returns:

  1. function iterator function
  2. tree or table the tree, tr
std.tree.merge (t, u)
Destructively deep-merge one tree into another.

Parameters:

  • t tree or table destination tree or table
  • u tree or table tree or table with nodes to merge

Returns:

    tree or table t with nodes from u merged in

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} where node-type is branch, join or leaf; tree-path is a list of keys used to reach this node, and tree-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 must table.clone a copy if you want to take a snap-shot of the current state of the tree-path list before the next iteration changes it.

Parameters:

  • tr tree or table tree or tree-like table to iterate over

Returns:

  1. function iterator function
  2. tree or table the tree, tr

See also:

Tables

std.tree
Tree prototype object.

Fields:

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, or self[i] otherwise
std.tree:__newindex (i, v)
Tree __newindex metamethod.

Sets self[i_1]...[i_n] = v if i is a table, or self[i] = v otherwise

Parameters:

  • i non-table, or list of keys {i_1 ... i_n}
  • v value
generated by LDoc 1.4.0