pub struct HeteroGraph { /* private fields */ }Expand description
A heterogeneous graph with multiple node and edge types.
§Node identifiers
All nodes share a single global NodeId namespace. Use
HeteroGraph::add_node to assign a node to a particular type; the
returned NodeId is globally unique.
§Edge types
Edges are grouped by HeteroEdgeType. Each group is an unordered list
of (src_id, dst_id) pairs; duplicate edges are allowed (useful for
multigraphs).
§Example
use scirs2_graph::heterogeneous::HeteroGraph;
let mut g = HeteroGraph::new();
let u0 = g.add_node("user", 0).unwrap();
let i0 = g.add_node("item", 0).unwrap();
g.add_edge("user", "buys", "item", u0, i0).unwrap();
assert_eq!(g.node_count(), 2);
assert_eq!(g.edge_count(), 1);Implementations§
Source§impl HeteroGraph
impl HeteroGraph
Sourcepub fn add_node(
&mut self,
type_name: impl Into<String>,
_hint: usize,
) -> Result<NodeId>
pub fn add_node( &mut self, type_name: impl Into<String>, _hint: usize, ) -> Result<NodeId>
Register a new node of type type_name.
_hint is an optional user-supplied integer label (e.g. a database
primary key); it is stored purely for user convenience and does not
affect the global NodeId that is returned.
§Errors
Returns GraphError::InvalidParameter if type_name is empty.
Sourcepub fn add_edge(
&mut self,
src_type: impl Into<String>,
relation: impl Into<String>,
dst_type: impl Into<String>,
src_id: NodeId,
dst_id: NodeId,
) -> Result<()>
pub fn add_edge( &mut self, src_type: impl Into<String>, relation: impl Into<String>, dst_type: impl Into<String>, src_id: NodeId, dst_id: NodeId, ) -> Result<()>
Add a directed typed edge src_id --relation--> dst_id.
Both nodes must already be present in the graph and must belong to
src_type and dst_type respectively.
§Errors
GraphError::NodeNotFound– node not registered.GraphError::InvalidParameter– node belongs to the wrong type.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Total number of registered nodes (across all types).
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Total number of registered edges (across all edge types).
Sourcepub fn node_type_names(&self) -> Vec<&str>
pub fn node_type_names(&self) -> Vec<&str>
List all node types present in the graph.
Sourcepub fn edge_type_list(&self) -> Vec<&HeteroEdgeType>
pub fn edge_type_list(&self) -> Vec<&HeteroEdgeType>
List all edge types present in the graph.
Sourcepub fn nodes_of_type(&self, type_name: &str) -> &[NodeId]
pub fn nodes_of_type(&self, type_name: &str) -> &[NodeId]
Return the nodes belonging to type_name.
Sourcepub fn edges_of_type(&self, et: &HeteroEdgeType) -> &[(NodeId, NodeId)]
pub fn edges_of_type(&self, et: &HeteroEdgeType) -> &[(NodeId, NodeId)]
Return all edges of a specific HeteroEdgeType.
Returns an empty slice if the edge type has no edges.
Sourcepub fn out_neighbors_typed(
&self,
node: NodeId,
et: &HeteroEdgeType,
) -> Vec<NodeId>
pub fn out_neighbors_typed( &self, node: NodeId, et: &HeteroEdgeType, ) -> Vec<NodeId>
Return the out-neighbours of node under a specific edge type.
Complexity: O(number of edges of that type).
Sourcepub fn all_out_neighbors_typed(
&self,
node: NodeId,
) -> Vec<(&HeteroEdgeType, Vec<NodeId>)>
pub fn all_out_neighbors_typed( &self, node: NodeId, ) -> Vec<(&HeteroEdgeType, Vec<NodeId>)>
Return all (edge_type, neighbours) pairs for node.
Useful for heterogeneous message passing.
Sourcepub fn contains_node(&self, node: NodeId) -> bool
pub fn contains_node(&self, node: NodeId) -> bool
Check whether a node is registered in the graph.
Sourcepub fn has_typed_edge(
&self,
et: &HeteroEdgeType,
src: NodeId,
dst: NodeId,
) -> bool
pub fn has_typed_edge( &self, et: &HeteroEdgeType, src: NodeId, dst: NodeId, ) -> bool
Check whether a typed edge exists.
Trait Implementations§
Source§impl Clone for HeteroGraph
impl Clone for HeteroGraph
Source§fn clone(&self) -> HeteroGraph
fn clone(&self) -> HeteroGraph
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HeteroGraph
impl Debug for HeteroGraph
Auto Trait Implementations§
impl Freeze for HeteroGraph
impl RefUnwindSafe for HeteroGraph
impl Send for HeteroGraph
impl Sync for HeteroGraph
impl Unpin for HeteroGraph
impl UnsafeUnpin for HeteroGraph
impl UnwindSafe for HeteroGraph
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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