1717//! ```
1818//!
1919//! A convenience [`parse`] method creates a `Grid` directly from a 2 dimenionsal set of
20- //! ASCII characters, a common occurence in Advent of Code inputs. The [`default_copy `] function
20+ //! ASCII characters, a common occurence in Advent of Code inputs. The [`same_size_with `] function
2121//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
2222//! location or for tracking cost in Djikstra.
2323//!
2424//! [`Point`]: crate::util::point
2525//! [`parse`]: Grid::parse
26- //! [`default_copy `]: Grid::default_copy
26+ //! [`same_size_with `]: Grid::same_size_with
2727use crate :: util:: point:: * ;
2828use std:: ops:: { Index , IndexMut } ;
2929
@@ -35,6 +35,7 @@ pub struct Grid<T> {
3535}
3636
3737impl Grid < u8 > {
38+ #[ inline]
3839 pub fn parse ( input : & str ) -> Self {
3940 let raw: Vec < _ > = input. lines ( ) . map ( str:: as_bytes) . collect ( ) ;
4041 let width = raw[ 0 ] . len ( ) as i32 ;
@@ -46,6 +47,7 @@ impl Grid<u8> {
4647}
4748
4849impl < T : Copy + PartialEq > Grid < T > {
50+ #[ inline]
4951 pub fn find ( & self , needle : T ) -> Option < Point > {
5052 let to_point = |index| {
5153 let x = ( index as i32 ) % self . width ;
@@ -63,11 +65,12 @@ impl<T: Copy> Grid<T> {
6365}
6466
6567impl < T > Grid < T > {
66- pub fn default_copy < U : Default + Copy > ( & self ) -> Grid < U > {
68+ #[ inline]
69+ pub fn same_size_with < U : Copy > ( & self , value : U ) -> Grid < U > {
6770 Grid {
6871 width : self . width ,
6972 height : self . height ,
70- bytes : vec ! [ U :: default ( ) ; ( self . width * self . height) as usize ] ,
73+ bytes : vec ! [ value ; ( self . width * self . height) as usize ] ,
7174 }
7275 }
7376
0 commit comments