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


Main Page   Class Hierarchy   Compound List   File List  

Vector26.h

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


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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