00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef BARRAY2D_SOURCES_
00027 #define BARRAY2D_SOURCES_
00028
00029 #include "matrix_global.h"
00030 #include <new>
00031 #include "barray2d.h"
00032
00035 namespace PLib {
00036
00037
00052 template <class T>
00053 inline void initMemory(T* pointer, int size){
00054 T* p1;
00055 p1 = pointer-1;
00056 for(int i = size; i > 0; --i)
00057 *(++p1) = T() ;
00058 }
00059
00072 template <class T>
00073 inline void initMemoryWithMemset(T* pointer, int size){
00074 memset((void*)pointer,0,sizeof(T)*size);
00075 }
00076
00082 template <class T>
00083 void
00084 initBasic2DArray(Basic2DArray<T> &a, const int r,const int c)
00085 {
00086 a.rz = r; a.cz = c;
00087 a.created = 0;
00088
00089 if ( r <= 0 || c <= 0 )
00090 {
00091 return ;
00092 }
00093
00094
00095 a.m = new T [a.rz*a.cz];
00096 a.created = 1 ;
00097 #ifdef COLUMN_ORDER
00098 a.vm = new T*[a.cz] ;
00099 #else
00100 a.vm = new T*[a.rz] ;
00101 #endif
00102
00103 const int sze = a.rz*a.cz ;
00104
00105 initMemory(a.m,sze);
00106
00107 int i ;
00108 #ifdef COLUMN_ORDER
00109 for(i=a.cz-1;i>=0;--i)
00110 a.vm[i] = &a.m[i*a.rz] ;
00111 #else
00112 for(i=a.rz-1;i>=0;--i)
00113 a.vm[i] = &a.m[i*a.cz] ;
00114 #endif
00115 }
00116
00125 template <class T>
00126 Basic2DArray<T>::Basic2DArray() {
00127 by_columns = 0 ;
00128 width = 2 ;
00129 created = 1 ;
00130 m = 0;
00131 vm = 0 ;
00132 init(0,0);
00133 }
00134
00146 template <class T>
00147 Basic2DArray<T>::Basic2DArray(const int r, const int c) {
00148 by_columns = 0 ;
00149 width = 2 ;
00150 created = 1 ;
00151 m = 0;
00152 vm = 0 ;
00153 init(r,c);
00154 }
00155
00156
00176 template <class T>
00177 Basic2DArray<T>::Basic2DArray(T* p, const int r, const int c) {
00178 created = 0 ;
00179 rz = r ;
00180 cz = c ;
00181 m = p ;
00182 by_columns = 0 ;
00183 width = 2 ;
00184 #ifdef COLUMN_ORDER
00185 vm = new T* [cz] ;
00186 for(int i=cz-1;i>=0;--i)
00187 vm[i] = &m[i*rz] ;
00188 #else
00189 vm = new T* [rz] ;
00190 for(int i=rz-1;i>=0;--i)
00191 vm[i] = &m[i*cz] ;
00192 #endif
00193 }
00194
00205 template <class T>
00206 Basic2DArray<T>::Basic2DArray(const Basic2DArray<T> &a)
00207 {
00208 int i;
00209 created = 1 ;
00210 m = 0 ;
00211 init(a.rows(),a.cols());
00212 by_columns = a.by_columns;
00213 width = a.width ;
00214
00215 T *p1,*pa ;
00216 int sz = a.rows()*a.cols() ;
00217 p1 = m-1 ;
00218 pa = a.m-1 ;
00219
00220 for (i = sz; i > 0 ; --i)
00221 *(++p1) = *(++pa) ;
00222 }
00223
00236 template <class T>
00237 Basic2DArray<T>& Basic2DArray<T>::operator=(const Basic2DArray<T> &a)
00238 {
00239 int i;
00240
00241 if ( this == &a )
00242 return *this;
00243
00244 if(rows() != a.rows() || cols() != a.cols()){
00245 resize(a.rows(),a.cols()) ;
00246 }
00247
00248 T *p1,*pa ;
00249 int sz = a.rows()*a.cols() ;
00250 p1 = m-1 ;
00251 pa = a.m-1 ;
00252
00253 for (i = sz; i > 0; --i)
00254 *(++p1) = *(++pa) ;
00255
00256 by_columns = a.by_columns;
00257 width = a.width ;
00258
00259 return *this;
00260 }
00261
00270 template <class T>
00271 Basic2DArray<T>::~Basic2DArray()
00272 {
00273 if(m && created){
00274 delete []m;
00275 }
00276 if(vm)
00277 delete []vm ;
00278 }
00279
00291 template <class T>
00292 void Basic2DArray<T>::resize(const int nr, const int nc)
00293 {
00294 if(rows()>1 || cols()>1){
00295 if(m && created)
00296 delete []m ;
00297 if(vm)
00298 delete []vm ;
00299 }
00300 else{
00301 if(m && created)
00302 delete []m ;
00303 if(vm)
00304 delete []vm ;
00305 }
00306
00307 init(nr, nc);
00308 }
00309
00310
00326 template <class T>
00327 void
00328 resizeKeepBasic2DArray(Basic2DArray<T> &a,const int nr,const int nc)
00329 {
00330
00331 if(nr==a.rz && nc==a.cz){
00332 return ;
00333 }
00334
00335 T *mn ;
00336 T *p,*pn ;
00337
00338 mn = new T[nr*nc] ;
00339
00340 int i,j ;
00341
00342
00343 #ifdef COLUMN_ORDER
00344 for(j=0;j<minimum(nc,a.cz);j++){
00345 p = &a.m[j*a.rz] -1;
00346 pn = &mn[j*nr] -1 ;
00347 for(i=0;i<minimum(nr,a.rz);i++){
00348 *(++pn) = *(++p) ;
00349 }
00350 for(i=a.rz;i<nr;i++)
00351 *(++pn) = T() ;
00352 }
00353
00354 for(j=a.cz;j<nc;j++){
00355 pn = &mn[j*nr]-1 ;
00356 for(i=0;i<nr;i++)
00357 *(++pn) = T() ;
00358 }
00359 #else
00360 for(i=0;i<minimum(nr,a.rz);i++){
00361 p = &a.m[i*a.cz] -1;
00362 pn = &mn[i*nc] -1 ;
00363 for(j=0;j<minimum(nc,a.cz);j++){
00364 *(++pn) = *(++p) ;
00365 }
00366 for(j=a.cz;j<nc;j++)
00367 *(++pn) = T() ;
00368 }
00369
00370 for(i=a.rz;i<nr;i++){
00371 pn = &mn[i*nc]-1 ;
00372 for(j=0;j<nc;j++)
00373 *(++pn) = T() ;
00374 }
00375 #endif
00376
00377
00378 a.rz = nr ;
00379 a.cz = nc ;
00380
00381 if(a.m && a.created)
00382 delete []a.m ;
00383 a.m = mn ;
00384 if(a.vm)
00385 delete []a.vm ;
00386 #ifdef COLUMN_ORDER
00387 a.vm = new T* [a.cz] ;
00388 for(i=0;i<a.cz;++i)
00389 a.vm[i] = &a.m[i*a.rz] ;
00390 #else
00391 a.vm = new T* [a.rz] ;
00392 for(i=0;i<a.rz;++i)
00393 a.vm[i] = &a.m[i*a.cz] ;
00394 #endif
00395 }
00396
00407 template <class T>
00408 void Basic2DArray<T>::reset(const T v)
00409 {
00410 T *p1 ;
00411 p1 = m-1 ;
00412 const int size = rows()*cols() ;
00413 for(int i=size;i>0; --i)
00414 *(++p1) = v ;
00415 }
00416
00417 #ifdef DEBUG_PLIB
00418
00434 template <class T>
00435 T& Basic2DArray<T>::elem(const int i,const int j)
00436 {
00437 if ( i < 0 || rows() <= i || j < 0 || cols() <= j )
00438 {
00439 #ifdef USE_EXCEPTION
00440 throw OutOfBound2D(i,j,0,rows()-1,0,cols()-1) ;
00441 #else
00442 Error error("T& Basic2DArray<T>::elem(int,int)") ;
00443 if (i < 0 || rows() <= i){
00444 error << "bad first index " << i << endl ;
00445 }
00446 if (j < 0 || cols() <= j){
00447 error << "bad second index " << j << endl ;
00448 }
00449 error.fatal() ;
00450 #endif
00451 }
00452 #ifdef COLUMN_ORDER
00453 return vm[j][i] ;
00454 #else
00455 return vm[i][j] ;
00456 #endif
00457 }
00458
00474 template <class T>
00475 T Basic2DArray<T>::elem(const int i,const int j) const
00476 {
00477 if ( i < 0 || rows() <= i || j < 0 || cols() <= j )
00478 {
00479 #ifdef USE_EXCEPTION
00480 throw OutOfBound2D(i,j,0,rows()-1,0,cols()-1) ;
00481 #else
00482 Error error("T Basic2DArray<T>::elem(int,int) const") ;
00483 if (i < 0 || rows() <= i){
00484 error << "bad first index " << i << endl ;
00485 }
00486 if (j < 0 || cols() <= j){
00487 error << "bad second index " << j << endl ;
00488 }
00489 error.fatal() ;
00490 #endif
00491 }
00492 #ifdef COLUMN_ORDER
00493 return vm[j][i] ;
00494 #else
00495 return vm[i][j] ;
00496 #endif
00497 }
00498 #endif
00499
00511 template <class T>
00512 istream&
00513 operator>> (istream& is,Basic2DArray<T>& a)
00514 {
00515 int i, j;
00516 int r = a.rows(), c = a.cols();
00517
00518 if ( a.by_columns )
00519 for (j = 0; j < c; j++)
00520 for (i = 0; i < r; i++)
00521 is >> a.elem(i,j) ;
00522 else
00523 for (i = 0; i < r; i++)
00524 for (j = 0; j < c; j++)
00525 is >> a.elem(i,j) ;
00526
00527 return is;
00528 }
00529
00539 template <class T>
00540 ostream&
00541 Basic2DArray<T>::print(ostream& os) const
00542 {
00543 int i, j;
00544 const int r = rows();
00545 const int c = cols();
00546
00547 if ( by_columns )
00548 for (j = 0; j < c; j++)
00549 {
00550 for (i = 0; i < r; i++) {
00551 os << setw(width) << elem(i,j) << ' ';
00552 }
00553 os << '\n';
00554 }
00555 else
00556 for (i = 0; i < r; i++)
00557 {
00558 for (j = 0; j < c; j++){
00559 os << setw(width) << elem(i,j) << ' ' ;
00560 }
00561 os << '\n';
00562 }
00563
00564 return os;
00565 }
00566
00576 template <class T>
00577 ostream&
00578 operator<<(ostream& os,const Basic2DArray<T>& a)
00579 {
00580 return a.print(os) ;
00581 }
00582
00583 }
00584
00585
00586 #endif