00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef AMROC_VECTORLARGE_H
00010 #define AMROC_VECTORLARGE_H
00011
00019 #include "generic.h"
00020 #include <iostream>
00021 #include <cassert>
00022 #include <cstring>
00023 #include <cmath>
00024
00031 template <class DataType, int size>
00032 class Vector {
00033 typedef Vector<DataType,size> TVector;
00034
00035 public:
00036 typedef DataType InternalDataType;
00037
00038 Vector() {}
00039 Vector(const TVector& x) {
00040 std::memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00041 }
00042 Vector(const DataType& v) {
00043 for (register int n=0; n<size; n++)
00044 data_[n] = v;
00045 }
00046
00047 ~Vector() {}
00048
00049 inline DataType * data() { return data_; }
00050 inline const DataType * data() const { return data_; }
00051 inline int length() const { return size; }
00052 inline static int Length() { return size; }
00053 enum { Size = size };
00054
00055 inline DataType operator()(int i) const {
00056 #ifdef DEBUG_PRINT
00057 assert((i >= 0) && (i < size));
00058 #endif
00059 return data_[i];
00060 }
00061 inline DataType& operator()(int i) {
00062 #ifdef DEBUG_PRINT
00063 assert((i >= 0) && (i < size));
00064 #endif
00065 return data_[i];
00066 }
00067
00068 inline DataType operator[](int i) const {
00069 #ifdef DEBUG_PRINT
00070 assert((i >= 0) && (i < size));
00071 #endif
00072 return data_[i];
00073 }
00074 inline DataType& operator[](int i) {
00075 #ifdef DEBUG_PRINT
00076 assert((i >= 0) && (i < size));
00077 #endif
00078 return data_[i];
00079 }
00080
00081 inline TVector& operator=(const TVector& x) {
00082 std::memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00083 return *this;
00084 }
00085 inline TVector& operator=(const DataType& v) {
00086 for (register int n=0; n<size; n++)
00087 data_[n] = v;
00088 return *this;
00089 }
00090
00091 #define Vector_Vector_Operator(ope,op) \
00092 inline TVector& operator ope (const TVector &x) \
00093 { \
00094 for (register int n=0; n<size; n++) \
00095 data_[n] ope x.data_[n]; \
00096 return(*this); \
00097 } \
00098 inline TVector operator op (const TVector &x) const \
00099 { \
00100 TVector ret(*this); \
00101 for (register int n=0; n<size; n++) \
00102 ret.data_[n] ope x.data_[n]; \
00103 return(ret); \
00104 }
00105 Vector_Vector_Operator(+=,+)
00106 Vector_Vector_Operator(-=,-)
00107 Vector_Vector_Operator(*=,*)
00108 Vector_Vector_Operator(/=,/)
00109 #undef Vector_Vector_Operator
00110
00111 #define Vector_Scalar_Operator(ope,op) \
00112 inline TVector& operator ope (const DataType &v) \
00113 { \
00114 for (register int n=0; n<size; n++) \
00115 data_[n] ope v; \
00116 return(*this); \
00117 } \
00118 inline TVector operator op (const DataType &v) const \
00119 { \
00120 TVector ret(*this); \
00121 for (register int n=0; n<size; n++) \
00122 ret.data_[n] ope v; \
00123 return(ret); \
00124 }
00125 Vector_Scalar_Operator(+=,+)
00126 Vector_Scalar_Operator(-=,-)
00127 Vector_Scalar_Operator(*=,*)
00128 Vector_Scalar_Operator(/=,/)
00129 #undef Vector_Scalar_Operator
00130
00131
00132 inline TVector operator - () const
00133 {
00134 TVector ret;
00135 for (register int n=0; n<size; n++)
00136 ret.data_[n] = -data_[n];
00137 return(ret);
00138 }
00139
00140 #define Vector_Vector_RelOperator(op) \
00141 inline int operator op (const TVector &x) const \
00142 { \
00143 bool ret = (data_[0] op x.data_[0]); \
00144 for (register int n=1; n<size; n++) \
00145 ret = ret && (data_[n] op x.data_[n]); \
00146 return(ret); \
00147 }
00148
00149 Vector_Vector_RelOperator(==)
00150 Vector_Vector_RelOperator(!=)
00151 #undef Vector_Vector_RelOperator
00152
00153 #define Vector_Vector_RelOperator(op) \
00154 inline int operator op (const TVector &x) const \
00155 { \
00156 bool ret = (data_[0] op x.data_[0]); \
00157 for (register int n=1; n<size; n++) \
00158 ret = ret || (data_[n] op x.data_[n]); \
00159 return(ret); \
00160 }
00161
00162 Vector_Vector_RelOperator(>)
00163 Vector_Vector_RelOperator(<)
00164 Vector_Vector_RelOperator(>=)
00165 Vector_Vector_RelOperator(<=)
00166 #undef Vector_Vector_RelOperator
00167
00168 #define Vector_Scalar_RelOperator(op) \
00169 inline int operator op (const DataType &v) const \
00170 { \
00171 bool ret = (data_[0] op v); \
00172 for (register int n=1; n<size; n++) \
00173 ret = ret && (data_[n] op v); \
00174 return(ret); \
00175 }
00176
00177 Vector_Scalar_RelOperator(==)
00178 Vector_Scalar_RelOperator(!=)
00179 #undef Vector_Scalar_RelOperator
00180
00181 #define Vector_Scalar_RelOperator(op) \
00182 inline int operator op (const DataType &v) const \
00183 { \
00184 bool ret = (data_[0] op v); \
00185 for (register int n=1; n<size; n++) \
00186 ret = ret || (data_[n] op v); \
00187 return(ret); \
00188 }
00189
00190 Vector_Scalar_RelOperator(>)
00191 Vector_Scalar_RelOperator(<)
00192 Vector_Scalar_RelOperator(>=)
00193 Vector_Scalar_RelOperator(<=)
00194 #undef Vector_Scalar_RelOperator
00195
00196
00197 inline void min(const TVector &x) {
00198 for (register int n=0; n<size; n++)
00199 data_[n] = (data_[n] < x.data_[n]) ? data_[n] : x.data_[n];
00200 }
00201 inline void max(const TVector &x) {
00202 for (register int n=0; n<size; n++)
00203 data_[n] = (data_[n] > x.data_[n]) ? data_[n] : x.data_[n];
00204 }
00205
00206
00207 inline DataType mincomp() const {
00208 DataType min = data_[0];
00209 for (register int n=1; n<size; n++)
00210 if (data_[n] < min) min = data_[n];
00211 return min;
00212 }
00213 inline DataType maxcomp() const {
00214 DataType max = data_[0];
00215 for (register int n=1; n<size; n++)
00216 if (data_[n] > max) max = data_[n];
00217 return max;
00218 }
00219
00220 inline double abs() const {
00221 DataType s = data_[0]*data_[0];
00222 for (register int n=1; n<size; n++)
00223 s += data_[n]*data_[n];
00224 return (std::sqrt(static_cast<double>(s)));
00225 }
00226
00227 inline TVector getmin(const TVector &x) const
00228 { TVector ret(*this); ret.min(x); return(ret); }
00229 inline TVector getmax(const TVector &x) const
00230 { TVector ret(*this); ret.max(x); return(ret); }
00231
00232 public:
00233 DataType data_[size];
00234 };
00235
00236
00237 template <class DataType, int size>
00238 inline std::ostream& operator << (std::ostream& os, const Vector<DataType,size> &v) {
00239 os << "(" << v.data_[0];
00240 for (register int n=1; n<size; n++)
00241 os << "," << v.data_[n];
00242 os << ")";
00243 return os;
00244 }
00245
00246
00247 #define Global_Vector_Scalar_Operator(op) \
00248 template <class DataType, int size> \
00249 inline Vector<DataType,size> operator op (const DataType &v, \
00250 const Vector<DataType,size> &x) \
00251 { return(x op v); }
00252
00253 Global_Vector_Scalar_Operator(+)
00254 Global_Vector_Scalar_Operator(-)
00255 Global_Vector_Scalar_Operator(*)
00256 Global_Vector_Scalar_Operator(/)
00257 #undef Global_Vector_Scalar_Operator
00258
00259
00260 #define Global_Vector_Function(fct) \
00261 template <class DataType, int size> \
00262 inline Vector<DataType,size> fct(const Vector<DataType,size> &x) \
00263 { \
00264 Vector<DataType,size> ret; \
00265 for (register int n=0; n<size; n++) \
00266 ret[n] = std::fct(x[n]); \
00267 return(ret); \
00268 }
00269
00270 Global_Vector_Function(fabs)
00271 Global_Vector_Function(sqrt)
00272 #undef Global_Vector_Function
00273
00274
00275 template <class DataType, int size>
00276 inline Vector<DataType,size> Min(const Vector<DataType,size> &x,
00277 const Vector<DataType,size> &y)
00278 { Vector<DataType,size> ret(x); ret.min(y); return(ret); }
00279
00280 template <class DataType, int size>
00281 inline Vector<DataType,size> Max(const Vector<DataType,size> &x,
00282 const Vector<DataType,size> &y)
00283 { Vector<DataType,size> ret(x); ret.max(y); return(ret); }
00284
00285 template <class DataType, int size>
00286 inline double Abs(const Vector<DataType,size> &x)
00287 { return (x.abs()); }
00288
00289 template <class DataType, int size>
00290 inline DataType mincomp(const Vector<DataType,size> &x)
00291 { return(x.mincomp()); }
00292
00293 template <class DataType, int size>
00294 inline DataType maxcomp(const Vector<DataType,size> &x)
00295 { return(x.maxcomp()); }
00296
00297
00298 #define Vector_Vector_Functions(NameTo,NameFrom,ope) \
00299 template <class DataType, class VectorType, int size> \
00300 inline void NameTo (Vector<DataType,size> &x, const VectorType &y) \
00301 { \
00302 for (register int n=0; n<size; n++) \
00303 x(n) ope y(n); \
00304 } \
00305 template <class DataType, class VectorType, int size> \
00306 inline void NameFrom (VectorType &x, const Vector<DataType,size> &y) \
00307 { \
00308 for (register int n=0; n<size; n++) \
00309 x(n) ope y(n); \
00310 }
00311
00312 Vector_Vector_Functions(equals_to,equals_from,=)
00313 Vector_Vector_Functions(plus_to,plus_from,+=)
00314 Vector_Vector_Functions(minus_to,minus_from,-=)
00315 Vector_Vector_Functions(multiply_to,multiply_from,*=)
00316 Vector_Vector_Functions(divide_to,divide_from,/=)
00317 #undef Vector_Vector_Functions
00318
00319
00320 #endif