lattice_graph/lattice_abstract/
nodes.rs1use std::iter::FusedIterator;
2
3use petgraph::visit::{
4 IntoNodeIdentifiers, IntoNodeReferences, NodeCompactIndexable, NodeIndexable,
5};
6
7use super::*;
8
9#[derive(Clone, Debug)]
11pub struct NodeIndices<S> {
12 index: usize,
13 s: S,
14}
15
16impl<S: shapes::Shape> Iterator for NodeIndices<S> {
17 type Item = <S as Shape>::Coordinate;
18
19 fn next(&mut self) -> Option<Self::Item> {
20 if self.index < self.s.node_count() {
21 let x = self.s.index_to_coordinate(self.index);
22 self.index += 1;
23 Some(x)
24 } else {
25 None
26 }
27 }
28
29 fn size_hint(&self) -> (usize, Option<usize>) {
30 let len = self.s.node_count() - self.index;
31 (len, Some(len))
32 }
33}
34
35impl<S: shapes::Shape> FusedIterator for NodeIndices<S> {}
36
37impl<S: shapes::Shape> ExactSizeIterator for NodeIndices<S> {}
38
39impl<N, E, S: Shape> IntoNodeIdentifiers for &LatticeGraph<N, E, S> {
40 type NodeIdentifiers = NodeIndices<S>;
41
42 fn node_identifiers(self) -> Self::NodeIdentifiers {
43 NodeIndices {
44 index: 0,
45 s: self.s.clone(),
46 }
47 }
48}
49
50pub struct NodeReferences<'a, N, E, S: Shape> {
52 graph: &'a LatticeGraph<N, E, S>,
53 index: usize,
54}
55
56impl<'a, N, E, S: Shape> Iterator for NodeReferences<'a, N, E, S> {
57 type Item = (<S as Shape>::Coordinate, &'a N);
58
59 fn next(&mut self) -> Option<Self::Item> {
60 if self.index < self.graph.s.node_count() {
61 let x = self.graph.s.index_to_coordinate(self.index);
62 self.index += 1;
63 Some((x, unsafe { self.graph.node_weight_unchecked(x) }))
64 } else {
65 None
66 }
67 }
68}
69
70impl<'a, N, E, S: Shape> FusedIterator for NodeReferences<'a, N, E, S> {}
71
72impl<'a, N, E, S: Shape> ExactSizeIterator for NodeReferences<'a, N, E, S> {}
73
74impl<'a, N, E, S: Shape> IntoNodeReferences for &'a LatticeGraph<N, E, S> {
75 type NodeRef = (<S as Shape>::Coordinate, &'a N);
76
77 type NodeReferences = NodeReferences<'a, N, E, S>;
78
79 fn node_references(self) -> Self::NodeReferences {
80 NodeReferences {
81 graph: self,
82 index: 0,
83 }
84 }
85}
86
87impl<N, E, S: Shape> NodeCount for LatticeGraph<N, E, S> {
88 fn node_count(&self) -> usize {
89 self.s.node_count()
90 }
91}
92
93impl<N, E, S: Shape> NodeIndexable for LatticeGraph<N, E, S> {
94 fn node_bound(&self) -> usize {
95 self.s.node_count()
96 }
97
98 fn to_index(&self, a: Self::NodeId) -> usize {
99 self.s.to_index(a).unwrap()
100 }
101
102 fn from_index(&self, i: usize) -> Self::NodeId {
103 self.s.index_to_coordinate(i)
104 }
105}
106
107impl<N, E, S: Shape> NodeCompactIndexable for LatticeGraph<N, E, S> {}