Macaw.Draw.Layout.Hierarchy (macaw_draw v0.1.0)

Copy Markdown View Source

The tree every hierarchical layout reads (spec/08 §2): Tree, Cluster, Pack, Partition and Treemap. A hierarchy is an immutable tree of %Hierarchy{} structs; every operation answers a new root.

From rows with roles (§2.2). stratify/3 builds one from a slot's id and parent columns, attaching every row the root reaches, whatever the row order, and answering the rows it does not reach:

iex> alias Macaw.Draw.Layout.Hierarchy
iex> {:ok, root, unreached} =
...>   Hierarchy.stratify(["gc", "root", "c", "lost"], ["c", nil, "root", "nobody"], [])
iex> {root.id, root.height, unreached}
{"root", 2, [3]}

From nested data, new/2 builds one from a nested map, its children under "children" or :children.

A node's parent is the parent as it was built (§2.1.1): its children nil, its height 0, its value and layout fields unset. Layouts and aggregations never rewrite it, so two siblings' parents are always equal.

This is visualize's Visualize.Layout.Hierarchy, its stratify/2 reading role columns rather than rows, with no root, duplicate ids and unequal lengths as faults and the rows it drops answered rather than dropped silently (spec/08 decision 2).

Summary

Types

A refusal: faults with their paths.

t()

Functions

The parent, grandparent and so on to the root; [] for the root (§2.4).

copy/2 with no transform.

The tree rebuilt with fresh parent links, every field copied (§2.3); given transform, each node's data becomes transform.(data).

Every leaf's value set to 1 and every internal node's to its count of leaves (§2.3).

The node and every descendant, pre-order (§2.4).

Calls fun on every node, pre-order (parent before children), and answers :ok (§2.4).

Calls fun on every node, post-order (children before parent), and answers :ok.

The descendants whose children is nil or [], pre-order (§2.4).

{parent, child} for every parent-child pair, in pre-order of the parent (§2.4).

new/2 with the default options.

A hierarchy from one nested map (§2.2). Option children: is a function of one datum answering its child data, nil or [] for a leaf; by default "children", else :children. A node's id is the datum's "id", else "name", else :id, else :name. A datum that is not a map is {path, {:type, :map}} at its path of child positions, [] for the root; an empty child list makes a leaf.

The shortest path from source to target through their lowest common ancestor, each endpoint once (§2.4): path(n, n) is [n]. Nodes are matched by the data of their ancestor chain from the root, so a node equals the snapshot of itself a descendant holds.

The children of every node sorted stably by comparator.(a, b), true when a sorts before b (§2.3): children the comparator does not order keep their order. Leaves are unchanged.

A hierarchy from the id and parent columns of one slot (§2.2): row i is the node of id ids[i] whose parent id is parents[i], its data the row i.

Every node's value set to value_fn.(node) plus its children's values, post-order (§2.3); value_fn is called for internal nodes and leaves alike, and a nil or non-numeric reading counts as 0. visualize passed the datum; here the node, so a column read by node.data and a nested datum read by key are both one argument.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

t()

@type t() :: %Macaw.Draw.Layout.Hierarchy{
  angle: number() | nil,
  children: [t()] | nil,
  data: term(),
  depth: non_neg_integer(),
  height: non_neg_integer(),
  id: term(),
  parent: t() | nil,
  r: number() | nil,
  radius: number() | nil,
  value: number() | nil,
  x: number() | nil,
  x0: number() | nil,
  x1: number() | nil,
  y: number() | nil,
  y0: number() | nil,
  y1: number() | nil
}

Functions

ancestors(node)

@spec ancestors(t()) :: [t()] | error()

The parent, grandparent and so on to the root; [] for the root (§2.4).

copy(node)

@spec copy(t()) :: t() | error()

copy/2 with no transform.

copy(node, transform)

@spec copy(t(), (term() -> term()) | nil) :: t() | error()

The tree rebuilt with fresh parent links, every field copied (§2.3); given transform, each node's data becomes transform.(data).

count(node)

@spec count(t()) :: t() | error()

Every leaf's value set to 1 and every internal node's to its count of leaves (§2.3).

descendants(node)

@spec descendants(t()) :: [t()] | error()

The node and every descendant, pre-order (§2.4).

each(node, fun)

@spec each(t(), (t() -> term())) :: :ok | error()

Calls fun on every node, pre-order (parent before children), and answers :ok (§2.4).

each_after(node, fun)

@spec each_after(t(), (t() -> term())) :: :ok | error()

Calls fun on every node, post-order (children before parent), and answers :ok.

leaves(node)

@spec leaves(t()) :: [t()] | error()

The descendants whose children is nil or [], pre-order (§2.4).

links(node)

@spec links(t()) :: [{t(), t()}] | error()

{parent, child} for every parent-child pair, in pre-order of the parent (§2.4).

new(data)

@spec new(map()) :: {:ok, t()} | error()

new/2 with the default options.

new(data, opts)

@spec new(map(), keyword()) :: {:ok, t()} | error()

A hierarchy from one nested map (§2.2). Option children: is a function of one datum answering its child data, nil or [] for a leaf; by default "children", else :children. A node's id is the datum's "id", else "name", else :id, else :name. A datum that is not a map is {path, {:type, :map}} at its path of child positions, [] for the root; an empty child list makes a leaf.

path(source, target)

@spec path(t(), t()) :: [t()] | error()

The shortest path from source to target through their lowest common ancestor, each endpoint once (§2.4): path(n, n) is [n]. Nodes are matched by the data of their ancestor chain from the root, so a node equals the snapshot of itself a descendant holds.

sort(node, comparator)

@spec sort(t(), (t(), t() -> boolean())) :: t() | error()

The children of every node sorted stably by comparator.(a, b), true when a sorts before b (§2.3): children the comparator does not order keep their order. Leaves are unchanged.

stratify(ids, parents, opts)

@spec stratify([term()], [term()], keyword()) ::
  {:ok, t(), [non_neg_integer()]} | error()

A hierarchy from the id and parent columns of one slot (§2.2): row i is the node of id ids[i] whose parent id is parents[i], its data the row i.

The root is the first row whose parent is nil or "". Parent links are resolved over the whole input first, so every row the root reaches is attached, children in row order. Answers {:ok, root, unreached}, unreached the rows the root does not reach, ascending: a row whose parent names no row, a second root, every row on a cycle. No root is {[:parent], :no_root}, an id given twice {[:ids, i], {:duplicate, id}} for each later row, and lists of two lengths {[:parents], {:length, length(ids)}}.

sum(node, value_fn)

@spec sum(t(), (t() -> term())) :: t() | error()

Every node's value set to value_fn.(node) plus its children's values, post-order (§2.3); value_fn is called for internal nodes and leaves alike, and a nil or non-numeric reading counts as 0. visualize passed the datum; here the node, so a column read by node.data and a nested datum read by key are both one argument.