Struct 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() -> Self

Creates a new builder

Source

pub fn csr_layout(self, csr_layout: CsrLayout) -> Self

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 gdl_str<NI, S>(self, gdl: S) -> GraphBuilder<FromGdlString<NI>>
where NI: Idx, S: Into<String>,

Available on crate feature gdl only.

Creates a graph using Graph Definition Language (GDL).

Creating graphs from GDL is recommended for small graphs only, e.g., during testing. The graph construction is not parallelized.

See GDL on crates.io for more information on how to define graphs using GDL.

§Example
use graph_builder::prelude::*;

let g: UndirectedCsrGraph<usize> = GraphBuilder::new()
    .gdl_str::<usize, _>("(a)-->(),(a)-->()")
    .build()
    .unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);

One can also create weighted graphs using GDL. There needs to be exactly one edge property with the same type as specified for the edge value. The property key is not relevant.

use graph_builder::prelude::*;

let g: UndirectedCsrGraph<usize, (), f32> = GraphBuilder::new()
    .csr_layout(CsrLayout::Sorted)
    .gdl_str::<usize, _>("(a)-[{f: 0.42}]->(),(a)-[{f: 13.37}]->()")
    .build()
    .unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);

let mut neighbors = g.neighbors_with_values(0);
assert_eq!(neighbors.next(), Some(&Target::new(1, 0.42)));
assert_eq!(neighbors.next(), Some(&Target::new(2, 13.37)));
assert_eq!(neighbors.next(), None);
Source

pub fn gdl_graph<NI>( self, gdl_graph: &Graph, ) -> GraphBuilder<FromGdlGraph<'_, NI>>
where NI: Idx,

Available on crate feature gdl only.

Creates a graph from an already constructed GDL graph.

Creating graphs from GDL is recommended for small graphs only, e.g., during testing. The graph construction is not parallelized.

See GDL on crates.io for more information on how to define graphs using GDL.

§Example
use graph_builder::prelude::*;

let gdl_graph = "(a)-->(),(a)-->()".parse::<::gdl::Graph>().unwrap();

let g: DirectedCsrGraph<usize> = GraphBuilder::new()
    .gdl_graph::<usize>(&gdl_graph)
    .build()
    .unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);

let id_a = gdl_graph.get_node("a").unwrap().id();

assert_eq!(g.out_neighbors(id_a).len(), 2);
Source

pub fn file_format<Format, Path, NI>( self, _format: Format, ) -> GraphBuilder<FromInput<NI, Path, Format>>
where Path: AsRef<StdPath>, NI: Idx, Format: InputCapabilities<NI>, Format::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) -> Graph
where 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) -> Graph
where Graph: From<(EdgeList<NI, EV>, CsrLayout)>,

Build the graph from the given vec of edges.

Source§

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

Source

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

Source§

impl<NI> GraphBuilder<FromGdlString<NI>>
where NI: Idx,

Source

pub fn build<Graph>(self) -> Result<Graph, Error>
where Graph: From<(Graph, CsrLayout)>,

Available on crate feature gdl only.

Builds the graph from the given GDL string.

Source§

impl<'a, NI> GraphBuilder<FromGdlGraph<'a, NI>>
where NI: Idx,

Source

pub fn build<Graph>(self) -> Result<Graph, Error>
where Graph: From<(&'a Graph, CsrLayout)>,

Available on crate feature gdl only.

Build the graph from the given GDL graph.

Source§

impl<NI, Path, Format> GraphBuilder<FromInput<NI, Path, Format>>
where Path: AsRef<Path>, NI: Idx, Format: InputCapabilities<NI>, Format::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::GraphInput: TryFrom<InputPath<Path>>, Error: From<<Format::GraphInput as TryFrom<InputPath<Path>>>::Error>,

Source

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

Build the graph from the given input format and path.

Trait Implementations§

Source§

impl Default for GraphBuilder<Uninitialized>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.