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>
impl GraphBuilder<Uninitialized>
sourcepub fn new() -> GraphBuilder<Uninitialized>
pub fn new() -> GraphBuilder<Uninitialized>
Creates a new builder
sourcepub fn csr_layout(self, csr_layout: CsrLayout) -> GraphBuilder<Uninitialized>
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]);
sourcepub fn edges<NI, Edges>(
self,
edges: Edges
) -> GraphBuilder<FromEdges<NI, Edges>>where
NI: Idx,
Edges: IntoIterator<Item = (NI, NI)>,
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);
sourcepub fn edges_with_values<NI, Edges, EV>(
self,
edges: Edges
) -> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where
NI: Idx,
Edges: IntoIterator<Item = (NI, NI, EV)>,
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);
sourcepub 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>>,
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)>,
impl<NI, Edges> GraphBuilder<FromEdges<NI, Edges>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI)>,
pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, ()>>where I: IntoIterator<Item = NV>,
source§impl<NI, Edges, EV> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where
NI: Idx,
EV: Sync,
Edges: IntoIterator<Item = (NI, NI, EV)>,
impl<NI, Edges, EV> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where NI: Idx, EV: Sync, Edges: IntoIterator<Item = (NI, NI, EV)>,
pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where I: IntoIterator<Item = NV>,
source§impl<NI, NV, EV> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where
NI: Idx,
impl<NI, NV, EV> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where NI: Idx,
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>>,
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>>,
sourcepub fn path(self, path: Path) -> GraphBuilder<FromPath<NI, Path, Format>>
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>,
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>,
sourcepub 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>,
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>
impl Default for GraphBuilder<Uninitialized>
source§fn default() -> GraphBuilder<Uninitialized>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more