library of assembled shared sources

http://lass.cocamware.com

vector_expressions.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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     // special VStorage members:
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 /** @internal
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 /** @internal
00104  */
00105 template <typename ExpressionType>
00106 struct VectorExpressionTraits
00107 {
00108     typedef const ExpressionType& TStorage;
00109 };
00110 
00111 /** @internal
00112  */
00113 template <typename T>
00114 struct VectorExpressionTraits<VScalar<T> >
00115 {
00116     typedef VScalar<T> TStorage;
00117 };
00118 
00119 
00120 
00121 /** @internal
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 /** @internal
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 /** @internal
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 // EOF

Generated on Mon Nov 10 14:22:02 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo