| 1 | #ifndef GRID_KEY_EXPRESSION |
| 2 | #define GRID_KEY_EXPRESSION |
| 3 | |
| 4 | #include "util/common.hpp" |
| 5 | |
| 6 | template<unsigned int dim, typename index_type = long int> class grid_key_dx; |
| 7 | template<int dim, typename exp1, typename exp2> class grid_key_dx_sum; |
| 8 | template<int dim, typename exp1, typename exp2> class grid_key_dx_sub; |
| 9 | |
| 10 | |
| 11 | /*! \brief Expression template for grid_key_dx |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | /*! \brief Main class that encapsulate an expression |
| 16 | * |
| 17 | * \param dim dimensionality |
| 18 | * \param exp expression type |
| 19 | * |
| 20 | */ |
| 21 | template<unsigned int dim, typename exp> |
| 22 | class grid_key_dx_expression |
| 23 | { |
| 24 | public: |
| 25 | |
| 26 | __device__ __host__ mem_id value(int i) const |
| 27 | { |
| 28 | return static_cast<const exp &>(*this).value(i); |
| 29 | } |
| 30 | |
| 31 | /* \brief subtract this expression with another expression |
| 32 | * |
| 33 | * \param key to subtract |
| 34 | * |
| 35 | * \return a grid_key_dx_expression that encapsulate the expression |
| 36 | * |
| 37 | */ |
| 38 | template<typename index_type> |
| 39 | __device__ __host__ inline grid_key_dx_sub<dim,grid_key_dx_expression<dim,exp>,grid_key_dx<dim,index_type>> operator-(const grid_key_dx<dim,index_type> & key) const |
| 40 | { |
| 41 | grid_key_dx_sub<dim,grid_key_dx_expression<dim,exp>,grid_key_dx<dim,index_type>> exp_sum(*this,key); |
| 42 | |
| 43 | return exp_sum; |
| 44 | } |
| 45 | |
| 46 | /* \brief subtract this expression a grid key |
| 47 | * |
| 48 | * \param key to subtract |
| 49 | * |
| 50 | * \return a grid_key_dx_expression that encapsulate the expression |
| 51 | * |
| 52 | */ |
| 53 | template <typename T> |
| 54 | __device__ __host__ inline grid_key_dx_sub<dim,grid_key_dx_expression<dim,exp>,grid_key_dx_expression<dim,T> > operator-(const grid_key_dx_expression<dim,T> & key) const |
| 55 | { |
| 56 | grid_key_dx_sub< dim,grid_key_dx_expression<dim,exp>,grid_key_dx_expression<dim,T> > exp_sum(*this,key); |
| 57 | |
| 58 | return exp_sum; |
| 59 | } |
| 60 | |
| 61 | /* \brief subtract this expression with another expression |
| 62 | * |
| 63 | * \param key to subtract |
| 64 | * |
| 65 | * \return a grid_key_dx_expression that encapsulate the expression |
| 66 | * |
| 67 | */ |
| 68 | template<typename index_type> |
| 69 | __device__ __host__ inline grid_key_dx_sum<dim,grid_key_dx_expression<dim,exp>,grid_key_dx<dim,index_type>> operator+(const grid_key_dx<dim,index_type> & key) const |
| 70 | { |
| 71 | grid_key_dx_sum<dim,grid_key_dx_expression<dim,exp>,grid_key_dx<dim,index_type>> exp_sum(*this,key); |
| 72 | |
| 73 | return exp_sum; |
| 74 | } |
| 75 | |
| 76 | /* \brief subtract this expression a grid key |
| 77 | * |
| 78 | * \param key to subtract |
| 79 | * |
| 80 | * \return a grid_key_dx_expression that encapsulate the expression |
| 81 | * |
| 82 | */ |
| 83 | template <typename T> |
| 84 | __device__ __host__ inline grid_key_dx_sum<dim,grid_key_dx_expression<dim,exp>,grid_key_dx_expression<dim,T> > operator+(const grid_key_dx_expression<dim,T> & key) const |
| 85 | { |
| 86 | grid_key_dx_sum< dim,grid_key_dx_expression<dim,exp>,grid_key_dx_expression<dim,T> > exp_sum(*this,key); |
| 87 | |
| 88 | return exp_sum; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | /*! \brief Main class that encapsulate a sum expression |
| 94 | * |
| 95 | * \param dim dimensionality |
| 96 | * \param exp1 expression 1 |
| 97 | * \param exp2 expression 2 |
| 98 | * |
| 99 | */ |
| 100 | template<int dim, typename exp1, typename exp2> |
| 101 | class grid_key_dx_sum : public grid_key_dx_expression<dim,grid_key_dx_sum<dim,exp1,exp2>> |
| 102 | { |
| 103 | const exp1 & e1; |
| 104 | const exp2 & e2; |
| 105 | |
| 106 | public: |
| 107 | |
| 108 | __device__ __host__ grid_key_dx_sum(const exp1 & ex1, const exp2 & ex2) |
| 109 | :e1(ex1),e2(ex2) |
| 110 | {} |
| 111 | |
| 112 | __device__ __host__ mem_id value(int i) const |
| 113 | { |
| 114 | return e1.value(i) + e2.value(i); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | /*! \brief Main class that encapsulate a sub expression |
| 119 | * |
| 120 | * \param dim dimensionality |
| 121 | * \param exp1 expression 1 |
| 122 | * \param exp2 expression 2 |
| 123 | * |
| 124 | */ |
| 125 | template<int dim, typename exp1, typename exp2> |
| 126 | class grid_key_dx_sub : public grid_key_dx_expression<dim,grid_key_dx_sub<dim,exp1,exp2>> |
| 127 | { |
| 128 | const exp1 & e1; |
| 129 | const exp2 & e2; |
| 130 | |
| 131 | public: |
| 132 | |
| 133 | __device__ __host__ grid_key_dx_sub(const exp1 & ex1, const exp2 & ex2) |
| 134 | :e1(ex1),e2(ex2) |
| 135 | {} |
| 136 | |
| 137 | __device__ __host__ mem_id value(int i) const |
| 138 | { |
| 139 | return e1.value(i) - e2.value(i); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | #endif |
| 144 | |