Docs / nablatensor-tensor / com.nablatensor.tensor.tree

final class

TreeUtil

Utilities for traversing and rebuilding nested parameter trees.

Methods

static Flattened flatten(Object tree)

Flattens all non-container values into leaves.

static Flattened flatten(Object tree, Predicate<Object> isLeaf)

Flattens a tree with an additional leaf predicate. null remains an empty node even when the predicate accepts it.

static Object unflatten(TreeDef def, List<?> leaves)

Rebuilds a tree, requiring exactly leafCount() leaves.

static List<Object> leaves(Object tree)

Returns a tree's leaves in deterministic traversal order.

static TreeDef structure(Object tree)

Returns a tree's immutable structural definition.

static Object map(UnaryOperator<T> fn, Object tree)

Applies a unary operation to every leaf and rebuilds the original structure.

static Object map(BiFunction<? super A, ? super B, ? extends R> fn, Object first, Object second)

Applies a binary operation to matching leaves in two trees.

static Object map(NaryOperator fn, Object first, Object... rest)

Applies an operation to matching leaves in one or more trees.

static T reduce(BinaryOperator<T> fn, Object tree, T initial)

Folds leaves from left to right, beginning with initial.

static Object transpose(TreeDef outer, TreeDef inner, Object tree)

Swaps two nested tree levels, as in JAX's tree_transpose.

static void register(Class<?> type, Flattener flattener, Unflattener unflattener)

Registers a node type. Registration is exact-class based, matching JAX's node registration semantics.

static void closeAll(Object tree)

Closes each distinct tensor leaf once. Identity, rather than equals, is intentional: a repeated tensor is one owned buffer.

static Ravelled ravel(Object tree)

Flattens a Tensor-leaf tree into one rank-1 Tensor. Until concat and slicing primitives exist, this operation intentionally transfers through host memory.