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