Struct simple_matrix::Matrix[][src]

pub struct Matrix<T> { /* fields omitted */ }

A 2-Dimensional, non-resisable container.

Methods

impl<T> Matrix<T>
[src]

Constructs a new, non-empty Matrix where cells are set to T::default.
Use Matrix::from_iter if you want to set the matrix from an iterator.

Panics

Panics if either rows or cols are equal to 0

Examples

let mut mat: Matrix<i32> = Matrix::new(3, 6);

Constructs a new, non-empty Matrix where cells are set from an iterator.
The matrix cells are set row by row.
The iterator can be infinite, this method only consume rows * cols values from the iterator.

Panics

Panics if either rows or cols are equal to 0.
Panics if the iterator does not have rows * cols values

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.get(0, 0).unwrap(), 0);
assert_eq!(mat.get(0, 1).unwrap(), 1);
assert_eq!(mat.get(1, 0).unwrap(), 6);

Returns the number of rows in the matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.rows(), 3);

Returns the number of columns in the matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.cols(), 6);

Try to get a reference to the value at given row & column.
Returns None if row or col is outside of the matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.get(0, 0).unwrap(), 0);
assert_eq!(mat.get(2, 5).unwrap(), 17);

assert!(mat.get(10, 2).is_err());

Try to get a mutable reference to the cell at given row & column.
Returns None if row or col is outside of the matrix.

Examples

let mut mat: Matrix<usize> = Matrix::new(3, 6, 0..);
assert_eq!(mat.get(0, 0).unwrap(), 0);

let cell = mat.get_mut(0, 0).unwrap();
*cell = 5;

assert_eq!(mat.get(0, 0).unwrap(), 5);

Try to set the cell at given row & column to the given value.
Returns false if row or col is outside of the matrix.
Returns true if the cell has been modified.

Examples

let mut mat: Matrix<usize> = Matrix::new(3, 6, 0..);
assert_eq!(mat.get(0, 0).unwrap(), 0);

mat.set(0, 0, 5);
assert_eq!(mat.get(0, 0).unwrap(), 5);

Try to get an iterator of all cells of the requested row.
Returns None if given row is outside of the matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.get_row(1).unwrap(), vec![6, 7, 8, 9, 10, 11]);

assert!(mat.get_row(5).is_err());

Try to get an iterator of all cells of the requested column.
Returns None if given row is outside of the matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);

assert_eq!(mat.get_col(1).unwrap(), vec![1, 7, 13]);

assert!(mat.get_col(10).is_err());

Take a MxN Matrix and construct the transposed NxM Matrix.

Examples

let mat: Matrix<usize> = Matrix::new(3, 6, 0..);
let mat_t = mat.transpose();

assert_eq!(mat.rows(), mat_t.cols());
assert_eq!(mat.cols(), mat_t.rows());

assert_eq!(mat.get(0, 0).unwrap(), mat_t.get(0, 0).unwrap());
assert_eq!(mat.get(1, 2).unwrap(), mat_t.get(2, 1).unwrap());

Apply a function to all cells of the matrix.
Cells are provided as immutable references to the function, if you want to modify the cells, use apply_mut.

Examples

// Get the sum of all cells
let mat: Matrix<usize> = Matrix::new(3, 6, 0..);
let mut sum = 0;
mat.apply(|n| sum += *n);

assert_eq!(sum, 153);

Apply a function to all cells of the matrix.
Cells are provided as mutable references to the function, and can therefore be modified.

Examples

// Modify all cells with a function
let mut mat: Matrix<usize> = Matrix::new(3, 6, 0..);
mat.apply_mut(|n| n *= 2);

assert_eq!(mat.get(0, 0).unwrap(), 0);
assert_eq!(mat.get(0, 1).unwrap(), 2);
assert_eq!(mat.get(0, 2).unwrap(), 4);

Methods from Deref<Target = Vec<T>>

Returns the number of elements the vector can hold without reallocating.

Examples

let vec: Vec<i32> = Vec::with_capacity(10);
assert_eq!(vec.capacity(), 10);

Important traits for &'a [u8]

Extracts a slice containing the entire vector.

Equivalent to &s[..].

Examples

use std::io::{self, Write};
let buffer = vec![1, 2, 3, 5, 8];
io::sink().write(buffer.as_slice()).unwrap();

Returns the number of elements in the vector, also referred to as its 'length'.

Examples

let a = vec![1, 2, 3];
assert_eq!(a.len(), 3);

Returns true if the vector contains no elements.

Examples

let mut v = Vec::new();
assert!(v.is_empty());

v.push(1);
assert!(!v.is_empty());

Trait Implementations

impl<T> IntoIterator for Matrix<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, T> IntoIterator for &'a Matrix<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, T> IntoIterator for &'a mut Matrix<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<T: Add<Output = T>> Add for Matrix<T>
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<'a: 'b, 'b, T> Add for &'a Matrix<T> where
    &'a T: Add<&'b T, Output = T>, 
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<T: AddAssign> AddAssign for Matrix<T>
[src]

Performs the += operation.

impl<'a, T: AddAssign<&'a T>> AddAssign<&'a Matrix<T>> for Matrix<T>
[src]

Performs the += operation.

impl<T: Sub<Output = T>> Sub for Matrix<T>
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl<'a: 'b, 'b, T> Sub for &'a Matrix<T> where
    &'a T: Sub<&'b T, Output = T>, 
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl<T: SubAssign> SubAssign for Matrix<T>
[src]

Performs the -= operation.

impl<'a, T: SubAssign<&'a T>> SubAssign<&'a Matrix<T>> for Matrix<T>
[src]

Performs the -= operation.

impl<T> Mul<Matrix<T>> for Matrix<T> where
    T: Mul<Output = T> + AddAssign + Copy
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl<'a, 'b, T: AddAssign> Mul<&'b Matrix<T>> for &'a Matrix<T> where
    &'a T: Mul<&'b T, Output = T>, 
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl<T: Clone> Clone for Matrix<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for Matrix<T>
[src]

Formats the value using the given formatter. Read more

impl<T: Hash> Hash for Matrix<T>
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<T: PartialEq> PartialEq for Matrix<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Eq> Eq for Matrix<T>
[src]

impl<T: PartialOrd> PartialOrd for Matrix<T>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T> Deref for Matrix<T>
[src]

The resulting type after dereferencing.

Dereferences the value.

Auto Trait Implementations

impl<T> Send for Matrix<T> where
    T: Send

impl<T> Sync for Matrix<T> where
    T: Sync