[][src]Struct toposort_scc::ArenaGraph

pub struct ArenaGraph<'a, T, A: ArenaBehavior> { /* fields omitted */ }

An adjacency-list-based graph data structure wrapping an Arena from the id-arena crate.

Stores graph vertices as lists of incoming and outgoing edges by their Id in the graph.

Implementations

impl<'a, T, A: ArenaBehavior> ArenaGraph<'a, T, A>[src]

pub fn from_graph<F>(g: &'a Arena<T, A>, f: F) -> ArenaGraph<'a, T, A> where
    F: FnMut(ArenaGraphBuilder<'_, 'a, T, A>, &T), 
[src]

Create a new graph from an existing Arena-based graph-like data structure

The given closure will be called once for every element of g, with an ArenaGraphBuilder instance so that edges can be easily added.

Example

This example creates a graph of dependencies in a hypothetical compiler or build tool, with edges from a dependency to the targets that use them.

use id_arena::Arena;
use id_arena::Id;
use toposort_scc::ArenaGraph;

// a target during compilation, having a name and dependencies
struct Target { name: &'static str, deps: Vec<Id<Target>> }
impl Target {
    fn new(name: &'static str) -> Self {
        Target { name, deps: Vec::new() }
    }
}

let mut arena: Arena<Target> = Arena::new();

let program = arena.alloc(Target::new("program"));
let main_c = arena.alloc(Target::new("main.c"));
let util_c = arena.alloc(Target::new("util.c"));
let util_h = arena.alloc(Target::new("util.h"));
let libfoo_so = arena.alloc(Target::new("libfoo_so"));

arena[program].deps.extend_from_slice(&[main_c, util_c, libfoo_so]);
arena[main_c].deps.push(util_h);
arena[util_c].deps.push(util_h);

let g = ArenaGraph::from_graph(&arena, |mut builder, target| {
    for &dep in &target.deps {
        builder.add_in_edge(dep);
    }
});

To get a graph with edges in the other direction, use .add_out_edge() or the .transpose() method of the graph.

pub fn arena_id(&self) -> u32[src]

Returns the id of the arena this graph belongs to

pub fn as_index_graph(&self) -> &IndexGraph[src]

Returns a reference to the underlying IndexGraph

pub fn into_index_graph(self) -> IndexGraph[src]

Returns the underlying IndexGraph

pub fn try_toposort(self) -> Result<Vec<A::Id>, ArenaGraph<'a, T, A>>[src]

Try to perform topological sort on the graph

If the graph contains no cycles, finds the topological ordering of this graph using Kahn's algorithm and returns it as Ok(sorted).

If the graph contains cycles, returns the graph as Err(self).

The difference between this function and IndexGraph::try_toposort() is that this function returns id-arena ids instead of indices.

For examples, see IndexGraph::toposort()

pub fn toposort(self) -> Option<Vec<A::Id>>[src]

Perform topological sort on the graph

If the graph contains no cycles, finds the topological ordering of this graph using Kahn's algorithm and returns it as Some(sorted).

If the graph contains cycles, returns None.

The difference between this function and IndexGraph::toposort() is that this function returns id-arena ids instead of indices.

For examples, see IndexGraph::toposort()

pub fn scc(self) -> Vec<Vec<A::Id>>[src]

Find strongly connected components

Finds the strongly connected components of this graph using Kosaraju's algorithm and returns them.

The difference between this function and IndexGraph::scc() is that this function returns id-arena ids instead of indices.

For examples, see IndexGraph::toposort_or_scc()

pub fn toposort_or_scc(self) -> Result<Vec<A::Id>, Vec<Vec<A::Id>>>[src]

Perform topological sort or find strongly connected components

If the graph contains no cycles, finds the topological ordering of this graph using Kahn's algorithm and returns it as Ok(sorted).

If the graph contains cycles, finds the strongly connected components of this graph using Kosaraju's algorithm and returns them as Err(cycles).

The difference between this function and IndexGraph::toposort_or_scc() is that this function returns id-arena ids instead of indices.

For examples, see IndexGraph::toposort_or_scc()

Trait Implementations

impl<'a, T: Clone, A: Clone + ArenaBehavior> Clone for ArenaGraph<'a, T, A>[src]

impl<'a, T: Debug, A: Debug + ArenaBehavior> Debug for ArenaGraph<'a, T, A>[src]

impl<'_, T, A: ArenaBehavior> Index<<A as ArenaBehavior>::Id> for ArenaGraph<'_, T, A>[src]

type Output = Vertex

The returned type after indexing.

Auto Trait Implementations

impl<'a, T, A> RefUnwindSafe for ArenaGraph<'a, T, A> where
    T: RefUnwindSafe

impl<'a, T, A> Send for ArenaGraph<'a, T, A> where
    T: Sync

impl<'a, T, A> Sync for ArenaGraph<'a, T, A> where
    T: Sync

impl<'a, T, A> Unpin for ArenaGraph<'a, T, A>

impl<'a, T, A> UnwindSafe for ArenaGraph<'a, T, A> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.