Created
March 5, 2019 19:31
-
-
Save 17cupsofcoffee/938e93f30a782baa5c2148207c4c4218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct VecGrid<T> { | |
data: Vec<T>, | |
width: usize, | |
height: usize, | |
} | |
impl<T> VecGrid<T> { | |
pub fn new(width: usize, height: usize) -> VecGrid<T> { | |
VecGrid { | |
data: Vec::with_capacity(width * height), | |
width, | |
height, | |
} | |
} | |
pub fn get(&self, x: usize, y: usize) -> Option<&T> { | |
self.data.get(x + (y * self.width)) | |
} | |
pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut T> { | |
self.data.get_mut(x + (y * self.width)) | |
} | |
} | |
impl<T: Clone> VecGrid<T> { | |
pub fn with(width: usize, height: usize, item: T) -> VecGrid<T> { | |
VecGrid { | |
data: vec![item; width * height], | |
width, | |
height, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment