]>
Dogcows Code - chaz/yoink/blob - cml/vector/class_ops.h
1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
10 * @brief Vector class operators.
13 #ifndef vector_class_ops_h
14 #define vector_class_ops_h
16 #if defined(_MSC_VER) && _MSC_VER < 1400
17 #pragma warning(disable:4003)
18 // XXX Horrible hack to turn off warnings about "not enough actual params"
19 // for the macros below.
22 /* XXX HACK!!! This is a hack to resize in the assign() functions only when
23 * auto resizing is turned off.
25 #if !defined(CML_VECTOR_RESIZE_ON_ASSIGNMENT)
26 #define _DO_VECTOR_SET_RESIZE(_N_) cml::et::detail::Resize(*this,_N_)
28 #define _DO_VECTOR_SET_RESIZE(_N_)
31 /** Set a vector from 2 values. */
32 #define CML_ASSIGN_VEC_2 \
34 set(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) { \
35 _DO_VECTOR_SET_RESIZE(2); \
36 /* This is overkill, but simplifies size checking: */ \
37 value_type v[] = {e0,e1}; \
38 typedef et::OpAssign<Element,Element> OpT; \
39 cml::vector< const value_type, external<2> > src(v); \
40 et::UnrollAssignment<OpT>(*this,src); \
44 /** Set a vector from 3 values. */
45 #define CML_ASSIGN_VEC_3 \
48 ELEMENT_ARG_TYPE e0, \
49 ELEMENT_ARG_TYPE e1, \
53 _DO_VECTOR_SET_RESIZE(3); \
54 /* This is overkill, but simplifies size checking: */ \
55 value_type v[] = {e0,e1,e2}; \
56 typedef et::OpAssign<Element,Element> OpT; \
57 cml::vector< const value_type, external<3> > src(v); \
58 et::UnrollAssignment<OpT>(*this,src); \
62 /** Create a vector from 4 values. */
63 #define CML_ASSIGN_VEC_4 \
66 ELEMENT_ARG_TYPE e0, \
67 ELEMENT_ARG_TYPE e1, \
68 ELEMENT_ARG_TYPE e2, \
72 _DO_VECTOR_SET_RESIZE(4); \
73 /* This is overkill, but simplifies size checking: */ \
74 value_type v[] = {e0,e1,e2,e3}; \
75 typedef et::OpAssign<Element,Element> OpT; \
76 cml::vector< const value_type, external<4> > src(v); \
77 et::UnrollAssignment<OpT>(*this,src); \
82 /** Create a vector from 2 values. */
83 #define CML_CONSTRUCT_VEC_2(_add_) \
84 vector(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) _add_ { \
88 /** Create a vector from 3 values. */
89 #define CML_CONSTRUCT_VEC_3(_add_) \
91 ELEMENT_ARG_TYPE e0, \
92 ELEMENT_ARG_TYPE e1, \
99 /** Create a vector from 4 values. */
100 #define CML_CONSTRUCT_VEC_4(_add_) \
102 ELEMENT_ARG_TYPE e0, \
103 ELEMENT_ARG_TYPE e1, \
104 ELEMENT_ARG_TYPE e2, \
105 ELEMENT_ARG_TYPE e3 \
111 /** Create a (fixed-size) N vector from an N-1-vector and a scalar. */
112 #define CML_CONSTRUCT_FROM_SUBVEC(_add_) \
114 const subvector_type& s, \
118 _DO_VECTOR_SET_RESIZE(s.size()+1); \
119 for(size_t i = 0; i < s.size(); ++ i) \
121 (*this)[s.size()] = e; \
124 /** Copy-construct a vector from a fixed-size array of values. */
125 #define CML_VEC_COPY_FROM_FIXED_ARRAY(_N_,_add_) \
126 vector(const value_type v[_N_]) _add_ { \
127 typedef et::OpAssign<Element,Element> OpT; \
128 cml::vector< const value_type, external<_N_> > src(v); \
129 et::UnrollAssignment<OpT>(*this,src); \
132 /** Copy-construct a vector from a runtime-sized array of values. */
133 #define CML_VEC_COPY_FROM_ARRAY(_add_) \
134 vector(const value_type* const v, size_t N) _add_ { \
135 typedef et::OpAssign<Element,Element> OpT; \
136 cml::vector<const value_type, external<> > src(v,N); \
137 et::UnrollAssignment<OpT>(*this,src); \
140 /** Copy-construct a vector.
142 * @internal This is required for GCC4, since it won't elide the default
145 #define CML_VEC_COPY_FROM_VECTYPE(_add_) \
146 vector(const vector_type& v) _add_ { \
147 typedef et::OpAssign<Element,Element> OpT; \
148 et::UnrollAssignment<OpT>(*this,v); \
151 /** Construct from an arbitrary vector.
153 * @param v the vector to copy from.
155 #define CML_VEC_COPY_FROM_VEC \
156 template<typename E, class AT> \
157 vector(const vector<E,AT>& m) { \
158 typedef et::OpAssign<Element,E> OpT; \
159 et::UnrollAssignment<OpT>(*this,m); \
162 /** Construct from a vector expression.
164 * @param expr the expression to copy from.
166 #define CML_VEC_COPY_FROM_VECXPR \
167 template<class XprT> \
168 vector(VECXPR_ARG_TYPE e) { \
169 /* Verify that a promotion exists at compile time: */ \
170 typedef typename et::VectorPromote< \
171 vector_type, typename XprT::result_type>::type result_type; \
172 typedef typename XprT::value_type src_value_type; \
173 typedef et::OpAssign<Element,src_value_type> OpT; \
174 et::UnrollAssignment<OpT>(*this,e); \
177 /** Assign from the same vector type.
179 * @param v the vector to copy from.
181 #define CML_VEC_ASSIGN_FROM_VECTYPE \
182 vector_type& operator=(const vector_type& v) { \
183 typedef et::OpAssign<Element,Element> OpT; \
184 et::UnrollAssignment<OpT>(*this,v); \
188 /** Assign this vector from another using the given elementwise op.
190 * This allows assignment from arbitrary vector types.
192 * @param _op_ the operator (e.g. +=)
193 * @param _op_name_ the op functor (e.g. et::OpAssign)
195 #define CML_VEC_ASSIGN_FROM_VEC(_op_, _op_name_) \
196 template<typename E, class AT> vector_type& \
197 operator _op_ (const cml::vector<E,AT>& m) { \
198 typedef _op_name_ <Element,E> OpT; \
199 cml::et::UnrollAssignment<OpT>(*this,m); \
203 /** Declare a function to assign this vector from a vector expression.
205 * @param _op_ the operator (e.g. +=)
206 * @param _op_name_ the op functor (e.g. et::OpAssign)
208 #define CML_VEC_ASSIGN_FROM_VECXPR(_op_, _op_name_) \
209 template<class XprT> vector_type& \
210 operator _op_ (VECXPR_ARG_TYPE e) { \
211 /* Verify that a promotion exists at compile time: */ \
212 typedef typename et::VectorPromote< \
213 vector_type, typename XprT::result_type>::type result_type; \
214 typedef typename XprT::value_type src_value_type; \
215 typedef _op_name_ <Element,src_value_type> OpT; \
216 cml::et::UnrollAssignment<OpT>(*this,e); \
220 /** Declare a function to assign this vector from a scalar.
222 * @param _op_ the operator (e.g. *=)
223 * @param _op_name_ the op functor (e.g. et::OpAssign)
225 * @internal This shouldn't be used for ops, like +=, which aren't
226 * defined in vector algebra.
228 #define CML_VEC_ASSIGN_FROM_SCALAR(_op_, _op_name_) \
229 vector_type& operator _op_ (ELEMENT_ARG_TYPE s) { \
230 typedef _op_name_ <Element,Element> OpT; \
231 cml::et::UnrollAssignment<OpT>(*this,s); \
237 // -------------------------------------------------------------------------
This page took 0.049638 seconds and 4 git commands to generate.