vtf-logo

VectorLarge.h

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

Generated on Fri Aug 24 13:00:29 2007 for AMROC's Hierachical Data Structures - by  doxygen 1.4.7