Struct graph::prelude::GraphBuilder

source ·
pub struct GraphBuilder<State> { /* private fields */ }
Expand description

A builder to create graphs in a type-safe way.

The builder implementation uses different states to allow staged building of graphs. Each individual state enables stage-specific methods on the builder.

Examples

Create a directed graph from a vec of edges:

use graph_builder::prelude::*;

let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
    .edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
    .build();

assert_eq!(graph.node_count(), 4);

Create an undirected graph from a vec of edges:

use graph_builder::prelude::*;

let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
    .edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
    .build();

assert_eq!(graph.node_count(), 4);

Implementations§

source§

impl GraphBuilder<Uninitialized>

source

pub fn new() -> GraphBuilder<Uninitialized>

Creates a new builder

source

pub fn csr_layout(self, csr_layout: CsrLayout) -> GraphBuilder<Uninitialized>

Sets the CsrLayout to use during CSR construction.

Examples

Store the neighbors sorted:

use graph_builder::prelude::*;

let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
    .csr_layout(CsrLayout::Sorted)
    .edges(vec![(0, 7), (0, 3), (0, 3), (0, 1)])
    .build();

assert_eq!(graph.neighbors(0).copied().collect::<Vec<_>>(), &[1, 3, 3, 7]);

Store the neighbors sorted and deduplicated:

use graph_builder::prelude::*;

let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
    .csr_layout(CsrLayout::Deduplicated)
    .edges(vec![(0, 7), (0, 3), (0, 3), (0, 1)])
    .build();

assert_eq!(graph.neighbors(0).copied().collect::<Vec<_>>(), &[1, 3, 7]);
source

pub fn edges<NI, Edges>( self, edges: Edges ) -> GraphBuilder<FromEdges<NI, Edges>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI)>,

Create a graph from the given edge tuples.

Example
use graph_builder::prelude::*;

let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
    .edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
    .build();

assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
source

pub fn edges_with_values<NI, Edges, EV>( self, edges: Edges ) -> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI, EV)>,

Create a graph from the given edge triplets.

Example
use graph_builder::prelude::*;

let graph: DirectedCsrGraph<usize, (), f32> = GraphBuilder::new()
    .edges_with_values(vec![(0, 1, 0.1), (0, 2, 0.2), (1, 2, 0.3), (1, 3, 0.4), (2, 3, 0.5)])
    .build();

assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
source

pub fn file_format<Format, Path, NI>( self, _format: Format ) -> GraphBuilder<FromInput<NI, Path, Format>>where Path: AsRef<Path>, NI: Idx, Format: InputCapabilities<NI>, <Format as InputCapabilities<NI>>::GraphInput: TryFrom<InputPath<Path>>,

Creates a graph by reading it from the given file format.

Examples

Read a directed graph from an edge list file:

use std::path::PathBuf;

use graph_builder::prelude::*;

let path = [env!("CARGO_MANIFEST_DIR"), "resources", "example.el"]
    .iter()
    .collect::<PathBuf>();

let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
    .file_format(EdgeListInput::default())
    .path(path)
    .build()
    .expect("loading failed");

assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
source§

impl<NI, Edges> GraphBuilder<FromEdges<NI, Edges>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI)>,

source

pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, ()>>where I: IntoIterator<Item = NV>,

source

pub fn build<Graph>(self) -> Graphwhere Graph: From<(EdgeList<NI, ()>, CsrLayout)>,

Build the graph from the given vec of edges.

source§

impl<NI, Edges, EV> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where NI: Idx, EV: Sync, Edges: IntoIterator<Item = (NI, NI, EV)>,

source

pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where I: IntoIterator<Item = NV>,

source

pub fn build<Graph>(self) -> Graphwhere Graph: From<(EdgeList<NI, EV>, CsrLayout)>,

Build the graph from the given vec of edges.

source§

impl<NI, NV, EV> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where NI: Idx,

source

pub fn build<Graph>(self) -> Graphwhere Graph: From<(NodeValues<NV>, EdgeList<NI, EV>, CsrLayout)>,

source§

impl<NI, Path, Format> GraphBuilder<FromInput<NI, Path, Format>>where Path: AsRef<Path>, NI: Idx, Format: InputCapabilities<NI>, <Format as InputCapabilities<NI>>::GraphInput: TryFrom<InputPath<Path>>,

source

pub fn path(self, path: Path) -> GraphBuilder<FromPath<NI, Path, Format>>

Set the location where the graph is stored.

source§

impl<NI, Path, Format> GraphBuilder<FromPath<NI, Path, Format>>where Path: AsRef<Path>, NI: Idx, Format: InputCapabilities<NI>, <Format as InputCapabilities<NI>>::GraphInput: TryFrom<InputPath<Path>>, Error: From<<<Format as InputCapabilities<NI>>::GraphInput as TryFrom<InputPath<Path>>>::Error>,

source

pub fn build<Graph>(self) -> Result<Graph, Error>where Graph: TryFrom<(<Format as InputCapabilities<NI>>::GraphInput, CsrLayout)>, Error: From<<Graph as TryFrom<(<Format as InputCapabilities<NI>>::GraphInput, CsrLayout)>>::Error>,

Build the graph from the given input format and path.

Trait Implementations§

source§

impl Default for GraphBuilder<Uninitialized>

source§

fn default() -> GraphBuilder<Uninitialized>

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

Auto Trait Implementations§

§

impl<State> RefUnwindSafe for GraphBuilder<State>where State: RefUnwindSafe,

§

impl<State> Send for GraphBuilder<State>where State: Send,

§

impl<State> Sync for GraphBuilder<State>where State: Sync,

§

impl<State> Unpin for GraphBuilder<State>where State: Unpin,

§

impl<State> UnwindSafe for GraphBuilder<State>where State: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.