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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// std imports

// external imports


// local imports
use algebra::structure::{MagmaBase};


/******************************************************
 *
 *   Iterator support of Matrix
 *
 *******************************************************/

/// An iterator over the elements of a matrix in a row
pub struct RowIterator<T:MagmaBase>{
    cols : usize,
    stride: usize,
    pos  : usize, 
    ptr : *const T
}



impl <T:MagmaBase> RowIterator<T> {
    /// Creates a new iterator object
    pub fn new(cols: usize, stride : usize, ptr : *const T)-> RowIterator<T>{
        RowIterator{cols : cols, stride : stride, pos : 0, ptr : ptr}
    }
}

impl <T:MagmaBase> Iterator for RowIterator<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if self.cols == self.pos{
            // No more data
            return None;
        }
        let offset = self.pos * self.stride;
        self.pos += 1;
        Some(unsafe{*self.ptr.offset(offset as isize).clone()})
    }
}

 /// An iterator over the elements of a matrix in a column
pub struct ColIterator<T:MagmaBase>{
    rows : usize,
    pos  : usize, 
    ptr : *const T
}


impl <T:MagmaBase> ColIterator<T> {
    /// Creates a new iterator object
    pub fn new(rows: usize, ptr : *const T)-> ColIterator<T>{
        ColIterator{rows : rows, pos : 0, ptr : ptr}
    }
}



impl <T:MagmaBase> Iterator for ColIterator<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if self.rows == self.pos{
            // No more data
            return None;
        }
        let offset = self.pos;
        self.pos += 1;
        Some(unsafe{*self.ptr.offset(offset as isize)})
    }
}

/// A column major iterator over the elements of a matrix
pub struct CellIterator<T:MagmaBase>{
    rows : usize,
    cols : usize,
    stride: usize, 
    r  : usize, 
    c : usize,
    ptr : *const T
}

impl <T:MagmaBase> CellIterator<T> {
    /// Creates a new iterator object
    pub fn new(rows: usize, cols: usize, stride : usize, ptr : *const T)-> CellIterator<T>{
        CellIterator{rows : rows, 
            cols : cols, 
            stride : stride, 
            r : 0, c : 0, 
            ptr : ptr}
    }
}


impl <T:MagmaBase> Iterator for CellIterator<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if self.cols == self.c{
            // No more data
            return None;
        }
        let offset = self.c * self.stride + self.r;
        self.r += 1;
        if self.r == self.rows {
            self.r = 0;
            self.c += 1;
        }
        Some(unsafe{*self.ptr.offset(offset as isize)})
    }
}

/// An iterator over the major diagonal of a matrix.
pub struct DiagIterator<T:MagmaBase>{
    min_dim_size : usize,
    step_size: usize,
    offset : usize,
    pos : usize, 
    ptr : *const T
}

impl <T:MagmaBase> DiagIterator<T> {
    /// Creates a new iterator object
    pub fn new(min_dim_size : usize, stride : usize, ptr : *const T)-> DiagIterator<T>{
        DiagIterator{min_dim_size : min_dim_size, step_size : stride + 1, offset : 0, pos : 0, ptr : ptr}
    }
}

impl <T:MagmaBase> Iterator for DiagIterator<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if self.min_dim_size == self.pos{
            // No more data
            return None;
        }
        let res = Some(unsafe{*self.ptr.offset(self.offset as isize).clone()});
        self.offset += self.step_size;
        self.pos += 1;
        res
    }
}