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 Specializations for external-memory vectors.
12 * @note Copy-constructing one external<> vector from another is not
13 * supported, since an external<> vector is essentially a wrapper for a
14 * pointer and has no allocated storage of its own.
17 #ifndef external_vector_h
18 #define external_vector_h
20 #include <cml/core/external_1D.h>
21 #include <cml/vector/vector_expr.h>
22 #include <cml/vector/class_ops.h>
23 #include <cml/vector/vector_unroller.h>
24 #include <cml/vector/dynamic.h>
28 /** Fixed-size, fixed-memory vector. */
29 template<typename Element
, int Size
>
30 class vector
< Element
, external
<Size
> >
31 : public external_1D
<Element
,Size
>
35 /* Shorthand for the generator: */
36 typedef external
<> storage_type
;
37 typedef external
<Size
> generator_type
;
39 /* Shorthand for the array type: */
40 typedef external_1D
<Element
,Size
> array_type
;
42 /* Shorthand for the type of this vector: */
43 typedef vector
<Element
,generator_type
> vector_type
;
45 /* The vector coordinate type: */
46 typedef Element coordinate_type
;
48 /* For integration into the expression template code: */
49 typedef vector_type expr_type
;
51 /* For integration into the expression template code: */
52 typedef vector
< Element
,fixed
<Size
> > temporary_type
;
53 typedef typename
temporary_type::subvector_type subvector_type
;
54 /* Note: this ensures that an external vector is copied into the proper
55 * temporary; external<> temporaries are not allowed.
59 typedef typename
array_type::value_type value_type
;
60 typedef typename
array_type::reference reference
;
61 typedef typename
array_type::const_reference const_reference
;
63 /* For integration into the expression templates code: */
64 typedef vector_type
& expr_reference
;
65 typedef const vector_type
& expr_const_reference
;
67 /* For matching by storage type: */
68 typedef typename
array_type::memory_tag memory_tag
;
70 /* For matching by size type: */
71 typedef typename
array_type::size_tag size_tag
;
73 /* For matching by result-type: */
74 typedef cml::et::vector_result_tag result_tag
;
76 /* For matching by assignability: */
77 typedef cml::et::assignable_tag assignable_tag
;
82 /** Static constant containing the vector's space dimension. */
83 enum { dimension
= Size
};
88 /** Return square of the length. */
89 value_type
length_squared() const {
90 return cml::dot(*this,*this);
93 /** Return the length. */
94 value_type
length() const {
95 return std::sqrt(length_squared());
98 /** Normalize the vector. */
99 vector_type
& normalize() {
100 return (*this /= length());
103 /** Set this vector to [0]. */
104 vector_type
& zero() {
105 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
106 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
110 /** Set this vector to a cardinal vector. */
111 vector_type
& cardinal(size_t i
) {
113 (*this)[i
] = Element(1);
117 /** Pairwise minimum of this vector with another. */
118 template<typename E
, class AT
>
119 void minimize(const vector
<E
,AT
>& v
) {
120 /* XXX This should probably use ScalarPromote: */
121 for (size_t i
= 0; i
< this->size(); ++i
) {
122 (*this)[i
] = std::min((*this)[i
],v
[i
]);
126 /** Pairwise maximum of this vector with another. */
127 template<typename E
, class AT
>
128 void maximize(const vector
<E
,AT
>& v
) {
129 /* XXX This should probably use ScalarPromote: */
130 for (size_t i
= 0; i
< this->size(); ++i
) {
131 (*this)[i
] = std::max((*this)[i
],v
[i
]);
135 /** Fill vector with random elements. */
136 void random(value_type min
, value_type max
) {
137 for (size_t i
= 0; i
< this->size(); ++i
) {
138 (*this)[i
] = cml::random_real(min
,max
);
145 /** Construct from an array of values. */
146 vector(Element
* const array
) : array_type(array
) {}
155 CML_VEC_ASSIGN_FROM_VECTYPE
157 /* Only assignment operators can be used to copy from other types: */
158 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign
)
159 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign
)
160 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign
)
162 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign
)
163 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign
)
164 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign
)
166 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign
)
167 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign
)
170 /** Run-time sized vector. */
171 template<typename Element
>
172 class vector
< Element
, external
<> >
173 : public external_1D
<Element
>
177 /* Shorthand for the generator: */
178 typedef external
<> storage_type
;
179 typedef external
<> generator_type
;
181 /* Shorthand for the array type: */
182 typedef external_1D
<Element
> array_type
;
184 /* Shorthand for the type of this vector: */
185 typedef vector
<Element
,generator_type
> vector_type
;
187 /* For integration into the expression template code: */
188 typedef vector_type expr_type
;
190 /* For integration into the expression template code: */
191 typedef vector
< Element
, dynamic
<> > temporary_type
;
192 /* Note: this ensures that an external vector is copied into the proper
193 * temporary; external<> temporaries are not allowed.
197 typedef typename
array_type::value_type value_type
;
198 typedef typename
array_type::reference reference
;
199 typedef typename
array_type::const_reference const_reference
;
201 /* For integration into the expression templates code: */
202 typedef vector_type
& expr_reference
;
203 typedef const vector_type
& expr_const_reference
;
205 /* For matching by storage type: */
206 typedef typename
array_type::memory_tag memory_tag
;
208 /* For matching by size type: */
209 typedef typename
array_type::size_tag size_tag
;
211 /* For matching by resizability: */
212 typedef typename
array_type::resizing_tag resizing_tag
;
214 /* For matching by result-type: */
215 typedef cml::et::vector_result_tag result_tag
;
217 /* For matching by assignability: */
218 typedef cml::et::assignable_tag assignable_tag
;
223 /** Return square of the length. */
224 value_type
length_squared() const {
225 return dot(*this,*this);
228 /** Return the length. */
229 value_type
length() const {
230 return std::sqrt(length_squared());
233 /** Normalize the vector. */
234 vector_type
& normalize() {
235 return (*this /= length());
238 /** Set this vector to [0]. */
239 vector_type
& zero() {
240 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
241 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
245 /** Set this vector to a cardinal vector. */
246 vector_type
& cardinal(size_t i
) {
248 (*this)[i
] = Element(1);
252 /** Pairwise minimum of this vector with another. */
253 template<typename E
, class AT
>
254 void minimize(const vector
<E
,AT
>& v
) {
255 /* XXX This should probably use ScalarPromote: */
256 for (size_t i
= 0; i
< this->size(); ++i
) {
257 (*this)[i
] = std::min((*this)[i
],v
[i
]);
261 /** Pairwise maximum of this vector with another. */
262 template<typename E
, class AT
>
263 void maximize(const vector
<E
,AT
>& v
) {
264 /* XXX This should probably use ScalarPromote: */
265 for (size_t i
= 0; i
< this->size(); ++i
) {
266 (*this)[i
] = std::max((*this)[i
],v
[i
]);
270 /** Fill vector with random elements. */
271 void random(value_type min
, value_type max
) {
272 for (size_t i
= 0; i
< this->size(); ++i
) {
273 (*this)[i
] = random_real(min
,max
);
280 /** Construct from an array of values and the size. */
281 vector(Element
* const array
, size_t size
)
282 : array_type(array
, size
) {}
287 /* Define class operators for external vectors. Note: external vectors
288 * cannot be copy-constructed, but they can be assigned to:
294 CML_VEC_ASSIGN_FROM_VECTYPE
296 /* Only assignment operators can be used to copy from other types: */
297 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign
)
298 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign
)
299 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign
)
301 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign
)
302 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign
)
303 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign
)
305 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign
)
306 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign
)
313 // -------------------------------------------------------------------------