ColumnIter
This commit is contained in:
parent
0397811247
commit
60a7ae725a
1 changed files with 17 additions and 2 deletions
|
@ -31,8 +31,8 @@ impl<T: Num + Clone> Matrix<T> {
|
|||
self.data.chunks(self.width)
|
||||
}
|
||||
|
||||
pub fn iter_column(&self) -> () {
|
||||
todo!()
|
||||
pub fn iter_column(&self, j: usize) -> ColumnIter<T> {
|
||||
ColumnIter { row_start: 0, column_index: j, matrix: self }
|
||||
}
|
||||
|
||||
pub fn iter_columns(&self) -> () {
|
||||
|
@ -62,6 +62,10 @@ pub struct IterIndexedMut<'a, T: Num> {
|
|||
matrix_iter_mut: std::slice::IterMut<'a, T>
|
||||
}
|
||||
|
||||
pub struct ColumnIter<'a, T: Num> {
|
||||
row_start: usize, column_index: usize, matrix: &'a Matrix<T>
|
||||
}
|
||||
|
||||
pub struct IterIndices {
|
||||
i: usize, j: usize, width: usize, height: usize
|
||||
}
|
||||
|
@ -84,6 +88,17 @@ impl<'a, T: Num> Iterator for IterIndexedMut<'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Num> Iterator for ColumnIter<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let elem_index = self.row_start + self.column_index;
|
||||
if elem_index >= self.matrix.data.len() { return None; }
|
||||
let elem = &self.matrix.data[elem_index];
|
||||
self.row_start += self.matrix.width;
|
||||
Some(elem)
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for IterIndices {
|
||||
type Item = (usize, usize);
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
Loading…
Reference in a new issue