SHOW:
|
|
- or go back to the newest paste.
1 | #ifndef _APMATRIX_H | |
2 | #define _APMATRIX_H | |
3 | ||
4 | #include "apvector.h" | |
5 | ||
6 | // ******************************************************************* | |
7 | // Last Revised: 8/14/98 | |
8 | // changed abort() to exit(1), dhj | |
9 | // | |
10 | // APCS matrix class | |
11 | // | |
12 | // extends apvector.h to two dimensional "safe" (range-checked) matrices | |
13 | // examples are given at the end of this file | |
14 | // ******************************************************************* | |
15 | ||
16 | template <class itemType> | |
17 | class apmatrix | |
18 | { | |
19 | public: | |
20 | ||
21 | // constructors/destructor | |
22 | apmatrix( ); // default size 0 x 0 | |
23 | apmatrix( int rows, int cols ); // size rows x cols | |
24 | apmatrix( int rows, int cols, | |
25 | const itemType & fillValue ); // all entries == fillValue | |
26 | apmatrix( const apmatrix & mat ); // copy constructor | |
27 | ~apmatrix( ); // destructor | |
28 | ||
29 | // assignment | |
30 | const apmatrix & operator = ( const apmatrix & rhs ); | |
31 | ||
32 | // accessors | |
33 | int numrows( ) const; // number of rows | |
34 | int numcols( ) const; // number of columns | |
35 | ||
36 | // indexing | |
37 | const apvector<itemType> & operator [ ] ( int k ) const; // range-checked indexing | |
38 | apvector<itemType> & operator [ ] ( int k ); // range-checked indexing | |
39 | ||
40 | // modifiers | |
41 | void resize( int newRows, int newCols ); // resizes matrix to newRows x newCols | |
42 | // (can result in losing values) | |
43 | private: | |
44 | ||
45 | int myRows; // # of rows (capacity) | |
46 | int myCols; // # of cols (capacity) | |
47 | apvector<apvector<itemType> > myMatrix; // the matrix of items | |
48 | }; | |
49 | ||
50 | ||
51 | // ******************************************************************* | |
52 | // Specifications for matrix functions | |
53 | // | |
54 | // To use this class, itemType must satisfy the same constraints | |
55 | // as forvector class. | |
56 | // | |
57 | // Any violation of a function's precondition will result in an error message | |
58 | // followed by a call to exit. | |
59 | // | |
60 | // constructors/destructor | |
61 | // | |
62 | // apmatrix( ); | |
63 | // postcondition: matrix of size 0x0 is constructed, and therefore | |
64 | // will need to be resized later | |
65 | // | |
66 | // apmatrix( int rows, int cols ); | |
67 | // precondition: 0 <= rows and 0 <= cols | |
68 | // postcondition: matrix of size rows x cols is constructed | |
69 | // | |
70 | // apmatrix( int rows, int cols, const itemType & fillValue ); | |
71 | // precondition: 0 <= rows and 0 <= cols | |
72 | // postcondition: matrix of size rows x cols is constructed | |
73 | // all entries are set by assignment to fillValue after | |
74 | // default construction | |
75 | // | |
76 | // apmatrix( const apmatrix<itemType> & mat ); | |
77 | // postcondition: matrix is a copy of mat | |
78 | // | |
79 | // ~apmatrix( ); | |
80 | // postcondition: matrix is destroyed | |
81 | // | |
82 | // assignment | |
83 | // | |
84 | // const apmatrix & operator = ( const apmatrix & rhs ); | |
85 | // postcondition: normal assignment via copying has been performed | |
86 | // (if matrix and rhs were different sizes, matrix has | |
87 | // been resized to match the size of rhs) | |
88 | // | |
89 | // accessors | |
90 | // | |
91 | // int numrows( ) const; | |
92 | // postcondition: returns number of rows | |
93 | // | |
94 | // int numcols( ) const; | |
95 | // postcondition: returns number of columns | |
96 | // | |
97 | // indexing | |
98 | // | |
99 | // const apvector<itemType> & operator [ ] ( int k ) const; | |
100 | // precondition: 0 <= k < number of rows | |
101 | // postcondition: returns k-th row | |
102 | // | |
103 | // apvector<itemType> & operator [ ] ( int k ); | |
104 | // precondition: 0 <= k < number of rows | |
105 | // postcondition: returns k-th row | |
106 | // | |
107 | // modifiers | |
108 | // | |
109 | // void resize( int newRows, int newCols ); | |
110 | // precondition: matrix size is rows X cols, | |
111 | // 0 <= newRows and 0 <= newCols | |
112 | // postcondition: matrix size is newRows X newCols; | |
113 | // for each 0 <= j <= min(rows,newRows) and | |
114 | // for each 0 <= k <= min(cols,newCols), matrix[j][k] is | |
115 | // a copy of the original; other elements of matrix are | |
116 | // initialized using the default constructor for itemType | |
117 | // Note: if newRows < rows or newCols < cols, | |
118 | // elements may be lost | |
119 | // | |
120 | // Examples of use: | |
121 | // | |
122 | // apmatrix<double> dmat( 100, 80 ); // 100 x 80 matrix of doubles | |
123 | // apmatrix<double> dzmat( 100, 80, 0.0 ); // initialized to 0.0 | |
124 | // apmatrix<apstring> smat( 300, 1 ); // 300 strings | |
125 | // apmatrix<int> imat; // has room for 0 ints | |
126 | ||
127 | #include "apmatrix.cpp" | |
128 | #endif | |
129 | ---------------------------------------------------------------------------------- | |
130 | // ******************************************************************* | |
131 | // Last Revised: 8/14/98 | |
132 | // abort changed to exit, dhj | |
133 | // | |
134 | // September 1, 1997 -- APCS matrix class IMPLEMENTATION | |
135 | // | |
136 | // see matrix.h for complete documentation of functions | |
137 | // | |
138 | // extends vector class to two-dimensional matrices | |
139 | // ******************************************************************* | |
140 | ||
141 | #include "apmatrix.h" | |
142 | #include <stdlib.h> | |
143 | #include <iostream> | |
144 | ||
145 | template <class itemType> | |
146 | apmatrix<itemType>::apmatrix() | |
147 | : myRows(0), | |
148 | myCols(0), | |
149 | myMatrix(0) | |
150 | ||
151 | // postcondition: matrix of size 0x0 is constructed, and therefore | |
152 | // will need to be resized later | |
153 | { | |
154 | ||
155 | } | |
156 | template <class itemType> | |
157 | apmatrix<itemType>::apmatrix(int rows,int cols) | |
158 | : myRows(rows), | |
159 | myCols(cols), | |
160 | myMatrix(rows) | |
161 | ||
162 | // precondition: 0 <= rows and 0 <= cols | |
163 | // postcondition: matrix of size rows x cols is constructed | |
164 | { | |
165 | int k; | |
166 | for(k=0; k < rows; k++) | |
167 | { | |
168 | myMatrix[k].resize(cols); | |
169 | } | |
170 | } | |
171 | ||
172 | template <class itemType> | |
173 | apmatrix<itemType>::apmatrix(int rows, int cols, const itemType & fillValue) | |
174 | : myRows(rows), | |
175 | myCols(cols), | |
176 | myMatrix(rows) | |
177 | ||
178 | // precondition: 0 <= rows and 0 <= cols | |
179 | // postcondition: matrix of size rows x cols is constructed | |
180 | // all entries are set by assignment to fillValue after | |
181 | // default construction | |
182 | // | |
183 | { | |
184 | int j,k; | |
185 | for(j=0; j < rows; j++) | |
186 | { | |
187 | myMatrix[j].resize(cols); | |
188 | for(k=0; k < cols; k++) | |
189 | { | |
190 | myMatrix[j][k] = fillValue; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | template <class itemType> | |
196 | apmatrix<itemType>::apmatrix(const apmatrix<itemType> & mat) | |
197 | : myRows(mat.myRows), | |
198 | myCols(mat.myCols), | |
199 | myMatrix(mat.myRows) | |
200 | ||
201 | // postcondition: matrix is a copy of mat | |
202 | { | |
203 | int k; | |
204 | // copy elements | |
205 | for(k = 0; k < myRows; k++) | |
206 | { | |
207 | // cast to avoid const problems (const -> non-const) | |
208 | myMatrix[k] = (apvector<itemType> &) mat.myMatrix[k]; | |
209 | } | |
210 | } | |
211 | ||
212 | template <class itemType> | |
213 | apmatrix<itemType>::~apmatrix () | |
214 | // postcondition: matrix is destroyed | |
215 | { | |
216 | // vector destructor frees everything | |
217 | } | |
218 | ||
219 | template <class itemType> | |
220 | const apmatrix<itemType> & | |
221 | apmatrix<itemType>::operator = (const apmatrix<itemType> & rhs) | |
222 | // postcondition: normal assignment via copying has been performed | |
223 | // (if matrix and rhs were different sizes, matrix has | |
224 | // been resized to match the size of rhs) | |
225 | { | |
226 | if (this != &rhs) // don't assign to self! | |
227 | { | |
228 | myMatrix.resize(rhs.myRows); // resize to proper # of rows | |
229 | myRows = rhs.myRows; // set dimensions | |
230 | myCols = rhs.myCols; | |
231 | ||
232 | // copy rhs | |
233 | int k; | |
234 | for(k=0; k < myRows; k++) | |
235 | { | |
236 | myMatrix[k] = rhs.myMatrix[k]; | |
237 | } | |
238 | } | |
239 | return *this; | |
240 | } | |
241 | ||
242 | template <class itemType> | |
243 | int apmatrix<itemType>::numrows() const | |
244 | // postcondition: returns number of rows | |
245 | { | |
246 | return myRows; | |
247 | } | |
248 | ||
249 | template <class itemType> | |
250 | int apmatrix<itemType>::numcols() const | |
251 | // postcondition: returns number of columns | |
252 | { | |
253 | return myCols; | |
254 | } | |
255 | ||
256 | ||
257 | template <class itemType> | |
258 | void apmatrix<itemType>::resize(int newRows, int newCols) | |
259 | // precondition: matrix size is rows X cols, | |
260 | // 0 <= newRows and 0 <= newCols | |
261 | // postcondition: matrix size is newRows X newCols; | |
262 | // for each 0 <= j <= min(rows,newRows) and | |
263 | // for each 0 <= k <= min(cols,newCols), matrix[j][k] is | |
264 | // a copy of the original; other elements of matrix are | |
265 | // initialized using the default constructor for itemType | |
266 | // Note: if newRows < rows or newCols < cols, | |
267 | // elements may be lost | |
268 | // | |
269 | { | |
270 | int k; | |
271 | myMatrix.resize(newRows); | |
272 | ||
273 | for(k=0; k < newRows; k++) | |
274 | { | |
275 | myMatrix[k].resize(newCols); | |
276 | } | |
277 | myRows = newRows; | |
278 | myCols = newCols; | |
279 | } | |
280 | ||
281 | template <class itemType> | |
282 | const apvector<itemType> & | |
283 | apmatrix<itemType>::operator [] (int k) const | |
284 | // precondition: 0 <= k < number of rows | |
285 | // postcondition: returns k-th row | |
286 | { | |
287 | if (k < 0 || myRows <= k) | |
288 | { | |
289 | cerr << "Illegal matrix index: " << k << " max index = "; | |
290 | cerr << myRows-1 << endl; | |
291 | exit(1); | |
292 | } | |
293 | return myMatrix[k]; | |
294 | } | |
295 | ||
296 | template <class itemType> | |
297 | apvector<itemType> & | |
298 | apmatrix<itemType>::operator [] (int k) | |
299 | // precondition: 0 <= k < number of rows | |
300 | // postcondition: returns k-th row | |
301 | { | |
302 | if (k < 0 || myRows <= k) | |
303 | { | |
304 | cerr << "Illegal matrix index: " << k << " max index = "; | |
305 | cerr << myRows-1 << endl; | |
306 | exit(1); | |
307 | } | |
308 | return myMatrix[k]; | |
309 | } |