1use core::cmp::max;
8use core::ops::{Index, IndexMut};
9
10use derive_more::derive::From;
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Eq, Ord, Hash, From)]
14#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
15pub struct NodeIndex(pub u32);
16
17impl NodeIndex {
18 pub const END: NodeIndex = NodeIndex(u32::MAX);
23
24 pub fn index(self) -> usize {
26 self.0 as usize
27 }
28
29 pub fn is_end(self) -> bool {
31 self == NodeIndex::END
32 }
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Eq, Ord, Hash, From)]
37#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
38pub struct EdgeIndex(pub u32);
39
40impl EdgeIndex {
41 pub const END: EdgeIndex = EdgeIndex(u32::MAX);
46
47 pub fn index(self) -> usize {
49 self.0 as usize
50 }
51
52 pub fn is_end(self) -> bool {
54 self == EdgeIndex::END
55 }
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
60#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
61#[repr(usize)]
62pub enum EdgeDirection {
63 Outgoing = 0,
65 Incoming = 1,
67}
68
69impl EdgeDirection {
70 pub const ALL: [EdgeDirection; 2] = [EdgeDirection::Outgoing, EdgeDirection::Incoming];
72
73 #[inline]
75 fn opposite(self) -> EdgeDirection {
76 match self {
77 EdgeDirection::Outgoing => EdgeDirection::Incoming,
78 EdgeDirection::Incoming => EdgeDirection::Outgoing,
79 }
80 }
81}
82
83#[derive(Clone, Copy, Debug)]
85#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
86pub struct Node<N> {
87 pub weight: N,
89 next: [EdgeIndex; 2],
91}
92
93#[derive(Clone, Copy, Debug)]
95#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
96pub struct Edge<E> {
97 pub weight: E,
99 next: [EdgeIndex; 2],
101 node: [NodeIndex; 2],
103}
104
105impl<E> Edge<E> {
106 pub fn source(&self) -> NodeIndex {
108 self.node[0]
109 }
110
111 pub fn target(&self) -> NodeIndex {
113 self.node[1]
114 }
115}
116
117#[derive(Clone, Debug)]
122#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
123pub struct UnGraph<N, E> {
124 nodes: Vec<Node<N>>,
125 edges: Vec<Edge<E>>,
126}
127
128impl<N, E> Default for UnGraph<N, E> {
129 fn default() -> Self {
130 Self {
131 nodes: Vec::new(),
132 edges: Vec::new(),
133 }
134 }
135}
136
137enum Pair<T> {
138 Both(T, T),
139 One(T),
140 None,
141}
142
143fn index_twice<T>(arr: &mut [T], a: usize, b: usize) -> Pair<&mut T> {
145 if max(a, b) >= arr.len() {
146 Pair::None
147 } else if a == b {
148 Pair::One(&mut arr[max(a, b)])
149 } else {
150 unsafe {
152 let ar = &mut *(arr.get_unchecked_mut(a) as *mut _);
153 let br = &mut *(arr.get_unchecked_mut(b) as *mut _);
154 Pair::Both(ar, br)
155 }
156 }
157}
158
159impl<N, E> UnGraph<N, E> {
160 pub fn with_capacity(nodes: usize, edges: usize) -> Self {
162 UnGraph {
163 nodes: Vec::with_capacity(nodes),
164 edges: Vec::with_capacity(edges),
165 }
166 }
167
168 pub fn node_count(&self) -> usize {
172 self.nodes.len()
173 }
174
175 pub fn edge_count(&self) -> usize {
179 self.edges.len()
180 }
181
182 pub fn add_node(&mut self, weight: N) -> NodeIndex {
192 let node = Node {
193 weight,
194 next: [EdgeIndex::END, EdgeIndex::END],
195 };
196 assert!(self.nodes.len() != usize::MAX);
197 let node_idx = NodeIndex(self.nodes.len() as u32);
198 self.nodes.push(node);
199 node_idx
200 }
201
202 pub fn node_weight(&self, a: NodeIndex) -> Option<&N> {
206 self.nodes.get(a.index()).map(|n| &n.weight)
207 }
208
209 pub fn add_edge(&mut self, a: NodeIndex, b: NodeIndex, weight: E) -> EdgeIndex {
223 assert!(self.edges.len() != usize::MAX);
224 let edge_idx = EdgeIndex(self.edges.len() as u32);
225 let mut edge = Edge {
226 weight,
227 node: [a, b],
228 next: [EdgeIndex::END; 2],
229 };
230 match index_twice(&mut self.nodes, a.index(), b.index()) {
231 Pair::None => panic!("`UnGraph::add_edge`: node indices out of bounds"),
232 Pair::One(an) => {
233 edge.next = an.next;
234 an.next[0] = edge_idx;
235 an.next[1] = edge_idx;
236 }
237 Pair::Both(an, bn) => {
238 edge.next = [an.next[0], bn.next[1]];
240 an.next[0] = edge_idx;
241 bn.next[1] = edge_idx;
242 }
243 }
244 self.edges.push(edge);
245 edge_idx
246 }
247
248 pub fn update_edge(&mut self, a: NodeIndex, b: NodeIndex, weight: E) -> EdgeIndex {
260 if let Some(ix) = self.find_edge(a, b) {
261 if let Some(ed) = self.edge_weight_mut(ix) {
262 *ed = weight;
263 return ix;
264 }
265 }
266 self.add_edge(a, b, weight)
267 }
268
269 pub fn edge_weight(&self, e: EdgeIndex) -> Option<&E> {
273 self.edges.get(e.index()).map(|e| &e.weight)
274 }
275
276 pub fn edge_weight_mut(&mut self, e: EdgeIndex) -> Option<&mut E> {
280 self.edges.get_mut(e.index()).map(|e| &mut e.weight)
281 }
282
283 pub fn edge_endpoints(&self, e: EdgeIndex) -> Option<(NodeIndex, NodeIndex)> {
285 self.edges
286 .get(e.index())
287 .map(|ed| (ed.source(), ed.target()))
288 }
289
290 pub fn remove_node_with<F>(&mut self, a: NodeIndex, mut edge_callback: F) -> Option<N>
303 where
304 F: FnMut(E),
305 {
306 self.nodes.get(a.index())?;
307 for d in EdgeDirection::ALL {
308 let k = d as usize;
309
310 loop {
312 let next = self.nodes[a.index()].next[k];
313 if next == EdgeIndex::END {
314 break;
315 }
316 let edge = self.remove_edge(next).expect("edge not found for removal");
317 edge_callback(edge);
318 }
319 }
320
321 let node = self.nodes.swap_remove(a.index());
325
326 let swap_edges = match self.nodes.get(a.index()) {
329 None => return Some(node.weight),
330 Some(ed) => ed.next,
331 };
332
333 let old_index = NodeIndex(self.nodes.len() as u32);
335 let new_index = a;
336
337 for d in EdgeDirection::ALL {
339 let k = d as usize;
340 let mut edges = EdgesWalkerMut {
341 edges: &mut self.edges,
342 next: swap_edges[k],
343 dir: d,
344 };
345 while let Some(curedge) = edges.next_edge() {
346 debug_assert!(curedge.node[k] == old_index);
347 curedge.node[k] = new_index;
348 }
349 }
350 Some(node.weight)
351 }
352
353 fn change_edge_links(
356 &mut self,
357 edge_node: [NodeIndex; 2],
358 e: EdgeIndex,
359 edge_next: [EdgeIndex; 2],
360 ) {
361 for d in EdgeDirection::ALL {
362 let k = d as usize;
363 let node = match self.nodes.get_mut(edge_node[k].index()) {
364 Some(r) => r,
365 None => {
366 debug_assert!(
367 false,
368 "Edge's endpoint dir={:?} index={:?} not found",
369 d, edge_node[k]
370 );
371 return;
372 }
373 };
374 let fst = node.next[k];
375 if fst == e {
376 node.next[k] = edge_next[k];
377 } else {
378 let mut edges = EdgesWalkerMut {
379 edges: &mut self.edges,
380 next: fst,
381 dir: d,
382 };
383 while let Some(curedge) = edges.next_edge() {
384 if curedge.next[k] == e {
385 curedge.next[k] = edge_next[k];
386 break;
388 }
389 }
390 }
391 }
392 }
393
394 pub fn remove_edge(&mut self, e: EdgeIndex) -> Option<E> {
402 let (edge_node, edge_next) = match self.edges.get(e.index()) {
405 None => return None,
406 Some(x) => (x.node, x.next),
407 };
408
409 self.change_edge_links(edge_node, e, edge_next);
412 self.remove_edge_adjust_indices(e)
413 }
414
415 fn remove_edge_adjust_indices(&mut self, e: EdgeIndex) -> Option<E> {
416 let edge = self.edges.swap_remove(e.index());
420 let swap = match self.edges.get(e.index()) {
421 None => return Some(edge.weight),
423 Some(ed) => ed.node,
424 };
425 let swapped_e = EdgeIndex(self.edges.len() as u32);
426
427 self.change_edge_links(swap, swapped_e, [e, e]);
430
431 Some(edge.weight)
432 }
433
434 pub fn neighbors(&self, a: NodeIndex) -> Neighbors<E> {
440 Neighbors {
441 skip_start: a,
442 edges: &self.edges,
443 next: match self.nodes.get(a.index()) {
444 None => [EdgeIndex::END, EdgeIndex::END],
445 Some(n) => n.next,
446 },
447 }
448 }
449
450 pub fn edges(&self, a: NodeIndex) -> Edges<E> {
456 Edges {
457 skip_start: a,
458 edges: &self.edges,
459 direction: EdgeDirection::Outgoing,
460 next: match self.nodes.get(a.index()) {
461 None => [EdgeIndex::END, EdgeIndex::END],
462 Some(n) => n.next,
463 },
464 }
465 }
466
467 pub fn edges_mut(&mut self, a: NodeIndex) -> EdgesMut<N, E> {
473 let incoming_edge = self.first_edge(a, EdgeDirection::Incoming);
474 let outgoing_edge = self.first_edge(a, EdgeDirection::Outgoing);
475
476 EdgesMut {
477 graph: self,
478 incoming_edge,
479 outgoing_edge,
480 }
481 }
482
483 pub fn contains_edge(&self, a: NodeIndex, b: NodeIndex) -> bool {
488 self.find_edge(a, b).is_some()
489 }
490
491 pub fn find_edge(&self, a: NodeIndex, b: NodeIndex) -> Option<EdgeIndex> {
496 let node = self.nodes.get(a.index())?;
497 for &d in &EdgeDirection::ALL {
498 let k = d as usize;
499 let mut edix = node.next[k];
500 while let Some(edge) = self.edges.get(edix.index()) {
501 if edge.node[1 - k] == b {
502 return Some(edix);
503 }
504 edix = edge.next[k];
505 }
506 }
507 None
508 }
509
510 pub fn node_indices(&self) -> impl DoubleEndedIterator<Item = NodeIndex> {
512 (0..self.node_count()).map(|i| NodeIndex(i as u32))
513 }
514
515 pub fn edge_indices(&self) -> impl DoubleEndedIterator<Item = EdgeIndex> {
517 (0..self.edge_count()).map(|i| EdgeIndex(i as u32))
518 }
519
520 pub fn edge_weights(&self, a: NodeIndex) -> EdgeWeights<E> {
522 EdgeWeights {
523 skip_start: a,
524 edges: &self.edges,
525 direction: EdgeDirection::Outgoing,
526 next: match self.nodes.get(a.index()) {
527 None => [EdgeIndex::END, EdgeIndex::END],
528 Some(n) => n.next,
529 },
530 }
531 }
532
533 pub fn edge_weights_mut(&mut self, a: NodeIndex) -> EdgeWeightsMut<N, E> {
535 let incoming_edge = self.first_edge(a, EdgeDirection::Incoming);
536 let outgoing_edge = self.first_edge(a, EdgeDirection::Outgoing);
537
538 EdgeWeightsMut {
539 graph: self,
540 incoming_edge,
541 outgoing_edge,
542 }
543 }
544
545 pub fn all_edge_weights(&self) -> AllEdgeWeights<E> {
550 AllEdgeWeights {
551 edges: self.edges.iter(),
552 }
553 }
554
555 pub fn all_edge_weights_mut(&mut self) -> AllEdgeWeightsMut<E> {
560 AllEdgeWeightsMut {
561 edges: self.edges.iter_mut(),
562 }
563 }
564
565 pub fn raw_nodes(&self) -> &[Node<N>] {
567 &self.nodes
568 }
569
570 pub fn raw_nodes_mut(&mut self) -> &mut [Node<N>] {
572 &mut self.nodes
573 }
574
575 pub fn raw_edges(&self) -> &[Edge<E>] {
577 &self.edges
578 }
579
580 pub fn raw_edges_mut(&mut self) -> &mut [Edge<E>] {
582 &mut self.edges
583 }
584
585 pub fn first_edge(&self, a: NodeIndex, dir: EdgeDirection) -> Option<EdgeIndex> {
587 match self.nodes.get(a.index()) {
588 None => None,
589 Some(node) => {
590 let edix = node.next[dir as usize];
591 if edix == EdgeIndex::END {
592 None
593 } else {
594 Some(edix)
595 }
596 }
597 }
598 }
599
600 pub fn next_edge(&self, e: EdgeIndex, dir: EdgeDirection) -> Option<EdgeIndex> {
602 match self.edges.get(e.index()) {
603 None => None,
604 Some(node) => {
605 let edix = node.next[dir as usize];
606 if edix == EdgeIndex::END {
607 None
608 } else {
609 Some(edix)
610 }
611 }
612 }
613 }
614
615 pub fn clear(&mut self) {
617 self.nodes.clear();
618 self.edges.clear();
619 }
620
621 pub fn clear_edges(&mut self) {
623 self.edges.clear();
624 for node in &mut self.nodes {
625 node.next = [EdgeIndex::END, EdgeIndex::END];
626 }
627 }
628
629 pub fn nodes_capacity(&self) -> usize {
631 self.nodes.capacity()
632 }
633
634 pub fn edges_capacity(&self) -> usize {
636 self.edges.capacity()
637 }
638
639 pub fn reserve_nodes(&mut self, additional: usize) {
646 self.nodes.reserve(additional);
647 }
648
649 pub fn reserve_edges(&mut self, additional: usize) {
656 self.edges.reserve(additional);
657 }
658
659 pub fn retain_nodes<F>(&mut self, mut visit: F)
667 where
668 F: FnMut(&Self, NodeIndex) -> bool,
669 {
670 for index in self.node_indices().rev() {
671 if !visit(self, index) {
672 let ret = self.remove_node_with(index, |_| ());
673 debug_assert!(ret.is_some());
674 let _ = ret;
675 }
676 }
677 }
678
679 pub fn retain_edges<F>(&mut self, mut visit: F)
687 where
688 F: FnMut(&Self, EdgeIndex) -> bool,
689 {
690 for index in self.edge_indices().rev() {
691 if !visit(self, index) {
692 let ret = self.remove_edge(index);
693 debug_assert!(ret.is_some());
694 let _ = ret;
695 }
696 }
697 }
698}
699
700#[derive(Debug)]
704pub struct Neighbors<'a, E: 'a> {
705 skip_start: NodeIndex,
707
708 edges: &'a [Edge<E>],
710
711 next: [EdgeIndex; 2],
713}
714
715impl<E> Iterator for Neighbors<'_, E> {
716 type Item = NodeIndex;
717
718 fn next(&mut self) -> Option<NodeIndex> {
719 match self.edges.get(self.next[0].index()) {
721 None => {}
722 Some(edge) => {
723 self.next[0] = edge.next[0];
724 return Some(edge.node[1]);
725 }
726 }
727
728 while let Some(edge) = self.edges.get(self.next[1].index()) {
732 self.next[1] = edge.next[1];
733 if edge.node[0] != self.skip_start {
734 return Some(edge.node[0]);
735 }
736 }
737 None
738 }
739}
740
741impl<E> Clone for Neighbors<'_, E> {
742 fn clone(&self) -> Self {
743 Neighbors {
744 skip_start: self.skip_start,
745 edges: self.edges,
746 next: self.next,
747 }
748 }
749}
750
751pub struct Edges<'a, E: 'a> {
753 skip_start: NodeIndex,
755
756 edges: &'a [Edge<E>],
758
759 next: [EdgeIndex; 2],
761
762 direction: EdgeDirection,
764}
765
766impl<'a, E> Iterator for Edges<'a, E> {
767 type Item = EdgeReference<'a, E>;
768
769 fn next(&mut self) -> Option<Self::Item> {
770 let (iterate_over, _reverse) = (None, Some(self.direction.opposite()));
780
781 if iterate_over.unwrap_or(EdgeDirection::Outgoing) == EdgeDirection::Outgoing {
782 let i = self.next[0].index();
783 if let Some(Edge { weight, next, .. }) = self.edges.get(i) {
784 self.next[0] = next[0];
785 return Some(EdgeReference {
786 index: EdgeIndex(i as u32),
787 weight,
788 });
789 }
790 }
791
792 if iterate_over.unwrap_or(EdgeDirection::Incoming) == EdgeDirection::Incoming {
793 while let Some(Edge { node, weight, next }) = self.edges.get(self.next[1].index()) {
794 let edge_index = self.next[1];
795 self.next[1] = next[1];
796
797 if iterate_over.is_none() && node[0] == self.skip_start {
800 continue;
801 }
802
803 return Some(EdgeReference {
804 index: edge_index,
805 weight,
806 });
807 }
808 }
809
810 None
811 }
812}
813
814impl<E> Clone for Edges<'_, E> {
815 fn clone(&self) -> Self {
816 Edges {
817 skip_start: self.skip_start,
818 edges: self.edges,
819 next: self.next,
820 direction: self.direction,
821 }
822 }
823}
824
825pub struct EdgesMut<'a, N, E> {
827 graph: &'a mut UnGraph<N, E>,
828 incoming_edge: Option<EdgeIndex>,
829 outgoing_edge: Option<EdgeIndex>,
830}
831
832impl<'a, N: Copy, E> Iterator for EdgesMut<'a, N, E> {
833 type Item = EdgeMut<'a, E>;
834
835 #[inline]
836 fn next(&mut self) -> Option<EdgeMut<'a, E>> {
837 if let Some(edge) = self.incoming_edge {
838 self.incoming_edge = self.graph.next_edge(edge, EdgeDirection::Incoming);
839 let weights = &mut self.graph[edge];
840 return Some(EdgeMut {
841 index: edge,
842 weight: unsafe { core::mem::transmute::<&mut E, &'a mut E>(weights) },
843 });
844 }
845
846 let edge = self.outgoing_edge?;
847 self.outgoing_edge = self.graph.next_edge(edge, EdgeDirection::Outgoing);
848 let weights = &mut self.graph[edge];
849 Some(EdgeMut {
850 index: edge,
851 weight: unsafe { core::mem::transmute::<&mut E, &'a mut E>(weights) },
852 })
853 }
854}
855
856pub struct EdgeWeights<'a, E: 'a> {
859 skip_start: NodeIndex,
861
862 edges: &'a [Edge<E>],
864
865 next: [EdgeIndex; 2],
867
868 direction: EdgeDirection,
870}
871
872impl<'a, E> Iterator for EdgeWeights<'a, E> {
873 type Item = &'a E;
874
875 fn next(&mut self) -> Option<Self::Item> {
876 let (iterate_over, _reverse) = (None, Some(self.direction.opposite()));
886
887 if iterate_over.unwrap_or(EdgeDirection::Outgoing) == EdgeDirection::Outgoing {
888 let i = self.next[0].index();
889 if let Some(Edge { weight, next, .. }) = self.edges.get(i) {
890 self.next[0] = next[0];
891 return Some(weight);
892 }
893 }
894
895 if iterate_over.unwrap_or(EdgeDirection::Incoming) == EdgeDirection::Incoming {
896 while let Some(Edge { node, weight, next }) = self.edges.get(self.next[1].index()) {
897 self.next[1] = next[1];
898
899 if iterate_over.is_none() && node[0] == self.skip_start {
902 continue;
903 }
904
905 return Some(weight);
906 }
907 }
908
909 None
910 }
911}
912
913impl<E> Clone for EdgeWeights<'_, E> {
914 fn clone(&self) -> Self {
915 EdgeWeights {
916 skip_start: self.skip_start,
917 edges: self.edges,
918 next: self.next,
919 direction: self.direction,
920 }
921 }
922}
923
924pub struct EdgeWeightsMut<'a, N, E> {
926 pub graph: &'a mut UnGraph<N, E>,
928
929 pub incoming_edge: Option<EdgeIndex>,
931
932 pub outgoing_edge: Option<EdgeIndex>,
934}
935
936impl<'a, N: Copy, E> Iterator for EdgeWeightsMut<'a, N, E> {
937 type Item = &'a mut E;
938
939 #[inline]
940 fn next(&mut self) -> Option<&'a mut E> {
941 if let Some(edge) = self.incoming_edge {
942 self.incoming_edge = self.graph.next_edge(edge, EdgeDirection::Incoming);
943 let weight = &mut self.graph[edge];
944 return Some(unsafe { core::mem::transmute::<&mut E, &'a mut E>(weight) });
945 }
946
947 let edge = self.outgoing_edge?;
948 self.outgoing_edge = self.graph.next_edge(edge, EdgeDirection::Outgoing);
949 let weight = &mut self.graph[edge];
950 Some(unsafe { core::mem::transmute::<&mut E, &'a mut E>(weight) })
951 }
952}
953
954pub struct AllEdgeWeights<'a, E: 'a> {
956 edges: core::slice::Iter<'a, Edge<E>>,
957}
958
959impl<'a, E> Iterator for AllEdgeWeights<'a, E> {
960 type Item = &'a E;
961
962 fn next(&mut self) -> Option<&'a E> {
963 self.edges.next().map(|edge| &edge.weight)
964 }
965
966 fn size_hint(&self) -> (usize, Option<usize>) {
967 self.edges.size_hint()
968 }
969}
970
971#[derive(Debug)]
973pub struct AllEdgeWeightsMut<'a, E: 'a> {
974 edges: core::slice::IterMut<'a, Edge<E>>,
975}
976
977impl<'a, E> Iterator for AllEdgeWeightsMut<'a, E> {
978 type Item = &'a mut E;
979
980 fn next(&mut self) -> Option<&'a mut E> {
981 self.edges.next().map(|edge| &mut edge.weight)
982 }
983
984 fn size_hint(&self) -> (usize, Option<usize>) {
985 self.edges.size_hint()
986 }
987}
988
989struct EdgesWalkerMut<'a, E: 'a> {
990 edges: &'a mut [Edge<E>],
991 next: EdgeIndex,
992 dir: EdgeDirection,
993}
994
995impl<E> EdgesWalkerMut<'_, E> {
996 fn next_edge(&mut self) -> Option<&mut Edge<E>> {
997 self.next().map(|t| t.1)
998 }
999
1000 fn next(&mut self) -> Option<(EdgeIndex, &mut Edge<E>)> {
1001 let this_index = self.next;
1002 let k = self.dir as usize;
1003 match self.edges.get_mut(self.next.index()) {
1004 None => None,
1005 Some(edge) => {
1006 self.next = edge.next[k];
1007 Some((this_index, edge))
1008 }
1009 }
1010 }
1011}
1012
1013impl<N, E> Index<NodeIndex> for UnGraph<N, E> {
1019 type Output = N;
1020 fn index(&self, index: NodeIndex) -> &N {
1021 &self.nodes[index.index()].weight
1022 }
1023}
1024
1025impl<N, E> IndexMut<NodeIndex> for UnGraph<N, E> {
1031 fn index_mut(&mut self, index: NodeIndex) -> &mut N {
1032 &mut self.nodes[index.index()].weight
1033 }
1034}
1035
1036impl<N, E> Index<EdgeIndex> for UnGraph<N, E> {
1042 type Output = E;
1043 fn index(&self, index: EdgeIndex) -> &E {
1044 &self.edges[index.index()].weight
1045 }
1046}
1047
1048impl<N, E> IndexMut<EdgeIndex> for UnGraph<N, E> {
1054 fn index_mut(&mut self, index: EdgeIndex) -> &mut E {
1055 &mut self.edges[index.index()].weight
1056 }
1057}
1058
1059#[derive(Debug)]
1061pub struct EdgeReference<'a, E: 'a> {
1062 index: EdgeIndex,
1063 weight: &'a E,
1064}
1065
1066impl<'a, E: 'a> EdgeReference<'a, E> {
1067 #[inline]
1069 pub fn index(&self) -> EdgeIndex {
1070 self.index
1071 }
1072
1073 #[inline]
1075 pub fn weight(&self) -> &'a E {
1076 self.weight
1077 }
1078}
1079
1080impl<E> Clone for EdgeReference<'_, E> {
1081 fn clone(&self) -> Self {
1082 *self
1083 }
1084}
1085
1086impl<E> Copy for EdgeReference<'_, E> {}
1087
1088impl<E> PartialEq for EdgeReference<'_, E>
1089where
1090 E: PartialEq,
1091{
1092 fn eq(&self, rhs: &Self) -> bool {
1093 self.index == rhs.index && self.weight == rhs.weight
1094 }
1095}
1096
1097#[derive(Debug)]
1099pub struct EdgeMut<'a, E: 'a> {
1100 index: EdgeIndex,
1101 weight: &'a mut E,
1102}
1103
1104impl<E> EdgeMut<'_, E> {
1105 #[inline]
1107 pub fn index(&self) -> EdgeIndex {
1108 self.index
1109 }
1110
1111 #[inline]
1113 pub fn weight(&self) -> &E {
1114 self.weight
1115 }
1116
1117 #[inline]
1119 pub fn weight_mut(&mut self) -> &mut E {
1120 self.weight
1121 }
1122}
1123
1124impl<E> PartialEq for EdgeMut<'_, E>
1125where
1126 E: PartialEq,
1127{
1128 fn eq(&self, rhs: &Self) -> bool {
1129 self.index == rhs.index && self.weight == rhs.weight
1130 }
1131}