use crate::matrices::{Matrix, Row, Column};
pub struct ColumnIterator<'a, T: Clone> {
matrix: &'a Matrix<T>,
column: Column,
counter: usize,
finished: bool,
}
impl <'a, T: Clone> ColumnIterator<'a, T> {
pub fn new(matrix: &Matrix<T>, column: Column) -> ColumnIterator<T> {
ColumnIterator {
matrix,
column,
counter: 0,
finished: false,
}
}
}
impl <'a, T: Clone> Iterator for ColumnIterator<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get(self.counter, self.column));
if self.counter == self.matrix.rows() - 1 {
self.finished = true;
}
self.counter += 1;
value
}
}
pub struct RowIterator<'a, T: Clone> {
matrix: &'a Matrix<T>,
row: Row,
counter: usize,
finished: bool,
}
impl <'a, T: Clone> RowIterator<'a, T> {
pub fn new(matrix: &Matrix<T>, row: Row) -> RowIterator<T> {
RowIterator {
matrix,
row,
counter: 0,
finished: false,
}
}
}
impl <'a, T: Clone> Iterator for RowIterator<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get(self.row, self.counter));
if self.counter == self.matrix.columns() - 1 {
self.finished = true;
}
self.counter += 1;
value
}
}
pub struct ColumnMajorIterator<'a, T: Clone> {
matrix: &'a Matrix<T>,
column_counter: Column,
row_counter: Row,
finished: bool,
}
impl <'a, T: Clone> ColumnMajorIterator<'a, T> {
pub fn new(matrix: &Matrix<T>) -> ColumnMajorIterator<T> {
ColumnMajorIterator {
matrix,
column_counter: 0,
row_counter: 0,
finished: false,
}
}
}
impl <'a, T: Clone> Iterator for ColumnMajorIterator<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get(self.row_counter, self.column_counter));
if self.row_counter == self.matrix.rows() - 1
&& self.column_counter == self.matrix.columns() -1 {
self.finished = true;
}
if self.row_counter == self.matrix.rows() - 1 {
self.row_counter = 0;
self.column_counter += 1;
} else {
self.row_counter += 1;
}
value
}
}
pub struct ColumnReferenceIterator<'a, T> {
matrix: &'a Matrix<T>,
column: Column,
counter: usize,
finished: bool,
}
impl <'a, T> ColumnReferenceIterator<'a, T> {
pub fn new(matrix: &Matrix<T>, column: Column) -> ColumnReferenceIterator<T> {
ColumnReferenceIterator {
matrix,
column,
counter: 0,
finished: false,
}
}
}
impl <'a, T> Iterator for ColumnReferenceIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get_reference(self.counter, self.column));
if self.counter == self.matrix.rows() - 1 {
self.finished = true;
}
self.counter += 1;
value
}
}
pub struct RowReferenceIterator<'a, T> {
matrix: &'a Matrix<T>,
row: Row,
counter: usize,
finished: bool,
}
impl <'a, T> RowReferenceIterator<'a, T> {
pub fn new(matrix: &Matrix<T>, row: Row) -> RowReferenceIterator<T> {
RowReferenceIterator {
matrix,
row,
counter: 0,
finished: false,
}
}
}
impl <'a, T> Iterator for RowReferenceIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get_reference(self.row, self.counter));
if self.counter == self.matrix.columns() - 1 {
self.finished = true;
}
self.counter += 1;
value
}
}
pub struct ColumnMajorReferenceIterator<'a, T> {
matrix: &'a Matrix<T>,
column_counter: Column,
row_counter: Row,
finished: bool,
}
impl <'a, T> ColumnMajorReferenceIterator<'a, T> {
pub fn new(matrix: &Matrix<T>) -> ColumnMajorReferenceIterator<T> {
ColumnMajorReferenceIterator {
matrix,
column_counter: 0,
row_counter: 0,
finished: false,
}
}
}
impl <'a, T> Iterator for ColumnMajorReferenceIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.finished {
return None
}
let value = Some(self.matrix.get_reference(self.row_counter, self.column_counter));
if self.row_counter == self.matrix.rows() - 1
&& self.column_counter == self.matrix.columns() -1 {
self.finished = true;
}
if self.row_counter == self.matrix.rows() - 1 {
self.row_counter = 0;
self.column_counter += 1;
} else {
self.row_counter += 1;
}
value
}
}