Skip to content

Instantly share code, notes, and snippets.

@17cupsofcoffee
Created March 5, 2019 19:31
Show Gist options
  • Save 17cupsofcoffee/938e93f30a782baa5c2148207c4c4218 to your computer and use it in GitHub Desktop.
Save 17cupsofcoffee/938e93f30a782baa5c2148207c4c4218 to your computer and use it in GitHub Desktop.
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