]> Dogcows Code - chaz/yoink/blob - src/moof/cml/vector/vector_expr.h
the massive refactoring effort
[chaz/yoink] / src / moof / cml / vector / vector_expr.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief Vector linear expression classes.
11 */
12
13 #ifndef vector_expr_h
14 #define vector_expr_h
15
16 #include <cmath>
17 #include <cml/et/size_checking.h>
18 #include <cml/vector/vector_traits.h>
19 #include <cml/vector/vector_promotions.h>
20
21 /* XXX Don't know which it should be just yet, since RVO seems to obviate need
22 * for a reference type. However, copy by value copies the *entire expression
23 * tree rooted at the VectorXpr<>, so this choice is bound to affect
24 * performace for some compiler or another:
25 */
26 #define VECXPR_ARG_TYPE const et::VectorXpr<XprT>&
27 #define VECXPR_ARG_TYPE_N(_N_) const et::VectorXpr<XprT##_N_>&
28
29 //#define VECXPR_ARG_TYPE const et::VectorXpr<XprT>
30 //#define VECXPR_ARG_TYPE_N(_N_) const et::VectorXpr<XprT##_N_>
31
32 namespace cml {
33 namespace et {
34
35 /** A placeholder for a vector expression in an expression tree. */
36 template<class ExprT>
37 class VectorXpr
38 {
39 public:
40
41 typedef VectorXpr<ExprT> expr_type;
42
43 /* Record ary-ness of the expression: */
44 typedef typename ExprT::expr_ary expr_ary;
45
46 /* Copy the expression by value into higher-up expressions: */
47 typedef expr_type expr_const_reference;
48
49 typedef typename ExprT::value_type value_type;
50 typedef vector_result_tag result_tag;
51 typedef typename ExprT::size_tag size_tag;
52
53 /* Store the expression traits: */
54 typedef ExprTraits<ExprT> expr_traits;
55
56 /* Get the reference type: */
57 typedef typename expr_traits::const_reference expr_reference;
58
59 /* Get the result type: */
60 typedef typename expr_traits::result_type result_type;
61
62 /* For matching by assignability: */
63 typedef cml::et::not_assignable_tag assignable_tag;
64
65 /* Get the temporary type: */
66 typedef typename result_type::temporary_type temporary_type;
67
68
69 public:
70
71 /** Record result size as an enum. */
72 enum { array_size = ExprT::array_size };
73
74
75 public:
76
77 /** Return square of the length. */
78 value_type length_squared() const {
79 return m_expr.length_squared();
80 }
81
82 /** Return the length. */
83 value_type length() const {
84 return m_expr.length();
85 }
86
87 /** Return the result as a normalized vector. */
88 result_type normalize() const {
89 return m_expr.normalize();
90 }
91
92 /** Compute value at index i of the result vector. */
93 value_type operator[](size_t i) const {
94 return m_expr[i];
95 }
96
97
98 public:
99
100 /** Return size of this expression (same as subexpression's size). */
101 size_t size() const {
102 return m_expr.size();
103 }
104
105 /** Return reference to contained expression. */
106 expr_reference expression() const { return m_expr; }
107
108
109 public:
110
111 /** Construct from the subexpression to store. */
112 explicit VectorXpr(expr_reference expr) : m_expr(expr) {}
113
114 /** Copy constructor. */
115 VectorXpr(const expr_type& e) : m_expr(e.m_expr) {}
116
117
118 protected:
119
120 expr_reference m_expr;
121
122
123 private:
124
125 /* Cannot be assigned to: */
126 expr_type& operator=(const expr_type&);
127 };
128
129 /** Expression traits class for VectorXpr<>. */
130 template<class ExprT>
131 struct ExprTraits< VectorXpr<ExprT> >
132 {
133 typedef VectorXpr<ExprT> expr_type;
134 typedef ExprT arg_type;
135 typedef typename expr_type::value_type value_type;
136 typedef typename expr_type::expr_const_reference const_reference;
137 typedef typename expr_type::result_tag result_tag;
138 typedef typename expr_type::size_tag size_tag;
139 typedef typename expr_type::result_type result_type;
140 typedef typename expr_type::assignable_tag assignable_tag;
141 typedef expr_node_tag node_tag;
142
143 value_type get(const expr_type& v, size_t i) const { return v[i]; }
144 size_t size(const expr_type& e) const { return e.size(); }
145 };
146
147
148 /** A unary vector expression.
149 *
150 * The operator's operator() method must take exactly one argument.
151 */
152 template<class ExprT, class OpT>
153 class UnaryVectorOp
154 {
155 public:
156
157 typedef UnaryVectorOp<ExprT,OpT> expr_type;
158
159 /* Record ary-ness of the expression: */
160 typedef unary_expression expr_ary;
161
162 /* Copy the expression by value into higher-up expressions: */
163 typedef expr_type expr_const_reference;
164
165 typedef typename OpT::value_type value_type;
166 typedef vector_result_tag result_tag;
167 typedef typename ExprT::size_tag size_tag;
168
169 /* Store the expression traits for the subexpression: */
170 typedef ExprTraits<ExprT> expr_traits;
171
172 /* Reference type for the subexpression: */
173 typedef typename expr_traits::const_reference expr_reference;
174
175 /* Get the result type (same as for subexpression): */
176 typedef typename expr_traits::result_type result_type;
177
178 /* For matching by assignability: */
179 typedef cml::et::not_assignable_tag assignable_tag;
180
181 /* Get the temporary type: */
182 typedef typename result_type::temporary_type temporary_type;
183
184
185 public:
186
187 /** Record result size as an enum. */
188 enum { array_size = ExprT::array_size };
189
190
191 public:
192
193 /** Return square of the length. */
194 value_type length_squared() const {
195 return dot(
196 VectorXpr<expr_type>(*this),
197 VectorXpr<expr_type>(*this));
198 }
199
200 /** Return the length. */
201 value_type length() const {
202 return std::sqrt(length_squared());
203 }
204
205 /** Return the result as a normalized vector. */
206 result_type normalize() const {
207 result_type v(VectorXpr<expr_type>(*this));
208 return v.normalize();
209 }
210
211 /** Compute value at index i of the result vector. */
212 value_type operator[](size_t i) const {
213
214 /* This uses the expression traits to figure out how to access the
215 * i'th index of the subexpression:
216 */
217 return OpT().apply(expr_traits().get(m_expr,i));
218 }
219
220
221 public:
222
223 /** Return size of this expression (same as argument's size). */
224 size_t size() const {
225 return m_expr.size();
226 }
227
228 /** Return reference to contained expression. */
229 expr_reference expression() const { return m_expr; }
230
231
232 public:
233
234 /** Construct from the subexpression. */
235 explicit UnaryVectorOp(expr_reference expr) : m_expr(expr) {}
236
237 /** Copy constructor. */
238 UnaryVectorOp(const expr_type& e) : m_expr(e.m_expr) {}
239
240
241 protected:
242
243 expr_reference m_expr;
244
245
246 private:
247
248 /* Cannot be assigned to: */
249 expr_type& operator=(const expr_type&);
250 };
251
252 /** Expression traits class for UnaryVectorOp<>. */
253 template<class ExprT, class OpT>
254 struct ExprTraits< UnaryVectorOp<ExprT,OpT> >
255 {
256 typedef UnaryVectorOp<ExprT,OpT> expr_type;
257 typedef ExprT arg_type;
258
259 typedef typename expr_type::value_type value_type;
260 typedef typename expr_type::expr_const_reference const_reference;
261 typedef typename expr_type::result_tag result_tag;
262 typedef typename expr_type::size_tag size_tag;
263 typedef typename expr_type::result_type result_type;
264 typedef typename expr_type::assignable_tag assignable_tag;
265 typedef expr_node_tag node_tag;
266
267 value_type get(const expr_type& v, size_t i) const { return v[i]; }
268 size_t size(const expr_type& e) const { return e.size(); }
269 };
270
271
272 /** A binary vector expression.
273 *
274 * The operator's operator() method must take exactly two arguments.
275 */
276 template<class LeftT, class RightT, class OpT>
277 class BinaryVectorOp
278 {
279 public:
280
281 typedef BinaryVectorOp<LeftT,RightT,OpT> expr_type;
282
283 /* Record ary-ness of the expression: */
284 typedef binary_expression expr_ary;
285
286 /* Copy the expression by value into higher-up expressions: */
287 typedef expr_type expr_const_reference;
288
289 typedef typename OpT::value_type value_type;
290 typedef vector_result_tag result_tag;
291
292 /* Store the expression traits types for the two subexpressions: */
293 typedef ExprTraits<LeftT> left_traits;
294 typedef ExprTraits<RightT> right_traits;
295
296 /* Reference types for the two subexpressions: */
297 typedef typename left_traits::const_reference left_reference;
298 typedef typename right_traits::const_reference right_reference;
299
300 /* Figure out the expression's resulting (vector) type: */
301 typedef typename left_traits::result_type left_result;
302 typedef typename right_traits::result_type right_result;
303 typedef typename VectorPromote<left_result,right_result>::type result_type;
304 typedef typename result_type::size_tag size_tag;
305
306 /* For matching by assignability: */
307 typedef cml::et::not_assignable_tag assignable_tag;
308
309 /* Get the temporary type: */
310 typedef typename result_type::temporary_type temporary_type;
311
312 /* Define a size checker: */
313 typedef GetCheckedSize<LeftT,RightT,size_tag> checked_size;
314
315
316 public:
317
318 /** Record result size as an enum (if applicable). */
319 enum { array_size = result_type::array_size };
320
321
322 public:
323
324 /** Return square of the length. */
325 value_type length_squared() const {
326 return dot(
327 VectorXpr<expr_type>(*this),
328 VectorXpr<expr_type>(*this));
329 }
330
331 /** Return the length. */
332 value_type length() const {
333 return std::sqrt(length_squared());
334 }
335
336 /** Return the result as a normalized vector. */
337 result_type normalize() const {
338 result_type v(VectorXpr<expr_type>(*this));
339 return v.normalize();
340 }
341
342 /** Compute value at index i of the result vector. */
343 value_type operator[](size_t i) const {
344
345 /* This uses the expression traits to figure out how to access the
346 * i'th index of the two subexpressions:
347 */
348 return OpT().apply(
349 left_traits().get(m_left,i),
350 right_traits().get(m_right,i));
351 }
352
353
354 public:
355
356 /** Return the size of the vector result.
357 *
358 * @throws std::invalid_argument if the expressions do not have the same
359 * size.
360 */
361 size_t size() const {
362 /* Note: This actually does a check only if
363 * CML_CHECK_VECTOR_EXPR_SIZES is set:
364 */
365 return CheckedSize(m_left,m_right,size_tag());
366 }
367
368 /** Return reference to left expression. */
369 left_reference left_expression() const { return m_left; }
370
371 /** Return reference to right expression. */
372 right_reference right_expression() const { return m_right; }
373
374
375 public:
376
377 /** Construct from the two subexpressions. */
378 explicit BinaryVectorOp(left_reference left, right_reference right)
379 : m_left(left), m_right(right) {}
380
381 /** Copy constructor. */
382 BinaryVectorOp(const expr_type& e)
383 : m_left(e.m_left), m_right(e.m_right) {}
384
385
386 protected:
387
388 left_reference m_left;
389 right_reference m_right;
390
391
392 private:
393
394 /* This ensures that a compile-time size check is executed: */
395 typename checked_size::check_type _dummy;
396
397
398 private:
399
400 /* Cannot be assigned to: */
401 expr_type& operator=(const expr_type&);
402 };
403
404 /** Expression traits class for BinaryVectorOp<>. */
405 template<class LeftT, class RightT, class OpT>
406 struct ExprTraits< BinaryVectorOp<LeftT,RightT,OpT> >
407 {
408 typedef BinaryVectorOp<LeftT,RightT,OpT> expr_type;
409 typedef LeftT left_type;
410 typedef RightT right_type;
411
412 typedef typename expr_type::value_type value_type;
413 typedef typename expr_type::expr_const_reference const_reference;
414 typedef typename expr_type::result_tag result_tag;
415 typedef typename expr_type::size_tag size_tag;
416 typedef typename expr_type::result_type result_type;
417 typedef typename expr_type::assignable_tag assignable_tag;
418 typedef expr_node_tag node_tag;
419
420 value_type get(const expr_type& v, size_t i) const { return v[i]; }
421 size_t size(const expr_type& e) const { return e.size(); }
422 };
423
424 /* Helper struct to verify that both arguments are vector expressions: */
425 template<typename LeftTraits, typename RightTraits>
426 struct VectorExpressions
427 {
428 /* Require that both arguments are vector expressions: */
429 typedef typename LeftTraits::result_tag left_result;
430 typedef typename RightTraits::result_tag right_result;
431 enum { is_true = (same_type<left_result,et::vector_result_tag>::is_true
432 && same_type<right_result,et::vector_result_tag>::is_true) };
433 };
434
435 namespace detail {
436
437 template<typename VecT, typename RT, typename MT> inline
438 void Resize(VecT&,size_t,RT,MT) {}
439
440 template<typename VecT> inline
441 void Resize(VecT& v, size_t S, resizable_tag, dynamic_memory_tag) {
442 v.resize(S);
443 }
444
445 template<typename VecT> inline
446 void Resize(VecT& v, size_t S) {
447 Resize(v, S, typename VecT::resizing_tag(), typename VecT::memory_tag());
448 }
449
450 } // namespace detail
451
452 } // namespace et
453 } // namespace cml
454
455 #endif
456
457 // -------------------------------------------------------------------------
458 // vim:ft=cpp
This page took 0.054564 seconds and 5 git commands to generate.