Skip to main content

Graph

Struct Graph 

Source
pub struct Graph<N> { /* private fields */ }
Expand description

A directed graph over nodes of type N.

Implementations§

Source§

impl<N> Graph<N>
where N: Hash + Eq + Clone + Ord,

Source

pub const fn new() -> Self

Construct an empty graph.

Source

pub fn add_node(&mut self, n: N)

Add a node. No-op if already present.

Source

pub fn add_edge(&mut self, from: N, to: N)

Add an edge from -> to, meaning from depends on to. Both endpoints are added as nodes if not already present.

The edge carries no explicit provenance; dep_edges() will report it as DepSource::Structural by default. Use add_dep_edge on Graph<NodeId> to record explicit provenance for v0.2 edges.

Source

pub fn remove_edge(&mut self, from: &N, to: &N)

Remove an edge from -> to. No-op if absent.

Does not touch edge_sources. If the edge was inserted via Graph::add_dep_edge on a Graph<NodeId>, the stale provenance entry will survive and be returned by Graph::dep_edges if the edge is re-added later. Use Graph::remove_dep_edge instead when provenance correctness matters.

Source

pub fn node_count(&self) -> usize

Number of nodes in the graph.

Source

pub fn nodes(&self) -> impl Iterator<Item = &N>

Iterate over all nodes in Ord order.

Source

pub fn dependencies_of<'a>( &'a self, n: &N, ) -> impl Iterator<Item = &'a N> + 'a + use<'a, N>

Iterate the dependencies of n (the set edges[n]).

Source

pub fn topological_sort(&self) -> Result<Vec<N>, Cycle<N>>

Topological sort using Kahn’s algorithm.

Returns nodes with no remaining dependencies first. Ties are broken by the smallest node per Ord, so the result is deterministic.

Source

pub fn reverse_topological_sort(&self) -> Result<Vec<N>, Cycle<N>>

Reverse topological sort: dependents first, dependencies last. Used for drop ordering (drop the index before the table it indexes).

Source

pub fn edges(&self) -> impl Iterator<Item = (&N, &N)>

Iterate all edges as (from, to) pairs in ascending (from, to) order by Ord.

Source§

impl Graph<NodeId>

Source

pub fn add_dep_edge(&mut self, from: NodeId, to: NodeId, source: DepSource)

Add an edge from -> to and record its DepSource.

If an edge between these two nodes already exists (inserted via Graph::add_edge or a prior call to this method), the source is overwritten with the new value. Both endpoints are added as nodes if not already present.

Use this instead of Graph::add_edge when populating v0.2 edges from AST walks (AstExtracted) or -- @pgevolve dep: directives (AstDeclared).

Source

pub fn remove_dep_edge(&mut self, from: &NodeId, to: &NodeId)

Remove an edge from -> to and its DepSource from the source map. No-op if the adjacency (or source entry) is absent.

Prefer this over Graph::remove_edge whenever the edge may have been inserted via Self::add_dep_edge; it prevents stale provenance from surviving a remove-then-re-add cycle.

Source

pub fn dep_edges(&self) -> impl Iterator<Item = DepEdge> + '_

Iterate edges as DepEdge records with per-edge provenance.

Edges inserted via Self::add_dep_edge carry their recorded DepSource. Edges inserted via Graph::add_edge default to DepSource::Structural, preserving v0.1 behaviour.

Trait Implementations§

Source§

impl<N: Clone> Clone for Graph<N>

Source§

fn clone(&self) -> Graph<N>

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<N: Debug> Debug for Graph<N>

Source§

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

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

impl<N> Default for Graph<N>
where N: Hash + Eq + Clone + Ord,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<N> Freeze for Graph<N>

§

impl<N> RefUnwindSafe for Graph<N>
where N: RefUnwindSafe,

§

impl<N> Send for Graph<N>
where N: Send,

§

impl<N> Sync for Graph<N>
where N: Sync,

§

impl<N> Unpin for Graph<N>

§

impl<N> UnsafeUnpin for Graph<N>

§

impl<N> UnwindSafe for Graph<N>
where N: RefUnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.