Add get unchecked

This commit is contained in:
Armin Becher 2020-04-05 15:03:31 +02:00
parent 9d77a6d4fd
commit 508148af21

View file

@ -134,11 +134,26 @@ impl<T: Clone> Grid<T> {
} }
} }
/// Returns a reference to an element, without performing bound checks.
/// Generally not recommended, use with caution!
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
pub unsafe fn get_unchecked(&self, row: usize, col: usize) -> &T {
self.data.get_unchecked(row * self.cols() + col)
}
/// Returns a mutable reference to an element, without performing bound checks.
/// Generally not recommended, use with caution!
/// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
pub unsafe fn get_unchecked_mut(&mut self, row: usize, col: usize) -> &mut T {
let cols = self.cols;
self.data.get_unchecked_mut(row * cols + col)
}
/// Access a certain element in the grid. /// Access a certain element in the grid.
/// Returns None if an element beyond the grid bounds is tried to be accessed. /// Returns None if an element beyond the grid bounds is tried to be accessed.
pub fn get(&self, row: usize, col: usize) -> Option<&T> { pub fn get(&self, row: usize, col: usize) -> Option<&T> {
if row < self.rows && col < self.cols() { if row < self.rows && col < self.cols {
unsafe { Some(&self.data.get_unchecked(row * self.cols() + col)) } unsafe { Some(self.get_unchecked(row, col)) }
} else { } else {
None None
} }
@ -147,9 +162,8 @@ impl<T: Clone> Grid<T> {
/// Mutable access to a certain element in the grid. /// Mutable access to a certain element in the grid.
/// Returns None if an element beyond the grid bounds is tried to be accessed. /// Returns None if an element beyond the grid bounds is tried to be accessed.
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> { pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
if row < self.rows && col < self.cols() { if row < self.rows && col < self.cols {
let cols = self.cols(); unsafe { Some(self.get_unchecked_mut(row, col)) }
unsafe { Some(self.data.get_unchecked_mut(row * cols + col)) }
} else { } else {
None None
} }