Struct Dag

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

An incremental directed acyclic graph, however the word ‘acyclic’ is used, this structure allows the existence of cycles in the hope that they will eventually be resolved and provides APIs to report the cycles to the user of the structure.

Implementations§

Source§

impl<N> Dag<N>
where N: Hash + Eq,

Source

pub fn new() -> Self

Create a new empty DAG instance.

Source

pub fn insert(&mut self, node: N) -> bool

Insert a new node to the graph. Returns false if the node already existed in the graph.

Source

pub fn remove<Q>(&mut self, node: &Q) -> Option<N>
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove a node from the graph. Returns the node which got removed in an Option, if the node did not exist in the first place None is returned.

Source

pub fn connect<Q>(&mut self, v: &Q, u: &Q) -> Result<bool, Error>
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

in the graph, Ok(false) is returned if the connection already existed in the graph.

If the same node is passed for both the values of v and u then Err(Error::SelfLoop) is returned.

use interactive_dag::{Dag, Error};
let mut g = Dag::<u32>::new();
assert_eq!(g.connect(&0, &0), Err(Error::NotFound));
g.insert(0);
assert_eq!(g.connect(&0, &0), Err(Error::SelfLoop));
g.insert(1);
assert_eq!(g.connect(&0, &1), Ok(true));
assert_eq!(g.connect(&0, &1), Ok(false));
Source

pub fn disconnect<Q>(&mut self, v: &Q, u: &Q) -> Result<bool, Error>
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an edge from the graph.

Source

pub fn contains<Q>(&self, v: &Q) -> bool
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the graph contains the given node.

Source

pub fn is_connected<Q>(&self, v: &Q, u: &Q) -> bool
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if there is a direct edge connection v to u.

Source

pub fn is_reachable<Q>(&self, v: &Q, u: &Q) -> bool
where N: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if there is a path from v to u.

Trait Implementations§

Source§

impl<N> Default for Dag<N>
where N: Hash + Eq,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<N> Freeze for Dag<N>

§

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

§

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

§

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

§

impl<N> Unpin for Dag<N>
where N: Unpin,

§

impl<N> UnwindSafe for Dag<N>
where N: UnwindSafe,

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