AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

Vector4.h

Go to the documentation of this file.
00001 #ifndef AMROC_VECTOR4_H
00002 #define AMROC_VECTOR4_H
00003 
00011 #include "CompConf.h"
00012 #include "generic.h"
00013 #include <iostream.h>
00014 #include <assert.h>
00015  
00016 #ifndef VectorName
00017 #define Vector(dim)      name2(Vector,dim)
00018 #define VectorName
00019 #endif
00020 
00028 template <class DataType>
00029 class Vector(4) {
00030   typedef Vector(4)<DataType> TVector;
00031 
00032 public:
00033   typedef DataType InternalDataType;
00034 
00035   Vector(4)() {}
00036   Vector(4)(const TVector& x) { 
00037     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00038   }
00039   Vector(4)(const DataType& v) { 
00040     data_[0] = v; 
00041     data_[1] = v; 
00042     data_[2] = v; 
00043     data_[3] = v; 
00044   }
00045 
00046   ~Vector(4)() {}
00047   
00048   DataType * data() { return data_; }
00049   const DataType * data() const { return data_; }
00050   int length() const { return 4; }
00051   static int Length() { return 4; }
00052 
00053   inline DataType operator()(int i) const {
00054 #ifdef DEBUG_PRINT
00055     assert((i >= 0) && (i < 4)); 
00056 #endif
00057     return data_[i];
00058   }
00059   inline DataType& operator()(int i) { 
00060 #ifdef DEBUG_PRINT
00061     assert((i >= 0) && (i < 4)); 
00062 #endif
00063     return data_[i];
00064   }
00065 
00066   inline DataType operator[](int i) const {
00067 #ifdef DEBUG_PRINT
00068     assert((i >= 0) && (i < 4)); 
00069 #endif
00070     return data_[i];
00071   }
00072   inline DataType& operator[](int i) { 
00073 #ifdef DEBUG_PRINT
00074     assert((i >= 0) && (i < 4)); 
00075 #endif
00076     return data_[i];
00077   }
00078 
00079   inline TVector& operator=(const TVector& x) { 
00080     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00081     return *this; 
00082   }
00083   inline TVector& operator=(const DataType& v) { 
00084     data_[0] = v; 
00085     data_[1] = v; 
00086     data_[2] = v; 
00087     data_[3] = v; 
00088     return *this; 
00089   }
00090 
00091 #define Vector_Vector_Operator(ope,op)                          \
00092    inline TVector& operator ope (const TVector &x)              \
00093      {                                                          \
00094       data_[0] ope x.data_[0];                                  \
00095       data_[1] ope x.data_[1];                                  \
00096       data_[2] ope x.data_[2];                                  \
00097       data_[3] ope x.data_[3];                                  \
00098       return(*this);                                            \
00099      }                                                          \
00100    inline TVector operator op (const TVector &x) const          \
00101      {                                                          \
00102       TVector ret(*this);                                       \
00103       ret.data_[0] ope x.data_[0];                              \
00104       ret.data_[1] ope x.data_[1];                              \
00105       ret.data_[2] ope x.data_[2];                              \
00106       ret.data_[3] ope x.data_[3];                              \
00107       return(ret);                                              \
00108      }
00109    Vector_Vector_Operator(+=,+)
00110    Vector_Vector_Operator(-=,-)
00111    Vector_Vector_Operator(*=,*)
00112    Vector_Vector_Operator(/=,/)
00113 #undef Vector_Vector_Operator
00114 
00115 #define Vector_Scalar_Operator(ope,op)                          \
00116    inline TVector& operator ope (const DataType &v)             \
00117      {                                                          \
00118       data_[0] ope v;                                           \
00119       data_[1] ope v;                                           \
00120       data_[2] ope v;                                           \
00121       data_[3] ope v;                                           \
00122       return(*this);                                            \
00123      }                                                          \
00124    inline TVector operator op (const DataType &v) const         \
00125      {                                                          \
00126       TVector ret(*this);                                       \
00127       ret.data_[0] ope v;                                       \
00128       ret.data_[1] ope v;                                       \
00129       ret.data_[2] ope v;                                       \
00130       ret.data_[3] ope v;                                       \
00131      return(ret);                                               \
00132      }
00133    Vector_Scalar_Operator(+=,+)
00134    Vector_Scalar_Operator(-=,-)
00135    Vector_Scalar_Operator(*=,*)
00136    Vector_Scalar_Operator(/=,/)
00137 #undef Vector_Scalar_Operator
00138 
00139    /* Define the negation operator */
00140    inline TVector operator - () const
00141      { 
00142        TVector ret;
00143        ret.data_[0] = -data_[0];
00144        ret.data_[1] = -data_[1];
00145        ret.data_[2] = -data_[2];
00146        ret.data_[3] = -data_[3];
00147        return(ret);
00148      }
00149 
00150 #define Vector_Vector_RelOperator(op)                           \
00151    inline int operator op (const TVector &x) const              \
00152      { return(                                                  \
00153                (data_[0] op x.data_[0])                         \
00154             && (data_[1] op x.data_[1])                         \
00155             && (data_[2] op x.data_[2])                         \
00156             && (data_[3] op x.data_[3])                         \
00157              ) ; }                     
00158 
00159    Vector_Vector_RelOperator(==)
00160    Vector_Vector_RelOperator(!=)
00161 #undef Vector_Vector_RelOperator
00162 
00163 #define Vector_Vector_RelOperator(op)                           \
00164    inline int operator op (const TVector &x) const              \
00165      { return(                                                  \
00166                (data_[0] op x.data_[0])                         \
00167             || (data_[1] op x.data_[1])                         \
00168             || (data_[2] op x.data_[2])                         \
00169             || (data_[3] op x.data_[3])                         \
00170              ) ; }                     
00171 
00172    Vector_Vector_RelOperator(>)
00173    Vector_Vector_RelOperator(<)
00174    Vector_Vector_RelOperator(>=)
00175    Vector_Vector_RelOperator(<=)
00176 #undef Vector_Vector_RelOperator
00177 
00178 #define Vector_Scalar_RelOperator(op)                           \
00179    inline int operator op (const DataType &v) const             \
00180      { return(                                                  \
00181                (data_[0] op v)                                  \
00182             && (data_[1] op v)                                  \
00183             && (data_[2] op v)                                  \
00184             && (data_[3] op v)                                  \
00185              ); }                     
00186 
00187    Vector_Scalar_RelOperator(==)
00188    Vector_Scalar_RelOperator(!=)
00189 #undef Vector_Scalar_RelOperator
00190 
00191 #define Vector_Scalar_RelOperator(op)                           \
00192    inline int operator op (const DataType &v) const             \
00193      { return(                                                  \
00194                (data_[0] op v)                                  \
00195             || (data_[1] op v)                                  \
00196             || (data_[2] op v)                                  \
00197             || (data_[3] op v)                                  \
00198               ); }                     
00199 
00200    Vector_Scalar_RelOperator(>)
00201    Vector_Scalar_RelOperator(<)
00202    Vector_Scalar_RelOperator(>=)
00203    Vector_Scalar_RelOperator(<=)
00204 #undef Vector_Scalar_RelOperator
00205 
00206    /* Define elementwise minimum and maximum functions for Vectors */
00207    inline void min(const TVector &x) { 
00208      data_[0] = (data_[0] < x.data_[0]) ? data_[0] : x.data_[0]; 
00209      data_[1] = (data_[1] < x.data_[1]) ? data_[1] : x.data_[1]; 
00210      data_[2] = (data_[2] < x.data_[2]) ? data_[2] : x.data_[2]; 
00211      data_[3] = (data_[3] < x.data_[3]) ? data_[3] : x.data_[3]; 
00212    }
00213    inline void max(const TVector &x) {
00214      data_[0] = (data_[0] > x.data_[0]) ? data_[0] : x.data_[0]; 
00215      data_[1] = (data_[1] > x.data_[1]) ? data_[1] : x.data_[1]; 
00216      data_[2] = (data_[2] > x.data_[2]) ? data_[2] : x.data_[2]; 
00217      data_[3] = (data_[3] > x.data_[3]) ? data_[3] : x.data_[3]; 
00218    }
00219 
00220    /* Define minimum and maximum of components */
00221    inline DataType mincomp() const { 
00222      DataType min;
00223      min = data_[0];
00224      if (data_[1] < min) min = data_[1]; 
00225      if (data_[2] < min) min = data_[2]; 
00226      if (data_[3] < min) min = data_[3]; 
00227      return min;
00228    }
00229    inline DataType maxcomp() const { 
00230      DataType max;
00231      max = data_[0];
00232      if (data_[1] > max) max = data_[1]; 
00233      if (data_[2] > max) max = data_[2]; 
00234      if (data_[3] > max) max = data_[3]; 
00235      return max;
00236    }
00237 
00238    inline double abs() const {
00239      DataType s = data_[0]*data_[0];
00240      s += data_[1]*data_[1]; 
00241      s += data_[2]*data_[2]; 
00242      s += data_[3]*data_[3]; 
00243      return (sqrt(double(s)));
00244    }
00245 
00246    inline TVector getmin(const TVector &x) const
00247      { TVector ret(*this); ret.min(x); return(ret); }
00248    inline TVector getmax(const TVector &x) const
00249      { TVector ret(*this); ret.max(x); return(ret); }
00250 
00251 public:
00252    DataType data_[4];
00253 };
00254 
00255 
00256 template <class DataType>
00257 inline ostream&  operator << (ostream& os, const Vector(4)<DataType> &v) {
00258   os << "(" 
00259      << v.data_[0] 
00260      << "," << v.data_[1] 
00261      << "," << v.data_[2] 
00262      << "," << v.data_[3] 
00263      << ")";
00264   return os;
00265 }
00266 
00267 template <class DataType>
00268 inline Vector(4)<DataType> Min(const Vector(4)<DataType> &x, 
00269                                    const Vector(4)<DataType> &y) 
00270   { Vector(4)<DataType> ret(x); ret.min(y); return(ret); }
00271 
00272 template <class DataType>
00273 inline Vector(4)<DataType> Max(const Vector(4)<DataType> &x, 
00274                                    const Vector(4)<DataType> &y) 
00275   { Vector(4)<DataType> ret(x); ret.max(y); return(ret); }
00276 
00277 template <class DataType>
00278 inline double abs(const Vector(4)<DataType> &x) 
00279   { return (x.abs()); }
00280 
00281 template <class DataType>
00282 inline DataType mincomp(const Vector(4)<DataType> &x) 
00283   { return(x.mincomp()); }
00284 
00285 template <class DataType>
00286 inline DataType maxcomp(const Vector(4)<DataType> &x) 
00287   { return(x.maxcomp()); }
00288 
00289 
00290 #define Global_Vector_Scalar_Operator(op)                                  \
00291 template <class DataType>                                                  \
00292 inline Vector(4)<DataType> operator op (const DataType &v,                 \
00293    const Vector(4)<DataType> &x)                                           \
00294    { return(x op v); }
00295 
00296 Global_Vector_Scalar_Operator(+)
00297 Global_Vector_Scalar_Operator(-)
00298 Global_Vector_Scalar_Operator(*)
00299 Global_Vector_Scalar_Operator(/)
00300 #undef Global_Vector_Scalar_Operator
00301 
00302 
00303 #define Global_Vector_Function(fct)                                       \
00304 template <class DataType>                                                 \
00305 inline Vector(4)<DataType> fct(const Vector(4)<DataType> &x)              \
00306    {                                                                      \
00307       Vector(4)<DataType> ret;                                            \
00308       ret[0] = fct(x[0]);                                                 \
00309       ret[1] = fct(x[1]);                                                 \
00310       ret[2] = fct(x[2]);                                                 \
00311       ret[3] = fct(x[3]);                                                 \
00312       return(ret);                                                        \
00313      }
00314 
00315 Global_Vector_Function(fabs)
00316 Global_Vector_Function(sqrt)
00317 #undef Global_Vector_Function
00318 
00319 
00320 #define Vector_Vector_Functions(NameTo,NameFrom,ope)                      \
00321   template <class DataType, class VectorType>                             \
00322   inline void NameTo (Vector(4)<DataType> &x, const VectorType &y)        \
00323     {                                                                     \
00324       x(0) ope y(0);                                                      \
00325       x(1) ope y(1);                                                      \
00326       x(2) ope y(2);                                                      \
00327       x(3) ope y(3);                                                      \
00328     }                                                                     \
00329   template <class DataType, class VectorType>                             \
00330   inline void NameFrom (VectorType &x, const Vector(4)<DataType> &y)      \
00331     {                                                                     \
00332       x(0) ope y(0);                                                      \
00333       x(1) ope y(1);                                                      \
00334       x(2) ope y(2);                                                      \
00335       x(3) ope y(3);                                                      \
00336     }
00337 
00338    Vector_Vector_Functions(equals_to,equals_from,=)
00339    Vector_Vector_Functions(plus_to,plus_from,+=)
00340    Vector_Vector_Functions(minus_to,minus_from,-=)
00341    Vector_Vector_Functions(multiply_to,multiply_from,*=)
00342    Vector_Vector_Functions(divide_to,divide_from,/=)
00343 #undef Vector_Vector_Functions
00344 
00345 
00346 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04