Changeset 787 in Sophya for trunk/SophyaLib/TArray/tarray.cc
- Timestamp:
- Mar 20, 2000, 7:22:49 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/TArray/tarray.cc
r785 r787 10 10 11 11 12 // Variables statiques globales13 template <class T>14 char * TArray<T>::ck_op_msg_[6] = {"???", "Size(int )", "IsPacked(int )",15 "Stride(int )", "ElemCheckBound()", "operator()" };16 template <class T>17 uint_4 TArray<T>::max_nprt_ = 50;18 19 // Methodes statiques globales20 template <class T>21 void TArray<T>::SetMaxPrint(uint_4 nprt)22 {23 max_nprt_ = nprt;24 }25 26 27 template <class T>28 uint_8 TArray<T>::ComputeTotalSize(uint_4 ndim, uint_4 * siz, uint_4 step, uint_8 offset)29 {30 uint_8 rs = step;31 for(int k=0; k<ndim; k++) rs *= siz[k];32 return(rs+offset);33 }34 12 35 13 // ------------------------------------------------------- … … 40 18 template <class T> 41 19 TArray<T>::TArray() 42 : mNDBlock() , mInfo(NULL)20 : mNDBlock() , BaseArray() 43 21 // Default constructor 44 22 { 45 ndim_ = 0;46 for(int k=0; k<TARRAY_MAXNDIMS; k++) step_[k] = size_[k] = 0;47 totsize_ = 0;48 minstep_ = 0;49 moystep_ = 0;50 offset_ = 0;51 // Default for matrices : Lines = Y , Columns = X52 macoli_ = 0;53 marowi_ = 1;54 23 } 55 24 56 25 template <class T> 57 26 TArray<T>::TArray(uint_4 ndim, uint_4 * siz, uint_4 step) 58 : mNDBlock(ComputeTotalSize(ndim, siz, step, 1)) , mInfo(NULL)27 : mNDBlock(ComputeTotalSize(ndim, siz, step, 1)) , BaseArray() 59 28 { 60 29 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, uint_4)"; … … 64 33 template <class T> 65 34 TArray<T>::TArray(uint_4 nx, uint_4 ny=1, uint_4 nz=1) 66 : mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)) , mInfo(NULL)67 { 68 uint_4 size[ TARRAY_MAXNDIMS];35 : mNDBlock(nx*((ny>0)?ny:1)*((nz>0)?nz:1)) , BaseArray() 36 { 37 uint_4 size[BASEARRAY_MAXNDIMS]; 69 38 size[0] = nx; size[1] = ny; size[2] = nz; 70 39 int ndim = 1; … … 78 47 template <class T> 79 48 TArray<T>::TArray(uint_4 ndim, uint_4 * siz, NDataBlock<T> & db, bool share, uint_4 step, uint_8 offset) 80 : mNDBlock(db, share) , mInfo(NULL)49 : mNDBlock(db, share) , BaseArray() 81 50 { 82 51 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, NDataBlock<T> & ... )"; … … 87 56 template <class T> 88 57 TArray<T>::TArray(uint_4 ndim, uint_4 * siz, T* values, uint_4 step, uint_8 offset, Bridge* br) 89 : mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br) , mInfo(NULL)58 : mNDBlock(ComputeTotalSize(ndim, siz, step, 1), values, br) , BaseArray() 90 59 { 91 60 string exmsg = "TArray<T>::TArray(uint_4, uint_4 *, T* ... )"; … … 95 64 template <class T> 96 65 TArray<T>::TArray(const TArray<T>& a) 97 : mNDBlock(a.mNDBlock) , mInfo(NULL)66 : mNDBlock(a.mNDBlock) , BaseArray() 98 67 { 99 68 string exmsg = "TArray<T>::TArray(const TArray<T>&)"; … … 104 73 template <class T> 105 74 TArray<T>::TArray(const TArray<T>& a, bool share) 106 : mNDBlock(a.mNDBlock, share) , mInfo(NULL)75 : mNDBlock(a.mNDBlock, share) , BaseArray() 107 76 { 108 77 string exmsg = "TArray<T>::TArray(const TArray<T>&, bool)"; … … 154 123 } 155 124 156 template <class T> 157 bool TArray<T>::CompareSizes(const TArray<T>& a) 158 { 159 if (ndim_ != a.ndim_) return(false); 160 for(int k=0; k<ndim_; k++) 161 if (size_[k] != a.size_[k]) return(false); 162 if ( (macoli_ != a.macoli_) || (marowi_ != a.marowi_) ) return(false); 163 return(true); 164 } 165 166 template <class T> 167 TArray<T>& TArray<T>::CompactDim() 168 { 169 if (ndim_ < 2) return(*this); 125 126 template <class T> 127 TArray<T>& TArray<T>::CompactAllDimensions() 128 { 129 CompactAllDim(); 130 return(*this); 131 } 132 133 template <class T> 134 TArray<T>& TArray<T>::CompactTrailingDimensions() 135 { 136 CompactTrailingDim(); 137 return(*this); 138 } 139 140 template <class T> 141 double TArray<T>::ValueAtPosition(uint_8 ip) const 142 { 143 #ifdef SO_BOUNDCHECKING 144 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") ); 145 #endif 146 return( (double)(*(mNDBlock.Begin()+Offset(ip))) ); 147 } 148 149 // For complex values, we return the module of the complex number 150 double TArray< complex<r_4> >::ValueAtPosition(uint_8 ip) const 151 { 152 #ifdef SO_BOUNDCHECKING 153 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") ); 154 #endif 155 complex<r_4> c = *(mNDBlock.Begin()+Offset(ip)); 156 double cr = (double)(c.real()); 157 double ci = (double)(c.imag()); 158 return( sqrt(cr*cr+ci*ci) ); 159 } 160 161 double TArray< complex<r_8> >::ValueAtPosition(uint_8 ip) const 162 { 163 #ifdef SO_BOUNDCHECKING 164 if (ip >= totsize_) throw( ParmError("TArray<T>::ValueAtPosition(uint_8 ip) Out-of-bound Error") ); 165 #endif 166 complex<r_8> c = *(mNDBlock.Begin()+Offset(ip)); 167 double cr = (double)(c.real()); 168 double ci = (double)(c.imag()); 169 return( sqrt(cr*cr+ci*ci) ); 170 } 171 172 173 // SubArrays 174 template <class T> 175 TArray<T> TArray<T>::operator () (Range rx, Range ry, Range rz, Range rt, Range ru) 176 { 170 177 uint_4 ndim = 0; 171 uint_4 size[TARRAY_MAXNDIMS]; 172 uint_4 step[TARRAY_MAXNDIMS]; 173 for(int k=0; k<ndim_; k++) { 174 if (size_[k] < 2) continue; 175 size[ndim] = size_[k]; 176 step[ndim] = step_[k]; 177 ndim++; 178 } 179 if (ndim == 0) { 180 size[0] = size_[0]; 181 step[0] = step_[0]; 182 ndim = 1; 183 } 184 string exmsg = "TArray<T>::CompactDim() "; 185 if (!UpdateSizes(ndim, size, step, offset_, exmsg)) throw( ParmError(exmsg) ); 186 return(*this); 187 } 188 189 190 template <class T> 191 uint_4 TArray<T>::MinStepKA() const 192 { 193 for(int ka=0; ka<ndim_; ka++) 194 if (step_[ka] == minstep_) return(ka); 195 return(0); 196 } 197 198 template <class T> 199 uint_4 TArray<T>::MaxSizeKA() const 200 { 201 uint_4 ka = 0; 202 uint_4 mx = size_[0]; 203 for(int k=0; k<ndim_; k++) 204 if (size_[k] > mx) { ka = k; mx = size_[k]; } 205 return(ka); 206 } 207 208 209 // Acces lineaire aux elements .... Calcul d'offset 210 211 template <class T> 212 uint_8 TArray<T>::Offset(uint_8 ip) const 213 { 214 if (ip == 0) return(offset_); 215 uint_4 idx[TARRAY_MAXNDIMS]; 216 int k; 217 uint_8 rest = ip; 218 for(k=0; k<ndim_; k++) { idx[k] = rest%size_[k]; rest /= size_[k]; } 219 uint_8 off = offset_; 220 for(k=0; k<ndim_; k++) off += idx[k]*step_[k]; 221 return (off); 222 } 223 224 // SubArrays 225 template <class T> 226 TArray<T> TArray<T>::operator () (Range rx, Range ry, Range rz, Range rt, Range ru) 227 { 228 uint_4 ndim = 0; 229 uint_4 size[TARRAY_MAXNDIMS]; 230 uint_4 step[TARRAY_MAXNDIMS]; 231 uint_4 pos[TARRAY_MAXNDIMS]; 178 uint_4 size[BASEARRAY_MAXNDIMS]; 179 uint_4 step[BASEARRAY_MAXNDIMS]; 180 uint_4 pos[BASEARRAY_MAXNDIMS]; 232 181 size[0] = rx.Size(); 233 182 size[1] = ry.Size(); … … 251 200 TArray<T> ra; 252 201 SubArray(ra, ndim, size, pos, step); 202 ra.DataBlock().Share(this->DataBlock()); 253 203 ra.SetTemp(true); 254 204 return(ra); … … 523 473 524 474 // ---------------------------------------------------- 525 // Application d'une fonction526 // ----------------------------------------------------527 template <class T>528 TArray<T>& TArray<T>::ApplyFunction(Arr_DoubleFunctionOfX f)529 {530 T * pe;531 uint_8 j,k;532 if (AvgStep() > 0) { // regularly spaced elements533 uint_8 step = AvgStep();534 uint_8 maxx = totsize_*step;535 pe = Data();536 for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((double)pe[k]));537 }538 else { // Non regular data spacing ...539 uint_4 ka = MaxSizeKA();540 uint_8 step = Step(ka);541 uint_8 gpas = Size(ka)*step;542 for(j=0; j<totsize_; j += Size(ka)) {543 pe = mNDBlock.Begin()+Offset(j);544 for(k=0; k<gpas; k+=step) pe[k] = (T)(f((double)pe[k]));545 }546 }547 return(*this);548 }549 550 template <class T>551 TArray<T>& TArray<T>::ApplyFunction(Arr_FloatFunctionOfX f)552 {553 T * pe;554 uint_8 j,k;555 if (AvgStep() > 0) { // regularly spaced elements556 uint_8 step = AvgStep();557 uint_8 maxx = totsize_*step;558 pe = Data();559 for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((float)pe[k]));560 }561 else { // Non regular data spacing ...562 uint_4 ka = MaxSizeKA();563 uint_8 step = Step(ka);564 uint_8 gpas = Size(ka)*step;565 for(j=0; j<totsize_; j += Size(ka)) {566 pe = mNDBlock.Begin()+Offset(j);567 for(k=0; k<gpas; k+=step) pe[k] = (T)(f((float)pe[k]));568 }569 }570 return(*this);571 }572 573 TArray< complex<r_4> >& TArray< complex<r_4> >::ApplyFunction(Arr_DoubleFunctionOfX f)574 {575 throw( NotAvailableOperation("TArray< complex<r_4> >::ApplyFunction() - Unsupported operation ") );576 }577 578 TArray< complex<r_4> >& TArray< complex<r_4> >::ApplyFunction(Arr_FloatFunctionOfX f)579 {580 throw(NotAvailableOperation( "TArray< complex<r_4> >::ApplyFunction() - Unsupported operation ") );581 }582 583 TArray< complex<r_8> >& TArray< complex<r_8> >::ApplyFunction(Arr_DoubleFunctionOfX f)584 {585 throw(NotAvailableOperation("TArray< complex<r_8> >::ApplyFunction() - Unsupported operation ") );586 }587 588 TArray< complex<r_8> >& TArray< complex<r_8> >::ApplyFunction(Arr_FloatFunctionOfX f)589 {590 throw(NotAvailableOperation("TArray< complex<r_8> >::ApplyFunction() - Unsupported operation ") );591 }592 593 // ----------------------------------------------------594 475 // Impression, etc ... 595 476 // ---------------------------------------------------- 596 477 597 478 template <class T> 598 void TArray<T>::Show(ostream& os, bool si) const 599 { 600 os << " TArray< " << typeid(T).name() << " NDim= " << ndim_ 601 << "(" << typeid(*this).name() << " )" << endl; 602 os << "TotSize=" << totsize_ << " Size(X*Y*...)= " ; 603 for(int k=0; k<ndim_; k++) { 604 os << size_[k]; 605 if (k<ndim_-1) os << " x "; 606 } 607 os << endl; 608 os << " Step(X Y ...)= " ; 609 for(int k=0; k<ndim_; k++) os << step_[k] << " " ; 610 os << " Offset= " << offset_ << "\n " << endl; 611 if (si && (mInfo != NULL)) os << (*mInfo) << endl; 612 479 string TArray<T>::DataType() const 480 { 481 string rs = typeid(T).name(); 482 return(rs); 613 483 } 614 484 … … 643 513 } 644 514 645 template <class T>646 DVList& TArray<T>::Info()647 {648 if (mInfo == NULL) mInfo = new DVList;649 return(*mInfo);650 }651 652 653 template <class T>654 bool TArray<T>::UpdateSizes(uint_4 ndim, const uint_4 * siz, uint_4 step, uint_8 offset, string & exmsg)655 {656 if (ndim >= TARRAY_MAXNDIMS) {657 exmsg += " NDim Error"; return false;658 }659 if (step < 1) {660 exmsg += " Step(=0) Error"; return false;661 }662 663 minstep_ = moystep_ = step;664 665 // Flagging bad updates ...666 ndim_ = 0;667 668 totsize_ = 1;669 int k;670 for(k=0; k<TARRAY_MAXNDIMS; k++) {671 size_[k] = 1;672 step_[k] = 0;673 }674 for(k=0; k<ndim; k++) {675 size_[k] = siz[k] ;676 step_[k] = totsize_*step;677 totsize_ *= size_[k];678 }679 if (totsize_ < 1) {680 exmsg += " Size Error"; return false;681 }682 offset_ = offset;683 // Default for matrices : Lines = Y , Columns = X684 macoli_ = 0;685 marowi_ = 1;686 // Update OK687 ndim_ = ndim;688 return true;689 }690 691 template <class T>692 bool TArray<T>::UpdateSizes(uint_4 ndim, const uint_4 * siz, const uint_4 * step, uint_8 offset, string & exmsg)693 {694 if (ndim >= TARRAY_MAXNDIMS) {695 exmsg += " NDim Error"; return false;696 }697 698 // Flagging bad updates ...699 ndim_ = 0;700 701 totsize_ = 1;702 int k;703 for(k=0; k<TARRAY_MAXNDIMS; k++) {704 size_[k] = 1;705 step_[k] = 0;706 }707 uint_4 minstep = step[0];708 for(k=0; k<ndim; k++) {709 size_[k] = siz[k] ;710 step_[k] = step[k];711 totsize_ *= size_[k];712 if (step_[k] < minstep) minstep = step_[k];713 }714 if (minstep < 1) {715 exmsg += " Step(=0) Error"; return false;716 }717 if (totsize_ < 1) {718 exmsg += " Size Error"; return false;719 }720 uint_8 plast = 0;721 for(k=0; k<ndim; k++) plast += (siz[k]-1)*step[k];722 if (plast == minstep*totsize_ ) moystep_ = minstep;723 else moystep_ = 0;724 minstep_ = minstep;725 offset_ = offset;726 // Default for matrices : Lines = Y , Columns = X727 macoli_ = 0;728 marowi_ = 1;729 // Update OK730 ndim_ = ndim;731 return true;732 }733 734 template <class T>735 bool TArray<T>::UpdateSizes(const TArray<T>& a, string & exmsg)736 {737 if (a.ndim_ >= TARRAY_MAXNDIMS) {738 exmsg += " NDim Error"; return false;739 }740 741 // Flagging bad updates ...742 ndim_ = 0;743 744 totsize_ = 1;745 int k;746 for(k=0; k<TARRAY_MAXNDIMS; k++) {747 size_[k] = 1;748 step_[k] = 0;749 }750 uint_4 minstep = a.step_[0];751 for(k=0; k<a.ndim_; k++) {752 size_[k] = a.size_[k] ;753 step_[k] = a.step_[k];754 totsize_ *= size_[k];755 if (step_[k] < minstep) minstep = step_[k];756 }757 if (minstep < 1) {758 exmsg += " Step(=0) Error"; return false;759 }760 if (totsize_ < 1) {761 exmsg += " Size Error"; return false;762 }763 764 minstep_ = a.minstep_;765 moystep_ = a.moystep_;766 offset_ = a.offset_;767 macoli_ = a.macoli_;768 marowi_ = a.marowi_;769 // Update OK770 ndim_ = a.ndim_;771 return true;772 }773 515 774 516 template <class T> … … 789 531 790 532 791 template <class T>792 void TArray<T>::SubArray(TArray<T> & ra, uint_4 ndim, uint_4 * siz, uint_4 * pos, uint_4 * step)793 {794 if ( (ndim > ndim_) || (ndim < 1) ) throw(SzMismatchError("TArray<T>::SubArray( ... ) NDim Error") );795 int k;796 for(k=0; k<ndim; k++)797 if ( (siz[k]*step[k]+pos[k]) > size_[k] )798 throw(SzMismatchError("TArray<T>::SubArray( ... ) Size/Pos Error") );799 (ra.mNDBlock).Share(mNDBlock);800 uint_8 offset = offset_;801 for(k=0; k<ndim_; k++) {802 offset += pos[k]*step_[k];803 step[k] *= step_[k];804 }805 string exm = "TArray<T>::SubArray() ";806 if (!ra.UpdateSizes(ndim, siz, step, offset, exm))807 throw( ParmError(exm) );808 (ra.mNDBlock).Share(mNDBlock);809 return;810 }811 533 812 534 ///////////////////////////////////////////////////////////////
Note:
See TracChangeset
for help on using the changeset viewer.