pub struct GraphBuilder<N, W = f64> { /* private fields */ }Expand description
A fluent builder for constructing graphs from either representation.
Call GraphBuilder::new, chain configuration and node/edge additions,
then finalise with build_adjacency_list
or build_adjacency_matrix.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.directed()
.node("A")
.node("B")
.node("C")
.edge(0, 1, 1.5)
.edge(1, 2, 2.0)
.build_adjacency_list();
assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);Implementations§
Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Sourcepub fn directed(self) -> Self
pub fn directed(self) -> Self
Configures the graph to be directed (default).
§Examples
use graph_core::GraphBuilder;
let b: GraphBuilder<(), f64> = GraphBuilder::new().directed();Sourcepub fn undirected(self) -> Self
pub fn undirected(self) -> Self
Configures the graph to be undirected.
Each call to edge will insert edges in both directions.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<(), f64>::new()
.undirected()
.node(())
.node(())
.edge(0, 1, 1.0)
.build_adjacency_list();
assert!(g.contains_edge(0usize.into(), 1usize.into()));
assert!(g.contains_edge(1usize.into(), 0usize.into()));Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Sourcepub fn node(self, data: N) -> Self
pub fn node(self, data: N) -> Self
Adds a node with the given data and returns self for chaining.
Nodes are assigned indices in insertion order (0, 1, 2 …).
§Examples
use graph_core::GraphBuilder;
let b = GraphBuilder::<&str, f64>::new()
.node("X")
.node("Y");Source§impl<N, W: Clone> GraphBuilder<N, W>
impl<N, W: Clone> GraphBuilder<N, W>
Sourcepub fn build_adjacency_list(self) -> AdjacencyList<N, W>
pub fn build_adjacency_list(self) -> AdjacencyList<N, W>
Consumes the builder and produces an AdjacencyList.
§Panics
Panics if any edge references a node index that is out of range.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.node("A")
.node("B")
.edge(0, 1, 5.0)
.build_adjacency_list();
assert_eq!(g.edge_count(), 1);Sourcepub fn build_adjacency_matrix(self) -> AdjacencyMatrix<N, W>
pub fn build_adjacency_matrix(self) -> AdjacencyMatrix<N, W>
Consumes the builder and produces an AdjacencyMatrix.
§Panics
Panics if any edge references a node index that is out of range.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.node("A")
.node("B")
.edge(0, 1, 2.0)
.build_adjacency_matrix();
assert!(g.contains_edge(0usize.into(), 1usize.into()));Trait Implementations§
Auto Trait Implementations§
impl<N, W> Freeze for GraphBuilder<N, W>
impl<N, W> RefUnwindSafe for GraphBuilder<N, W>where
N: RefUnwindSafe,
W: RefUnwindSafe,
impl<N, W> Send for GraphBuilder<N, W>
impl<N, W> Sync for GraphBuilder<N, W>
impl<N, W> Unpin for GraphBuilder<N, W>
impl<N, W> UnwindSafe for GraphBuilder<N, W>where
N: UnwindSafe,
W: 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