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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::{
graph::{iter, Node},
Map, MapWithCapacity, OrderedKeyMap,
};
use core::{
fmt::Debug,
marker::PhantomData,
ops::{Index, IndexMut, RangeBounds},
};
#[cfg(feature = "rayon")]
use {
crate::{
map::ParIterMap,
util::{ParNodeWeightIter, ParNodeWeightIterMut},
},
rayon::iter::ParallelIterator,
};
/// A wrapper for node maps that disallows structural changes to the graph.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
pub(super) map: NM,
#[cfg_attr(feature = "serde", serde(skip_serializing, default))]
_marker: PhantomData<(N, EI)>,
}
impl<N, EI, NM> Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
/// Returns true if the node map contains the provided index.
pub fn contains_key(&self, index: NM::Key) -> bool {
self.map.contains_key(index)
}
/// Returns an immutable reference to a node if the index is present in the node map.
pub fn get(&self, index: NM::Key) -> Option<&Node<N, EI>> {
self.map.get(index)
}
/// Returns a mutable reference to a node if the index is present in the node map.
pub fn get_mut(&mut self, index: NM::Key) -> Option<&mut Node<N, EI>> {
self.map.get_mut(index)
}
/// Returns an immutable reference to a node's weight if the index is present in the node map.
pub fn weight(&self, index: NM::Key) -> Option<&N> {
self.map.get(index).map(Node::weight)
}
/// Returns a mutable reference to a node's weight if the index is present in the node map.
pub fn weight_mut(&mut self, index: NM::Key) -> Option<&mut N> {
self.map.get_mut(index).map(Node::weight_mut)
}
/// Returns an immutable iterator over the nodes in the node map.
pub fn iter(&self) -> NM::Iter<'_> {
self.map.iter()
}
/// Returns a mutable iterator over the nodes in the node map.
pub fn iter_mut(&mut self) -> NM::IterMut<'_> {
self.map.iter_mut()
}
/// Returns an immutable iterator over the weights of nodes stored in the node map.
pub fn iter_weights(&self) -> iter::NodeWeights<'_, N, NM::Key, EI, NM> {
iter::NodeWeights { inner: self.iter() }
}
/// Returns a mutable iterator over the weights of nodes stored in the node map.
pub fn iter_weights_mut(&mut self) -> iter::NodeWeightsMut<'_, N, NM::Key, EI, NM> {
iter::NodeWeightsMut {
inner: self.iter_mut(),
}
}
/// Returns the amount of nodes stored in the nodes map.
pub fn len(&self) -> usize {
self.map.len()
}
/// Returns a boolean indicating whether the nodes map contains no nodes.
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Returns a walker over the successors of a node.
pub fn walk_successors(&self, index: NM::Key) -> iter::WalkSuccessors<EI> {
let next = self[index].next_outgoing_edge;
iter::WalkSuccessors { next }
}
/// Returns a walker over the predecessors of a node.
pub fn walk_predecessors(&self, index: NM::Key) -> iter::WalkPredecessors<EI> {
let next = self[index].next_incoming_edge;
iter::WalkPredecessors { next }
}
/// Returns a walker over a node's outputs which can be used either with a complete graph or
/// with an [`Edges`] reference.
///
/// # Panics
///
/// This function will panic in case the passed in node index is invalid.
///
/// Additionally, the walker may panic in case one of the outgoing edges of the node are
/// removed.
///
/// [`Edges`]: super::Edges
pub fn walk_outputs(&self, index: NM::Key) -> iter::WalkOutputs<EI> {
iter::WalkOutputs {
next: self
.get(index)
.expect("invalid node index")
.next_outgoing_edge,
}
}
/// Returns a walker over a node's inputs which can be used either with a complete graph or
/// with an [`Edges`] reference.
///
/// # Panics
///
/// This function will panic in case the passed in node index is invalid.
///
/// Additionally, the walker may panic in case one of the incoming edges of the node are
/// removed.
///
/// [`Edges`]: super::Edges
pub fn walk_inputs(&self, index: NM::Key) -> iter::WalkInputs<EI> {
iter::WalkInputs {
next: self
.get(index)
.expect("invalid node index")
.next_incoming_edge,
}
}
}
impl<N, EI, NM> Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: MapWithCapacity<Node<N, EI>>,
{
pub(super) fn with_capacity(capacity: usize) -> Self {
Self {
map: NM::with_capacity(capacity),
_marker: PhantomData,
}
}
}
impl<N, EI, NM> Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: OrderedKeyMap<Node<N, EI>>,
NM::Key: Ord,
{
/// Returns an immutable iterator over nodes with indices that fall into the specified range.
///
/// # Order of iteration
///
/// The order the nodes are returned in depends on the range iterator type provided by the node
/// map in its [`OrderedKeyMap`] implementation as the [`OrderedKeyMap::Range`] associated type.
pub fn range<R: RangeBounds<NM::Key>>(&self, range: R) -> NM::Range<'_> {
self.map.range(range)
}
/// Returns a mutable iterator over nodes with indices that fall into the specified range.
///
/// # Order of iteration
///
/// The order the nodes are returned in depends on the range iterator type provided by the node
/// map in its [`OrderedKeyMap`] implementation as the [`OrderedKeyMap::Range`] associated type.
pub fn range_mut<R: RangeBounds<NM::Key>>(&mut self, range: R) -> NM::RangeMut<'_> {
self.map.range_mut(range)
}
/// Returns an immutable reference to the first node in the node map in case it isn't empty.
pub fn first(&self) -> Option<(NM::Key, &Node<N, EI>)> {
self.map.first()
}
/// Returns an immutable reference to the last node in the node map in case it isn't empty.
pub fn last(&self) -> Option<(NM::Key, &Node<N, EI>)> {
self.map.last()
}
/// Returns a mutable reference to the first node in the node map in case it isn't empty.
pub fn first_mut(&mut self) -> Option<(NM::Key, &mut Node<N, EI>)> {
self.map.first_mut()
}
/// Returns a mutable reference to the last node in the node map in case it isn't empty.
pub fn last_mut(&mut self) -> Option<(NM::Key, &mut Node<N, EI>)> {
self.map.last_mut()
}
/// Returns an immutable reference to the weight of the first node in the node map in case the
/// map isn't empty.
pub fn first_weight(&self) -> Option<(NM::Key, &N)> {
self.map.first().map(|(index, node)| (index, node.weight()))
}
/// Returns an immutable reference to the weight of the last node in the node map in case the
/// map isn't empty.
pub fn last_weight(&self) -> Option<(NM::Key, &N)> {
self.map.last().map(|(index, node)| (index, node.weight()))
}
/// Returns a mutable reference to the weight of the first node in the node map in case the
/// map isn't empty.
pub fn first_weight_mut(&mut self) -> Option<(NM::Key, &mut N)> {
self.map
.first_mut()
.map(|(index, node)| (index, node.weight_mut()))
}
/// Returns a mutable reference to the weight of the last node in the node map in case the
/// map isn't empty.
pub fn last_weight_mut(&mut self) -> Option<(NM::Key, &mut N)> {
self.map
.last_mut()
.map(|(index, node)| (index, node.weight_mut()))
}
}
#[cfg(feature = "rayon")]
impl<N, EI, NM> Nodes<N, EI, NM>
where
N: Sync + Send,
EI: Copy + Eq + Debug + 'static,
NM: ParIterMap<Node<N, EI>>,
NM::Key: Send + Sync,
{
/// Returns an immutable parallel iterator over the nodes in the node map.
pub fn par_iter(&self) -> NM::ParIter<'_> {
self.map.par_iter()
}
/// Returns a mutable iterator over the nodes in the node map.
pub fn par_iter_mut(&mut self) -> NM::ParIterMut<'_> {
self.map.par_iter_mut()
}
/// Returns an immutable iterator over the weights of nodes stored in the node map.
pub fn par_iter_weights(&self) -> ParNodeWeightIter<NM::ParIter<'_>, NM::Key, EI, N> {
self.map
.par_iter()
.map(|(index, node)| (index, node.weight()))
}
/// Returns a mutable iterator over the weights of nodes stored in the node map.
pub fn par_iter_weights_mut(
&mut self,
) -> ParNodeWeightIterMut<NM::ParIterMut<'_>, NM::Key, EI, N> {
self.map
.par_iter_mut()
.map(|(index, node)| (index, node.weight_mut()))
}
}
impl<N, EI, NM> Default for Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
fn default() -> Self {
Self {
map: Default::default(),
_marker: PhantomData,
}
}
}
impl<N, EI, NM> Index<NM::Key> for Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
type Output = Node<N, EI>;
fn index(&self, index: NM::Key) -> &Self::Output {
self.map.get(index).expect("invalid node index")
}
}
impl<N, EI, NM> IndexMut<NM::Key> for Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
fn index_mut(&mut self, index: NM::Key) -> &mut Self::Output {
self.map.get_mut(index).expect("invalid node index")
}
}
impl<'a, N, EI, NM> IntoIterator for &'a Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
type Item = (NM::Key, &'a Node<N, EI>);
type IntoIter = NM::Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter()
}
}
impl<'a, N, EI, NM> IntoIterator for &'a mut Nodes<N, EI, NM>
where
EI: Copy + Eq + Debug + 'static,
NM: Map<Node<N, EI>>,
{
type Item = (NM::Key, &'a mut Node<N, EI>);
type IntoIter = NM::IterMut<'a>;
fn into_iter(self) -> Self::IntoIter {
self.map.iter_mut()
}
}