graphfind_rs/graph/
graph_trait.rs

1use std::{fmt::Debug, hash::Hash};
2/// Graph is a generic trait specifying the functionality that must be implemented by Graph storage backends used for Querying.
3pub trait Graph<NodeWeight, EdgeWeight> {
4    /// NodeRef is the associated type for node references.
5    ///
6    /// It implements the Ord trait (compare references),
7    /// Eq + Hash (allows insertion of Node Weights and their references into a Table),
8    /// Copy (allows use of references in function parameters),
9    /// and Debug (simplifies debugging).
10    type NodeRef: Copy + Eq + Hash + Ord + Debug;
11    /// EdgeRef is the associated type for edge references.
12    ///
13    /// It implements the same traits as NodeRef.
14    type EdgeRef: Copy + Eq + Hash + Ord + Debug;
15    /// Checks if the edges of this graph are directed.
16    fn is_directed(&self) -> bool;
17
18    /// Checks if the given edge is directed.
19    fn is_directed_edge(&self, edge: Self::EdgeRef) -> bool;
20
21    type AdjacentEdgesIterator<'a>: Iterator<Item = Self::EdgeRef>
22    where
23        Self: 'a;
24    /// Gets a readonly handle of all adjacent edges of a node.
25    ///
26    /// For directed graphs, this includes all incoming and outgoing
27    /// edges.
28    fn adjacent_edges(&self, node: Self::NodeRef) -> Self::AdjacentEdgesIterator<'_>;
29    type IncomingEdgesIterator<'a>: Iterator<Item = Self::EdgeRef>
30    where
31        Self: 'a;
32    /// Gets a readonly handle of all incoming edges of a node.
33    ///
34    /// For undirected graphs this is equivalent to calling `adjacent_edges`.
35    fn incoming_edges(&self, node: Self::NodeRef) -> Self::IncomingEdgesIterator<'_>;
36    type OutgoingEdgesIterator<'a>: Iterator<Item = Self::EdgeRef>
37    where
38        Self: 'a;
39    /// Gets a readonly handle of all outgoing edges of a node.
40    ///
41    /// For undirected graphs this is equivalent to calling `adjacent_edges`.
42    fn outgoing_edges(&self, node: Self::NodeRef) -> Self::OutgoingEdgesIterator<'_>;
43
44    /// Gets a readonly handle of the nodes an edge connects.
45    ///
46    /// If the edge is directed, the first node is its source, and the second node its destination.
47    fn adjacent_nodes(&self, edge: Self::EdgeRef) -> (Self::NodeRef, Self::NodeRef);
48
49    /// Retrieve weight from a node reference.
50    fn node_weight(&self, node: Self::NodeRef) -> &NodeWeight;
51
52    /// Retrieve weight from an edge reference.
53    fn edge_weight(&self, edge: Self::EdgeRef) -> &EdgeWeight;
54
55    type NodeWeightsIterator<'a>: Iterator<Item = &'a NodeWeight>
56    where
57        Self: 'a,
58        NodeWeight: 'a;
59    /// Returns an Iterator over all node weights.
60    fn node_weights(&self) -> Self::NodeWeightsIterator<'_>;
61
62    type EdgeWeightsIterator<'a>: Iterator<Item = &'a EdgeWeight>
63    where
64        Self: 'a,
65        EdgeWeight: 'a;
66    /// Returns an Iterator over all edge weights.
67    fn edge_weights(&self) -> Self::EdgeWeightsIterator<'_>;
68
69    type NodesIterator<'a>: Iterator<Item = Self::NodeRef>
70    where
71        Self: 'a;
72
73    /// Returns an Iterator over all nodes by their references.
74    fn nodes(&self) -> Self::NodesIterator<'_>;
75
76    type EdgesIterator<'a>: Iterator<Item = Self::EdgeRef>
77    where
78        Self: 'a;
79    /// Returns an Iterator over all edges by their references.
80    fn edges(&self) -> Self::EdgesIterator<'_>;
81
82    /// Tests if the given graph is empty.
83    fn is_empty_graph(&self) -> bool {
84        self.count_nodes() == 0
85    }
86
87    /// Returns the number of nodes in this graph.
88    fn count_nodes(&self) -> usize;
89    /// Returns the number of edges in this graph.
90    fn count_edges(&self) -> usize;
91}