00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "CTBnum.hxx"
00015 #include "CTBosFill.hxx"
00016 #include "CTBexceptionIndexRange.hxx"
00017 #include "CTBexceptionConformity.hxx"
00018
00024
00026
00027 template <class T>
00028 inline CTBmatrixCDsc<T>::CTBmatrixCDsc()
00029 : mp_data(0),
00030 mi_nrow(0),
00031 mi_ncol(0),
00032 mi_strrow(0),
00033 mi_strcol(0)
00034 {}
00035
00036
00038
00049 template <class T>
00050 inline CTBmatrixCDsc<T>::CTBmatrixCDsc(const T* p_data, CTBint i_nrow,
00051 CTBint i_ncol)
00052 : mp_data((i_nrow > 0 && i_ncol > 0) ? (T*) p_data : 0),
00053 mi_nrow(i_nrow),
00054 mi_ncol(i_ncol),
00055 mi_strrow(i_ncol),
00056 mi_strcol(1)
00057 {}
00058
00059
00061
00072 template <class T>
00073 inline CTBmatrixCDsc<T>::CTBmatrixCDsc(const T* p_data, CTBint i_nrow,
00074 CTBint i_ncol, CTBint i_strrow,
00075 CTBint i_strcol)
00076 : mp_data((i_nrow > 0 && i_ncol > 0) ? (T*) p_data : 0),
00077 mi_nrow(i_nrow),
00078 mi_ncol(i_ncol),
00079 mi_strrow(i_strrow),
00080 mi_strcol(i_strcol)
00081 {}
00082
00083
00085
00086 template <class T>
00087 inline CTBmatrixCDsc<T>::CTBmatrixCDsc(const T* p_data, CTBint i_ncol,
00088 const CTBrange& rrow,
00089 const CTBrange& rcol)
00090 {
00091 mp_data = (T*)p_data + (rrow.Begin()*i_ncol + rcol.Begin());
00092 if (rrow.End() >= rrow.Begin()) {
00093 mi_nrow = rrow.End()-rrow.Begin()+1;
00094 mi_strrow = i_ncol;
00095 } else {
00096 mi_nrow = rrow.Begin()-rrow.End()+1;
00097 mi_strrow = -i_ncol;
00098 }
00099 if (rcol.End() >= rcol.Begin()) {
00100 mi_ncol = rcol.End()-rcol.Begin()+1;
00101 mi_strcol = 1;
00102 } else {
00103 mi_ncol = rcol.Begin()-rcol.End()+1;
00104 mi_strcol = -1;
00105 }
00106 }
00107
00108
00110
00111 template <class T>
00112 inline const T* CTBmatrixCDsc<T>::Data() const
00113 {
00114 return mp_data;
00115 }
00116
00117
00119
00120 template <class T>
00121 inline CTBint CTBmatrixCDsc<T>::NRow() const
00122 {
00123 return mi_nrow;
00124 }
00125
00126
00128
00129 template <class T>
00130 inline CTBint CTBmatrixCDsc<T>::NColumn() const
00131 {
00132 return mi_ncol;
00133 }
00134
00135
00137
00138 template <class T>
00139 inline CTBint CTBmatrixCDsc<T>::Size() const
00140 {
00141 return mi_nrow*mi_ncol;
00142 }
00143
00144
00146
00147 template <class T>
00148 inline CTBint CTBmatrixCDsc<T>::StrideRow() const
00149 {
00150 return mi_strrow;
00151 }
00152
00153
00155
00156 template <class T>
00157 inline CTBint CTBmatrixCDsc<T>::StrideColumn() const
00158 {
00159 return mi_strcol;
00160 }
00161
00162
00164
00165 template <class T>
00166 inline CTBvectorCDsc<T> CTBmatrixCDsc<T>::Row(CTBint i_row) const
00167 {
00168 #ifdef CTB__IndexCheck
00169 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixCDsc::Row()");
00170 #endif
00171 return CTBvectorCDsc<T>(mp_data+i_row*mi_strrow,mi_ncol,mi_strcol);
00172 }
00173
00174
00176
00177 template <class T>
00178 inline CTBvectorCDsc<T> CTBmatrixCDsc<T>::Column(CTBint i_col) const
00179 {
00180 #ifdef CTB__IndexCheck
00181 CTBindexRangeCheck(i_col,mi_ncol,"CTBmatrixCDsc::Column()");
00182 #endif
00183 return CTBvectorCDsc<T>(mp_data+i_col*mi_strcol,mi_nrow,mi_strrow);
00184 }
00185
00186
00188
00189 template <class T>
00190 inline CTBvectorCDsc<T> CTBmatrixCDsc<T>::Diagonal() const
00191 {
00192 return CTBvectorCDsc<T>(mp_data,CTBmin(mi_nrow,mi_ncol),mi_strrow+mi_strcol);
00193 }
00194
00195
00197
00198 template <class T>
00199 inline void CTBmatrixCDsc<T>::Dump(int i_indent, ostream& os,
00200 const char* p_text) const
00201 {
00202 CTBosFill bl(i_indent);
00203
00204 os << bl << "--CTBmatrixCDsc<T> ";
00205 if (p_text) os << p_text;
00206 os << " @ " << this << endl;
00207 os << bl << " mp_data: " << (void*) mp_data << endl;
00208 os << bl << " mi_nrow: " << mi_nrow << endl;
00209 os << bl << " mi_ncol: " << mi_ncol << endl;
00210 os << bl << " mi_strrow: " << mi_strrow << endl;
00211 os << bl << " mi_strcol: " << mi_strcol << endl;
00212 return;
00213 }
00214
00215
00217
00218 template <class T>
00219 inline CTBvectorCDsc<T> CTBmatrixCDsc<T>::operator[](CTBint i_row) const
00220 {
00221 #ifdef CTB__IndexCheck
00222 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixCDsc::operator[]");
00223 #endif
00224 return CTBvectorCDsc<T>(mp_data+i_row*mi_strrow,mi_ncol,mi_strcol);
00225 }
00226
00227
00229
00230 template <class T>
00231 inline const T& CTBmatrixCDsc<T>::operator()(CTBint i_row, CTBint i_col) const
00232 {
00233 #ifdef CTB__IndexCheck
00234 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixCDsc::operator(row,)");
00235 CTBindexRangeCheck(i_col,mi_ncol,"CTBmatrixCDsc::operator(,col)");
00236 #endif
00237 return mp_data[i_row*mi_strrow+i_col*mi_strcol];
00238 }
00239
00240
00242
00243 template <class T>
00244 inline bool CTBmatrixCDsc<T>::operator!() const
00245 {
00246 return mi_nrow == 0 || mi_ncol == 0;
00247 }
00248
00249
00251
00252 template <class T>
00253 inline CTBmatrixCDsc<T>::operator void* () const
00254 {
00255 return mp_data;
00256 }
00257
00258
00264 template <class T>
00265 inline ostream& operator<<(ostream& os, CTBmatrixCDsc<T> obj)
00266 {
00267 int i_width = os.width();
00268
00269 os << setw(0) << "{";
00270 for (CTBint i = 0; i < obj.NRow(); i++) {
00271 if (i > 0) os << ",";
00272 os << "{";
00273 for (CTBint j = 0; j < obj.NColumn(); j++) {
00274 if (j > 0) os << ",";
00275 os << setw(i_width) << obj(i,j);
00276 }
00277 os << "}";
00278 }
00279 os << "}";
00280 return os;
00281 }
00282
00283
00290
00292
00293 template <class T>
00294 inline CTBmatrixDsc<T>::CTBmatrixDsc()
00295 : CTBmatrixCDsc<T>()
00296 {}
00297
00298
00300
00311 template <class T>
00312 inline CTBmatrixDsc<T>::CTBmatrixDsc(T* p_data, CTBint i_nrow, CTBint i_ncol)
00313 : CTBmatrixCDsc<T>(p_data,i_nrow,i_ncol)
00314 {}
00315
00316
00318
00329 template <class T>
00330 inline CTBmatrixDsc<T>::CTBmatrixDsc(T* p_data, CTBint i_nrow, CTBint i_ncol,
00331 CTBint i_strrow, CTBint i_strcol)
00332 : CTBmatrixCDsc<T>(p_data,i_nrow,i_ncol,i_strrow,i_strcol)
00333 {}
00334
00335
00337
00338 template <class T>
00339 inline CTBmatrixDsc<T>::CTBmatrixDsc(T* p_data, CTBint i_ncol,
00340 const CTBrange& rrow,
00341 const CTBrange& rcol)
00342 : CTBmatrixCDsc<T>(p_data,i_ncol,rrow,rcol)
00343 {}
00344
00345
00347
00348 template <class T>
00349 inline T* CTBmatrixDsc<T>::Data() const
00350 {
00351 return mp_data;
00352 }
00353
00354
00356
00357 template <class T>
00358 inline CTBvectorDsc<T> CTBmatrixDsc<T>::Row(CTBint i_row) const
00359 {
00360 #ifdef CTB__IndexCheck
00361 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixDsc::Row()");
00362 #endif
00363 return CTBvectorDsc<T>(mp_data+i_row*mi_strrow,mi_ncol,mi_strcol);
00364 }
00365
00366
00368
00369 template <class T>
00370 inline CTBvectorDsc<T> CTBmatrixDsc<T>::Column(CTBint i_col) const
00371 {
00372 #ifdef CTB__IndexCheck
00373 CTBindexRangeCheck(i_col,mi_ncol,"CTBmatrixDsc::Column()");
00374 #endif
00375 return CTBvectorDsc<T>(mp_data+i_col*mi_strcol,mi_nrow,mi_strrow);
00376 }
00377
00378
00380
00381 template <class T>
00382 inline CTBvectorDsc<T> CTBmatrixDsc<T>::Diagonal() const
00383 {
00384 return CTBvectorDsc<T>(mp_data,CTBmin(mi_nrow,mi_ncol),mi_strrow+mi_strcol);
00385 }
00386
00387
00389
00390 template <class T>
00391 inline CTBvectorDsc<T> CTBmatrixDsc<T>::operator[](CTBint i_row) const
00392 {
00393 #ifdef CTB__IndexCheck
00394 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixDsc::operator[]");
00395 #endif
00396 return CTBvectorDsc<T>(mp_data+i_row*mi_strrow,mi_ncol,mi_strcol);
00397 }
00398
00399
00401
00402 template <class T>
00403 inline T& CTBmatrixDsc<T>::operator()(CTBint i_row, CTBint i_col) const
00404 {
00405 #ifdef CTB__IndexCheck
00406 CTBindexRangeCheck(i_row,mi_nrow,"CTBmatrixDsc::operator(row,)");
00407 CTBindexRangeCheck(i_col,mi_ncol,"CTBmatrixDsc::operator(,col)");
00408 #endif
00409 return mp_data[i_row*mi_strrow+i_col*mi_strcol];
00410 }
00411
00412
00414
00415 template <class T>
00416 const CTBmatrixDsc<T>& CTBmatrixDsc<T>::operator=(const T& rhs) const
00417 {
00418 T* po = mp_data;
00419 CTBint i_do = mi_strrow - mi_ncol*mi_strcol;
00420
00421 for (CTBint i_row = 0; i < mi_nrow; i++) {
00422 for (CTBint i_col = 0; i < mi_ncol; i++) {
00423 *po = rhs;
00424 po += mi_strcol;
00425 }
00426 po += i_do;
00427 }
00428 return *this;
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00440
00441 template <class T>
00442 const CTBmatrixDsc<T>& CTBmatrixDsc<T>::operator=(
00443 const CTBmatrixDsc<T>& rhs) const
00444 {
00445 return operator=((const CTBmatrixCDsc<T>) rhs);
00446 }
00447
00448
00450
00451 template <class T>
00452 const CTBmatrixDsc<T>& CTBmatrixDsc<T>::operator=(
00453 const CTBmatrixCDsc<T>& rhs) const
00454 {
00455 CTBint i_rmin = CTBmin(mi_nrow,rhs.NRow());
00456 CTBint i_cmin = CTBmin(mi_ncol,rhs.NCol());
00457 CTBint i_socol = mi_strcol;
00458 CTBint i_s1col = rhs.StrideCol();
00459 CTBint i_do = mi_strrow - i_cmin*i_socol;
00460 CTBint i_d1 = rhs.StrideRow() - i_cmin*i_s1col;
00461 T* po = mp_data;
00462 const T* p1 = rhs.Data();
00463
00464 #ifdef CTB__ConformityCheck
00465 if (mi_nrow != rhs.NRow())
00466 throw CTBexceptionConformity(mi_nrow,rhs.NRow(),
00467 "CTBmatrixDsc::operator=(CTBmatrixDsc&)");
00468 if (mi_ncol != rhs.NCol())
00469 throw CTBexceptionConformity(mi_ncol,rhs.NCol(),
00470 "CTBmatrixDsc::operator=(CTBmatrixDsc&)");
00471 #endif
00472
00473 for (CTBint i_row = 0; i_row < i_rmin; i_row++) {
00474 for (CTBint i_col = 0; i_col < i_cmin; i_col++) {
00475 *po = *p1;
00476 po += i_socol;
00477 p1 += i_s1col;
00478 }
00479 po += i_do;
00480 p1 += i_d1;
00481 }
00482 return *this;
00483 }
00484
00485
00486
00487
00488
00489