Struct fast_graph::Graph
source · pub struct Graph<N, E> {
pub nodes: SlotMap<NodeID, Node<N>>,
pub edges: SlotMap<EdgeID, Edge<E>>,
}
Expand description
The default Graph struct which implements the GraphInterface trait.
§Examples
use fast_graph::{Graph, Node, Edge};
/* We need to have this trait in scope: */
use fast_graph::{GraphInterface};
#[derive(Clone, Debug)]
struct EdgeData(String);
#[derive(Clone, Debug)]
struct NodeData(String);
let mut graph: Graph<NodeData, EdgeData> = Graph::new();
let node1 = graph.add_node(NodeData("Node 1".into()));
let node2 = graph.add_node(NodeData("Node 2".into()));
let edge1 = graph.add_edge(node1, node2, EdgeData("Edge 1".into()));
assert_eq!(graph.node(node1).unwrap().id, node1);
assert_eq!(graph.edge(edge1).unwrap().id, edge1);
graph.remove_node(node1).unwrap();
assert!(graph.node(node1).is_err());
assert_eq!(graph.node(node2).unwrap().id, node2);
println!("{:#?}", graph);
Fields§
§nodes: SlotMap<NodeID, Node<N>>
§edges: SlotMap<EdgeID, Edge<E>>
Implementations§
Trait Implementations§
source§impl<N, E> GraphInterface for Graph<N, E>
impl<N, E> GraphInterface for Graph<N, E>
type NodeData = N
type EdgeData = E
fn nodes(&self) -> impl Iterator<Item = NodeID>
fn node_count(&self) -> usize
fn node(&self, id: NodeID) -> Result<&Node<N>, GraphError>
fn node_mut(&mut self, id: NodeID) -> Result<&mut Node<N>, GraphError>
fn edge(&self, id: EdgeID) -> Result<&Edge<E>, GraphError>
fn edge_mut(&mut self, id: EdgeID) -> Result<&mut Edge<E>, GraphError>
fn remove_node(&mut self, id: NodeID) -> Result<(), GraphError>
fn remove_edge(&mut self, id: EdgeID) -> Result<(), GraphError>
fn add_node(&mut self, data: N) -> NodeID
fn add_nodes(&mut self, data: &[N]) -> Vec<NodeID>where
N: Clone,
fn add_edges(&mut self, data: &[(NodeID, NodeID)]) -> Vec<EdgeID>
fn add_edge(&mut self, from: NodeID, to: NodeID, data: E) -> EdgeID
fn remove_nodes(&mut self, ids: &[NodeID]) -> Result<(), GraphError>
Auto Trait Implementations§
impl<N, E> Freeze for Graph<N, E>
impl<N, E> RefUnwindSafe for Graph<N, E>where
N: RefUnwindSafe,
E: RefUnwindSafe,
impl<N, E> Send for Graph<N, E>
impl<N, E> Sync for Graph<N, E>
impl<N, E> Unpin for Graph<N, E>
impl<N, E> UnwindSafe for Graph<N, E>where
N: UnwindSafe,
E: UnwindSafe,
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
source§impl<'a, G> IterDepthFirst<'a, G> for Gwhere
G: GraphInterface,
impl<'a, G> IterDepthFirst<'a, G> for Gwhere
G: GraphInterface,
source§fn connected_components(&'a self) -> Vec<HashSet<NodeID>>
fn connected_components(&'a self) -> Vec<HashSet<NodeID>>
Returns a vector of sets of node IDs, where each set is a connected component.
Starts a DFS at every node (except if it’s already been visited) and marks all reachable nodes as being part of the same component.
source§fn iter_depth_first(&'a self, start: NodeID) -> DepthFirstSearch<'a, G> ⓘ
fn iter_depth_first(&'a self, start: NodeID) -> DepthFirstSearch<'a, G> ⓘ
Returns a depth first search iterator starting from a given node