vector_expressions.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_VECTOR_EXPRESSIONS_H
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_VECTOR_EXPRESSIONS_H
00047
00048 #include "../num_common.h"
00049
00050 namespace lass
00051 {
00052 namespace num
00053 {
00054 namespace impl
00055 {
00056
00057 template <typename T>
00058 class VStorage
00059 {
00060 public:
00061 enum { lvalue = true };
00062 typedef typename util::CallTraits<T>::TValue TValue;
00063 typedef typename util::CallTraits<T>::TParam TParam;
00064 typedef typename util::CallTraits<T>::TReference TReference;
00065 typedef typename util::CallTraits<T>::TConstReference TConstReference;
00066 typedef size_t TSize;
00067
00068 VStorage(): storage_() {}
00069 VStorage(TSize iSize, TParam iInitialValue): storage_(iSize, iInitialValue) {}
00070 TReference operator[](TSize iIndex) { LASS_ASSERT(iIndex < size()); return storage_[iIndex]; }
00071 TConstReference operator[](TSize iIndex) const { LASS_ASSERT(iIndex < size()); return storage_[iIndex]; }
00072 TSize size() const { return storage_.size(); }
00073
00074
00075
00076 void resize(TSize iSize) { storage_.resize(iSize, T()); }
00077 void swap(VStorage<T>& iOther) { storage_.swap(iOther.storage_); }
00078 private:
00079 std::vector<T> storage_;
00080 };
00081
00082
00083
00084 template <typename T>
00085 class VScalar
00086 {
00087 public:
00088 enum { lvalue = false };
00089 typedef typename util::CallTraits<T>::TValue TValue;
00090 typedef typename util::CallTraits<T>::TParam TParam;
00091 typedef size_t TSize;
00092
00093 VScalar(TSize iSize, TParam iValue): value_(iValue), size_(iSize) {}
00094 TParam operator[](TSize iIndex) const { LASS_ASSERT(iIndex < size_); return value_; }
00095 TSize size() const { return size_; }
00096 private:
00097 TValue value_;
00098 TSize size_;
00099 };
00100
00101
00102
00103
00104
00105 template <typename ExpressionType>
00106 struct VectorExpressionTraits
00107 {
00108 typedef const ExpressionType& TStorage;
00109 };
00110
00111
00112
00113 template <typename T>
00114 struct VectorExpressionTraits<VScalar<T> >
00115 {
00116 typedef VScalar<T> TStorage;
00117 };
00118
00119
00120
00121
00122
00123 #define LASS_NUM_VECTOR_BINARY_EXPRESSION(i_name, c_operator)\
00124 template <typename T, typename Operand1, typename Operand2>\
00125 class LASS_CONCATENATE(V, i_name)\
00126 {\
00127 public:\
00128 enum { lvalue = false };\
00129 typedef typename util::CallTraits<T>::TValue TValue;\
00130 typedef size_t TSize;\
00131 LASS_CONCATENATE(V, i_name)(const Operand1& iA, const Operand2& iB):\
00132 operand1_(iA), operand2_(iB)\
00133 {\
00134 LASS_ASSERT(operand1_.size() == operand2_.size());\
00135 }\
00136 TValue operator[](TSize iIndex) const { return operand1_[iIndex] c_operator operand2_[iIndex]; }\
00137 TSize size() const { return operand1_.size(); }\
00138 private:\
00139 typename VectorExpressionTraits<Operand1>::TStorage operand1_;\
00140 typename VectorExpressionTraits<Operand2>::TStorage operand2_;\
00141 }
00142
00143 LASS_NUM_VECTOR_BINARY_EXPRESSION(Add, +);
00144 LASS_NUM_VECTOR_BINARY_EXPRESSION(Sub, -);
00145 LASS_NUM_VECTOR_BINARY_EXPRESSION(Mul, *);
00146 LASS_NUM_VECTOR_BINARY_EXPRESSION(Div, /);
00147
00148
00149
00150
00151
00152 #define LASS_NUM_VECTOR_UNARY_EXPRESSION(i_name, c_operator)\
00153 template <typename T, typename Operand1>\
00154 class LASS_CONCATENATE(V, i_name)\
00155 {\
00156 public:\
00157 enum { lvalue = false };\
00158 typedef typename util::CallTraits<T>::TValue TValue;\
00159 typedef size_t TSize;\
00160 LASS_CONCATENATE(V, i_name)(const Operand1& iA): operand1_(iA) {}\
00161 TValue operator[](TSize iIndex) const { return c_operator operand1_[iIndex]; }\
00162 TSize size() const { return operand1_.size(); }\
00163 private:\
00164 typename VectorExpressionTraits<Operand1>::TStorage operand1_;\
00165 }
00166
00167 LASS_NUM_VECTOR_UNARY_EXPRESSION(Neg, -);
00168 LASS_NUM_VECTOR_UNARY_EXPRESSION(Rec, ::lass::num::NumTraits<T>::one /);
00169
00170
00171
00172
00173
00174 template <typename T, typename Operand1>
00175 class VFun
00176 {
00177 public:
00178 enum { lvalue = false };
00179 typedef T (*TOperator)(T);
00180 typedef typename util::CallTraits<T>::TValue TValue;
00181 typedef size_t TSize;
00182 VFun(const Operand1& iA, TOperator iOperator): operand1_(iA), operator_(iOperator) {}
00183 TValue operator[](TSize iIndex) const { return operator_(operand1_[iIndex]); }
00184 TSize size() const { return operand1_.size(); }
00185 private:
00186 typename VectorExpressionTraits<Operand1>::TStorage operand1_;
00187 TOperator operator_;
00188 };
00189
00190
00191
00192
00193 }
00194
00195 }
00196
00197 }
00198
00199 #endif
00200
00201