src/Utility/SequenceGen.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 SEQUENCE_GEN_H
00012 #define SEQUENCE_GEN_H
00013 
00014 /***********************************************************************
00015  * 
00016  * class SequenceGen
00017  *
00018  * SequenceGen generates a sequence of doubles from some algorithm, such
00019  * a uniform or gaussian random number generator.  It is templated on
00020  * a class which actually generates the number sequence.  SequenceGen
00021  * provides an expression-template-aware wrapper around this generator.
00022  *
00023  * A SequenceGen is created by giving it a reference to an instance
00024  * of the specific generator it should use.  It keeps a copy of this
00025  * generator, so the classes used as template parameters for SequenceGen
00026  * must have a copy constructor.  It should also have a default constructor.
00027  * Also, it uses the () operator to get
00028  * the next number in the sequence.  The particular generator itself
00029  * may be accessed through the 'getGenerator' method.
00030  *
00031  * The generator class used as the template parameter must also provide
00032  * a typedef indicating the return type of the generator; this typedef
00033  * must be called Return_t.
00034  *
00035  ***********************************************************************/
00036 
00037 // include files
00038 #include "PETE/IpplExpressions.h"
00039 
00040 // some macro definitions to set up basic binary math operations
00041 
00042 #define RNG_OPERATOR_WITH_SCALAR(GEN,SCA,OP,APP)                        \
00043                                                                         \
00044 inline PETEBinaryReturn<GEN,SCA,APP>::type                              \
00045 OP(const GEN& lhs, SCA sca)                                             \
00046 {                                                                       \
00047   return PETE_apply( APP(), lhs(), sca );                               \
00048 }                                                                       \
00049                                                                         \
00050 inline PETEBinaryReturn<SCA,GEN,APP>::type                              \
00051 OP(SCA sca, const GEN& rhs)                                             \
00052 {                                                                       \
00053   return PETE_apply( APP(), sca, rhs() );                               \
00054 }
00055 
00056 #define RNG_OPERATOR(GEN,OP,APP)                                        \
00057                                                                         \
00058 RNG_OPERATOR_WITH_SCALAR(GEN,short,OP,APP)                              \
00059 RNG_OPERATOR_WITH_SCALAR(GEN,int,OP,APP)                                \
00060 RNG_OPERATOR_WITH_SCALAR(GEN,long,OP,APP)                               \
00061 RNG_OPERATOR_WITH_SCALAR(GEN,float,OP,APP)                              \
00062 RNG_OPERATOR_WITH_SCALAR(GEN,double,OP,APP)                             \
00063 RNG_OPERATOR_WITH_SCALAR(GEN,dcomplex,OP,APP)
00064 
00065 #define RNG_BASIC_MATH(GEN)                                             \
00066                                                                         \
00067 RNG_OPERATOR(GEN,operator+,OpAdd)                                       \
00068 RNG_OPERATOR(GEN,operator-,OpSubtract)                                  \
00069 RNG_OPERATOR(GEN,operator*,OpMultipply)                                  \
00070 RNG_OPERATOR(GEN,operator/,OpDivide)
00071 
00072 
00073 // the guts of the sequence generator
00074 template <class GT>
00075 class SequenceGen : public PETE_Expr< SequenceGen<GT> >
00076 {
00077 
00078 public:
00079   SequenceGen() { }
00080   SequenceGen(const GT& gen) : Gen(gen) { }
00081 
00082   // Interface for PETE.
00083   enum { IsExpr = 1 };  // Treat SequenceGen as a PETE_Expr in expressions.
00084   typedef SequenceGen<GT> PETE_Expr_t;
00085   typedef typename GT::Return_t PETE_Return_t;
00086   const PETE_Expr_t& MakeExpression() const { return *this; }
00087   PETE_Expr_t&       MakeExpression()       { return *this; }
00088 
00089   // typedefs for functions below
00090   typedef typename GT::Return_t Return_t;
00091 
00092   // return access to the generator
00093   GT& getGenerator() { return Gen; }
00094   const GT& getGenerator() const { return Gen; }
00095 
00096   // get the next value in the sequence
00097   inline Return_t operator()(void) const { return Gen(); }
00098 
00099 private:
00100   // the number generator
00101   GT Gen;
00102 };
00103 
00104 // define for_each operations for SequenceGen objects
00105 #include "Utility/RNGAssignDefs.h"
00106 
00107 #endif // SEQUENCE_GEN_H
00108 
00109 /***************************************************************************
00110  * $RCSfile: SequenceGen.h,v $   $Author: adelmann $
00111  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00112  * IPPL_VERSION_ID: $Id: SequenceGen.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00113  ***************************************************************************/

Generated on Mon Jan 16 13:23:59 2006 for IPPL by  doxygen 1.4.6