1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Submodule providing the [`MonoplexGraph`] trait.
//!
//! A monoplex graph is a graph where all edges are of the same type,
//! i.e., there is no distinction between different types of edges.
use super::Edges;
use crate::traits::{SizedRowsSparseMatrix2D, SparseMatrix, SparseMatrix2D};
/// Trait for monoplex graphs.
pub trait MonoplexGraph: super::Graph {
/// The type of the edge in the graph.
type Edge: super::Edge;
/// The type of the edges in the graph.
type Edges: super::Edges<Edge = Self::Edge>;
/// Returns a reference to the edges of the graph.
fn edges(&self) -> &Self::Edges;
/// Returns the successors of the node with the given identifier.
///
/// # Arguments
///
/// * `source_node_id` - The identifier of the source node.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// let successors: Vec<usize> = graph.successors(0).collect();
/// assert_eq!(successors, vec![1, 2]);
/// # }
/// ```
#[inline]
fn successors(
&self,
source_node_id: <Self::Edges as super::Edges>::SourceNodeId,
) -> <<Self::Edges as Edges>::Matrix as SparseMatrix2D>::SparseRow<'_> {
self.edges().successors(source_node_id)
}
/// Returns whether the given source node has successors.
///
/// # Arguments
///
/// * `source_node_id` - The identifier of the source node.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert!(graph.has_successors(0));
/// assert!(graph.has_successors(1));
/// assert!(!graph.has_successors(2));
/// # }
/// ```
#[inline]
fn has_successors(&self, source_node_id: <Self::Edges as super::Edges>::SourceNodeId) -> bool {
self.edges().has_successors(source_node_id)
}
/// Returns whether the given source node has a successor with the given
/// destination node identifier.
///
/// # Arguments
///
/// * `source_node_id` - The identifier of the source node.
/// * `destination_node_id` - The identifier of the destination node.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert!(graph.has_successor(0, 1));
/// assert!(graph.has_successor(0, 2));
/// assert!(!graph.has_successor(0, 3));
/// # }
/// ```
#[inline]
fn has_successor(
&self,
source_node_id: <Self::Edges as super::Edges>::SourceNodeId,
destination_node_id: <Self::Edges as super::Edges>::DestinationNodeId,
) -> bool {
self.edges().has_successor(source_node_id, destination_node_id)
}
/// Returns the outbound degree of the node with the given identifier.
///
/// # Arguments
///
/// * `source_node_id` - The identifier of the source node.
#[inline]
fn out_degree(
&self,
source_node_id: <Self::Edges as super::Edges>::SourceNodeId,
) -> <Self::Edges as super::Edges>::DestinationNodeId {
self.edges().out_degree(source_node_id)
}
/// Iterates across all out degrees of the graph.
#[inline]
fn out_degrees(
&self,
) -> <<Self::Edges as Edges>::Matrix as SizedRowsSparseMatrix2D>::SparseRowSizes<'_> {
self.edges().out_degrees()
}
/// Returns the iterator over all sparse coordinates of the matrix.
#[inline]
fn sparse_coordinates(
&self,
) -> <<Self::Edges as Edges>::Matrix as SparseMatrix>::SparseCoordinates<'_> {
self.edges().sparse_coordinates()
}
/// Returns the number of edges in the graph.
#[inline]
fn number_of_edges(&self) -> <Self::Edges as super::Edges>::EdgeId {
self.edges().number_of_edges()
}
}