@@ -114,52 +114,6 @@ impl Grid {
114114 . map ( |( i, _) | self . pos ( i) )
115115 }
116116
117- pub fn neighbors ( & self , index : usize ) -> impl Neighbors {
118- let ( row, col) = self . coords ( index) ;
119- NeighborsIterOld ( [
120- // north
121- row. checked_sub ( 1 )
122- . filter ( |& r| self . in_bounds ( r, col) )
123- . map ( |r| r * self . width + col) ,
124- // east
125- Some ( ( row, col + 1 ) )
126- . filter ( |& ( r, c) | self . in_bounds ( r, c) )
127- . map ( |( r, c) | r * self . width + c) ,
128- // south
129- Some ( ( row + 1 , col) )
130- . filter ( |& ( r, c) | self . in_bounds ( r, c) )
131- . map ( |( r, c) | r * self . width + c) ,
132- // west
133- col. checked_sub ( 1 )
134- . filter ( |& c| self . in_bounds ( row, c) )
135- . map ( |c| row * self . width + c) ,
136- ] )
137- }
138-
139- /// Returns an Iterator over the 4 diagonal neighbors of a grid location
140- pub fn neighbors_diag ( & self , index : usize ) -> impl NeighborsDiag {
141- let ( row, col) = self . coords ( index) ;
142- NeighborsIterOld ( [
143- // north east
144- row. checked_sub ( 1 )
145- . filter ( |& r| self . in_bounds ( r, col + 1 ) )
146- . map ( |r| r * self . width + ( col + 1 ) ) ,
147- // south east
148- Some ( ( row + 1 , col + 1 ) )
149- . filter ( |& ( r, c) | self . in_bounds ( r, c) )
150- . map ( |( r, c) | r * self . width + c) ,
151- // south west
152- col. checked_sub ( 1 )
153- . filter ( |& c| self . in_bounds ( row + 1 , c) )
154- . map ( |c| ( row + 1 ) * self . width + c) ,
155- // north west
156- row. checked_sub ( 1 )
157- . zip ( col. checked_sub ( 1 ) )
158- . filter ( |& ( r, c) | self . in_bounds ( r, c) )
159- . map ( |( r, c) | r * self . width + c) ,
160- ] )
161- }
162-
163117 pub fn pos ( & self , index : usize ) -> Pos {
164118 Pos {
165119 index,
@@ -292,47 +246,7 @@ impl Pos {
292246 }
293247}
294248
295- pub trait Neighbors : Iterator < Item = usize > {
296- fn north ( & self ) -> Option < usize > ;
297- fn east ( & self ) -> Option < usize > ;
298- fn south ( & self ) -> Option < usize > ;
299- fn west ( & self ) -> Option < usize > ;
300- }
301-
302- pub trait NeighborsDiag : Iterator < Item = usize > {
303- fn north_east ( & self ) -> Option < usize > ;
304- fn south_east ( & self ) -> Option < usize > ;
305- fn south_west ( & self ) -> Option < usize > ;
306- fn north_west ( & self ) -> Option < usize > ;
307- }
308-
309249pub struct NeighborsIter ( [ Option < Pos > ; 8 ] ) ;
310- pub struct NeighborsIterOld ( [ Option < usize > ; 4 ] ) ;
311-
312- impl Neighbors for NeighborsIterOld {
313- fn north ( & self ) -> Option < usize > {
314- self . 0 [ 0 ]
315- }
316-
317- fn east ( & self ) -> Option < usize > {
318- self . 0 [ 1 ]
319- }
320-
321- fn south ( & self ) -> Option < usize > {
322- self . 0 [ 2 ]
323- }
324-
325- fn west ( & self ) -> Option < usize > {
326- self . 0 [ 3 ]
327- }
328- }
329-
330- impl Iterator for NeighborsIterOld {
331- type Item = usize ;
332- fn next ( & mut self ) -> Option < Self :: Item > {
333- self . 0 . iter_mut ( ) . find_map ( |opt| opt. take ( ) )
334- }
335- }
336250
337251impl Iterator for NeighborsIter {
338252 type Item = Pos ;
@@ -341,24 +255,6 @@ impl Iterator for NeighborsIter {
341255 }
342256}
343257
344- impl NeighborsDiag for NeighborsIterOld {
345- fn north_east ( & self ) -> Option < usize > {
346- self . 0 [ 0 ]
347- }
348-
349- fn south_east ( & self ) -> Option < usize > {
350- self . 0 [ 1 ]
351- }
352-
353- fn south_west ( & self ) -> Option < usize > {
354- self . 0 [ 2 ]
355- }
356-
357- fn north_west ( & self ) -> Option < usize > {
358- self . 0 [ 3 ]
359- }
360- }
361-
362258#[ cfg( test) ]
363259mod test {
364260 use super :: * ;
0 commit comments