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