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


Main Page   Class Hierarchy   Compound List   File List  

Vector2.h

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


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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