Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

src/AppTypes/Vektor.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef VEKTOR_H
00012 #define VEKTOR_H
00013 
00014 // include files
00015 #include "Utility/PAssert.h"
00016 #include "Message/Message.h"
00017 #include "PETE/IpplExpressions.h"
00018 #include "AppTypes/TSVMeta.h"
00019 
00020 #ifdef IPPL_USE_STANDARD_HEADERS
00021 #include <iostream>
00022 using namespace std;
00023 #else
00024 #include <iostream.h>
00025 #endif
00026 
00028 //
00029 // Definition of class Vektor.
00030 //
00032 
00033 template<class T, unsigned D>
00034 class Vektor
00035 {
00036 public:
00037 
00038   typedef T Element_t;
00039   enum { ElemDim = 1 };
00040   enum { Size = D };
00041 
00042   // Default Constructor initializes to zero.
00043   Vektor() { 
00044     TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,T(0));
00045   }
00046 
00047   // A noninitializing ctor.
00048   class DontInitialize {};
00049   Vektor(DontInitialize) {}
00050 
00051   // Copy Constructor 
00052   Vektor(const Vektor<T,D> &rhs) {
00053     TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign >::apply(*this,rhs);
00054   }
00055 
00056   // Templated Vektor constructor.
00057   template<class T1, unsigned D1>
00058   Vektor(const Vektor<T1,D1> &rhs) {
00059     for (unsigned d=0; d<D; ++d)
00060       X[d] = (d < D1) ? rhs[d] : T1(0);
00061   }
00062 
00063   // Constructor from a single T
00064   Vektor(const T& x00) {
00065     TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,x00);
00066   }
00067 
00068   // Constructors for fixed dimension
00069   Vektor(const T& x00, const T& x01) {
00070     PInsist(D==2, "Number of arguments does not match Vektor dimension!!");
00071     X[0] = x00;
00072     X[1] = x01;
00073   }
00074   Vektor(const T& x00, const T& x01, const T& x02) {
00075     PInsist(D==3, "Number of arguments does not match Vektor dimension!!");
00076     X[0] = x00;
00077     X[1] = x01;
00078     X[2] = x02;
00079   }
00080 
00081   // Destructor 
00082   ~Vektor() { }
00083 
00084   // Assignment Operators
00085   const Vektor<T,D>& operator=(const Vektor<T,D> &rhs)
00086   {
00087     TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
00088     return *this;
00089   }
00090   template<class T1>
00091   const Vektor<T,D>& operator=(const Vektor<T1,D> &rhs)
00092   {
00093     TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> ,OpAssign> :: apply(*this,rhs);
00094     return *this;
00095   }
00096   const Vektor<T,D>& operator=(const T& rhs)
00097   {
00098     TSV_MetaAssignScalar< Vektor<T,D> , T ,OpAssign > :: apply(*this,rhs);
00099     return *this;
00100   }
00101 
00102   // Accumulation Operators
00103   template<class T1>
00104   Vektor<T,D>& operator+=(const Vektor<T1,D> &rhs)
00105   {
00106     TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpAddAssign > :: apply(*this,rhs);
00107     return *this;
00108   }
00109   Vektor<T,D>& operator+=(const T& rhs)
00110   {
00111     TSV_MetaAssignScalar< Vektor<T,D> , T , OpAddAssign > :: apply(*this,rhs);
00112     return *this;
00113   }
00114 
00115   template<class T1>
00116   Vektor<T,D>& operator-=(const Vektor<T1,D> &rhs)
00117   {
00118     TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpSubtractAssign > :: apply(*this,rhs);
00119     return *this;
00120   }
00121   Vektor<T,D>& operator-=(const T& rhs)
00122   {
00123     TSV_MetaAssignScalar< Vektor<T,D> , T , OpSubtractAssign > :: apply(*this,rhs);
00124     return *this;
00125   }
00126 
00127   template<class T1>
00128   Vektor<T,D>& operator*=(const Vektor<T1,D> &rhs)
00129   {
00130     TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpMultipplyAssign > :: apply(*this,rhs);
00131     return *this;
00132   }
00133   Vektor<T,D>& operator*=(const T& rhs)
00134   {
00135     TSV_MetaAssignScalar< Vektor<T,D> , T , OpMultipplyAssign > :: apply(*this,rhs);
00136     return *this;
00137   }
00138 
00139   template<class T1>
00140   Vektor<T,D>& operator/=(const Vektor<T1,D> &rhs)
00141   {
00142     TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpDivideAssign > :: 
00143       apply(*this,rhs);
00144     return *this;
00145   }
00146   Vektor<T,D>& operator/=(const T& rhs)
00147   {
00148     TSV_MetaAssignScalar< Vektor<T,D> , T , OpDivideAssign > :: 
00149       apply(*this,rhs);
00150     return *this;
00151   }
00152 
00153   // Get and Set Operations
00154   Element_t& operator[](unsigned int i)
00155   { 
00156     PAssert (i<D);
00157     return X[i];
00158   }
00159   Element_t operator[](unsigned int i) const
00160   { 
00161     PAssert (i<D);
00162     return X[i];
00163   }
00164   Element_t& operator()(unsigned int i)
00165   { 
00166     PAssert (i<D);
00167     return X[i];
00168   }
00169   Element_t operator()( unsigned int i) const
00170   { 
00171     PAssert (i<D);
00172     return X[i];
00173   }
00174 
00175   // Comparison operators.
00176   bool operator==(const Vektor<T,D>& that) const {
00177     return TSV_MetaCompareArrays<T,T,D>::apply(X,that.X);
00178   }
00179   bool operator!=(const Vektor<T,D>& that) const {
00180     return !(*this == that);
00181   }
00182 
00183   //----------------------------------------------------------------------
00184   // parallel communication
00185   Message& putMessage(Message& m) const {
00186     m.setCopy(true);
00187     ::putMessage(m, X, X + D);
00188     return m;
00189   }
00190 
00191   Message& getMessage(Message& m) {
00192     ::getMessage(m, X, X + D);
00193     return m;
00194   }
00195 
00196 private:
00197 
00198   // Just store D elements of type T.
00199   T X[Size];
00200 
00201 };
00202 
00204 //
00205 // Unary Operators
00206 //
00208 
00209 //----------------------------------------------------------------------
00210 // unary operator-
00211 template<class T, unsigned D>
00212 inline Vektor<T,D> operator-(const Vektor<T,D> &op)
00213 {
00214   return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
00215 }
00216 
00217 //----------------------------------------------------------------------
00218 // unary operator+
00219 template<class T, unsigned D>
00220 inline const Vektor<T,D> &operator+(const Vektor<T,D> &op)
00221 {
00222   return op;
00223 }
00224 
00226 //
00227 // Binary Operators
00228 //
00230 
00231 //
00232 // Elementwise operators.
00233 //
00234 
00235 TSV_ELEMENTWISE_OPERATOR(Vektor,operator+,OpAdd)
00236 TSV_ELEMENTWISE_OPERATOR(Vektor,operator-,OpSubtract)
00237 TSV_ELEMENTWISE_OPERATOR(Vektor,operator*,OpMultipply)
00238 TSV_ELEMENTWISE_OPERATOR(Vektor,operator/,OpDivide)
00239 TSV_ELEMENTWISE_OPERATOR(Vektor,Min,FnMin)
00240 TSV_ELEMENTWISE_OPERATOR(Vektor,Max,FnMax)
00241 
00242 //----------------------------------------------------------------------
00243 // dot product
00244 //----------------------------------------------------------------------
00245 
00246 template < class T1, class T2, unsigned D >
00247 inline typename PETEBinaryReturn<T1,T2,OpMultipply>::type
00248 dot(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs) 
00249 {
00250   return TSV_MetaDot< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
00251 }
00252 
00253 //----------------------------------------------------------------------
00254 // cross product
00255 //----------------------------------------------------------------------
00256 
00257 template < class T1, class T2, unsigned D >
00258 inline Vektor<typename PETEBinaryReturn<T1,T2,OpMultipply>::type,D>
00259 cross(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs) 
00260 {
00261   return TSV_MetaCross< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
00262 }
00263 
00264 //----------------------------------------------------------------------
00265 // I/O
00266 template<class T, unsigned D>
00267 ostream& operator<<(ostream& out, const Vektor<T,D>& rhs)
00268 {
00269   if (D >= 1) {
00270     out << "( ";
00271     for (int i=0; i<D - 1; i++)
00272       out << rhs[i] << " , ";
00273     out << rhs[D - 1] << " )";
00274   } else {
00275     out << "( " << rhs[0] << " )";
00276   }
00277 
00278   return out;
00279 }
00280 
00281 #endif // VEKTOR_H
00282 
00283 /***************************************************************************
00284  * $RCSfile: Vektor.h,v $   $Author: adelmann $
00285  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:24 $
00286  * IPPL_VERSION_ID: $Id: Vektor.h,v 1.1.1.1 2003/01/23 07:40:24 adelmann Exp $ 
00287  ***************************************************************************/

Generated on Fri Nov 2 01:25:55 2007 for IPPL by doxygen 1.3.5