1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#![allow(non_snake_case)]
#![allow(dead_code)]

//! A wrapper for a 2D vector.
//! 
//! This Module contains the Matrix type that allows
//! you to create a matrix of any type that implements the
//! Default trait.

use std::default::Default;
use std::ops::{Index, IndexMut};


/// A wrapper for a 2D vector.
/// 
/// The Matrix struct accepts any type that implements the Default trait.
///    
/// # Examples
/// 
/// ```
///     let rows = 10;
///     let cols = 10;
///     let mut m : Matrix<isize> = Matrix::new(rows, cols);
///     m[(0, 0)] = 3;
///     m[(0, 1)] = 5;
/// 
///     let num = m[(0, 0)];
/// 
///     let score = m.get(0, 1).expect("Error getting an immutable reference");
/// 
///     // *score = 34; Uncommenting this line will result as an error    
/// 
///     let score = m.get_mut(0, 1).expect("Error getting a mutable reference");
/// 
///     *score = 10; // now m[(0, 1)] is equal to 10
/// ```
pub struct Matrix<T> {
    vec: Vec<T>,
    cols: usize,
}

impl<T> Matrix<T>
where
    T: Default,
{
    /// Creates a new instance of Matrix 
    /// filling it with the default value of the type T
    /// 
    /// # Example
    /// ```
    ///     let mut m : Matrix<isize> = Matrix::new(10, 20);
    ///     // Now the matrix is filled entirely with the default value
    ///     // of the isize primitive type which is 0.
    /// ```
    /// 
    pub fn new(n: usize, m: usize) -> Matrix<T> {
        Matrix {
            vec: Matrix::vec_2d(n, m),
            cols: m,
        }
    }

    /// Get an immutable reference of the value on matrix[(i, j)]
    /// **Return**: An Option that returns None if the index is out of range
    ///             or the reference within a [`Some`] variant.
    pub fn get(&self, i: usize, j: usize) -> Option<&T> {
        self.vec.get(i * self.cols + j)
    }

    /// Get an mutable reference of the value on matrix[(i, j)]
    /// **Return**: An Option that returns None if the index is out of range
    ///             or the reference within a [`Some`] variant.
    pub fn get_mut(&mut self, i: usize, j: usize) -> Option<&mut T> {
        self.vec.get_mut(i * self.cols + j)
    }

    fn vec_2d(n: usize, m: usize) -> Vec<T> {
        let mut matrix: Vec<T> = Vec::with_capacity(n * m);
        matrix.resize_with(n * m, Default::default);
        matrix
    }
}

impl<T> Index<(usize, usize)> for Matrix<T>
where
    T: Default,
{
    type Output = T;
    fn index(&self, idx: (usize, usize)) -> &Self::Output {
        self.get(idx.0, idx.1).expect("Index out of bounds")
    }
}

impl<T> IndexMut<(usize, usize)> for Matrix<T>
where
    T: Default,
{
    fn index_mut(&mut self, idx: (usize, usize)) -> &mut Self::Output {
        self.get_mut(idx.0, idx.1).expect("Index out of bounds")
    }
}