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