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