1- // source: https://github.com/maneatingape/advent-of-code-rust/blob/72c8feb22030d55d7a08f16c119425b2ba759470 /src/util/hash .rs
1+ // source: https://github.com/maneatingape/advent-of-code-rust/blob/109bf05f8cb5026d97af42b42ea3985afe600dfb /src/util/grid .rs
22
33//! Fast 2 dimensional Grid backed by a single `vec`. This module is designed to work with [`Point`].
44//!
@@ -52,14 +52,6 @@ impl Grid<u8> {
5252}
5353
5454impl < T : Copy + PartialEq > Grid < T > {
55- pub fn default_copy < U : Default + Copy > ( & self ) -> Grid < U > {
56- Grid {
57- width : self . width ,
58- height : self . height ,
59- bytes : vec ! [ U :: default ( ) ; ( self . width * self . height) as usize ] ,
60- }
61- }
62-
6355 pub fn find ( & self , needle : T ) -> Option < Point > {
6456 let to_point = |index| {
6557 let x = ( index as i32 ) % self . width ;
@@ -68,6 +60,26 @@ impl<T: Copy + PartialEq> Grid<T> {
6860 } ;
6961 self . bytes . iter ( ) . position ( |& h| h == needle) . map ( to_point)
7062 }
63+ }
64+
65+ impl < T : Copy > Grid < T > {
66+ pub fn new ( width : i32 , height : i32 , value : T ) -> Grid < T > {
67+ Grid {
68+ width,
69+ height,
70+ bytes : vec ! [ value; ( width * height) as usize ] ,
71+ }
72+ }
73+ }
74+
75+ impl < T > Grid < T > {
76+ pub fn default_copy < U : Default + Copy > ( & self ) -> Grid < U > {
77+ Grid {
78+ width : self . width ,
79+ height : self . height ,
80+ bytes : vec ! [ U :: default ( ) ; ( self . width * self . height) as usize ] ,
81+ }
82+ }
7183
7284 #[ inline]
7385 pub fn contains ( & self , point : Point ) -> bool {
@@ -79,14 +91,14 @@ impl<T> Index<Point> for Grid<T> {
7991 type Output = T ;
8092
8193 #[ inline]
82- fn index ( & self , point : Point ) -> & Self :: Output {
83- & self . bytes [ ( self . width * point . y + point . x ) as usize ]
94+ fn index ( & self , index : Point ) -> & Self :: Output {
95+ & self . bytes [ ( self . width * index . y + index . x ) as usize ]
8496 }
8597}
8698
8799impl < T > IndexMut < Point > for Grid < T > {
88100 #[ inline]
89- fn index_mut ( & mut self , point : Point ) -> & mut Self :: Output {
90- & mut self . bytes [ ( self . width * point . y + point . x ) as usize ]
101+ fn index_mut ( & mut self , index : Point ) -> & mut Self :: Output {
102+ & mut self . bytes [ ( self . width * index . y + index . x ) as usize ]
91103 }
92104}
0 commit comments