Table of Contents

Class PreorderAncestry

Namespace
Graphity
Assembly
Graphity.Core.dll

An Ancestry that also holds the preorder of its forest, for an ancestor test in O(1) and the nodes of every subtree.

public sealed class PreorderAncestry : Ancestry
Inheritance
PreorderAncestry
Inherited Members

Examples

Here, the basic blocks of the method load that body dominates, from the dominators of load:

PreorderAncestry dominance = PreorderAncestry.Of(Ancestry.Of(dominators));
ReadOnlySpan<int> dominatedByBody = dominance.SubtreeOf(body);
// dominatedByBody: [2, 3, 4] - body, then and next

Remarks

Of(Ancestry) builds it from an index and shares the arrays of that index. In preorder, every subtree is a contiguous range: the subtree of node takes the positions PreorderIndexOf(node)..PreorderIndexOf(node) + SubtreeSizeOf(node) - 1 of Preorder. So IsAncestorOf(int, int) is a range test, and SubtreeOf(int) returns a view of that range.

The preorder visits the roots in ascending order, and the children of each node in ascending order. If the node IDs were assigned in this order, as is usual for a syntax tree numbered in source order, the preorder is 0..NodeCount-1, and the subtree of node is node..node + SubtreeSizeOf(node) - 1.

Instances are immutable and safe to share between threads.

Memory: 12 x NodeCount bytes on top of the Ancestry it extends.

Properties

Preorder

Gets all nodes in preorder: the roots in ascending order, and the children of each node in ascending order.

public ImmutableArray<int> Preorder { get; }

Property Value

ImmutableArray<int>

Remarks

The result is an unmodifiable view of this index's own array, not a copy.

Time: O(1).

Methods

IsAncestorOf(int, int)

Returns whether node is an ancestor of descendant: whether it lies on the path from the root of its tree to descendant. A node is its own ancestor.

public override bool IsAncestorOf(int node, int descendant)

Parameters

node int

Node ID in 0..NodeCount-1.

descendant int

Node ID in 0..NodeCount-1.

Returns

bool

true if node is an ancestor of descendant.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node or descendant is out of range.

Of(Ancestry)

Returns a PreorderAncestry for the forest of ancestry.

public static PreorderAncestry Of(Ancestry ancestry)

Parameters

ancestry Ancestry

The index to add the preorder to.

Returns

PreorderAncestry

The index with the preorder.

Remarks

The result shares the arrays of ancestry instead of copying them. The preorder is computed anew on every call, not cached: keep the result to use it more than once. If ancestry already is a PreorderAncestry, returns it.

Time: O(NodeCount), or O(1) if already a PreorderAncestry.
Memory: +12 x NodeCount bytes for the result; while it runs, 8 x NodeCount bytes more; none if already a PreorderAncestry.

PreorderIndexOf(int)

Returns the position of node in Preorder.

public int PreorderIndexOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The position in 0..NodeCount-1.

Remarks

Data in an array indexed by preorder position has every subtree in a contiguous range. Prefix sums over such an array, for example, give the sum over any subtree in O(1).

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

SubtreeOf(int)

Returns the nodes of the subtree of node, in preorder, starting with node.

public ReadOnlySpan<int> SubtreeOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

ReadOnlySpan<int>

The nodes of the subtree.

Remarks

The result is a read-only span over this index's own array, not a copy; to keep it beyond a method, copy it with ToArray().

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

SubtreeSizeOf(int)

Returns the number of nodes in the subtree of node, node included.

public int SubtreeSizeOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The subtree size, at least 1.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.