petgraph::stable_graph

Struct WalkNeighbors

Source
pub struct WalkNeighbors<Ix> { /* private fields */ }
Expand description

A “walker” object that can be used to step through the edge list of a node.

See .detach() for more information.

The walker does not borrow from the graph, so it lets you step through neighbors or incident edges while also mutating graph weights, as in the following example:

use petgraph::visit::Dfs;
use petgraph::Incoming;
use petgraph::stable_graph::StableGraph;

let mut gr = StableGraph::new();
let a = gr.add_node(0.);
let b = gr.add_node(0.);
let c = gr.add_node(0.);
gr.add_edge(a, b, 3.);
gr.add_edge(b, c, 2.);
gr.add_edge(c, b, 1.);

// step through the graph and sum incoming edges into the node weight
let mut dfs = Dfs::new(&gr, a);
while let Some(node) = dfs.next(&gr) {
    // use a detached neighbors walker
    let mut edges = gr.neighbors_directed(node, Incoming).detach();
    while let Some(edge) = edges.next_edge(&gr) {
        gr[node] += gr[edge];
    }
}

// check the result
assert_eq!(gr[a], 0.);
assert_eq!(gr[b], 4.);
assert_eq!(gr[c], 2.);

Implementations§

Source§

impl<Ix: IndexType> WalkNeighbors<Ix>

Source

pub fn next<N, E, Ty: EdgeType>( &mut self, g: &StableGraph<N, E, Ty, Ix>, ) -> Option<(EdgeIndex<Ix>, NodeIndex<Ix>)>

Step to the next edge and its endpoint node in the walk for graph g.

The next node indices are always the others than the starting point where the WalkNeighbors value was created. For an Outgoing walk, the target nodes, for an Incoming walk, the source nodes of the edge.

Source

pub fn next_node<N, E, Ty: EdgeType>( &mut self, g: &StableGraph<N, E, Ty, Ix>, ) -> Option<NodeIndex<Ix>>

Source

pub fn next_edge<N, E, Ty: EdgeType>( &mut self, g: &StableGraph<N, E, Ty, Ix>, ) -> Option<EdgeIndex<Ix>>

Trait Implementations§

Source§

impl<Ix: IndexType> Clone for WalkNeighbors<Ix>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<Ix> Freeze for WalkNeighbors<Ix>
where Ix: Freeze,

§

impl<Ix> RefUnwindSafe for WalkNeighbors<Ix>
where Ix: RefUnwindSafe,

§

impl<Ix> Send for WalkNeighbors<Ix>
where Ix: Send,

§

impl<Ix> Sync for WalkNeighbors<Ix>
where Ix: Sync,

§

impl<Ix> Unpin for WalkNeighbors<Ix>
where Ix: Unpin,

§

impl<Ix> UnwindSafe for WalkNeighbors<Ix>
where Ix: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.