Skip to main content

PreorderAncestry

Struct PreorderAncestry 

Source
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 next

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..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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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>§

Source

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.

Source

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.

Source

pub fn depth_of(&self, node: u32) -> u32

Returns the depth of node: the number of edges from the root of its tree to it. A root has depth 0.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn parent_of(&self, node: u32) -> Option<u32>

Returns the parent of node, or None if it is the root of its tree - Java’s -1.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Trait Implementations§

Source§

impl AsRef<Ancestry> for PreorderAncestry

Source§

fn as_ref(&self) -> &Ancestry

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for PreorderAncestry

Source§

fn clone(&self) -> PreorderAncestry

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PreorderAncestry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for PreorderAncestry

Source§

type Target = Ancestry

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Ancestry

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.