fixed permissions for stlplus files
[chaz/yoink] / src / Moof / stlplus / matrix.tpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9
10 namespace stlplus
11 {
12
13 ////////////////////////////////////////////////////////////////////////////////
14
15 template<typename T>
16 matrix<T>::matrix(unsigned rows, unsigned cols, const T& fill) throw()
17 {
18 m_rows = 0;
19 m_cols = 0;
20 m_data = 0;
21 resize(rows,cols,fill);
22 }
23
24 template<typename T>
25 matrix<T>::~matrix(void) throw()
26 {
27 for (unsigned row = 0; row < m_rows; row++)
28 delete[] m_data[row];
29 delete[] m_data;
30 }
31
32 template<typename T>
33 matrix<T>::matrix(const matrix<T>& r) throw()
34 {
35 m_rows = 0;
36 m_cols = 0;
37 m_data = 0;
38 *this = r;
39 }
40
41 template<typename T>
42 matrix<T>& matrix<T>::operator =(const matrix<T>& right) throw()
43 {
44 // clear the old values
45 for (unsigned row = 0; row < m_rows; row++)
46 delete[] m_data[row];
47 delete[] m_data;
48 m_rows = 0;
49 m_cols = 0;
50 m_data = 0;
51 // now reconstruct with the new
52 resize(right.m_rows, right.m_cols);
53 for (unsigned row = 0; row < m_rows; row++)
54 for (unsigned col = 0; col < m_cols; col++)
55 m_data[row][col] = right.m_data[row][col];
56 return *this;
57 }
58
59 template<typename T>
60 void matrix<T>::resize(unsigned rows, unsigned cols, const T& fill) throw()
61 {
62 // a grid is an array of rows, where each row is an array of T
63 // a zero-row or zero-column matrix has a null grid
64 // TODO - make this exception-safe - new could throw here and that would cause a memory leak
65 T** new_grid = 0;
66 if (rows && cols)
67 {
68 new_grid = new T*[rows];
69 for (unsigned row = 0; row < rows; row++)
70 {
71 new_grid[row] = new T[cols];
72 // copy old items to the new grid but only within the bounds of the intersection of the old and new grids
73 // fill the rest of the grid with the initial value
74 for (unsigned col = 0; col < cols; col++)
75 if (row < m_rows && col < m_cols)
76 new_grid[row][col] = m_data[row][col];
77 else
78 new_grid[row][col] = fill;
79 }
80 }
81 // destroy the old grid
82 for (unsigned row = 0; row < m_rows; row++)
83 delete[] m_data[row];
84 delete[] m_data;
85 // move the new data into the matrix
86 m_data = new_grid;
87 m_rows = rows;
88 m_cols = cols;
89 }
90
91 template<typename T>
92 unsigned matrix<T>::rows(void) const throw()
93 {
94 return m_rows;
95 }
96
97 template<typename T>
98 unsigned matrix<T>::columns(void) const throw()
99 {
100 return m_cols;
101 }
102
103 template<typename T>
104 void matrix<T>::erase(const T& fill) throw()
105 {
106 for (unsigned row = 0; row < m_rows; row++)
107 for (unsigned col = 0; col < m_cols; col++)
108 insert(row,col,fill);
109 }
110
111 template<typename T>
112 void matrix<T>::erase(unsigned row, unsigned col, const T& fill) throw(std::out_of_range)
113 {
114 insert(row,col,fill);
115 }
116
117 template<typename T>
118 void matrix<T>::insert(unsigned row, unsigned col, const T& element) throw(std::out_of_range)
119 {
120 if (row >= m_rows) throw std::out_of_range("matrix::insert row");
121 if (col >= m_cols) throw std::out_of_range("matrix::insert col");
122 m_data[row][col] = element;
123 }
124
125 template<typename T>
126 const T& matrix<T>::item(unsigned row, unsigned col) const throw(std::out_of_range)
127 {
128 if (row >= m_rows) throw std::out_of_range("matrix::item row");
129 if (col >= m_cols) throw std::out_of_range("matrix::item col");
130 return m_data[row][col];
131 }
132
133 template<typename T>
134 T& matrix<T>::item(unsigned row, unsigned col) throw(std::out_of_range)
135 {
136 if (row >= m_rows) throw std::out_of_range("matrix::item row");
137 if (col >= m_cols) throw std::out_of_range("matrix::item col");
138 return m_data[row][col];
139 }
140
141 template<typename T>
142 const T& matrix<T>::operator()(unsigned row, unsigned col) const throw(std::out_of_range)
143 {
144 if (row >= m_rows) throw std::out_of_range("matrix::operator() row");
145 if (col >= m_cols) throw std::out_of_range("matrix::operator() col");
146 return m_data[row][col];
147 }
148
149 template<typename T>
150 T& matrix<T>::operator()(unsigned row, unsigned col) throw(std::out_of_range)
151 {
152 if (row >= m_rows) throw std::out_of_range("matrix::operator() row");
153 if (col >= m_cols) throw std::out_of_range("matrix::operator() col");
154 return m_data[row][col];
155 }
156
157 template<typename T>
158 void matrix<T>::fill(const T& item) throw()
159 {
160 erase(item);
161 }
162
163 template<typename T>
164 void matrix<T>::fill_column(unsigned col, const T& item) throw (std::out_of_range)
165 {
166 if (col >= m_cols) throw std::out_of_range("matrix::fill_column");
167 for (unsigned row = 0; row < m_rows; row++)
168 insert(row, col, item);
169 }
170
171 template<typename T>
172 void matrix<T>::fill_row(unsigned row, const T& item) throw (std::out_of_range)
173 {
174 if (row >= m_rows) throw std::out_of_range("matrix::fill_row");
175 for (unsigned col = 0; col < m_cols; col++)
176 insert(row, col, item);
177 }
178
179 template<typename T>
180 void matrix<T>::fill_leading_diagonal(const T& item) throw()
181 {
182 for (unsigned i = 0; i < m_cols && i < m_rows; i++)
183 insert(i, i, item);
184 }
185
186 template<typename T>
187 void matrix<T>::fill_trailing_diagonal(const T& item) throw()
188 {
189 for (unsigned i = 0; i < m_cols && i < m_rows; i++)
190 insert(i, m_cols-i-1, item);
191 }
192
193 template<typename T>
194 void matrix<T>::make_identity(const T& one, const T& zero) throw()
195 {
196 fill(zero);
197 fill_leading_diagonal(one);
198 }
199
200 template<typename T>
201 void matrix<T>::transpose(void) throw()
202 {
203 // no gain in manipulating this, since building a new matrix is no less efficient
204 matrix<T> transposed(columns(), rows());
205 for (unsigned row = 0; row < rows(); row++)
206 for (unsigned col = 0; col < columns(); col++)
207 transposed.insert(col,row,item(row,col));
208 // TODO - avoid an extra copy by swapping the member data here
209 *this = transposed;
210 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213
214 } // end namespace stlplus
215
This page took 0.040434 seconds and 4 git commands to generate.