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 /* For integration into the expression template code: */
46 typedef vector_type expr_type
;
48 /* For integration into the expression template code: */
49 typedef vector
< Element
,fixed
<Size
> > temporary_type
;
50 typedef typename
temporary_type::subvector_type subvector_type
;
51 /* Note: this ensures that an external vector is copied into the proper
52 * temporary; external<> temporaries are not allowed.
56 typedef typename
array_type::value_type value_type
;
57 typedef typename
array_type::reference reference
;
58 typedef typename
array_type::const_reference const_reference
;
60 /* For integration into the expression templates code: */
61 typedef vector_type
& expr_reference
;
62 typedef const vector_type
& expr_const_reference
;
64 /* For matching by storage type: */
65 typedef typename
array_type::memory_tag memory_tag
;
67 /* For matching by size type: */
68 typedef typename
array_type::size_tag size_tag
;
70 /* For matching by result-type: */
71 typedef cml::et::vector_result_tag result_tag
;
73 /* For matching by assignability: */
74 typedef cml::et::assignable_tag assignable_tag
;
79 /** Return square of the length. */
80 value_type
length_squared() const {
81 return cml::dot(*this,*this);
84 /** Return the length. */
85 value_type
length() const {
86 return std::sqrt(length_squared());
89 /** Normalize the vector. */
90 vector_type
& normalize() {
91 return (*this /= length());
94 /** Set this vector to [0]. */
96 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
97 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
101 /** Set this vector to a cardinal vector. */
102 vector_type
& cardinal(size_t i
) {
104 (*this)[i
] = Element(1);
108 /** Pairwise minimum of this vector with another. */
109 template<typename E
, class AT
>
110 void minimize(const vector
<E
,AT
>& v
) {
111 /* XXX This should probably use ScalarPromote: */
112 for (size_t i
= 0; i
< this->size(); ++i
) {
113 (*this)[i
] = std::min((*this)[i
],v
[i
]);
117 /** Pairwise maximum of this vector with another. */
118 template<typename E
, class AT
>
119 void maximize(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::max((*this)[i
],v
[i
]);
126 /** Fill vector with random elements. */
127 void random(value_type min
, value_type max
) {
128 for (size_t i
= 0; i
< this->size(); ++i
) {
129 (*this)[i
] = cml::random_real(min
,max
);
136 /** Construct from an array of values. */
137 vector(Element
* const array
) : array_type(array
) {}
146 CML_VEC_ASSIGN_FROM_VECTYPE
148 /* Only assignment operators can be used to copy from other types: */
149 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign
)
150 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign
)
151 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign
)
153 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign
)
154 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign
)
155 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign
)
157 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign
)
158 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign
)
161 /** Run-time sized vector. */
162 template<typename Element
>
163 class vector
< Element
, external
<> >
164 : public external_1D
<Element
>
168 /* Shorthand for the generator: */
169 typedef external
<> storage_type
;
170 typedef external
<> generator_type
;
172 /* Shorthand for the array type: */
173 typedef external_1D
<Element
> array_type
;
175 /* Shorthand for the type of this vector: */
176 typedef vector
<Element
,generator_type
> vector_type
;
178 /* For integration into the expression template code: */
179 typedef vector_type expr_type
;
181 /* For integration into the expression template code: */
182 typedef vector
< Element
, dynamic
<> > temporary_type
;
183 /* Note: this ensures that an external vector is copied into the proper
184 * temporary; external<> temporaries are not allowed.
188 typedef typename
array_type::value_type value_type
;
189 typedef typename
array_type::reference reference
;
190 typedef typename
array_type::const_reference const_reference
;
192 /* For integration into the expression templates code: */
193 typedef vector_type
& expr_reference
;
194 typedef const vector_type
& expr_const_reference
;
196 /* For matching by storage type: */
197 typedef typename
array_type::memory_tag memory_tag
;
199 /* For matching by size type: */
200 typedef typename
array_type::size_tag size_tag
;
202 /* For matching by resizability: */
203 typedef typename
array_type::resizing_tag resizing_tag
;
205 /* For matching by result-type: */
206 typedef cml::et::vector_result_tag result_tag
;
208 /* For matching by assignability: */
209 typedef cml::et::assignable_tag assignable_tag
;
214 /** Return square of the length. */
215 value_type
length_squared() const {
216 return dot(*this,*this);
219 /** Return the length. */
220 value_type
length() const {
221 return std::sqrt(length_squared());
224 /** Normalize the vector. */
225 vector_type
& normalize() {
226 return (*this /= length());
229 /** Set this vector to [0]. */
230 vector_type
& zero() {
231 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
232 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
236 /** Set this vector to a cardinal vector. */
237 vector_type
& cardinal(size_t i
) {
239 (*this)[i
] = Element(1);
243 /** Pairwise minimum of this vector with another. */
244 template<typename E
, class AT
>
245 void minimize(const vector
<E
,AT
>& v
) {
246 /* XXX This should probably use ScalarPromote: */
247 for (size_t i
= 0; i
< this->size(); ++i
) {
248 (*this)[i
] = std::min((*this)[i
],v
[i
]);
252 /** Pairwise maximum of this vector with another. */
253 template<typename E
, class AT
>
254 void maximize(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::max((*this)[i
],v
[i
]);
261 /** Fill vector with random elements. */
262 void random(value_type min
, value_type max
) {
263 for (size_t i
= 0; i
< this->size(); ++i
) {
264 (*this)[i
] = random_real(min
,max
);
271 /** Construct from an array of values and the size. */
272 vector(Element
* const array
, size_t size
)
273 : array_type(array
, size
) {}
278 /* Define class operators for external vectors. Note: external vectors
279 * cannot be copy-constructed, but they can be assigned to:
285 CML_VEC_ASSIGN_FROM_VECTYPE
287 /* Only assignment operators can be used to copy from other types: */
288 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign
)
289 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign
)
290 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign
)
292 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign
)
293 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign
)
294 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign
)
296 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign
)
297 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign
)
304 // -------------------------------------------------------------------------