Skip to content

Commit

Permalink
Added "get" and "get_mut" to BaseMatrix and BaseMatrixMut (#158)
Browse files Browse the repository at this point in the history
* Added a "get" method to BaseMatrix.

This resolves issue #151.

* Added a "get_mut" function to BaseMatrixMut.

This is related to issue #151.

* Changed indentation and comments.

As discussed, changed "a point" to "an element" in the comments for the
"get" family of functions.

Also fixed the indentation of the new "get" and "get_mut" functions by
changing tabs to spaces.
  • Loading branch information
prismatica authored and AtheMathmo committed Mar 7, 2017
1 parent 2239a6f commit 00e8b70
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/matrix/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,40 @@ pub trait BaseMatrix<T>: Sized {
}
}

/// Get a reference to a point in the matrix without bounds checking.
/// Get a reference to an element in the matrix without bounds checking.
unsafe fn get_unchecked(&self, index: [usize; 2]) -> &T {
&*(self.as_ptr().offset((index[0] * self.row_stride() + index[1]) as isize))
}

/// Get a reference to an element in the matrix.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg; fn main() {
/// use rulinalg::matrix::{Matrix, BaseMatrix};
///
/// let mat = matrix![0, 1;
/// 3, 4;
/// 6, 7];
///
/// assert_eq!(mat.get([0, 2]), None);
/// assert_eq!(mat.get([3, 0]), None);
///
/// assert_eq!( *mat.get([0, 0]).unwrap(), 0)
/// # }
/// ```
fn get(&self, index: [usize; 2]) -> Option<&T> {
let row_ind = index[0];
let col_ind = index[1];

if row_ind >= self.rows() || col_ind >= self.cols() {
None
} else {
unsafe { Some(self.get_unchecked(index)) }
}
}

/// Returns the column of a matrix at the given index.
/// `None` if the index is out of bounds.
///
Expand Down Expand Up @@ -1139,11 +1168,42 @@ pub trait BaseMatrixMut<T>: BaseMatrix<T> {
}
}

/// Get a mutable reference to a point in the matrix without bounds checks.
/// Get a mutable reference to an element in the matrix without bounds checks.
unsafe fn get_unchecked_mut(&mut self, index: [usize; 2]) -> &mut T {
&mut *(self.as_mut_ptr().offset((index[0] * self.row_stride() + index[1]) as isize))
}

/// Get a mutable reference to an element in the matrix.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg; fn main() {
/// use rulinalg::matrix::{Matrix, BaseMatrix, BaseMatrixMut};
///
/// let mut mat = matrix![0, 1;
/// 3, 4;
/// 6, 7];
///
/// assert_eq!(mat.get_mut([0, 2]), None);
/// assert_eq!(mat.get_mut([3, 0]), None);
///
/// assert_eq!(*mat.get_mut([0, 0]).unwrap(), 0);
/// *mat.get_mut([0,0]).unwrap() = 2;
/// assert_eq!(*mat.get_mut([0, 0]).unwrap(), 2);
/// # }
/// ```
fn get_mut(&mut self, index: [usize; 2]) -> Option<&mut T> {
let row_ind = index[0];
let col_ind = index[1];

if row_ind >= self.rows() || col_ind >= self.cols() {
None
} else {
unsafe { Some(self.get_unchecked_mut(index)) }
}
}

/// Returns a mutable iterator over the matrix.
///
/// # Examples
Expand Down

0 comments on commit 00e8b70

Please sign in to comment.