Main Page   Class Hierarchy   Compound List   File List   Compound Members  

vector.cpp

00001 /*=============================================================================
00002         File: vector.cpp
00003      Purpose:
00004     Revision: $Id: vector.cpp,v 1.2 2002/05/13 21:07:45 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 VECTOR_SOURCES_
00027 #define VECTOR_SOURCES_
00028 
00029 #include "matrix_global.h"
00030 
00031 #include "vector.h"
00032 
00035 namespace PLib {
00036 
00049 template <class T> Vector<T>& Vector<T>::operator=(const Vector<T> &b)
00050 {
00051   if(this==&b)
00052     return *this ;
00053 
00054   if ( n() != b.n())
00055     {
00056       resize(b.n()) ;
00057     }
00058 
00059   sze = b.n() ;
00060   T *pa, *pb ;
00061   pa = x-1 ;
00062   pb = b.x-1 ;
00063   for(int i=n();i>0;--i){
00064     *(++pa) = *(++pb) ;
00065   }
00066   return *this;
00067 
00068 }
00069 
00079 template <class T>
00080 Vector<T>& Vector<T>::operator=(const BasicArray<T> &b)
00081 {
00082   if ( size() != b.size())
00083     {
00084       resize(b.size()) ;
00085     }
00086   T *ptr ;
00087   ptr = x - 1 ;
00088   for(int i=size()-1;i>=0;--i)
00089      *(++ptr) = b[i] ;
00090 
00091   return *this;
00092 }
00093 
00105 template <class T>
00106 T Vector<T>::operator=(const T d)
00107 {
00108   const int sz = size(); 
00109   T *ptr ;
00110   ptr = x-1 ;
00111   for (int i = sz; i > 0; --i)
00112     *(++ptr) = d ;
00113 
00114   return d;     
00115 }
00116 
00130 template <class T>
00131 Vector<T>& Vector<T>::operator+=(const Vector<T> &a)
00132 {
00133   if ( a.size() != size())
00134     {
00135 #ifdef USE_EXCEPTION
00136       throw WrongSize(size(),a.size()) ;
00137 #else
00138       Error error("Vector<T>::operator+=(Vector<T>&)");
00139       error << "Vector<T> a += Vector<T> b different sizes, a = " << size() << ", b = " << a.size() ;
00140       error.fatal() ;
00141 #endif
00142     }
00143   const int sz = size();
00144   T *ptr,*aptr ;
00145   ptr = x-1 ;
00146   aptr = a.x-1 ;
00147   for (int i = sz; i >0; --i)
00148     *(++ptr) += *(++aptr) ;
00149   
00150   return *this;
00151 }
00152 
00165 template <class T>
00166 Vector<T>& Vector<T>::operator-=(const Vector<T> &a)
00167 {
00168   if ( a.size() != size())
00169     {
00170 #ifdef USE_EXCEPTION
00171       throw WrongSize(size(),a.size()) ;
00172 #else
00173       Error error("Vector<T>::operator-=(Vector<T>&)");
00174       error << "Vector<T> a -= Vector<T> b different sizes, a = " << size() << ", b = " << a.size() ;
00175       error.fatal() ;
00176 #endif
00177     }
00178   
00179   const int sz = size(); 
00180   T *ptr,*aptr ;
00181   ptr = x-1 ;
00182   aptr = a.x-1 ;
00183   for (int i = sz; i > 0; --i)
00184     *(++ptr) -= *(++aptr) ;
00185   
00186   return *this;
00187 }
00188 
00201 template <class T>
00202 Vector<T> operator+(const Vector<T>& a, const Vector<T> &b)     
00203 {
00204   Vector<T> sum(a);
00205   sum += b ;
00206   
00207   return sum;
00208 }
00209 
00223 template <class T>
00224 Vector<T> operator-(const Vector<T>& a, const Vector<T> &b)
00225 {
00226   Vector<T> diff(a);
00227   diff -= b ;
00228   return diff;
00229 }
00230 
00244 template <class T>
00245 T operator*(const Vector<T> &a,const Vector<T> &b)
00246 {
00247   if ( a.size() != b.size() )
00248     {
00249 #ifdef USE_EXCEPTION
00250       throw WrongSize(a.size(),b.size()) ;
00251 #else
00252       Error error("Vector<T>::operator=(Vector<T>&)");
00253       error << "Vector<T> a = Vector<T> b different sizes, a = " << a.size() << ", b = " << b.size() ;
00254       error.fatal() ;
00255 #endif
00256     }
00257 
00258   int i, sz = a.size();
00259   T prod, zero = (T)0;
00260   T *aptr,*bptr ;
00261   aptr = a.x-1 ;
00262   bptr = b.x-1 ;
00263   prod = zero ;
00264   for (i = sz ; i > 0; --i)
00265     prod += (*(++aptr)) * (*(++bptr)) ;
00266   
00267   return prod;
00268 }
00269 
00283 template <class T>
00284 Vector<T>  operator*(const Vector<T>& v, const double d)
00285 {
00286   int i, sz = v.size();
00287   Vector<T> b(v.size());
00288   
00289   T *aptr,*bptr ;
00290   aptr = v.x-1 ;
00291   bptr = b.x-1 ;
00292   for (i = sz; i > 0; --i){
00293     *(++bptr) = (T)(d * (*(++aptr)) ) ;
00294   }
00295   
00296   return b;
00297 
00298 }
00299 
00314 template <class T>
00315 Vector<T>  operator*(const Vector<T>& v, const Complex d)
00316 {
00317   int i, sz = v.size();
00318   Vector<T> b = v;
00319   
00320   T *bptr ;
00321   bptr = b.x-1 ;
00322   for (i = sz; i > 0; --i){
00323     ++bptr ;
00324     (*bptr) = (T)(real(d)*(*bptr));
00325   }
00326   
00327   return b;
00328 
00329 }
00330 
00331 
00343 template <class T>
00344 int operator==(const Vector<T> &a,const Vector<T> &b)
00345 {
00346   if ( a.size() != b.size() )
00347     {
00348       return 0 ;
00349     }
00350 
00351   int i, sz = a.size();
00352   int l = 1;
00353   
00354   T *aptr,*bptr ;
00355   aptr = a.x-1 ;
00356   bptr = b.x-1 ;
00357 
00358   for (i = sz; i > 0; --i)
00359     l = l && ( *(++aptr) == *(++bptr) );
00360   
00361   return l;
00362 }
00363 
00379 template <class T> 
00380 void Vector<T>::as(int i, const Vector<T> &b)
00381 {
00382   if ( (i + b.rows()) > rows() )
00383     {
00384 #ifdef USE_EXCEPTION
00385       throw MatrixErr() ;
00386 #else
00387       Error error("Vector<T>::as(int,Vector<T>)");
00388       error << "Vector is too long to fit at i= " << i << endl ;
00389       error.fatal() ;
00390 #endif
00391     }
00392 
00393   T *aptr,*bptr ;
00394   aptr = &x[i]-1 ;
00395   bptr = b.x-1 ;
00396   for ( int j = b.rows(); j > 0; --j)
00397       *(++aptr) = *(++bptr) ;
00398 }
00399 
00416 template <class T>
00417 Vector<T> Vector<T>::get(int i, int l)
00418 {
00419   if ( (i + l) > rows() )
00420     {
00421 #ifdef USE_EXCEPTION
00422       throw MatrixErr() ;
00423 #else
00424       Error error("Vector<T>::get(int,Vector<T>)");
00425       error << "Vector is too long to extract from  i= " << i << "from l=" << l << endl ;
00426       error.fatal() ;
00427 #endif
00428     }
00429 
00430   Vector<T> subvec(l) ;
00431   T *aptr, *bptr ;
00432   aptr = &x[i] - 1 ;
00433   bptr = subvec.x -1 ;
00434   for ( int j = l; j > 0; --j)
00435     *(++bptr) = *(++aptr) ;
00436   return subvec ;
00437 }
00438 
00450 template <class T>
00451 int Vector<T>::minIndex() const {
00452   T min = x[0] ;
00453   int index = 0 ;
00454 
00455   for(int i=1;i<n();i++){
00456     if(x[i]<=min){
00457       min = x[i] ;
00458       index = i ;
00459     }
00460   }
00461   return index ;
00462 }
00463 
00464 
00479 template <class T>
00480 void Vector<T>::qSortStd(){
00481 #ifdef USE_EXCEPTION
00482   throw MatrixErr() ;
00483 #else
00484   Error error("Vector<T>::qSort()");
00485   error << "qSortStd is not defined for that type.\nPlease defined it in your .C file!";
00486   error.fatal() ;
00487 #endif
00488 }
00489 
00490 
00491 template <class T>
00492 inline void swap(T& a, T& b){
00493   T temp = a ;
00494   a = b ;
00495   b = temp ;
00496 }
00497 
00517 template <class T>
00518 void Vector<T>::qSort(int M){
00519   const int Nstack=50 ; 
00520   int i,ir,j,k,l ;
00521   Vector<int> istack(Nstack) ;
00522   int jstack=0;
00523   T a ;
00524   T *v1,*v2  ;
00525 
00526   ir = sze-1 ;
00527   l = 0 ;
00528   
00529   while(1){
00530     if(ir-l<M){ // perform an insertion sort when the array is small enough
00531       v1 = &x[l] ;
00532       for(j=l+1;j<=ir;++j){
00533         a = *(++v1) ;
00534         v2 = v1 ;
00535         --v2 ;
00536         for(i=j-1;i>=0;--i){
00537           if(*v2 <= a) break ;
00538           *(v2+1) = *v2 ;
00539           --v2 ;
00540         }
00541         ++v2 ;
00542         *(v2) = a ;
00543       }
00544       if(jstack==0) break ;
00545       ir=istack[--jstack] ;
00546       l=istack[--jstack] ;
00547     }
00548     else{
00549       k=(l+ir) >> 1 ;
00550       swap(x[k],x[l+1]) ;
00551       if(x[l+1] > x[ir]){
00552         swap(x[l+1],x[ir]) ;
00553       }
00554       if(x[l]> x[ir]){
00555         swap(x[l],x[ir]) ;
00556       }
00557       if(x[l+1] > x[l]){
00558         swap(x[l+1],x[l]) ;
00559       }
00560       i=l+1 ;
00561       j=ir ;
00562       a=x[l] ;
00563       v1 = &x[i] ;
00564       v2 = &x[j] ;
00565       while(1){
00566         while(*v1 < a) { ++i ; ++v1 ; }
00567         while(*v2 > a) { --j ; --v2 ; }
00568         if(j<i) break ;
00569         if(*v1 == *v2)  // both are equal to a...
00570           break ;
00571         swap(x[i],x[j]) ;
00572       }
00573       x[l] = x[j] ;
00574       x[j] = a ;
00575       jstack += 2 ;
00576       if(jstack>=Nstack){
00577         istack.resize(istack.n()+Nstack) ; // increase the vector size
00578       }
00579       if(ir-i+1 >= j-l){
00580         istack[jstack-1]=ir ;
00581         istack[jstack-2] = i ;
00582         ir=j-1 ;
00583       }
00584       else{
00585         istack[jstack-1] = j-1 ;
00586         istack[jstack-2] = l ;
00587         l=i ;
00588       }
00589     }
00590   }
00591 }
00592 
00593 
00613 template <class T>
00614 void Vector<T>::sortIndex(Vector<int>& index, int M) const{
00615   const int Nstack=50 ; 
00616   int i,ir,j,k,l,indext ;
00617   Vector<int> istack(Nstack) ;
00618   int jstack=0;
00619   T a ;
00620 
00621   ir = sze-1 ;
00622   l = 0 ;
00623   
00624   index.resize(sze) ;
00625   for(i=0;i<index.n();++i)
00626     index[i] = i ;
00627 
00628   while(1){
00629     if(ir-l<M){ // perform an insertion sort when the array is small enough
00630       for(j=l+1;j<=ir;++j){
00631         indext = index[j] ;
00632         a = x[indext] ;
00633         for(i=j-1;i>=0;--i){
00634           if(x[index[i]] <= a) break ;
00635           index[i+1] = index[i] ;
00636         }
00637         index[i+1] = indext ;
00638       }
00639       if(jstack==0) break ;
00640       ir=istack[--jstack] ;
00641       l=istack[--jstack] ;
00642     }
00643     else{
00644       k=(l+ir) >> 1 ;
00645       swap(index[k],index[l+1]) ;
00646       if(x[index[l+1]] > x[index[ir]]){
00647         swap(index[l+1],index[ir]) ;
00648       }
00649       if(x[index[l]]> x[index[ir]]){
00650         swap(index[l],index[ir]) ;
00651       }
00652       if(x[index[l+1]] > x[index[l]]){
00653         swap(index[l+1],index[l]) ;
00654       }
00655       i=l+1 ;
00656       j=ir ;
00657       indext = index[l] ;
00658       a=x[indext] ;
00659       while(1){
00660         while(x[index[i]] < a) { ++i ; }
00661         while(x[index[j]] > a) { --j ; }
00662         if(j<i) break ;
00663         if(x[index[i]] == x[index[j]])
00664           break ;
00665         swap(index[i],index[j]) ;
00666       }
00667       index[l] = index[j] ;
00668       index[j] = indext ;
00669       jstack += 2 ;
00670       if(jstack>=Nstack){
00671         istack.resize(istack.n()+Nstack) ; // increase the vector size
00672       }
00673       if(ir-i+1 >= j-l){
00674         istack[jstack-1]=ir ;
00675         istack[jstack-2] = i ;
00676         ir=j-1 ;
00677       }
00678       else{
00679         istack[jstack-1] = j-1 ;
00680         istack[jstack-2] = l ;
00681         l=i ;
00682       }
00683     }
00684   }
00685 }
00686 
00687 
00688 }
00689 
00690 
00691 #endif

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