Main Page   Class Hierarchy   Compound List   File List   Compound Members  

barray.cpp

00001 /*=============================================================================
00002         File: barray.cpp
00003      Purpose:       
00004     Revision: $Id: barray.cpp,v 1.5 2003/05/15 02:34:55 philosophil Exp $
00005   Created by: Philippe Lavoie          (3 Oct, 1996)
00006  Modified by: 
00007 
00008  Copyright notice:
00009           Copyright (C) 1996-1998 Philippe Lavoie
00010  
00011           This library is free software; you can redistribute it and/or
00012           modify it under the terms of the GNU Library General Public
00013           License as published by the Free Software Foundation; either
00014           version 2 of the License, or (at your option) any later version.
00015  
00016           This library is distributed in the hope that it will be useful,
00017           but WITHOUT ANY WARRANTY; without even the implied warranty of
00018           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019           Library General Public License for more details.
00020  
00021           You should have received a copy of the GNU Library General Public
00022           License along with this library; if not, write to the Free
00023           Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024 =============================================================================*/
00025 
00026 #ifndef BARRAY_SOURCES_
00027 #define BARRAY_SOURCES_
00028 
00029 
00030 #include "matrix_global.h"
00031 #include <new>
00032 #include "barray.h"
00033 #include <cstring>
00034 
00037 namespace PLib {
00038 
00045 template<class T>
00046 BasicArray<T>::BasicArray() : rsize(0), wdth(0), sze(0)
00047 {
00048   x = 0;
00049   destruct = 0 ;
00050 }
00051 
00060 template<class T>
00061 BasicArray<T>::BasicArray(const int ni) {
00062   x = 0 ;
00063   sze = wdth = rsize = 0 ;
00064   resize(ni) ;
00065   destruct = 1 ;
00066 }
00067 
00082 template<class T>
00083 BasicArray<T>::BasicArray(T* ap, const int size) : rsize(size), wdth(size+1), sze(size)
00084 {
00085   x = ap ;
00086   destruct = 0 ;
00087 }
00088 
00097 template<class T> BasicArray<T>::BasicArray(const BasicArray<T>& f2) 
00098 {
00099   rsize = sze = 0 ;
00100   x = 0 ;
00101   resize(f2.size());
00102   T *p2,*p1 ;
00103   p1 = x-1 ;
00104   p2 = f2.x - 1 ;
00105   
00106   for (int k = rsize; k >0; --k)
00107     *(++p1) = *(++p2) ;
00108   destruct = 1 ;
00109 }
00110 
00119 template<class T> BasicArray<T>::BasicArray(BasicList<T> &list) 
00120 {
00121   BasicNode<T> *node = list.goToFirst() ;
00122   rsize = sze = 0 ;
00123   x = 0 ;
00124   resize(list.size());
00125 
00126   for (int k = rsize; k>0; --k){
00127     x[k] = *node->data ;
00128     node = list.goToNext() ;
00129   }
00130   destruct = 1 ;
00131 }
00132 
00139 template<class T> BasicArray<T>::~BasicArray()
00140 {
00141   clear();
00142 }
00143 
00153 template<class T> void BasicArray<T>::clear()
00154 {
00155   if(destruct){
00156     if ( x ) delete []x;
00157     x = 0;
00158     rsize = sze = 0;                 
00159   }
00160 }
00161 
00171 template<class T> 
00172 BasicArray<T>& BasicArray<T>::operator=(const BasicArray<T>& f2)
00173 {
00174   if ( this == &f2 )
00175     return *this;
00176 
00177   resize(f2.size()) ;
00178   
00179   // wdth = f2.wdth;
00180   T *p1,*p2 ;
00181   p1 = x-1 ;
00182   p2 = f2.x-1 ;
00183 
00184   for (int k = sze; k >0; --k)
00185     *(++p1) = *(++p2) ;
00186 
00187   return *this; 
00188 }
00189 
00198 template<class T>
00199 void 
00200 BasicArray<T>::trim(const int nsize)    
00201 {
00202   if ( nsize >= 0 && nsize <= rsize )
00203     sze = nsize;
00204 }
00205 
00214 template<class T>
00215 void 
00216 BasicArray<T>::reset(const T val)
00217 {
00218   T *p1 ;
00219   p1 = x-1 ;
00220   for (int k = sze; k >0; --k)
00221     *(++p1) = val;
00222 }
00223 
00240 template <class T>
00241 void resizeBasicArray(BasicArray<T>& a, int nsize)
00242 {
00243   int k;
00244   
00245   if ( nsize == a.rsize ){
00246     a.sze = nsize ;
00247     return;                     // nothing to do
00248   }
00249   
00250   if(a.sze>nsize){
00251     a.sze = nsize ;
00252     return ;
00253   }
00254 
00255   if((a.sze<nsize) && (nsize<a.rsize)){
00256     for(k=a.sze;k<nsize;++k)
00257       a.x[k] = T() ;
00258     a.sze = nsize ;
00259     return ;
00260   }
00261 
00262   T *xn ; 
00263   T *p,*pn ;
00264   xn = new T[nsize];
00265   p = a.x ;
00266   pn = xn ;
00267   
00268   if ( a.x )
00269     {
00270       // copy the old data
00271       memcpy((void*)xn,(void*)a.x,a.sze*sizeof(T)) ;
00272       if(a.sze<nsize)
00273         memset((void*)(xn+a.sze),0,(nsize-a.sze)*sizeof(T)) ;
00274       if(a.destruct)
00275         delete []a.x;
00276     }
00277   else
00278     memset((void*)xn,0,nsize*sizeof(T)) ;
00279   
00280   a.rsize = nsize;
00281   a.sze = a.rsize ;
00282   a.x = xn;
00283   a.destruct = 1 ;
00284   a.wdth = a.rsize + 1;
00285 }
00286 
00287 
00299 template<class T>
00300 istream&  
00301 operator>> (istream& is, BasicArray<T>& arry)
00302 {
00303   T new_x;
00304   int k, kend;
00305 
00306   if ( is.eof() )
00307     return is;
00308   
00309   
00310   kend = arry.size();
00311   
00312   for (k = 0; k < kend; ++k)
00313         {               
00314           is >> new_x;    
00315           // end of file reached, abort the read
00316           if ( is.eof() || is.fail() )
00317             return is;
00318           
00319           arry[k] = new_x;              
00320         }
00321   
00322   return is;
00323         
00324 }
00325 
00338 template<class T>
00339 ostream& BasicArray<T>::print(ostream& os) const
00340 {
00341   const int iend = size();
00342   
00343   for (int i = 0; i < iend;  )
00344     {
00345       os << x[i] ; // << endl;
00346       if ( (++i % wdth) == 0 )
00347         os << '\n';
00348       else
00349         os << "   "; 
00350     }
00351   os << '\n' ;
00352   
00353   return os;    
00354 }
00355 
00356 
00368 template<class T>
00369 ostream&  
00370 operator<<(ostream& os,const BasicArray<T>& arry)
00371 {
00372   return arry.print(os);        
00373 }
00374 
00375 
00393 template<class T>
00394 int  
00395 operator!=(const BasicArray<T> &a,const BasicArray<T> &b) 
00396 {
00397   if ( a.size() != b.size() )
00398     return 1;
00399   
00400   const int sz = a.size();
00401   int l = 1;
00402   
00403   for (int i = 0; i < sz; ++i){
00404     l = l && ( a[i] == b[i] );
00405     if(!l) break ;
00406   }
00407   
00408   return (l==0 ? 1 : 0);
00409   
00410 }
00411 
00422 template<class T>
00423 int  
00424 operator==(const BasicArray<T> &a,const BasicArray<T> &b) 
00425 {
00426   if ( a.size() != b.size() )
00427     return 0;
00428   
00429   const int sz = a.size();
00430   int l = 1;
00431   
00432   for (int i = 0; i < sz; ++i)
00433     l = l && ( a[i] == b[i] );
00434   
00435   return l; 
00436 }
00437 
00438 #ifdef DEBUG_PLIB
00439 template <class T>
00440 T& BasicArray<T>::elem(const int i) {
00441   if(i<0 || i>=sze){
00442 #ifdef USE_EXCEPTION
00443     throw OutOfBound(i,0,n()-1) ;
00444 #else
00445     Error error("BasicArray<T>::operator[]") ;
00446     error << "Error Accessing the vector (press C-c to stop)\n" ;
00447     error << "Bad index " << i << endl ;
00448     error.fatal() ;
00449 #endif
00450   }
00451   return x[i] ;
00452   
00453 }
00454 
00455 template <class T>
00456 T  BasicArray<T>::elem(const int i) const {
00457   if(i<0 || i>=n()){
00458 #ifdef USE_EXCEPTION
00459     throw OutOfBound(i,0,n()-1) ;
00460 #else
00461     Error error("BasicArray<T>::operator[]") ;
00462     error << "Error Accessing the vector (press C-c to stop)\n" ;
00463     error << "Bad index " << i << endl ;
00464     error.fatal() ;
00465 #endif
00466   }
00467   return x[i] ;
00468 }
00469 
00470 #endif
00471 
00485 template<class T>  
00486 T& BasicArray<T>::push_back(const T i, int end_buffer, double end_mult){
00487   int old_size = sze;
00488   if(sze>=rsize){
00489     int n=sze; 
00490     if(end_mult>1.0){
00491       n = (int)( double(rsize)*end_mult) ;
00492     }
00493     else if(end_buffer<0){
00494       n++;
00495     }
00496     else{
00497       n += end_buffer;
00498     }
00499     resize(n); // allocate new memory, set rsze
00500   }
00501   resize(old_size+1); // set sze
00502   x[old_size] = i ; 
00503   return x[old_size];
00504 }
00505 
00506 }
00507 
00508 
00509 #endif //BARRAY_SOURCES

Generated on Tue Jun 24 13:26:54 2003 for NURBS++ by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002