Skip to main content

Ancestry

Struct Ancestry 

Source
pub struct Ancestry { /* private fields */ }
Expand description

An index of a forest for ancestor queries: the parent and depth of a node, whether one node is an ancestor of another, and the lowest common ancestor of two nodes.

Ancestry::of builds the index of a forest whose edges point from parent to child, such as a syntax tree. For a forest whose edges point from child to parent, pass the transposed graph: Ancestry::of(&BiAdjacentGraph::of(&forest).transposed()). Ancestry::of_dominators builds the index of a dominator tree, in which the ancestors of a node are its dominators. The index is built once and answers each query in O(log node_count) at most, however deep the forest; PreorderAncestry::of adds an ancestor test in O(1) and the nodes of each subtree.

A node is its own ancestor, so is_ancestor_of(a, a) is true and lowest_common_ancestor_of(a, a) is Some(a). Two nodes in different trees of the forest have no common ancestor. Uses heavy-light decomposition.

use graphity_core::{Ancestry, GraphBuilder, PreorderAncestry};

//      0
//    1   2
//   3 4   5
let mut b = GraphBuilder::new();
b.add_nodes(6);
b.add_edge_pairs(&[0, 1, 0, 2, 1, 3, 1, 4, 2, 5]);
let ancestry = Ancestry::of(&b.build()).unwrap();
assert_eq!(ancestry.lowest_common_ancestor_of(3, 4), Some(1));
assert_eq!(ancestry.lowest_common_ancestor_of(4, 5), Some(0));
assert!(ancestry.is_ancestor_of(1, 4));
assert_eq!(ancestry.depth_of(5), 2);

let preorder = PreorderAncestry::of(&ancestry);
assert_eq!(preorder.subtree_of(1), [1, 3, 4]);

Where Java’s PreorderAncestry extends Ancestry and overrides isAncestorOf, a PreorderAncestry dereferences to its Ancestry, and an Ancestry that belongs to a PreorderAncestry answers is_ancestor_of in O(1) as well.

Instances are immutable and safe to share between threads (Send and Sync).

Memory: 12 x node_count bytes, of which an index built by Ancestry::of_dominators shares 4 x node_count bytes with the dominators.

Implementations§

Source§

impl Ancestry

Source

pub fn of(forest: &Graph) -> Result<Ancestry, GraphError>

Returns the ancestor index of forest, whose edges point from parent to child - Java’s overload of(Graph).

In such a forest, no node has more than one in-node, and there is no cycle, not even a self-loop; graphs::is_forest tests this without an error. The roots of the trees are the root nodes of the graph.

Time: O(node_count + edge_count). Memory: 12 x node_count bytes for the result; while it runs, 12 x node_count bytes more.

§Errors

An InvalidArgument error if a node of forest has more than one in-node, or forest has a cycle.

Source

pub fn of_dominators(dominators: &Dominators) -> Ancestry

Returns the ancestor index of the dominator tree of dominators, in which the ancestors of a node are its dominators - Java’s overload of(Dominators).

is_ancestor_of(a, b) returns whether a dominates b. lowest_common_ancestor_of(a, b) returns the closest node that dominates both, or None if no node does. With several entries, only a virtual root lies above them. A node that no entry reaches is a tree of its own and dominates only itself here; Dominators::is_reachable returns false for such a node.

The result is the same as that of Ancestry::of(&graphs::dominator_tree(dominators)), but shares the immediate dominators instead of building the tree as a graph.

Time: O(node_count). Memory: 8 x node_count bytes for the result, which shares the immediate dominators; while it runs, 20 x node_count bytes more.

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 Ancestry

Source§

fn clone(&self) -> Ancestry

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 Ancestry

Source§

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

Formats the value using the given formatter. Read more

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