routine push, successfully implemented the [] operator
This commit is contained in:
parent
48acb9a639
commit
f621d03f1a
@ -1,41 +0,0 @@
|
||||
//A matrix is constructed by chaining together elements
|
||||
|
||||
//Before even a struct, we make an enum!
|
||||
//The enum can allow us to set Number or Nothing,
|
||||
//Stopping the infinite size issues
|
||||
|
||||
//Element : either a Number or Nothing
|
||||
|
||||
//Number : a node that containes a value,
|
||||
// and points to other elements
|
||||
|
||||
|
||||
|
||||
struct Number {
|
||||
value : isize,
|
||||
// the value to the right
|
||||
// By putting the value in a Box<>, we point to a
|
||||
right : Option<Box<Number>>,
|
||||
// the value below
|
||||
below : Option<Box<Number>>
|
||||
|
||||
}
|
||||
|
||||
struct Matrix {
|
||||
size : (usize, usize),
|
||||
elements : Vec<Vec<i32>>
|
||||
}
|
||||
|
||||
|
||||
impl Matrix {
|
||||
pub fn new(rows : usize, columns : usize) -> Matrix {
|
||||
//Construct an empty vector
|
||||
let mut elements : Vec<Vec<i32>> = vec![vec![0;columns];rows];
|
||||
|
||||
Matrix {
|
||||
size : (rows , columns),
|
||||
elements : elements
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
12
src/matrix/addition.rs
Normal file
12
src/matrix/addition.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use super::Matrix;
|
||||
use std::ops::Add;
|
||||
|
||||
impl Add for Matrix {
|
||||
type Output = Matrix;
|
||||
|
||||
fn add(self, other: Matrix) -> Matrix {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
109
src/matrix/mod.rs
Normal file
109
src/matrix/mod.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use std::ops::{Add, Index, IndexMut};
|
||||
|
||||
// TODO: create a generic type that has some restrictions:
|
||||
// Needs to implement the add trait
|
||||
struct Matrix {
|
||||
size : (usize, usize),
|
||||
elements : Vec<Vec<i32>>
|
||||
}
|
||||
|
||||
|
||||
impl Matrix {
|
||||
//Creates a matrix struct
|
||||
pub fn new(rows : usize, columns : usize) -> Matrix {
|
||||
|
||||
//Construct an empty vector
|
||||
let elements : Vec<Vec<i32>> = vec![vec![0;columns];rows];
|
||||
|
||||
//Construct the struct
|
||||
Matrix {
|
||||
size : (rows , columns),
|
||||
elements
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the size of the matrix
|
||||
pub fn size(& self) -> (usize, usize) {
|
||||
self.size
|
||||
}
|
||||
}
|
||||
// implementation of the fmt::Display trait
|
||||
|
||||
// implementation of index ([])
|
||||
// Technically ([]) is unsafe too, but...
|
||||
// I probably should just let the vector handle the error
|
||||
|
||||
// After doing more research, I can instead use get in order to make the output
|
||||
// an Option<>
|
||||
impl Index<(usize, usize)> for Matrix {
|
||||
type Output = Option<i32>;
|
||||
|
||||
fn index(&self, index: (usize, usize)) -> &Self::Output {
|
||||
//Since it returns an Option, its not as simple as a get
|
||||
let row : Option<&Vec<i32>> = self.elements.get(index.0); //the 'T' in Option<T> from get is a reference
|
||||
//Now, we need to unpack the Option
|
||||
match row
|
||||
}
|
||||
}
|
||||
|
||||
//mutable variant - so we can modify the entries
|
||||
impl IndexMut<(usize, usize)> for Matrix {
|
||||
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
|
||||
&mut self.elements[index.0][index.1]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// implementation of add (+)
|
||||
// (+) is unsafe; they could add two matricies of different sizes
|
||||
// that's undefined behavior. SO, we're going to pass an error if that happens
|
||||
impl Add for Matrix {
|
||||
type Output = Result<Matrix, String>;
|
||||
|
||||
fn add(self, rhs: Self) -> Result<Matrix, String> {
|
||||
// Returns Err if they are not the same size
|
||||
if self.size() != rhs.size() {
|
||||
return Err("Both matricies must be of the same size!".to_string());
|
||||
}
|
||||
|
||||
// Makes a new matrix
|
||||
let mut return_matrix = Matrix::new(self.size.0, self.size.1);
|
||||
|
||||
//Go through each element
|
||||
for r in 0..self.size().0{
|
||||
for c in 0..self.size().1 {
|
||||
//Can't assign it???
|
||||
//Ahhhhh ok, so I must implement both the immutable and mutable versions of an
|
||||
//index before I am able to call it
|
||||
return_matrix[(r,c)] = self[(r,c)] + rhs[(r,c)];
|
||||
}
|
||||
}
|
||||
|
||||
Ok(return_matrix)
|
||||
}
|
||||
}
|
||||
|
||||
// implementation of eq (==)
|
||||
impl PartialEq for Matrix {
|
||||
fn eq(& self, other : &Matrix) -> bool {
|
||||
|
||||
//First check if they are the same size
|
||||
if self.size != other.size {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Next check if each element is the same
|
||||
//We dont need to check EVERY element, just each vector
|
||||
let rows = self.size.1;
|
||||
|
||||
for i in 0..rows {
|
||||
//Oh nice, it implements PartialEq. I would have to check
|
||||
//in C++ normally, and it would probably be the same address of stuff
|
||||
if self.elements[i] != other.elements[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user