@@ -25,3 +25,140 @@ impl From<Cluster> for RegisterCluster {
25
25
RegisterCluster :: Cluster ( cluser)
26
26
}
27
27
}
28
+
29
+ /// Register iterator
30
+ pub struct RegisterIter < ' a > {
31
+ pub ( crate ) all : std:: slice:: Iter < ' a , RegisterCluster > ,
32
+ }
33
+
34
+ impl < ' a > std:: iter:: Iterator for RegisterIter < ' a > {
35
+ type Item = & ' a Register ;
36
+ fn next ( & mut self ) -> Option < Self :: Item > {
37
+ match self . all . next ( ) {
38
+ None => None ,
39
+ Some ( RegisterCluster :: Register ( reg) ) => Some ( reg) ,
40
+ _ => self . next ( ) ,
41
+ }
42
+ }
43
+ }
44
+
45
+ /// Mutable register iterator
46
+ pub struct RegisterIterMut < ' a > {
47
+ pub ( crate ) all : std:: slice:: IterMut < ' a , RegisterCluster > ,
48
+ }
49
+
50
+ impl < ' a > std:: iter:: Iterator for RegisterIterMut < ' a > {
51
+ type Item = & ' a mut Register ;
52
+ fn next ( & mut self ) -> Option < Self :: Item > {
53
+ match self . all . next ( ) {
54
+ None => None ,
55
+ Some ( RegisterCluster :: Register ( reg) ) => Some ( reg) ,
56
+ _ => self . next ( ) ,
57
+ }
58
+ }
59
+ }
60
+
61
+ /// Cluster iterator
62
+ pub struct ClusterIter < ' a > {
63
+ pub ( crate ) all : std:: slice:: Iter < ' a , RegisterCluster > ,
64
+ }
65
+
66
+ impl < ' a > std:: iter:: Iterator for ClusterIter < ' a > {
67
+ type Item = & ' a Cluster ;
68
+ fn next ( & mut self ) -> Option < Self :: Item > {
69
+ match self . all . next ( ) {
70
+ None => None ,
71
+ Some ( RegisterCluster :: Cluster ( c) ) => Some ( c) ,
72
+ _ => self . next ( ) ,
73
+ }
74
+ }
75
+ }
76
+
77
+ /// Mutable cluster iterator
78
+ pub struct ClusterIterMut < ' a > {
79
+ pub ( crate ) all : std:: slice:: IterMut < ' a , RegisterCluster > ,
80
+ }
81
+
82
+ impl < ' a > std:: iter:: Iterator for ClusterIterMut < ' a > {
83
+ type Item = & ' a mut Cluster ;
84
+ fn next ( & mut self ) -> Option < Self :: Item > {
85
+ match self . all . next ( ) {
86
+ None => None ,
87
+ Some ( RegisterCluster :: Cluster ( c) ) => Some ( c) ,
88
+ _ => self . next ( ) ,
89
+ }
90
+ }
91
+ }
92
+
93
+ /// Iterator over all registers
94
+ pub struct AllRegistersIter < ' a > {
95
+ pub ( crate ) rem : Vec < & ' a RegisterCluster > ,
96
+ }
97
+
98
+ impl < ' a > std:: iter:: Iterator for AllRegistersIter < ' a > {
99
+ type Item = & ' a Register ;
100
+ fn next ( & mut self ) -> Option < Self :: Item > {
101
+ while let Some ( b) = self . rem . pop ( ) {
102
+ match b {
103
+ RegisterCluster :: Register ( reg) => {
104
+ return Some ( reg) ;
105
+ }
106
+ RegisterCluster :: Cluster ( cluster) => {
107
+ for c in cluster. children . iter ( ) . rev ( ) {
108
+ self . rem . push ( c) ;
109
+ }
110
+ }
111
+ }
112
+ }
113
+ None
114
+ }
115
+ }
116
+
117
+ /// Mutable iterator over all registers
118
+ pub struct AllRegistersIterMut < ' a > {
119
+ pub ( crate ) rem : Vec < & ' a mut RegisterCluster > ,
120
+ }
121
+
122
+ impl < ' a > std:: iter:: Iterator for AllRegistersIterMut < ' a > {
123
+ type Item = & ' a mut Register ;
124
+ fn next ( & mut self ) -> Option < Self :: Item > {
125
+ while let Some ( b) = self . rem . pop ( ) {
126
+ match b {
127
+ RegisterCluster :: Register ( reg) => {
128
+ return Some ( reg) ;
129
+ }
130
+ RegisterCluster :: Cluster ( cluster) => {
131
+ for c in cluster. children . iter_mut ( ) . rev ( ) {
132
+ self . rem . push ( c) ;
133
+ }
134
+ }
135
+ }
136
+ }
137
+ None
138
+ }
139
+ }
140
+
141
+ /// Iterates over optional iterator
142
+ pub struct OptIter < I > ( Option < I > )
143
+ where
144
+ I : Iterator ;
145
+
146
+ impl < I > OptIter < I >
147
+ where
148
+ I : Iterator ,
149
+ {
150
+ /// Create new optional iterator
151
+ pub fn new ( o : Option < I > ) -> Self {
152
+ Self ( o)
153
+ }
154
+ }
155
+
156
+ impl < ' a , I > Iterator for OptIter < I >
157
+ where
158
+ I : Iterator ,
159
+ {
160
+ type Item = I :: Item ;
161
+ fn next ( & mut self ) -> Option < Self :: Item > {
162
+ self . 0 . as_mut ( ) . and_then ( I :: next)
163
+ }
164
+ }
0 commit comments