Struct gchemol_graph::NxGraph

source ·
pub struct NxGraph<N, E>
where N: Default, E: Default,
{ /* private fields */ }
Expand description

networkx-like API wrapper around petgraph

Implementations§

source§

impl<N, E> NxGraph<N, E>
where N: Default, E: Default,

Build/Read/Edit Graph

§Example

use gchemol_graph::NxGraph;
 
let mut g = NxGraph::path_graph(2);
let u = g.add_node(2);
let v = g.add_node(3);
g.add_edge(u, v, 5);
 
assert!(g.has_node(u));
assert!(g.has_edge(u, v));
 
// loop over neighbors of node u
for x in g.neighbors(u) {
    dbg!(x);
}
source

pub fn new() -> Self

Build a default Graph

source

pub fn neighbors(&self, n: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_

Returns an iterator over all neighbors of node n.

§Reference
  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html
source

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

Return an iterator over the node indices of the graph

source

pub fn has_node(&self, n: NodeIndex) -> bool

Returns true if the graph contains the node n.

source

pub fn has_edge(&self, u: NodeIndex, v: NodeIndex) -> bool

Returns true if the edge (u, v) is in the graph.

source

pub fn number_of_nodes(&self) -> usize

Returns the number of nodes in the graph.

source

pub fn number_of_edges(&self) -> usize

Returns the number of edges in the graph.

source

pub fn add_node(&mut self, data: N) -> NodeIndex

Add a node with associated data into graph.

source

pub fn add_nodes_from<M: IntoIterator<Item = N>>( &mut self, nodes: M ) -> Vec<NodeIndex>

Add multiple nodes.

source

pub fn add_edge(&mut self, u: NodeIndex, v: NodeIndex, data: E)

Add an edge with data between u and v (no parallel edge). If edge u–v already exists, the associated data will be updated.

§Panics
  • To avoid self-loop, this method will panic if node u and v are the same.
source

pub fn add_edges_from<M: IntoIterator<Item = (NodeIndex, NodeIndex, E)>>( &mut self, edges: M )

Add multiple edges from edges.

source

pub fn remove_edge(&mut self, node1: NodeIndex, node2: NodeIndex) -> Option<E>

Remove an edge between node1 and node2. Return None if trying to remove a non-existent edge.

source

pub fn remove_node(&mut self, n: NodeIndex) -> Option<N>

Removes the node n and all adjacent edges. Return None if trying to remove a non-existent node.

source

pub fn clear(&mut self)

Remove all nodes and edges

source

pub fn clear_edges(&mut self)

Remove all edges

source§

impl<N, E> NxGraph<N, E>
where N: Default, E: Default,

source

pub fn raw_graph(&self) -> &StableUnGraph<N, E>

Provides read access to raw Graph struct.

source

pub fn raw_graph_mut(&mut self) -> &mut StableUnGraph<N, E>

Provides mut access to raw Graph struct.

source§

impl<N, E> NxGraph<N, E>
where N: Default + Clone, E: Default + Clone,

Methods for creating NxGraph struct

source

pub fn from_raw_graph(graph: StableUnGraph<N, E>) -> Self

Return NxGraph from raw petgraph struct.

source§

impl NxGraph<usize, usize>

source

pub fn path_graph(n: usize) -> Self

Returns the Path graph P_n of linearly connected nodes. Node data and edge data are usize type, mainly for test purpose.

source§

impl<N, E> NxGraph<N, E>
where N: Default, E: Default,

Node view and Edge view for NxGraph.

§Example

use gchemol_graph::NxGraph;
 
let mut g = NxGraph::path_graph(3);
let u = g.add_node(5);
let v = g.add_node(2);
let w = g.add_node(1);
g.add_edge(u, v, 7);
g.add_edge(u, w, 6);
 
// loop over nodes
for (node_index, node_data) in g.nodes() {
    // do something
}
 
// get node data of node `u`
let nodes = g.nodes();
let node_u = nodes[u];
assert_eq!(node_u, 5);
 
// Collect nodes into HashMap
let nodes: std::collections::HashMap<_, _> = g.nodes().collect();
assert_eq!(nodes.len(), 6);
 
// loop over edges
for (u, v, edge_data) in g.edges() {
    // dbg!(u, v, edge_data)
}
 
// get edge data
let edges = g.edges();
let edge_uv = edges[(u, v)];
assert_eq!(edge_uv, 7);
source

pub fn nodes(&self) -> Nodes<'_, N, E>

A Node view of the Graph.

§Reference
  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html
source

pub fn edges(&self) -> Edges<'_, N, E>

An Edge view of the Graph.

§Reference
  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.edges.html

Trait Implementations§

source§

impl<N, E> Clone for NxGraph<N, E>
where N: Default + Clone, E: Default + Clone,

source§

fn clone(&self) -> NxGraph<N, E>

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
source§

impl<N, E> Debug for NxGraph<N, E>
where N: Default + Debug, E: Default + Debug,

source§

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

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

impl<N, E> Default for NxGraph<N, E>
where N: Default + Default, E: Default + Default,

source§

fn default() -> NxGraph<N, E>

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

impl<'de, N, E> Deserialize<'de> for NxGraph<N, E>
where N: Default + Deserialize<'de>, E: Default + Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<N, E> Index<(NodeIndex, NodeIndex)> for NxGraph<N, E>
where N: Default, E: Default,

§

type Output = E

The returned type after indexing.
source§

fn index(&self, e: (NodeIndex, NodeIndex)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<N, E> Index<NodeIndex> for NxGraph<N, E>
where N: Default, E: Default,

§

type Output = N

The returned type after indexing.
source§

fn index(&self, n: NodeIndex) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<N, E> IndexMut<(NodeIndex, NodeIndex)> for NxGraph<N, E>
where N: Default, E: Default,

source§

fn index_mut(&mut self, e: (NodeIndex, NodeIndex)) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<N, E> IndexMut<NodeIndex> for NxGraph<N, E>
where N: Default, E: Default,

source§

fn index_mut(&mut self, n: NodeIndex) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<N, E> Serialize for NxGraph<N, E>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<N, E> RefUnwindSafe for NxGraph<N, E>

§

impl<N, E> Send for NxGraph<N, E>
where E: Send, N: Send,

§

impl<N, E> Sync for NxGraph<N, E>
where E: Sync, N: Sync,

§

impl<N, E> Unpin for NxGraph<N, E>
where E: Unpin, N: Unpin,

§

impl<N, E> UnwindSafe for NxGraph<N, E>
where E: UnwindSafe, 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> ToOwned for T
where T: Clone,

§

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

§

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

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,