pub struct PreorderAncestry { /* private fields */ }Expand description
An Ancestry that also holds the preorder of its forest, for an ancestor test in O(1) and the
nodes of every subtree.
PreorderAncestry::of 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
preorder_index_of(node)..preorder_index_of(node) + subtree_size_of(node) of
preorder. So is_ancestor_of is a range test, and
subtree_of returns a view of that range. Here, the basic blocks of the method
load that body dominates, from the dominators of load:
let dominance = PreorderAncestry::of(&Ancestry::of_dominators(&dominators));
let dominated_by_body = dominance.subtree_of(body); // [2, 3, 4] - body, then and nextThe 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..node_count, and the subtree of node is
node..node + subtree_size_of(node).
Java’s PreorderAncestry extends Ancestry; this type dereferences to its Ancestry instead.
So every method of the index applies, and a &PreorderAncestry is accepted where a &Ancestry is.
Instances are immutable and safe to share between threads (Send and Sync).
Memory: 12 x node_count bytes on top of the Ancestry it extends.
Implementations§
Source§impl PreorderAncestry
impl PreorderAncestry
Sourcepub fn of(ancestry: &Ancestry) -> PreorderAncestry
pub fn of(ancestry: &Ancestry) -> PreorderAncestry
Returns a PreorderAncestry for the forest of ancestry.
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 belongs
to a PreorderAncestry, the result shares its preorder as well.
Time: O(node_count), or O(1) if already a PreorderAncestry.
Memory: +12 x node_count bytes for the result; while it runs, 8 x node_count bytes more;
none if already a PreorderAncestry.
Sourcepub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
pub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
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.
Time: O(1).
§Panics
With an index out of bounds message if node or descendant is not in 0..node_count.
Sourcepub fn subtree_size_of(&self, node: u32) -> u32
pub fn subtree_size_of(&self, node: u32) -> u32
Returns the number of nodes in the subtree of node, node included.
Time: O(1).
§Panics
With an index out of bounds message if node is not in 0..node_count.
Sourcepub fn subtree_of(&self, node: u32) -> &[u32]
pub fn subtree_of(&self, node: u32) -> &[u32]
Returns the nodes of the subtree of node, in preorder, starting with node.
The result is a view of this index’s own array, not a copy.
Time: O(1).
§Panics
With an index out of bounds message if node is not in 0..node_count.
Sourcepub fn preorder(&self) -> &[u32]
pub fn preorder(&self) -> &[u32]
Returns all nodes in preorder: the roots in ascending order, and the children of each node in ascending order.
The result is a view of this index’s own array, not a copy.
Time: O(1).
Sourcepub fn preorder_index_of(&self, node: u32) -> u32
pub fn preorder_index_of(&self, node: u32) -> u32
Returns the position of node in preorder.
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).
§Panics
With an index out of bounds message if node is not in 0..node_count.
Sourcepub fn as_ancestry(&self) -> &Ancestry
pub fn as_ancestry(&self) -> &Ancestry
Returns this index as a plain Ancestry; the same as dereferencing.
Time: O(1).
Methods from Deref<Target = Ancestry>§
Sourcepub fn lowest_common_ancestor_of(&self, a: u32, b: u32) -> Option<u32>
pub fn lowest_common_ancestor_of(&self, a: u32, b: u32) -> Option<u32>
Returns the lowest common ancestor of a and b, or None if they are in different trees -
Java’s -1.
The lowest common ancestor is the deepest node that is an ancestor of both. As a node is its own
ancestor, the result is a if a is an ancestor of b.
Time: O(log node_count).
§Panics
With an index out of bounds message if a or b is not in 0..node_count.
Sourcepub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
pub fn is_ancestor_of(&self, node: u32, descendant: u32) -> bool
Returns whether node is an ancestor of descendant: whether it lies on the path from the root of
its tree to descendant. Equivalent to
lowest_common_ancestor_of(node, descendant) == Some(node).
A node is its own ancestor. PreorderAncestry answers in O(1), and so does an Ancestry that
belongs to one.
Time: O(log node_count), or O(1) for the Ancestry of a PreorderAncestry.
§Panics
With an index out of bounds message if node or descendant is not in 0..node_count.
Trait Implementations§
Source§impl AsRef<Ancestry> for PreorderAncestry
impl AsRef<Ancestry> for PreorderAncestry
Source§impl Clone for PreorderAncestry
impl Clone for PreorderAncestry
Source§fn clone(&self) -> PreorderAncestry
fn clone(&self) -> PreorderAncestry
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more