Library of Assembled Shared Sources
vector_expressions.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_VECTOR_EXPRESSIONS_H
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_VECTOR_EXPRESSIONS_H
47
48#include "../num_common.h"
49
50namespace lass
51{
52namespace num
53{
54namespace impl
55{
56
57template <typename T>
58class VStorage
59{
60public:
61 enum { lvalue = true };
62 typedef typename util::CallTraits<T>::TValue TValue;
63 typedef typename util::CallTraits<T>::TParam TParam;
64 typedef typename util::CallTraits<T>::TReference TReference;
65 typedef typename util::CallTraits<T>::TConstReference TConstReference;
66 typedef size_t TSize;
67
68 VStorage(): storage_() {}
69 VStorage(TSize iSize, TParam iInitialValue): storage_(iSize, iInitialValue) {}
70 TReference operator[](TSize iIndex) { LASS_ASSERT(iIndex < size()); return storage_[iIndex]; }
71 TConstReference operator[](TSize iIndex) const { LASS_ASSERT(iIndex < size()); return storage_[iIndex]; }
72 TSize size() const { return storage_.size(); }
73
74 // special VStorage members:
75 //
76 void resize(TSize iSize) { storage_.resize(iSize, T()); }
77 void swap(VStorage<T>& iOther) { storage_.swap(iOther.storage_); }
78private:
79 std::vector<T> storage_;
80};
81
82/** @internal
83 */
84template <typename T>
85class VScalar
86{
87public:
88 enum { lvalue = false };
89 typedef typename util::CallTraits<T>::TValue TValue;
90 typedef typename util::CallTraits<T>::TParam TParam;
91 typedef size_t TSize;
92
93 VScalar(TSize iSize, TParam iValue): value_(iValue), size_(iSize) {}
94 TParam operator[]([[maybe_unused]] TSize iIndex) const { LASS_ASSERT(iIndex < size_); return value_; }
95 TSize size() const { return size_; }
96private:
97 TValue value_;
98 TSize size_;
99};
100
101
102
103/** @internal
104 */
105template <typename ExpressionType>
106struct VectorExpressionTraits
107{
108 typedef const ExpressionType& TStorage;
109};
110
111/** @internal
112 */
113template <typename T>
114struct VectorExpressionTraits<VScalar<T> >
115{
116 typedef VScalar<T> TStorage;
117};
118
119
120
121/** @internal
122 */
123#define LASS_NUM_VECTOR_BINARY_EXPRESSION(i_name, c_operator)\
124template <typename T, typename Operand1, typename Operand2>\
125class LASS_CONCATENATE(V, i_name)\
126{\
127public:\
128 enum { lvalue = false };\
129 typedef typename util::CallTraits<T>::TValue TValue;\
130 typedef size_t TSize;\
131 LASS_CONCATENATE(V, i_name)(const Operand1& iA, const Operand2& iB):\
132 operand1_(iA), operand2_(iB)\
133 {\
134 LASS_ASSERT(operand1_.size() == operand2_.size());\
135 }\
136 TValue operator[](TSize iIndex) const { return operand1_[iIndex] c_operator operand2_[iIndex]; }\
137 TSize size() const { return operand1_.size(); }\
138private:\
139 typename VectorExpressionTraits<Operand1>::TStorage operand1_;\
140 typename VectorExpressionTraits<Operand2>::TStorage operand2_;\
141}
142
143LASS_NUM_VECTOR_BINARY_EXPRESSION(Add, +);
144LASS_NUM_VECTOR_BINARY_EXPRESSION(Sub, -);
145LASS_NUM_VECTOR_BINARY_EXPRESSION(Mul, *);
146LASS_NUM_VECTOR_BINARY_EXPRESSION(Div, /);
147
148
149
150/** @internal
151 */
152#define LASS_NUM_VECTOR_UNARY_EXPRESSION(i_name, c_operator)\
153template <typename T, typename Operand1>\
154class LASS_CONCATENATE(V, i_name)\
155{\
156public:\
157 enum { lvalue = false };\
158 typedef typename util::CallTraits<T>::TValue TValue;\
159 typedef size_t TSize;\
160 LASS_CONCATENATE(V, i_name)(const Operand1& iA): operand1_(iA) {}\
161 TValue operator[](TSize iIndex) const { return c_operator operand1_[iIndex]; }\
162 TSize size() const { return operand1_.size(); }\
163private:\
164 typename VectorExpressionTraits<Operand1>::TStorage operand1_;\
165}
166
167LASS_NUM_VECTOR_UNARY_EXPRESSION(Neg, -);
168LASS_NUM_VECTOR_UNARY_EXPRESSION(Rec, ::lass::num::NumTraits<T>::one /);
169
170
171
172/** @internal
173 */
174template <typename T, typename Operand1>
175class VFun
176{
177public:
178 enum { lvalue = false };
179 typedef T (*TOperator)(T);
180 typedef typename util::CallTraits<T>::TValue TValue;
181 typedef size_t TSize;
182 VFun(const Operand1& iA, TOperator iOperator): operand1_(iA), operator_(iOperator) {}
183 TValue operator[](TSize iIndex) const { return operator_(operand1_[iIndex]); }
184 TSize size() const { return operand1_.size(); }
185private:
186 typename VectorExpressionTraits<Operand1>::TStorage operand1_;
187 TOperator operator_;
188};
189
190
191
192
193}
194
195}
196
197}
198
199#endif
200
201// EOF
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53