Trait rusty_machine::linalg::BaseSlice [] [src]

pub trait BaseSlice<T> {
    fn rows(&self) -> usize;
    fn cols(&self) -> usize;
    fn row_stride(&self) -> usize;
    fn as_ptr(&self) -> *const T;

    unsafe fn get_unchecked(&self, index: [usize; 2]) -> &T { ... }
    fn get_row(&self, index: usize) -> Option<&[T]> { ... }
    unsafe fn get_row_unchecked(&self, index: usize) -> &[T] { ... }
}

Trait for Matrix Slices.

Required Methods

fn rows(&self) -> usize

Rows in the slice.

fn cols(&self) -> usize

Columns in the slice.

fn row_stride(&self) -> usize

Row stride in the slice.

fn as_ptr(&self) -> *const T

Top left index of the slice.

Provided Methods

unsafe fn get_unchecked(&self, index: [usize; 2]) -> &T

Get a reference to a point in the slice without bounds checking.

fn get_row(&self, index: usize) -> Option<&[T]>

Returns the row of a Matrix at the given index. None if the index is out of bounds.

Examples

use rulinalg::matrix::{Matrix, MatrixSlice};
use rulinalg::matrix::slice::BaseSlice;

let mut a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
let mut slice = MatrixSlice::from_matrix(&mut a, [1,1], 2, 2);
let row = slice.get_row(1);
let mut expected = vec![7usize, 8];
assert_eq!(row, Some(&*expected));
assert!(slice.get_row(5).is_none());

unsafe fn get_row_unchecked(&self, index: usize) -> &[T]

Returns the row of a BaseSlice at the given index without doing unbounds checking

Examples

use rulinalg::matrix::{Matrix, MatrixSlice};
use rulinalg::matrix::slice::BaseSlice;

let mut a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
let mut slice = MatrixSlice::from_matrix(&mut a, [1,1], 2, 2);
let row = unsafe { slice.get_row_unchecked(1) };
let mut expected = vec![7usize, 8];
assert_eq!(row, &*expected);

Implementors