pub struct Builder<T> { /* private fields */ }Expand description
Graph builder.
Implementations§
Source§impl<T> Builder<T>
impl<T> Builder<T>
Sourcepub fn add_node(&mut self, data: T) -> usize
pub fn add_node(&mut self, data: T) -> usize
Adds a node to the graph.
§Examples
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");Sourcepub fn add_edge(&mut self, source: usize, target: usize) -> Result
pub fn add_edge(&mut self, source: usize, target: usize) -> Result
Adds an edge to the graph.
§Errors
In case the source or target node doesn’t exist, Error::NotFound is
returned, to make sure the graph does not contain stale node references.
By returning an error instead of panicking, we can provide recoverable
and proper error handling to the caller.
This is mentionable, as some other graph libraries will just panic and
crash the program, like the popular petgraph crate. Additionally,
note that this method does not check whether an edge already exists, as
the existence of multiple edges is a valid use case in some scenarios.
§Examples
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;Sourcepub fn iter(&self) -> Range<usize>
pub fn iter(&self) -> Range<usize>
Creates an iterator over the graph builder.
This iterator emits the node indices, which is exactly the same as
iterating over the adjacency list using 0..self.len().
§Examples
use zrx_graph::topology::Adjacency;
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;
// Create iterator over graph builder
for node in builder.iter() {
println!("{node:?}");
}Sourcepub fn build(self) -> Graph<T>
pub fn build(self) -> Graph<T>
Builds the graph.
This method creates the actual graph from the builder, which brings the graph into an executable form to allow for very efficient traversal.
§Examples
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;
// Create graph from builder
let graph = builder.build();Trait Implementations§
Source§impl<T> From<Builder<T>> for Graph<T>
impl<T> From<Builder<T>> for Graph<T>
Source§fn from(builder: Builder<T>) -> Self
fn from(builder: Builder<T>) -> Self
Creates a graph from a builder.
§Examples
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;
// Create graph from builder
let graph = Graph::from(builder);Source§impl<T> Index<usize> for Builder<T>
impl<T> Index<usize> for Builder<T>
Source§fn index(&self, index: usize) -> &Self::Output
fn index(&self, index: usize) -> &Self::Output
Returns a reference to the node at the index.
§Panics
Panics if the index is out of bounds.
§Examples
use zrx_graph::topology::Adjacency;
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;
// Obtain references to nodes
assert_eq!(&builder[a], &"a");
assert_eq!(&builder[b], &"b");
assert_eq!(&builder[c], &"c");Source§impl<T> IntoIterator for &Builder<T>
impl<T> IntoIterator for &Builder<T>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator over the graph builder.
This iterator emits the node indices, which is exactly the same as
iterating over the adjacency list using 0..self.len().
§Examples
use zrx_graph::topology::Adjacency;
use zrx_graph::Graph;
// Create graph builder and add nodes
let mut builder = Graph::builder();
let a = builder.add_node("a");
let b = builder.add_node("b");
let c = builder.add_node("c");
// Create edges between nodes
builder.add_edge(a, b)?;
builder.add_edge(b, c)?;
// Create iterator over graph builder
for node in &builder {
println!("{node:?}");
}