1use std::ops::DerefMut;
2
3use crate::prelude::*;
4#[cfg(feature = "rayon")]
5use rayon::prelude::*;
6
7pub trait Weights<'a, N: 'a, E: 'a>: GraphBasics<'a> {
13 fn node_weights(&'a self) -> impl Iterator<Item = &'a N> + 'a;
14 fn edge_weights(&'a self) -> impl Iterator<Item = &'a E> + 'a;
15 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut N> + 'a;
16 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut E> + 'a;
17 fn node_weight_mut(
18 &'a mut self,
19 node_index: <Self as GraphBasics<'a>>::NodeIndex,
20 ) -> Option<&'a mut N>;
21 fn edge_weight_mut(
22 &'a mut self,
23 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
24 ) -> Option<&'a mut E>;
25 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a N>;
26 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a E>;
27 unsafe fn node_weight_unchecked(
29 &'a self,
30 node_index: <Self as GraphBasics<'a>>::NodeIndex,
31 ) -> &'a N;
32 unsafe fn edge_weight_unchecked(
33 &'a self,
34 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
35 ) -> &'a E;
36 unsafe fn node_weight_unchecked_mut(
37 &'a mut self,
38 node_index: <Self as GraphBasics<'a>>::NodeIndex,
39 ) -> &'a mut N;
40 unsafe fn edge_weight_unchecked_mut(
41 &'a mut self,
42 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
43 ) -> &'a mut E;
44 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E>
45 where
46 E: Copy;
47 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N>
48 where
49 N: Copy;
50 unsafe fn edge_weight_copied_unchecked(
51 &'a self,
52 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
53 ) -> E
54 where
55 E: Copy;
56 unsafe fn node_weight_copied_unchecked(
57 &'a self,
58 node_index: <Self as GraphBasics<'a>>::NodeIndex,
59 ) -> N
60 where
61 N: Copy;
62}
63
64#[macro_export]
65macro_rules! impl_weights {
66 ($graph:ty$(, <$($gens:tt),*>)? $(, |$(const $cgens:ident: $ity:ty),*|)?) => {
67 impl<'a, N: 'a, E: 'a$(, $($gens),*)?$(, $(const $cgens: $ity),*)?> Weights<'a, N, E> for $graph
68 where
69 N: Clone + Eq + Hash,
70 E: Clone + Eq + Hash,
71 {
72 fn node_weights(&'a self) -> impl Iterator<Item = &'a N> + 'a {
73 self.nodes.iter().map(|node| &node.weight)
74 }
75
76 fn edge_weights(&'a self) -> impl Iterator<Item = &'a E> + 'a {
77 self.edges.iter().map(|edge| &edge.weight)
78 }
79
80 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut N> + 'a {
81 self.nodes.iter_mut().map(|node| &mut node.weight)
82 }
83
84 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut E> + 'a {
85 self.edges.iter_mut().map(|edge| &mut edge.weight)
86 }
87
88 fn node_weight_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a mut N> {
89 self.nodes.get_mut(node_index).map(|node| &mut node.weight)
90 }
91
92 fn edge_weight_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a mut E> {
93 self.edges.get_mut(edge_index).map(|edge| &mut edge.weight)
94 }
95
96 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a N> {
97 self.nodes.get(node_index).map(|node| &node.weight)
98 }
99
100 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a E> {
101 self.edges.get(edge_index).map(|edge| &edge.weight)
102 }
103
104 unsafe fn node_weight_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a N {
105 unsafe {&self.nodes.get_unchecked(node_index).weight}
106 }
107
108 unsafe fn edge_weight_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a E {
109 unsafe {&self.edges.get_unchecked(edge_index).weight}
110 }
111
112 unsafe fn node_weight_unchecked_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a mut N {
113 unsafe {&mut self.nodes.get_unchecked_mut(node_index).weight}
114 }
115
116 unsafe fn edge_weight_unchecked_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a mut E {
117 unsafe {&mut self.edges.get_unchecked_mut(edge_index).weight}
118 }
119
120 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E> where E: Copy {
121 Some(self.edges[edge_index].weight)
122 }
123
124 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N> where N: Copy {
125 Some(self.nodes[node_index].weight)
126 }
127
128 unsafe fn edge_weight_copied_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> E where E: Copy {
129 unsafe{self.edges.get_unchecked(edge_index).weight}
130 }
131
132 unsafe fn node_weight_copied_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> N where N: Copy {
133 unsafe{self.nodes.get_unchecked(node_index).weight}
134 }
135 }
136
137 }
138}
139
140#[macro_export]
141macro_rules! impl_weights_wrapper {
142 (
143 $graph:ty,
144 reduce N = $node_weight:tt,
145 reduce E = $edge_weight:tt
146 $(, <$($gens:tt),*>)?
147 $(, |$(const $cgens:ident: $ity:ty),*|)?
148 ) => {
149 impl<'a, $(, $($gens),*)?$(, $(const $cgens: $ity),*)?> Weights<'a, $node_weight, $edge_weight> for $graph
150 {
151 fn node_weights(&'a self) -> impl Iterator<Item = &'a $node_weight> + 'a {
152 self.inner.nodes.iter().map(|node| &node.weight)
153 }
154
155 fn edge_weights(&'a self) -> impl Iterator<Item = &'a $edge_weight> + 'a {
156 self.inner.edges.iter().map(|edge| &edge.weight)
157 }
158
159 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut $node_weight> + 'a {
160 self.inner.nodes.iter_mut().map(|node| &mut node.weight)
161 }
162
163 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut $edge_weight> + 'a {
164 self.inner.edges.iter_mut().map(|edge| &mut edge.weight)
165 }
166
167 fn node_weight_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a mut $node_weight> {
168 self.inner.nodes.get_mut(node_index).map(|node| &mut node.weight)
169 }
170
171 fn edge_weight_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a mut $edge_weight> {
172 self.inner.edges.get_mut(edge_index).map(|edge| &mut edge.weight)
173 }
174
175 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a $node_weight> {
176 self.inner.nodes.get(node_index).map(|node| &node.weight)
177 }
178
179 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a $edge_weight> {
180 self.inner.edges.get(edge_index).map(|edge| &edge.weight)
181 }
182
183 unsafe fn node_weight_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a $node_weight {
184 unsafe {&self.inner.nodes.get_unchecked(node_index).weight}
185 }
186
187 unsafe fn edge_weight_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a $edge_weight {
188 unsafe {&self.inner.edges.get_unchecked(edge_index).weight}
189 }
190
191 unsafe fn node_weight_unchecked_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a mut $node_weight {
192 unsafe {&mut self.inner.nodes.get_unchecked_mut(node_index).weight}
193 }
194
195 unsafe fn edge_weight_unchecked_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a mut $edge_weight {
196 unsafe {&mut self.inner.edges.get_unchecked_mut(edge_index).weight}
197 }
198
199 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E> where $edge_weight: Copy {
200 Some(self.inner.edges[edge_index].weight)
201 }
202
203 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N> where $node_weight: Copy {
204 Some(self.inner.nodes[node_index].weight)
205 }
206
207 unsafe fn edge_weight_copied_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> $edge_weight where $edge_weight: Copy {
208 unsafe{self.inner.edges.get_unchecked(edge_index).weight}
209 }
210
211 unsafe fn node_weight_copied_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> $node_weight where $node_weight: Copy {
212 unsafe{self.inner.nodes.get_unchecked(node_index).weight}
213 }
214 }
215
216 };
217 (
218 $graph:ty,
219 reduce N = $node_weight:tt
220 $(, <$($gens:tt),*>)?
221 $(, |$(const $cgens:ident: $ity:ty),*|)?
222 ) => {
223 impl<'a, E $(, $($gens),*)?$(, $(const $cgens: $ity),*)?> Weights<'a, $node_weight, E> for $graph
224 where
225 E: Clone + Eq + Hash + 'a,
226 {
227 fn node_weights(&'a self) -> impl Iterator<Item = &'a $node_weight> + 'a {
228 self.inner.nodes.iter().map(|node| &node.weight)
229 }
230
231 fn edge_weights(&'a self) -> impl Iterator<Item = &'a E> + 'a {
232 self.inner.edges.iter().map(|edge| &edge.weight)
233 }
234
235 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut $node_weight> + 'a {
236 self.inner.nodes.iter_mut().map(|node| &mut node.weight)
237 }
238
239 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut E> + 'a {
240 self.inner.edges.iter_mut().map(|edge| &mut edge.weight)
241 }
242
243 fn node_weight_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a mut $node_weight> {
244 self.inner.nodes.get_mut(node_index).map(|node| &mut node.weight)
245 }
246
247 fn edge_weight_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a mut E> {
248 self.inner.edges.get_mut(edge_index).map(|edge| &mut edge.weight)
249 }
250
251 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a $node_weight> {
252 self.inner.nodes.get(node_index).map(|node| &node.weight)
253 }
254
255 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a E> {
256 self.inner.edges.get(edge_index).map(|edge| &edge.weight)
257 }
258
259 unsafe fn node_weight_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a $node_weight {
260 unsafe {&self.inner.nodes.get_unchecked(node_index).weight}
261 }
262
263 unsafe fn edge_weight_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a E {
264 unsafe {&self.inner.edges.get_unchecked(edge_index).weight}
265 }
266
267 unsafe fn node_weight_unchecked_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a mut $node_weight {
268 unsafe {&mut self.inner.nodes.get_unchecked_mut(node_index).weight}
269 }
270
271 unsafe fn edge_weight_unchecked_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a mut E {
272 unsafe {&mut self.inner.edges.get_unchecked_mut(edge_index).weight}
273 }
274
275 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E> where E: Copy {
276 Some(self.inner.edges[edge_index].weight)
277 }
278
279 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<$node_weight> where $node_weight: Copy {
280 Some(self.inner.nodes[node_index].weight)
281 }
282
283 unsafe fn edge_weight_copied_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> E where E: Copy {
284 unsafe{self.inner.edges.get_unchecked(edge_index).weight}
285 }
286
287 unsafe fn node_weight_copied_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> $node_weight where $node_weight: Copy {
288 unsafe{self.inner.nodes.get_unchecked(node_index).weight}
289 }
290 }
291 };
292 (
293 $graph:ty,
294 reduce E = $edge_weight:tt
295 $(, <$($gens:tt),*>)?
296 $(, |$(const $cgens:ident: $ity:ty),*|)?
297 ) => {
298 impl<'a, N$(, $($gens),*)?$(, $(const $cgens: $ity),*)?> Weights<'a, N, $edge_weight> for $graph
299 where
300 N: Clone + Eq + Hash + 'a,
301 {
302 fn node_weights(&'a self) -> impl Iterator<Item = &'a N> + 'a {
303 self.inner.nodes.iter().map(|node| &node.weight)
304 }
305
306 fn edge_weights(&'a self) -> impl Iterator<Item = &'a $edge_weight> + 'a {
307 self.inner.edges.iter().map(|edge| &edge.weight)
308 }
309
310 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut N> + 'a {
311 self.inner.nodes.iter_mut().map(|node| &mut node.weight)
312 }
313
314 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut $edge_weight> + 'a {
315 self.inner.edges.iter_mut().map(|edge| &mut edge.weight)
316 }
317
318 fn node_weight_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a mut N> {
319 self.inner.nodes.get_mut(node_index).map(|node| &mut node.weight)
320 }
321
322 fn edge_weight_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a mut $edge_weight> {
323 self.inner.edges.get_mut(edge_index).map(|edge| &mut edge.weight)
324 }
325
326 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a N> {
327 self.inner.nodes.get(node_index).map(|node| &node.weight)
328 }
329
330 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a $edge_weight>
331 {
332 self.inner.edges.get(edge_index).map(|edge| &edge.weight)
333 }
334
335 unsafe fn node_weight_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a N {
336 unsafe {&self.inner.nodes.get_unchecked(node_index).weight}
337 }
338
339 unsafe fn edge_weight_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a $edge_weight {
340 unsafe {&self.inner.edges.get_unchecked(edge_index).weight}
341 }
342
343 unsafe fn node_weight_unchecked_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a mut N {
344 unsafe {&mut self.inner.nodes.get_unchecked_mut(node_index).weight}
345 }
346
347 unsafe fn edge_weight_unchecked_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a mut $edge_weight {
348 unsafe {&mut self.inner.edges.get_unchecked_mut(edge_index).weight}
349 }
350
351 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<$edge_weight> where $edge_weight: Copy {
352 Some(self.inner.edges[edge_index].weight)
353 }
354
355 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N> where N: Copy {
356 Some(self.inner.nodes[node_index].weight)
357 }
358
359 unsafe fn edge_weight_copied_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> $edge_weight where $edge_weight: Copy {
360 unsafe{self.inner.edges.get_unchecked(edge_index).weight}
361 }
362
363 unsafe fn node_weight_copied_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> N where N: Copy {
364 unsafe{self.inner.nodes.get_unchecked(node_index).weight}
365 }
366 }
367 };
368 (
369 $graph:ty
370 $(, <$($gens:tt),*>)?
371 $(, |$(const $cgens:ident: $ity:ty),*|)?
372 ) => {
373 impl<'a, N, E $(, $($gens),*)?$(, $(const $cgens: $ity),*)?> Weights<'a, N, E> for $graph
374 where
375 N: Clone + Eq + Hash + 'a,
376 E: Clone + Eq + Hash + 'a,
377 {
378 fn node_weights(&'a self) -> impl Iterator<Item = &'a N> + 'a {
379 self.inner.nodes.iter().map(|node| &node.weight)
380 }
381
382 fn edge_weights(&'a self) -> impl Iterator<Item = &'a E> + 'a {
383 self.inner.edges.iter().map(|edge| &edge.weight)
384 }
385
386 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut N> + 'a {
387 self.inner.nodes.iter_mut().map(|node| &mut node.weight)
388 }
389
390 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut E> + 'a {
391 self.inner.edges.iter_mut().map(|edge| &mut edge.weight)
392 }
393
394 fn node_weight_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a mut N> {
395 self.inner.nodes.get_mut(node_index).map(|node| &mut node.weight)
396 }
397
398 fn edge_weight_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a mut E> {
399 self.inner.edges.get_mut(edge_index).map(|edge| &mut edge.weight)
400 }
401
402 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a N> {
403 self.inner.nodes.get(node_index).map(|node| &node.weight)
404 }
405
406 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a E> {
407 self.inner.edges.get(edge_index).map(|edge| &edge.weight)
408 }
409
410 unsafe fn node_weight_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a N {
411 unsafe {&self.inner.nodes.get_unchecked(node_index).weight}
412 }
413
414 unsafe fn edge_weight_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a E {
415 unsafe {&self.inner.edges.get_unchecked(edge_index).weight}
416 }
417
418 unsafe fn node_weight_unchecked_mut(&'a mut self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> &'a mut N {
419 unsafe {&mut self.inner.nodes.get_unchecked_mut(node_index).weight}
420 }
421
422 unsafe fn edge_weight_unchecked_mut(&'a mut self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> &'a mut E {
423 unsafe {&mut self.inner.edges.get_unchecked_mut(edge_index).weight}
424 }
425
426 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E> where E: Copy {
427 Some(self.inner.edges[edge_index].weight)
428 }
429
430 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N> where N: Copy {
431 Some(self.inner.nodes[node_index].weight)
432 }
433
434 unsafe fn edge_weight_copied_unchecked(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> E where E: Copy {
435 unsafe{self.inner.edges.get_unchecked(edge_index).weight}
436 }
437
438 unsafe fn node_weight_copied_unchecked(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> N where N: Copy {
439 unsafe{self.inner.nodes.get_unchecked(node_index).weight}
440 }
441 }
442
443 };
444}
445
446impl<'a, T: 'a, U, N: 'a, E: 'a> Weights<'a, N, E> for U
447where
448 T: Weights<'a, N, E>,
449 U: DerefMut<Target = T>,
450{
451 fn node_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut N> + 'a {
452 (**self).node_weights_mut()
453 }
454
455 fn edge_weights_mut(&'a mut self) -> impl Iterator<Item = &'a mut E> + 'a {
456 (**self).edge_weights_mut()
457 }
458
459 fn node_weight_mut(
460 &'a mut self,
461 node_index: <Self as GraphBasics<'a>>::NodeIndex,
462 ) -> Option<&'a mut N> {
463 (**self).node_weight_mut(node_index)
464 }
465
466 fn edge_weight_mut(
467 &'a mut self,
468 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
469 ) -> Option<&'a mut E> {
470 (**self).edge_weight_mut(edge_index)
471 }
472
473 fn node_weight(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<&'a N> {
474 (**self).node_weight(node_index)
475 }
476
477 fn edge_weight(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<&'a E> {
478 (**self).edge_weight(edge_index)
479 }
480
481 unsafe fn node_weight_unchecked(
482 &'a self,
483 node_index: <Self as GraphBasics<'a>>::NodeIndex,
484 ) -> &'a N {
485 unsafe { (**self).node_weight_unchecked(node_index) }
486 }
487
488 unsafe fn edge_weight_unchecked(
489 &'a self,
490 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
491 ) -> &'a E {
492 unsafe { (**self).edge_weight_unchecked(edge_index) }
493 }
494
495 unsafe fn node_weight_unchecked_mut(
496 &'a mut self,
497 node_index: <Self as GraphBasics<'a>>::NodeIndex,
498 ) -> &'a mut N {
499 unsafe { (**self).node_weight_unchecked_mut(node_index) }
500 }
501
502 unsafe fn edge_weight_unchecked_mut(
503 &'a mut self,
504 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
505 ) -> &'a mut E {
506 unsafe { (**self).edge_weight_unchecked_mut(edge_index) }
507 }
508
509 fn edge_weight_copied(&'a self, edge_index: <Self as GraphBasics<'a>>::EdgeIndex) -> Option<E>
510 where
511 E: Copy,
512 {
513 (**self).edge_weight_copied(edge_index)
514 }
515
516 fn node_weight_copied(&'a self, node_index: <Self as GraphBasics<'a>>::NodeIndex) -> Option<N>
517 where
518 N: Copy,
519 {
520 (**self).node_weight_copied(node_index)
521 }
522
523 unsafe fn edge_weight_copied_unchecked(
524 &'a self,
525 edge_index: <Self as GraphBasics<'a>>::EdgeIndex,
526 ) -> E
527 where
528 E: Copy,
529 {
530 unsafe { (**self).edge_weight_copied_unchecked(edge_index) }
531 }
532
533 unsafe fn node_weight_copied_unchecked(
534 &'a self,
535 node_index: <Self as GraphBasics<'a>>::NodeIndex,
536 ) -> N
537 where
538 N: Copy,
539 {
540 unsafe { (**self).node_weight_copied_unchecked(node_index) }
541 }
542
543 fn node_weights(&'a self) -> impl Iterator<Item = &'a N> + 'a {
544 (**self).node_weights()
545 }
546
547 fn edge_weights(&'a self) -> impl Iterator<Item = &'a E> + 'a {
548 (**self).edge_weights()
549 }
550}
551
552#[cfg(feature = "rayon")]
553pub trait ParallelWeights<'a, N: 'a, E: 'a>: GraphBasics<'a> + Weights<'a, N, E> {
554 fn par_node_weights(&'a self) -> impl ParallelIterator<Item = &'a N>;
555 fn par_edge_weights(&'a self) -> impl ParallelIterator<Item = &'a E>;
556}
557
558#[cfg(feature = "rayon")]
559impl<'a, N, E, G> ParallelWeights<'a, N, E> for G
560where
561 G: GraphBasics<'a> + Weights<'a, N, E> + Sync,
562 N: 'a + Send + Sync,
563 E: 'a + Send + Sync,
564{
565 fn par_node_weights(&'a self) -> impl ParallelIterator<Item = &'a N> {
566 (0..self.node_count())
567 .into_par_iter()
568 .map(|x| unsafe { self.node_weight_unchecked(x.into()) })
569 }
570
571 fn par_edge_weights(&'a self) -> impl ParallelIterator<Item = &'a E> {
572 (0..self.edge_count())
573 .into_par_iter()
574 .map(|x| unsafe { self.edge_weight_unchecked(x.into()) })
575 }
576}