pub struct Subgraph<G, S = ()>where
G: GraphBase,{ /* private fields */ }Expand description
A subgraph of a graph as determined by the vertex and edge predicates.
The vertex or edge presence determination is done lazily. This has a performance impact on some operations that are usually very fast.
§Examples
use gryf::{
adapt::Subgraph,
core::{facts, marker::Undirected, EdgeSet, VertexSet},
Graph,
};
let vertices = 0..10;
let mut graph = Graph::<u32, (), Undirected>::with_capacity(
vertices.len(),
facts::complete_graph_edge_count::<Undirected>(vertices.len()),
);
graph.extend_with_vertices(vertices);
graph.connect_vertices(|_, _| Some(()));
assert_eq!(graph.vertex_count(), 10);
assert_eq!(graph.edge_count(), 55);
// Subgraph with only "even" vertices.
let subgraph = Subgraph::new(&graph).filter_vertex(|v, g, _| g[v] % 2 == 0);
assert_eq!(subgraph.vertex_count(), 5);
assert_eq!(subgraph.edge_count(), 15);Implementations§
Source§impl<G> Subgraph<G>where
G: GraphBase,
impl<G> Subgraph<G>where
G: GraphBase,
Sourcepub fn new(graph: G) -> Self
pub fn new(graph: G) -> Self
Creates a new subgraph of the given graph.
Specify the subset of vertices and/or edges by
filter_vertex and
filter_edge, respectively. If neither is
defined, the subgraph represents the full original graph.
If you need some owned additional state for determining the subsets, use
the Subgraph::with_state constructor.
Source§impl<G, S> Subgraph<G, S>where
G: GraphBase,
impl<G, S> Subgraph<G, S>where
G: GraphBase,
Sourcepub fn with_state(graph: G, state: S) -> Self
pub fn with_state(graph: G, state: S) -> Self
Creates a new subgraph of the given graph and a state to be used in the predicates.
Specify the subset of vertices and/or edges by
filter_vertex and
filter_edge, respectively. If neither is
defined, the subgraph represents the full original graph.
If you don’t need any state for determining the subsets, use the
Subgraph::new constructor.
Sourcepub fn into_inner(self) -> G
pub fn into_inner(self) -> G
Returns the original graph.
Sourcepub fn filter_vertex<F>(self, predicate: F) -> Self
pub fn filter_vertex<F>(self, predicate: F) -> Self
Specifies the subset of vertices by a predicate.
The predicate takes the vertex ID, the original graph and the state that
was passed in via Subgraph::with_state (if any).
If you run into “something may not live long enough” compiler error due
to 'static lifetime bound on the predicate, pass the borrowed value as
part of the state via Subgraph::with_state and access it through the
last argument of the predicate.
Sourcepub fn filter_edge<F>(self, predicate: F) -> Self
pub fn filter_edge<F>(self, predicate: F) -> Self
Specifies the subset of edges by a predicate.
The predicate takes the edge ID, the original graph and the state that
was passed in via Subgraph::with_state (if any).
If you run into “something may not live long enough” compiler error due
to 'static lifetime bound on the predicate, pass the borrowed value as
part of the state via Subgraph::with_state and access it through the
last argument of the predicate.
Trait Implementations§
Source§impl<G, S> EdgeSet for Subgraph<G, S>where
G: EdgeSet,
impl<G, S> EdgeSet for Subgraph<G, S>where
G: EdgeSet,
Source§type EdgesByIdIter<'a> = SubgraphIter<'a, <G as EdgeSet>::EdgesByIdIter<'a>, <G as GraphBase>::EdgeId>
where
Self: 'a
type EdgesByIdIter<'a> = SubgraphIter<'a, <G as EdgeSet>::EdgesByIdIter<'a>, <G as GraphBase>::EdgeId> where Self: 'a
Source§type EdgeIdIter<'a> = SubgraphIter<'a, <G as EdgeSet>::EdgeIdIter<'a>, <G as GraphBase>::EdgeId>
where
Self: 'a
type EdgeIdIter<'a> = SubgraphIter<'a, <G as EdgeSet>::EdgeIdIter<'a>, <G as GraphBase>::EdgeId> where Self: 'a
Source§fn edges_by_id(&self) -> Self::EdgesByIdIter<'_>
fn edges_by_id(&self) -> Self::EdgesByIdIter<'_>
Source§fn edge_id(
&self,
from: &Self::VertexId,
to: &Self::VertexId,
) -> Self::EdgeIdIter<'_>
fn edge_id( &self, from: &Self::VertexId, to: &Self::VertexId, ) -> Self::EdgeIdIter<'_>
Source§fn endpoints(
&self,
id: &Self::EdgeId,
) -> Option<(Self::VertexId, Self::VertexId)>
fn endpoints( &self, id: &Self::EdgeId, ) -> Option<(Self::VertexId, Self::VertexId)>
Source§fn edge_bound(&self) -> usizewhere
Self::EdgeId: IntegerIdType,
fn edge_bound(&self) -> usizewhere
Self::EdgeId: IntegerIdType,
Source§fn contains_edge(&self, id: &Self::EdgeId) -> bool
fn contains_edge(&self, id: &Self::EdgeId) -> bool
true if the graph contains the given edge.Source§fn edge_count(&self) -> usize
fn edge_count(&self) -> usize
Source§fn contains_edge_between(
&self,
from: &Self::VertexId,
to: &Self::VertexId,
) -> bool
fn contains_edge_between( &self, from: &Self::VertexId, to: &Self::VertexId, ) -> bool
true if the graph contains an edge between two vertices.Source§fn edge_id_any(
&self,
from: &Self::VertexId,
to: &Self::VertexId,
) -> Option<Self::EdgeId>
fn edge_id_any( &self, from: &Self::VertexId, to: &Self::VertexId, ) -> Option<Self::EdgeId>
Source§fn edge_id_map(&self) -> CompactIdMap<Self::EdgeId>
fn edge_id_map(&self) -> CompactIdMap<Self::EdgeId>
Source§impl<G, S> GraphBase for Subgraph<G, S>
impl<G, S> GraphBase for Subgraph<G, S>
Source§fn vertex_count_hint(&self) -> Option<usize>
fn vertex_count_hint(&self) -> Option<usize>
Source§fn edge_count_hint(&self) -> Option<usize>
fn edge_count_hint(&self) -> Option<usize>
Source§fn is_directed(&self) -> bool
fn is_directed(&self) -> bool
true if the graph is directed.Source§impl<V, E, G, S> GraphRef<V, E> for Subgraph<G, S>where
G: GraphRef<V, E>,
impl<V, E, G, S> GraphRef<V, E> for Subgraph<G, S>where
G: GraphRef<V, E>,
Source§type VertexRef<'a> = <G as GraphRef<V, E>>::VertexRef<'a>
where
Self: 'a,
V: 'a
type VertexRef<'a> = <G as GraphRef<V, E>>::VertexRef<'a> where Self: 'a, V: 'a
Source§type VerticesIter<'a> = SubgraphIter<'a, <G as GraphRef<V, E>>::VerticesIter<'a>, <G as GraphRef<V, E>>::VertexRef<'a>>
where
Self: 'a,
V: 'a
type VerticesIter<'a> = SubgraphIter<'a, <G as GraphRef<V, E>>::VerticesIter<'a>, <G as GraphRef<V, E>>::VertexRef<'a>> where Self: 'a, V: 'a
Source§type EdgeRef<'a> = <G as GraphRef<V, E>>::EdgeRef<'a>
where
Self: 'a,
E: 'a
type EdgeRef<'a> = <G as GraphRef<V, E>>::EdgeRef<'a> where Self: 'a, E: 'a
Source§type EdgesIter<'a> = SubgraphIter<'a, <G as GraphRef<V, E>>::EdgesIter<'a>, <G as GraphRef<V, E>>::EdgeRef<'a>>
where
Self: 'a,
E: 'a
type EdgesIter<'a> = SubgraphIter<'a, <G as GraphRef<V, E>>::EdgesIter<'a>, <G as GraphRef<V, E>>::EdgeRef<'a>> where Self: 'a, E: 'a
Source§fn vertices(&self) -> Self::VerticesIter<'_>
fn vertices(&self) -> Self::VerticesIter<'_>
Source§fn edges(&self) -> Self::EdgesIter<'_>
fn edges(&self) -> Self::EdgesIter<'_>
Source§fn vertex(&self, id: &Self::VertexId) -> Option<&V>
fn vertex(&self, id: &Self::VertexId) -> Option<&V>
Source§impl<G, S> Neighbors for Subgraph<G, S>where
G: Neighbors,
impl<G, S> Neighbors for Subgraph<G, S>where
G: Neighbors,
Source§type NeighborRef<'a> = <G as Neighbors>::NeighborRef<'a>
where
Self: 'a
type NeighborRef<'a> = <G as Neighbors>::NeighborRef<'a> where Self: 'a
Source§type NeighborsIter<'a> = SubgraphIter<'a, <G as Neighbors>::NeighborsIter<'a>, <G as Neighbors>::NeighborRef<'a>>
where
Self: 'a
type NeighborsIter<'a> = SubgraphIter<'a, <G as Neighbors>::NeighborsIter<'a>, <G as Neighbors>::NeighborRef<'a>> where Self: 'a
Source§fn neighbors_undirected(&self, from: &G::VertexId) -> Self::NeighborsIter<'_>
fn neighbors_undirected(&self, from: &G::VertexId) -> Self::NeighborsIter<'_>
Source§fn neighbors_directed(
&self,
from: &G::VertexId,
dir: Direction,
) -> Self::NeighborsIter<'_>
fn neighbors_directed( &self, from: &G::VertexId, dir: Direction, ) -> Self::NeighborsIter<'_>
Source§impl<G, S> VertexSet for Subgraph<G, S>where
G: VertexSet,
impl<G, S> VertexSet for Subgraph<G, S>where
G: VertexSet,
Source§type VerticesByIdIter<'a> = SubgraphIter<'a, <G as VertexSet>::VerticesByIdIter<'a>, <G as GraphBase>::VertexId>
where
Self: 'a
type VerticesByIdIter<'a> = SubgraphIter<'a, <G as VertexSet>::VerticesByIdIter<'a>, <G as GraphBase>::VertexId> where Self: 'a
Source§fn vertices_by_id(&self) -> Self::VerticesByIdIter<'_>
fn vertices_by_id(&self) -> Self::VerticesByIdIter<'_>
Source§fn vertex_bound(&self) -> usizewhere
Self::VertexId: IntegerIdType,
fn vertex_bound(&self) -> usizewhere
Self::VertexId: IntegerIdType,
Source§fn contains_vertex(&self, id: &Self::VertexId) -> bool
fn contains_vertex(&self, id: &Self::VertexId) -> bool
true if the graph contains the given vertex.Source§fn vertex_count(&self) -> usize
fn vertex_count(&self) -> usize
Source§fn vertex_id_map(&self) -> CompactIdMap<Self::VertexId>
fn vertex_id_map(&self) -> CompactIdMap<Self::VertexId>
Auto Trait Implementations§
impl<G, S> Freeze for Subgraph<G, S>
impl<G, S = ()> !RefUnwindSafe for Subgraph<G, S>
impl<G, S = ()> !Send for Subgraph<G, S>
impl<G, S = ()> !Sync for Subgraph<G, S>
impl<G, S> Unpin for Subgraph<G, S>
impl<G, S> UnsafeUnpin for Subgraph<G, S>where
G: UnsafeUnpin,
S: UnsafeUnpin,
impl<G, S = ()> !UnwindSafe for Subgraph<G, S>
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
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<V, E, G> GraphWeak<V, E> for Gwhere
G: GraphRef<V, E>,
impl<V, E, G> GraphWeak<V, E> for Gwhere
G: GraphRef<V, E>,
Source§fn vertex_weak<'a>(
&'a self,
id: &'a <G as GraphBase>::VertexId,
) -> Option<OwnableRef<'a, V>>
fn vertex_weak<'a>( &'a self, id: &'a <G as GraphBase>::VertexId, ) -> Option<OwnableRef<'a, V>>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.