Skip to main content

Builder

Struct Builder 

Source
pub struct Builder<T> { /* private fields */ }
Expand description

Graph builder.

Implementations§

Source§

impl<T> Builder<T>

Source

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");
Source

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)?;
Source

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:?}");
}
Source

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();
Source§

impl<T> Builder<T>

Source

pub fn nodes(&self) -> &[T]

Returns a reference to the nodes.

Source

pub fn edges(&self) -> &[Edge]

Returns a reference to the edges.

Source

pub fn len(&self) -> usize

Returns the number of nodes.

Source

pub fn is_empty(&self) -> bool

Returns whether there are any nodes.

Trait Implementations§

Source§

impl<T: Clone> Clone for Builder<T>

Source§

fn clone(&self) -> Builder<T>

Returns a duplicate 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<T: Debug> Debug for Builder<T>

Source§

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

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

impl<T> Default for Builder<T>

Source§

fn default() -> Self

Creates a graph builder.

§Examples
use zrx_graph::Builder;

// Create graph builder
let mut builder = Builder::default();
Source§

impl<T> From<Builder<T>> for Graph<T>

Source§

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>

Source§

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§

type Output = T

The returned type after indexing.
Source§

impl<T> IntoIterator for &Builder<T>

Source§

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:?}");
}
Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = Range<usize>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

impl<T> Freeze for Builder<T>

§

impl<T> RefUnwindSafe for Builder<T>
where T: RefUnwindSafe,

§

impl<T> Send for Builder<T>
where T: Send,

§

impl<T> Sync for Builder<T>
where T: Sync,

§

impl<T> Unpin for Builder<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Builder<T>

§

impl<T> UnwindSafe for Builder<T>
where T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

Source§

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

Source§

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.