Landin library reference discuss source

Shared: available on every enabled target.

An append-only store of named leaves and branches addressed by node identifiers.

  • A branch names a contiguous range of previously published nodes.
  • Leaf totals are precomputed, so queries use constant stack and time.
  • Shared children count once for each incoming path.
  • Names borrow their UTF-8 backing; releasing the tree frees node storage, not names.
  • for id in t walks every node's identifier in the order the nodes were added, so each branch comes after its children.

Items

public node: type = node_value

Opaque named leaf or branch with a precomputed leaf total. The name is borrowed UTF-8.

public node_id: type = identifier

Opaque target-sized ordinal identifying a node in a particular tree. It does not retain or identify the tree itself.

public node_kind: type = leaf_node | branch_node

What a node is: leaf_node or branch_node.

public tree: type = tree_value

Append-only store of named nodes. Branches reference contiguous ranges of already published nodes; allocation is supplied explicitly.

public branch_node: atom

A node over a range of earlier nodes, its children.

invalid_children

atom core/tree/tree.ldn:21 ↑ index
public invalid_children: atom

The requested child interval is not wholly within already published nodes.

public leaf_node: atom

A node with no children of its own.

too_many_leaves

atom core/tree/tree.ldn:25 ↑ index
public too_many_leaves: atom

The branch's leaf total cannot be represented in target usize.

too_many_nodes

atom core/tree/tree.ldn:23 ↑ index
public too_many_nodes: atom

Another node identifier cannot be represented in target usize.

add_branch

function core/tree/tree.ldn:99 ↑ index
public add_branch: (provider: type is mem.allocator, inout value: tree,
                    memory: provider, escaping into value name: utf8,
                    first: node_id, count: usize)
                   -> (id: node_id)
                   ! mem.out_of_memory | too_many_nodes
                   | invalid_children | too_many_leaves

Append a branch over a contiguous interval of existing nodes. Retains the name, validates children and leaf totals before publication, and permits an empty interval.

Edges name only previously published nodes. Their leaf totals are fixed, so their cumulative totals give bounded-stack queries at any depth. Shared children count once for each incoming path, as recursive counting would. An empty child interval has total zero.

add_leaf

function core/tree/tree.ldn:68 ↑ index
public add_leaf: (provider: type is mem.allocator, inout value: tree,
                  memory: provider, escaping into value name: utf8)
                 -> (id: node_id) ! mem.out_of_memory | too_many_nodes

Append a named leaf and return its identifier. Retains the name's backing; allocation or identifier exhaustion leaves existing nodes unchanged.

children

function core/tree/tree.ldn:196 ↑ index
public children: (value: node) -> (first: node_id, count: usize)

Return a branch's children: the count nodes from first on. A leaf has none, and returns the empty range, first and count both zero.

public get: (value: tree, id: node_id)
            -> (result: node from held value) ! mem.not_found

Copy the node selected by an identifier. Reports mem.not_found when the identifier is outside the tree's stored node range; the name remains borrowed from its original backing.

public id: (ordinal: usize) -> (value: node_id)

Wrap an ordinal as a node identifier without validating membership in a tree.

public kind: (value: node) -> (result: node_kind)

Whether value is a leaf or a branch.

public leaves: (value: node) -> (count: usize)

The leaves value counts, precomputed, in constant time: one for a leaf, and a branch's children's total, shared children counting once per incoming path and an empty branch counting zero.

public length: (value: tree) -> (count: usize)

Return the number of published nodes.

public name: (value: node) -> (text: utf8 from value)

Lend the UTF-8 name retained in a node.

public new: () -> (result: tree)

Create an empty tree without allocating.

ordinal

function core/tree/tree.ldn:17 ↑ index
public ordinal: (value: node_id) -> (number: usize)

Return the ordinal carried by a node identifier.

public release: (provider: type is mem.allocator, inout value: tree,
                 memory: provider) -> none

Release node storage through the original allocator and reset the tree. Name backing remains owned by its caller.

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 20 lines
compiler/tests/fixtures/runtime/library-shared-tree/main.ldn20 lines
import core/mem
import core/tree

exercise: () -> (ok: bool) ! mem.not_found =
    ok = false
    value := tree.new()
    return when tree.length(value) <> 0 or tree.ordinal(tree.id(usize(42))) <> 42
    _ = tree.get(value, tree.id(usize(0))) else (problem)
        ok = problem == mem.not_found
        return
    end
end exercise

public main: () -> (code: i32) =
    code = 1
    ok := exercise() else false
    if ok then
        code = 42
    end if
end main