pub struct Graph { /* private fields */ }Expand description
A directed acyclic graph of named nodes.
The invariant — unique ids, edges between nodes that exist, no cycles — is
upheld by the constructors, so topological_sort cannot fail. Adjacency is
computed on the fly: O(n) where it could be O(1), deliberately.
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn add_node(&mut self, id: impl Into<NodeId>) -> Result<&NodeId, GraphError>
pub fn add_node(&mut self, id: impl Into<NodeId>) -> Result<&NodeId, GraphError>
Adds a node, unless the id is taken.
Sourcepub fn add_edge(
&mut self,
source: impl Into<NodeId>,
target: impl Into<NodeId>,
) -> Result<&Edge, GraphError>
pub fn add_edge( &mut self, source: impl Into<NodeId>, target: impl Into<NodeId>, ) -> Result<&Edge, GraphError>
Connects two nodes that exist, unless the edge is already there or would close a cycle.
Sourcepub fn free_id(&self, wanted: &str) -> NodeId
pub fn free_id(&self, wanted: &str) -> NodeId
A free id starting from the one you want, suffixing _2, _3, … if needed.
Sourcepub fn predecessors(&self, id: &NodeId) -> Vec<&NodeId>
pub fn predecessors(&self, id: &NodeId) -> Vec<&NodeId>
The nodes feeding into id, in their edges’ insertion order.
Sourcepub fn successors(&self, id: &NodeId) -> Vec<&NodeId>
pub fn successors(&self, id: &NodeId) -> Vec<&NodeId>
The nodes id feeds into, in their edges’ insertion order.
Sourcepub fn topological_sort(&self) -> Vec<&NodeId>
pub fn topological_sort(&self) -> Vec<&NodeId>
The nodes in an order where each comes after its predecessors. Ties break by insertion order, so it is deterministic.
Trait Implementations§
impl Eq for Graph
impl StructuralPartialEq for Graph
Auto Trait Implementations§
impl Freeze for Graph
impl RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl UnsafeUnpin for Graph
impl UnwindSafe for Graph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more