Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

vec.h

Go to the documentation of this file.
00001 
00017 #ifndef VEC_H
00018 #define VEC_H
00019 
00020 #include "haconst.h"
00021 #include <stdlib.h>
00022 #include <assert.h>
00023 
00024 template <class T>
00025 class NumVector
00027 {
00028 
00029   public:
00030 
00031     typedef Subscript   size_type;
00032     typedef         T   value_type;
00033     typedef         T   element_type;
00034     typedef         T*  pointer;
00035     typedef         T*  iterator;
00036     typedef         T&  reference;
00037     typedef const   T*  const_iterator;
00038     typedef const   T&  const_reference;
00039 
00040     Subscript lbound() const { return 1;}
00041         
00042   protected:
00043     T* v_;                  
00044     T* vm1_;        // pointer adjustment for optimzied 1-offset indexing
00045     Subscript n_;
00046 
00047     // internal helper function to create the array
00048     // of row pointers
00049 
00050   public:
00051 
00052     void initialize(Subscript N)
00053     {
00054         // adjust pointers so that they are 1-offset:
00055         // v_[] is the internal contiguous array, it is still 0-offset
00056         //
00057         assert(v_ == NULL);
00058         if(N==0) 
00059                         return;
00060 //        v_ = new T[N];
00061         v_=(pointer)malloc(N*sizeof(T));
00062         assert(v_  != NULL);
00063         vm1_ = v_-1;
00064         n_ = N;
00065     }
00066    
00067     void copy(const T*  v)
00068     {
00069         Subscript N = n_;
00070         Subscript i;
00071 
00072 #ifdef TNT_UNROLL
00073         Subscript Nmod4 = N & 4;
00074         Subscript N4 = N - Nmod4;
00075 
00076         for (i=0; i<N4; i+=4)
00077         {
00078             v_[i] = v[i];
00079             v_[i+1] = v[i+1];
00080             v_[i+2] = v[i+2];
00081             v_[i+3] = v[i+3];
00082         }
00083 
00084         for (i=N4; i< N; i++)
00085             v_[i] = v[i];
00086 #else
00087 
00088         for (i=0; i< N; i++)
00089             v_[i] = v[i];
00090 #endif      
00091     }
00092 
00093     void set(const T& val)
00094     {
00095         Subscript N = n_;
00096         Subscript i;
00097 
00098 #ifdef TNT_UNROLL
00099         Subscript Nmod4 = N & 4;
00100         Subscript N4 = N - Nmod4;
00101 
00102         for (i=0; i<N4; i+=4)
00103         {
00104             v_[i] = val;
00105             v_[i+1] = val;
00106             v_[i+2] = val;
00107             v_[i+3] = val; 
00108         }
00109 
00110         for (i=N4; i< N; i++)
00111             v_[i] = val;
00112 #else
00113 
00114         for (i=0; i< N; i++)
00115             v_[i] = val;
00116         
00117 #endif      
00118     }
00119 
00120     
00121         bool operator == (const NumVector<T>& rhs ) const       
00122         {
00123                 if(n_ != rhs.n_) return false;
00124                 int i;
00125                 for(i = 0; i < n_; i++)
00126                 {
00127                         if(v_[i] != rhs.v_[i]) return false;
00128                 }
00129                 return true;
00130         }
00131 
00132         bool operator < (const NumVector<T>& rhs) const
00133         {
00134                 if(n_ > rhs.n_) return false;
00135                 if(n_ < rhs.n_) return true;
00136                 int i;
00137                 for(i = 0; i < n_; i++)
00138                 {
00139                         if(v_[i] > rhs.v_[i]) return false;
00140                         if(v_[i] < rhs.v_[i]) return true;
00141                 }
00142                 return false;
00143         }
00144 
00145     void destroy()
00146     {     
00147         /* do nothing, if no memory has been previously allocated */
00148         if (v_ == NULL) return ;
00149 
00150         /* if we are here, then matrix was previously allocated */
00151 //        delete [] (v_);     
00152         free(v_);
00153             n_ = 0;
00154         v_ = NULL;
00155         vm1_ = NULL;
00156       }
00157 
00158 
00159   public:
00160 
00161     // access
00162 
00163     iterator begin() { return v_;}
00164     iterator end()   { return v_ + n_; }
00165     const iterator begin() const { return v_;}
00166     const iterator end() const  { return v_ + n_; }
00167 
00168     // destructor
00169 
00170     ~NumVector() 
00171     {
00172         destroy();
00173     }
00174 
00175     // constructors
00176 
00177     NumVector() : v_(0), vm1_(0), n_(0)  {};
00178 
00179     NumVector(const NumVector<T> &A) : v_(0), vm1_(0), n_(0)
00180     {
00181         initialize(A.n_);
00182         copy(A.v_);
00183     }
00184 
00185     NumVector(Subscript N, const T& value = T(0)) :  v_(0), vm1_(0), n_(0)
00186     {
00187         initialize(N);
00188         set(value);
00189     }
00190 
00191     NumVector(Subscript N, const T* v) :  v_(0), vm1_(0), n_(0)
00192     {
00193         initialize(N);
00194         copy(v);
00195     }
00196 
00197     NumVector(Subscript N, char *s) :  v_(0), vm1_(0), n_(0)
00198     {
00199         initialize(N);
00200         istrstream ins(s);
00201 
00202         Subscript i;
00203 
00204         for (i=0; i<N; i++)
00205                 ins >> v_[i];
00206     }
00207 
00208 
00209     // methods
00210     // 
00211     void newsize(Subscript N)
00212     {
00213         if (n_ == N) return;
00214 
00215         destroy();
00216         initialize(N);
00217 
00218         return;
00219     }
00220 
00221 
00222     // assignments
00223     //
00224     NumVector<T>& operator=(const NumVector<T> &A)
00225     {
00226         if (v_ == A.v_)
00227             return *this;
00228 
00229         if (n_ == A.n_)         // no need to re-alloc
00230             copy(A.v_);
00231 
00232         else
00233         {
00234             destroy();
00235             initialize(A.n_);
00236             copy(A.v_);
00237         }
00238 
00239         return *this;
00240     }
00241         
00242     NumVector<T>& operator=(const T& scalar)
00243     { 
00244         set(scalar);  
00245         return *this;
00246     }
00247 
00248     Subscript dim() const 
00249     {
00250         return  n_; 
00251     }
00252 
00253     Subscript size() const 
00254     {
00255         return  n_; 
00256     }
00257 
00258         inline value_type GetVal(Subscript i) const
00259         {
00260 #ifdef TNT_BOUNDS_CHECK
00261         assert(1<=i);
00262         assert(i <= n_) ;
00263 #endif
00264         return vm1_[i]; 
00265     }
00266 
00267         inline void SetVal(Subscript i, const value_type new_val)
00268         {
00269 #ifdef TNT_BOUNDS_CHECK
00270         assert(1<=i);
00271         assert(i <= n_) ;
00272 #endif
00273                 vm1_[i] = new_val;
00274         }
00275 
00276 
00277 
00278     inline reference operator()(Subscript i)
00279     { 
00280 #ifdef TNT_BOUNDS_CHECK
00281         assert(1<=i);
00282         assert(i <= n_) ;
00283 #endif
00284         return vm1_[i]; 
00285     }
00286 
00287     inline const_reference operator() (Subscript i) const
00288     {
00289 #ifdef TNT_BOUNDS_CHECK
00290         assert(1<=i);
00291         assert(i <= n_) ;
00292 #endif
00293         return vm1_[i]; 
00294     }
00295 
00296     inline reference operator[](Subscript i)
00297     { 
00298 #ifdef TNT_BOUNDS_CHECK
00299         assert(0<=i);
00300         assert(i < n_) ;
00301 #endif
00302         return v_[i]; 
00303     }
00304 
00305     inline const_reference operator[](Subscript i) const
00306     {
00307 #ifdef TNT_BOUNDS_CHECK
00308         assert(0<=i);
00309         assert(i < n_) ;
00310 #endif
00311         return v_[i]; 
00312     }
00313 
00314 #if defined(_MSC_VER) || defined(__DECCXX) || defined(__xlC__) 
00315     friend istream& operator>>(istream &s, NumVector<T> &A);
00316 #else
00317     friend istream& operator>><>(istream &s, NumVector<T> &A);
00318 #endif
00319 
00320 };
00321 
00322 
00323 /* ***************************  I/O  ********************************/
00324 
00325 template <class T>
00326 ostream& operator<<(ostream &s, const NumVector<T> &A)
00327 {
00328     Subscript N=A.dim();
00329 
00330     s <<  N << endl;
00331 
00332     for (Subscript i=0; i<N; i++)
00333         s   << A[i] << " " << endl;
00334     s << endl;
00335 
00336     return s;
00337 }
00338 
00339 template <class T>
00340 istream& operator>>(istream &s, NumVector<T> &A)
00341 {
00342 
00343     Subscript N;
00344 
00345     s >> N;
00346 
00347     if ( !(N == A.n_) )
00348     {
00349         A.destroy();
00350         A.initialize(N);
00351     }
00352 
00353 
00354     for (Subscript i=0; i<N; i++)
00355             s >>  A[i];
00356 
00357 
00358     return s;
00359 }
00360 
00361 //*******************[ basic matrix algorithms ]***************************
00362 
00363 
00364 template <class T>
00365 NumVector<T> operator+(const NumVector<T> &A, 
00366     const NumVector<T> &B)
00367 {
00368     Subscript N = A.dim();
00369 
00370     assert(N==B.dim());
00371 
00372     NumVector<T> tmp(N);
00373     Subscript i;
00374 
00375     for (i=0; i<N; i++)
00376             tmp[i] = A[i] + B[i];
00377 
00378     return tmp;
00379 }
00380 
00381 template <class T>
00382 NumVector<T> operator-(const NumVector<T> &A, 
00383     const NumVector<T> &B)
00384 {
00385     Subscript N = A.dim();
00386 
00387     assert(N==B.dim());
00388 
00389     NumVector<T> tmp(N);
00390     Subscript i;
00391 
00392     for (i=0; i<N; i++)
00393             tmp[i] = A[i] - B[i];
00394 
00395     return tmp;
00396 }
00397 
00398 template <class T>
00399 NumVector<T> operator*(const NumVector<T> &A, 
00400     const NumVector<T> &B)
00401 {
00402     Subscript N = A.dim();
00403 
00404     assert(N==B.dim());
00405 
00406     NumVector<T> tmp(N);
00407     Subscript i;
00408 
00409     for (i=0; i<N; i++)
00410             tmp[i] = A[i] * B[i];
00411 
00412     return tmp;
00413 }
00414 
00415 template <class T>
00416 NumVector<T> operator/(const NumVector<T> &A, 
00417     const NumVector<T> &B)
00418 {
00419     Subscript N = A.dim();
00420 
00421     assert(N==B.dim());
00422 
00423     NumVector<T> tmp(N);
00424     Subscript i;
00425 
00426     for (i=0; i<N; i++)
00427             tmp[i] = A[i] / B[i];
00428 
00429     return tmp;
00430 }
00431 
00432 /*
00433 template <class T>
00434 int operator < (const NumVector<T> &A, 
00435     const NumVector<T> &B)
00436 {
00437    Subscript N = A.dim();
00438 
00439     if(N > B.dim()) return 0;
00440         if(N < B.dim()) return 1;
00441 
00442     Subscript i;
00443 
00444     for (i=0; i<N; i++)
00445         {
00446        if( (A[i] - B[i]) > 1.0E-10)  return 0;
00447            if( (A[i] - B[i]) < -1.0E-10) return 1;
00448         }
00449 
00450     return 1;
00451 }
00452 
00453 template <class T>
00454 int operator == (const NumVector<T> &A, 
00455     const NumVector<T> &B)
00456 {
00457     Subscript N = A.dim();
00458 
00459     if(N != B.dim()) return 0;
00460 
00461     Subscript i;
00462 
00463     for (i=0; i<N; i++)
00464         {
00465        if( fabs(A[i] - B[i]) > 1.0E-10) return 0;
00466         }
00467 
00468     return 1;
00469 }
00470 */
00471 
00472 template <class T>
00473 int operator == (const NumVector<T> &A, 
00474     const T & t)
00475 {
00476     Subscript N = A.dim();
00477 
00478     Subscript i;
00479 
00480     for (i=0; i<N; i++)
00481         {
00482        if( A[i] != t) return 0;
00483         }
00484 
00485     return 1;
00486 }
00487 
00488 
00489 
00490 template <class T>
00491 T dot_prod(const NumVector<T> &A, const NumVector<T> &B)
00492 {
00493     Subscript N = A.dim();
00494     assert(N == B.dim());
00495 
00496     Subscript i;
00497     T sum = 0;
00498 
00499     for (i=0; i<N; i++)
00500         sum += A[i] * B[i];
00501 
00502     return sum;
00503 }
00504 
00505 #endif /* if(!Defined(VEC_H))  */
00506  
00507 
00508 

Generated on Tue Feb 17 02:03:08 2004 for harlem by doxygen 1.3.6