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 to_edge_graph(&self) -> Builder<Edge>
👎Deprecated since 0.0.6: Edge graphs are no longer needed for action graphs
pub fn to_edge_graph(&self) -> Builder<Edge>
Creates the edge graph of the graph.
This method derives a new graph from the given graph in which each edge represents a transition from one edge to another based on their source and target nodes in the original graph, which means that the nodes of the edge graph are the edges of the original graph.
Edge graphs are necessary for representing relationships between edges, which is exactly what we need for action graphs, where edges represent actions and their dependencies. During execution, we don’t need to know the actual nodes, but rather the dependencies between the edges.
§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 edge graph
let edges = builder.to_edge_graph();
assert_eq!(edges.nodes(), builder.edges());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");