GraphBuilder

Struct GraphBuilder 

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

Source

pub fn new() -> Self

Creates a new builder with no nodes or edges (directed by default).

§Examples
use graph_core::GraphBuilder;

let b: GraphBuilder<(), f64> = GraphBuilder::new();
Source§

impl<N, W> GraphBuilder<N, W>

Source

pub fn directed(self) -> Self

Configures the graph to be directed (default).

§Examples
use graph_core::GraphBuilder;

let b: GraphBuilder<(), f64> = GraphBuilder::new().directed();
Source

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>

Source

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

pub fn edge(self, from: usize, to: usize, weight: W) -> Self

Adds an edge from from to to with weight and returns self.

Node indices are the order they were added via node. Invalid indices will panic at build time.

§Examples
use graph_core::GraphBuilder;

let b = GraphBuilder::<(), f64>::new()
    .node(())
    .node(())
    .edge(0, 1, 3.0);
Source§

impl<N, W: Clone> GraphBuilder<N, W>

Source

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

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§

Source§

impl<N: Debug, W: Debug> Debug for GraphBuilder<N, W>

Source§

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

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

impl<N, W> Default for GraphBuilder<N, W>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<N, W> Freeze for GraphBuilder<N, W>

§

impl<N, W> RefUnwindSafe for GraphBuilder<N, W>

§

impl<N, W> Send for GraphBuilder<N, W>
where N: Send, W: Send,

§

impl<N, W> Sync for GraphBuilder<N, W>
where N: Sync, W: Sync,

§

impl<N, W> Unpin for GraphBuilder<N, W>
where N: Unpin, W: Unpin,

§

impl<N, W> UnwindSafe for GraphBuilder<N, W>
where N: UnwindSafe, W: 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> 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, 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.