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


Main Page   Class Hierarchy   Compound List   File List  

Vector10.h

Go to the documentation of this file.
00001 #ifndef AMROC_VECTOR10_H
00002 #define AMROC_VECTOR10_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(10) {
00030   typedef Vector(10)<DataType> TVector;
00031 
00032 public:
00033   typedef DataType InternalDataType;
00034 
00035   Vector(10)() {}
00036   Vector(10)(const TVector& x) { 
00037     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00038   }
00039   Vector(10)(const DataType& v) { 
00040     data_[0] = v; 
00041     data_[1] = v; 
00042     data_[2] = v; 
00043     data_[3] = v; 
00044     data_[4] = v; 
00045     data_[5] = v; 
00046     data_[6] = v; 
00047     data_[7] = v; 
00048     data_[8] = v; 
00049     data_[9] = v; 
00050   }
00051 
00052   ~Vector(10)() {}
00053   
00054   DataType * data() { return data_; }
00055   const DataType * data() const { return data_; }
00056   int length() const { return 10; }
00057   static int Length() { return 10; }
00058 
00059   inline DataType operator()(int i) const {
00060 #ifdef DEBUG_PRINT
00061     assert((i >= 0) && (i < 10)); 
00062 #endif
00063     return data_[i];
00064   }
00065   inline DataType& operator()(int i) { 
00066 #ifdef DEBUG_PRINT
00067     assert((i >= 0) && (i < 10)); 
00068 #endif
00069     return data_[i];
00070   }
00071 
00072   inline DataType operator[](int i) const {
00073 #ifdef DEBUG_PRINT
00074     assert((i >= 0) && (i < 10)); 
00075 #endif
00076     return data_[i];
00077   }
00078   inline DataType& operator[](int i) { 
00079 #ifdef DEBUG_PRINT
00080     assert((i >= 0) && (i < 10)); 
00081 #endif
00082     return data_[i];
00083   }
00084 
00085   inline TVector& operator=(const TVector& x) { 
00086     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00087     return *this; 
00088   }
00089   inline TVector& operator=(const DataType& v) { 
00090     data_[0] = v; 
00091     data_[1] = v; 
00092     data_[2] = v; 
00093     data_[3] = v; 
00094     data_[4] = v; 
00095     data_[5] = v; 
00096     data_[6] = v; 
00097     data_[7] = v; 
00098     data_[8] = v; 
00099     data_[9] = v; 
00100     return *this; 
00101   }
00102 
00103 #define Vector_Vector_Operator(ope,op)                          \
00104    inline TVector& operator ope (const TVector &x)              \
00105      {                                                          \
00106       data_[0] ope x.data_[0];                                  \
00107       data_[1] ope x.data_[1];                                  \
00108       data_[2] ope x.data_[2];                                  \
00109       data_[3] ope x.data_[3];                                  \
00110       data_[4] ope x.data_[4];                                  \
00111       data_[5] ope x.data_[5];                                  \
00112       data_[6] ope x.data_[6];                                  \
00113       data_[7] ope x.data_[7];                                  \
00114       data_[8] ope x.data_[8];                                  \
00115       data_[9] ope x.data_[9];                                  \
00116       return(*this);                                            \
00117      }                                                          \
00118    inline TVector operator op (const TVector &x) const          \
00119      {                                                          \
00120       TVector ret(*this);                                       \
00121       ret.data_[0] ope x.data_[0];                              \
00122       ret.data_[1] ope x.data_[1];                              \
00123       ret.data_[2] ope x.data_[2];                              \
00124       ret.data_[3] ope x.data_[3];                              \
00125       ret.data_[4] ope x.data_[4];                              \
00126       ret.data_[5] ope x.data_[5];                              \
00127       ret.data_[6] ope x.data_[6];                              \
00128       ret.data_[7] ope x.data_[7];                              \
00129       ret.data_[8] ope x.data_[8];                              \
00130       ret.data_[9] ope x.data_[9];                              \
00131       return(ret);                                              \
00132      }
00133    Vector_Vector_Operator(+=,+)
00134    Vector_Vector_Operator(-=,-)
00135    Vector_Vector_Operator(*=,*)
00136    Vector_Vector_Operator(/=,/)
00137 #undef Vector_Vector_Operator
00138 
00139 #define Vector_Scalar_Operator(ope,op)                          \
00140    inline TVector& operator ope (const DataType &v)             \
00141      {                                                          \
00142       data_[0] ope v;                                           \
00143       data_[1] ope v;                                           \
00144       data_[2] ope v;                                           \
00145       data_[3] ope v;                                           \
00146       data_[4] ope v;                                           \
00147       data_[5] ope v;                                           \
00148       data_[6] ope v;                                           \
00149       data_[7] ope v;                                           \
00150       data_[8] ope v;                                           \
00151       data_[9] ope v;                                           \
00152       return(*this);                                            \
00153      }                                                          \
00154    inline TVector operator op (const DataType &v) const         \
00155      {                                                          \
00156       TVector ret(*this);                                       \
00157       ret.data_[0] ope v;                                       \
00158       ret.data_[1] ope v;                                       \
00159       ret.data_[2] ope v;                                       \
00160       ret.data_[3] ope v;                                       \
00161       ret.data_[4] ope v;                                       \
00162       ret.data_[5] ope v;                                       \
00163       ret.data_[6] ope v;                                       \
00164       ret.data_[7] ope v;                                       \
00165       ret.data_[8] ope v;                                       \
00166       ret.data_[9] ope v;                                       \
00167      return(ret);                                               \
00168      }
00169    Vector_Scalar_Operator(+=,+)
00170    Vector_Scalar_Operator(-=,-)
00171    Vector_Scalar_Operator(*=,*)
00172    Vector_Scalar_Operator(/=,/)
00173 #undef Vector_Scalar_Operator
00174 
00175    /* Define the negation operator */
00176    inline TVector operator - () const
00177      { 
00178        TVector ret;
00179        ret.data_[0] = -data_[0];
00180        ret.data_[1] = -data_[1];
00181        ret.data_[2] = -data_[2];
00182        ret.data_[3] = -data_[3];
00183        ret.data_[4] = -data_[4];
00184        ret.data_[5] = -data_[5];
00185        ret.data_[6] = -data_[6];
00186        ret.data_[7] = -data_[7];
00187        ret.data_[8] = -data_[8];
00188        ret.data_[9] = -data_[9];
00189        return(ret);
00190      }
00191 
00192 #define Vector_Vector_RelOperator(op)                           \
00193    inline int operator op (const TVector &x) const              \
00194      { return(                                                  \
00195                (data_[0] op x.data_[0])                         \
00196             && (data_[1] op x.data_[1])                         \
00197             && (data_[2] op x.data_[2])                         \
00198             && (data_[3] op x.data_[3])                         \
00199             && (data_[4] op x.data_[4])                         \
00200             && (data_[5] op x.data_[5])                         \
00201             && (data_[6] op x.data_[6])                         \
00202             && (data_[7] op x.data_[7])                         \
00203             && (data_[8] op x.data_[8])                         \
00204             && (data_[9] op x.data_[9])                         \
00205              ) ; }                     
00206 
00207    Vector_Vector_RelOperator(==)
00208    Vector_Vector_RelOperator(!=)
00209 #undef Vector_Vector_RelOperator
00210 
00211 #define Vector_Vector_RelOperator(op)                           \
00212    inline int operator op (const TVector &x) const              \
00213      { return(                                                  \
00214                (data_[0] op x.data_[0])                         \
00215             || (data_[1] op x.data_[1])                         \
00216             || (data_[2] op x.data_[2])                         \
00217             || (data_[3] op x.data_[3])                         \
00218             || (data_[4] op x.data_[4])                         \
00219             || (data_[5] op x.data_[5])                         \
00220             || (data_[6] op x.data_[6])                         \
00221             || (data_[7] op x.data_[7])                         \
00222             || (data_[8] op x.data_[8])                         \
00223             || (data_[9] op x.data_[9])                         \
00224              ) ; }                     
00225 
00226    Vector_Vector_RelOperator(>)
00227    Vector_Vector_RelOperator(<)
00228    Vector_Vector_RelOperator(>=)
00229    Vector_Vector_RelOperator(<=)
00230 #undef Vector_Vector_RelOperator
00231 
00232 #define Vector_Scalar_RelOperator(op)                           \
00233    inline int operator op (const DataType &v) const             \
00234      { return(                                                  \
00235                (data_[0] op v)                                  \
00236             && (data_[1] op v)                                  \
00237             && (data_[2] op v)                                  \
00238             && (data_[3] op v)                                  \
00239             && (data_[4] op v)                                  \
00240             && (data_[5] op v)                                  \
00241             && (data_[6] op v)                                  \
00242             && (data_[7] op v)                                  \
00243             && (data_[8] op v)                                  \
00244             && (data_[9] op v)                                  \
00245              ); }                     
00246 
00247    Vector_Scalar_RelOperator(==)
00248    Vector_Scalar_RelOperator(!=)
00249 #undef Vector_Scalar_RelOperator
00250 
00251 #define Vector_Scalar_RelOperator(op)                           \
00252    inline int operator op (const DataType &v) const             \
00253      { return(                                                  \
00254                (data_[0] op v)                                  \
00255             || (data_[1] op v)                                  \
00256             || (data_[2] op v)                                  \
00257             || (data_[3] op v)                                  \
00258             || (data_[4] op v)                                  \
00259             || (data_[5] op v)                                  \
00260             || (data_[6] op v)                                  \
00261             || (data_[7] op v)                                  \
00262             || (data_[8] op v)                                  \
00263             || (data_[9] op v)                                  \
00264               ); }                     
00265 
00266    Vector_Scalar_RelOperator(>)
00267    Vector_Scalar_RelOperator(<)
00268    Vector_Scalar_RelOperator(>=)
00269    Vector_Scalar_RelOperator(<=)
00270 #undef Vector_Scalar_RelOperator
00271 
00272    /* Define elementwise minimum and maximum functions for Vectors */
00273    inline void min(const TVector &x) { 
00274      data_[0] = (data_[0] < x.data_[0]) ? data_[0] : x.data_[0]; 
00275      data_[1] = (data_[1] < x.data_[1]) ? data_[1] : x.data_[1]; 
00276      data_[2] = (data_[2] < x.data_[2]) ? data_[2] : x.data_[2]; 
00277      data_[3] = (data_[3] < x.data_[3]) ? data_[3] : x.data_[3]; 
00278      data_[4] = (data_[4] < x.data_[4]) ? data_[4] : x.data_[4]; 
00279      data_[5] = (data_[5] < x.data_[5]) ? data_[5] : x.data_[5]; 
00280      data_[6] = (data_[6] < x.data_[6]) ? data_[6] : x.data_[6]; 
00281      data_[7] = (data_[7] < x.data_[7]) ? data_[7] : x.data_[7]; 
00282      data_[8] = (data_[8] < x.data_[8]) ? data_[8] : x.data_[8]; 
00283      data_[9] = (data_[9] < x.data_[9]) ? data_[9] : x.data_[9]; 
00284    }
00285    inline void max(const TVector &x) {
00286      data_[0] = (data_[0] > x.data_[0]) ? data_[0] : x.data_[0]; 
00287      data_[1] = (data_[1] > x.data_[1]) ? data_[1] : x.data_[1]; 
00288      data_[2] = (data_[2] > x.data_[2]) ? data_[2] : x.data_[2]; 
00289      data_[3] = (data_[3] > x.data_[3]) ? data_[3] : x.data_[3]; 
00290      data_[4] = (data_[4] > x.data_[4]) ? data_[4] : x.data_[4]; 
00291      data_[5] = (data_[5] > x.data_[5]) ? data_[5] : x.data_[5]; 
00292      data_[6] = (data_[6] > x.data_[6]) ? data_[6] : x.data_[6]; 
00293      data_[7] = (data_[7] > x.data_[7]) ? data_[7] : x.data_[7]; 
00294      data_[8] = (data_[8] > x.data_[8]) ? data_[8] : x.data_[8]; 
00295      data_[9] = (data_[9] > x.data_[9]) ? data_[9] : x.data_[9]; 
00296    }
00297 
00298    /* Define minimum and maximum of components */
00299    inline DataType mincomp() const { 
00300      DataType min;
00301      min = data_[0];
00302      if (data_[1] < min) min = data_[1]; 
00303      if (data_[2] < min) min = data_[2]; 
00304      if (data_[3] < min) min = data_[3]; 
00305      if (data_[4] < min) min = data_[4]; 
00306      if (data_[5] < min) min = data_[5]; 
00307      if (data_[6] < min) min = data_[6]; 
00308      if (data_[7] < min) min = data_[7]; 
00309      if (data_[8] < min) min = data_[8]; 
00310      if (data_[9] < min) min = data_[9]; 
00311      return min;
00312    }
00313    inline DataType maxcomp() const { 
00314      DataType max;
00315      max = data_[0];
00316      if (data_[1] > max) max = data_[1]; 
00317      if (data_[2] > max) max = data_[2]; 
00318      if (data_[3] > max) max = data_[3]; 
00319      if (data_[4] > max) max = data_[4]; 
00320      if (data_[5] > max) max = data_[5]; 
00321      if (data_[6] > max) max = data_[6]; 
00322      if (data_[7] > max) max = data_[7]; 
00323      if (data_[8] > max) max = data_[8]; 
00324      if (data_[9] > max) max = data_[9]; 
00325      return max;
00326    }
00327 
00328    inline double abs() const {
00329      DataType s = data_[0]*data_[0];
00330      s += data_[1]*data_[1]; 
00331      s += data_[2]*data_[2]; 
00332      s += data_[3]*data_[3]; 
00333      s += data_[4]*data_[4]; 
00334      s += data_[5]*data_[5]; 
00335      s += data_[6]*data_[6]; 
00336      s += data_[7]*data_[7]; 
00337      s += data_[8]*data_[8]; 
00338      s += data_[9]*data_[9];
00339      return (sqrt(double(s)));
00340    }
00341 
00342    inline TVector getmin(const TVector &x) const
00343      { TVector ret(*this); ret.min(x); return(ret); }
00344    inline TVector getmax(const TVector &x) const
00345      { TVector ret(*this); ret.max(x); return(ret); }
00346 
00347 public:
00348    DataType data_[10];
00349 };
00350 
00351 
00352 template <class DataType>
00353 inline ostream&  operator << (ostream& os, const Vector(10)<DataType> &v) {
00354   os << "(" 
00355      << v.data_[0] 
00356      << "," << v.data_[1] 
00357      << "," << v.data_[2] 
00358      << "," << v.data_[3] 
00359      << "," << v.data_[4] 
00360      << "," << v.data_[5] 
00361      << "," << v.data_[6] 
00362      << "," << v.data_[7] 
00363      << "," << v.data_[8] 
00364      << "," << v.data_[9] 
00365      << ")";
00366   return os;
00367 }
00368 
00369 template <class DataType>
00370 inline Vector(10)<DataType> Min(const Vector(10)<DataType> &x, 
00371                                    const Vector(10)<DataType> &y) 
00372   { Vector(10)<DataType> ret(x); ret.min(y); return(ret); }
00373 
00374 template <class DataType>
00375 inline Vector(10)<DataType> Max(const Vector(10)<DataType> &x, 
00376                                    const Vector(10)<DataType> &y) 
00377   { Vector(10)<DataType> ret(x); ret.max(y); return(ret); }
00378 
00379 template <class DataType>
00380 inline double abs(const Vector(10)<DataType> &x) 
00381   { return (x.abs()); }
00382 
00383 template <class DataType>
00384 inline DataType mincomp(const Vector(10)<DataType> &x) 
00385   { return(x.mincomp()); }
00386 
00387 template <class DataType>
00388 inline DataType maxcomp(const Vector(10)<DataType> &x) 
00389   { return(x.maxcomp()); }
00390 
00391 
00392 #define Global_Vector_Scalar_Operator(op)                                  \
00393 template <class DataType>                                                  \
00394 inline Vector(10)<DataType> operator op (const DataType &v,                 \
00395    const Vector(10)<DataType> &x)                                           \
00396    { return(x op v); }
00397 
00398 Global_Vector_Scalar_Operator(+)
00399 Global_Vector_Scalar_Operator(-)
00400 Global_Vector_Scalar_Operator(*)
00401 Global_Vector_Scalar_Operator(/)
00402 #undef Global_Vector_Scalar_Operator
00403 
00404 
00405 #define Global_Vector_Function(fct)                                       \
00406 template <class DataType>                                                 \
00407 inline Vector(10)<DataType> fct(const Vector(10)<DataType> &x)              \
00408    {                                                                      \
00409       Vector(10)<DataType> ret;                                            \
00410       ret[0] = fct(x[0]);                                                 \
00411       ret[1] = fct(x[1]);                                                 \
00412       ret[2] = fct(x[2]);                                                 \
00413       ret[3] = fct(x[3]);                                                 \
00414       ret[4] = fct(x[4]);                                                 \
00415       ret[5] = fct(x[5]);                                                 \
00416       ret[6] = fct(x[6]);                                                 \
00417       ret[7] = fct(x[7]);                                                 \
00418       ret[8] = fct(x[8]);                                                 \
00419       ret[9] = fct(x[9]);                                                 \
00420       return(ret);                                                        \
00421      }
00422 
00423 Global_Vector_Function(fabs)
00424 Global_Vector_Function(sqrt)
00425 #undef Global_Vector_Function
00426 
00427 
00428 #define Vector_Vector_Functions(NameTo,NameFrom,ope)                      \
00429   template <class DataType, class VectorType>                             \
00430   inline void NameTo (Vector(10)<DataType> &x, const VectorType &y)        \
00431     {                                                                     \
00432       x(0) ope y(0);                                                      \
00433       x(1) ope y(1);                                                      \
00434       x(2) ope y(2);                                                      \
00435       x(3) ope y(3);                                                      \
00436       x(4) ope y(4);                                                      \
00437       x(5) ope y(5);                                                      \
00438       x(6) ope y(6);                                                      \
00439       x(7) ope y(7);                                                      \
00440       x(8) ope y(8);                                                      \
00441       x(9) ope y(9);                                                      \
00442     }                                                                     \
00443   template <class DataType, class VectorType>                             \
00444   inline void NameFrom (VectorType &x, const Vector(10)<DataType> &y)      \
00445     {                                                                     \
00446       x(0) ope y(0);                                                      \
00447       x(1) ope y(1);                                                      \
00448       x(2) ope y(2);                                                      \
00449       x(3) ope y(3);                                                      \
00450       x(4) ope y(4);                                                      \
00451       x(5) ope y(5);                                                      \
00452       x(6) ope y(6);                                                      \
00453       x(7) ope y(7);                                                      \
00454       x(8) ope y(8);                                                      \
00455       x(9) ope y(9);                                                      \
00456     }
00457 
00458    Vector_Vector_Functions(equals_to,equals_from,=)
00459    Vector_Vector_Functions(plus_to,plus_from,+=)
00460    Vector_Vector_Functions(minus_to,minus_from,-=)
00461    Vector_Vector_Functions(multiply_to,multiply_from,*=)
00462    Vector_Vector_Functions(divide_to,divide_from,/=)
00463 #undef Vector_Vector_Functions
00464 
00465 
00466 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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