17
17
//! ```
18
18
//!
19
19
//! 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
21
21
//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
22
22
//! location or for tracking cost in Djikstra.
23
23
//!
24
24
//! [`Point`]: crate::util::point
25
25
//! [`parse`]: Grid::parse
26
- //! [`default_copy `]: Grid::default_copy
26
+ //! [`same_size_with `]: Grid::same_size_with
27
27
use crate :: util:: point:: * ;
28
28
use std:: ops:: { Index , IndexMut } ;
29
29
@@ -35,6 +35,7 @@ pub struct Grid<T> {
35
35
}
36
36
37
37
impl Grid < u8 > {
38
+ #[ inline]
38
39
pub fn parse ( input : & str ) -> Self {
39
40
let raw: Vec < _ > = input. lines ( ) . map ( str:: as_bytes) . collect ( ) ;
40
41
let width = raw[ 0 ] . len ( ) as i32 ;
@@ -46,6 +47,7 @@ impl Grid<u8> {
46
47
}
47
48
48
49
impl < T : Copy + PartialEq > Grid < T > {
50
+ #[ inline]
49
51
pub fn find ( & self , needle : T ) -> Option < Point > {
50
52
let to_point = |index| {
51
53
let x = ( index as i32 ) % self . width ;
@@ -63,11 +65,12 @@ impl<T: Copy> Grid<T> {
63
65
}
64
66
65
67
impl < 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 > {
67
70
Grid {
68
71
width : self . width ,
69
72
height : self . height ,
70
- bytes : vec ! [ U :: default ( ) ; ( self . width * self . height) as usize ] ,
73
+ bytes : vec ! [ value ; ( self . width * self . height) as usize ] ,
71
74
}
72
75
}
73
76
0 commit comments