]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/vector/external.h
cml version bump to 1.0.1
[chaz/yoink] / src / Moof / cml / vector / external.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 Specializations for external-memory vectors.
11 *
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.
15 */
16
17 #ifndef external_vector_h
18 #define external_vector_h
19
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>
25
26 namespace cml {
27
28 /** Fixed-size, fixed-memory vector. */
29 template<typename Element, int Size>
30 class vector< Element, external<Size> >
31 : public external_1D<Element,Size>
32 {
33 public:
34
35 /* Shorthand for the generator: */
36 typedef external<> storage_type;
37 typedef external<Size> generator_type;
38
39 /* Shorthand for the array type: */
40 typedef external_1D<Element,Size> array_type;
41
42 /* Shorthand for the type of this vector: */
43 typedef vector<Element,generator_type> vector_type;
44
45 /* The vector coordinate type: */
46 typedef Element coordinate_type;
47
48 /* For integration into the expression template code: */
49 typedef vector_type expr_type;
50
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.
56 */
57
58 /* Standard: */
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;
62
63 /* For integration into the expression templates code: */
64 typedef vector_type& expr_reference;
65 typedef const vector_type& expr_const_reference;
66
67 /* For matching by storage type: */
68 typedef typename array_type::memory_tag memory_tag;
69
70 /* For matching by size type: */
71 typedef typename array_type::size_tag size_tag;
72
73 /* For matching by result-type: */
74 typedef cml::et::vector_result_tag result_tag;
75
76 /* For matching by assignability: */
77 typedef cml::et::assignable_tag assignable_tag;
78
79
80 public:
81
82 /** Static constant containing the vector's space dimension. */
83 enum { dimension = Size };
84
85
86 public:
87
88 /** Return square of the length. */
89 value_type length_squared() const {
90 return cml::dot(*this,*this);
91 }
92
93 /** Return the length. */
94 value_type length() const {
95 return std::sqrt(length_squared());
96 }
97
98 /** Normalize the vector. */
99 vector_type& normalize() {
100 return (*this /= length());
101 }
102
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));
107 return *this;
108 }
109
110 /** Set this vector to a cardinal vector. */
111 vector_type& cardinal(size_t i) {
112 zero();
113 (*this)[i] = Element(1);
114 return *this;
115 }
116
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]);
123 }
124 }
125
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]);
132 }
133 }
134
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);
139 }
140 }
141
142
143 public:
144
145 /** Construct from an array of values. */
146 vector(Element* const array) : array_type(array) {}
147
148
149 public:
150
151 CML_ASSIGN_VEC_2
152 CML_ASSIGN_VEC_3
153 CML_ASSIGN_VEC_4
154
155 CML_VEC_ASSIGN_FROM_VECTYPE
156
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)
161
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)
165
166 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
167 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
168 };
169
170 /** Run-time sized vector. */
171 template<typename Element>
172 class vector< Element, external<> >
173 : public external_1D<Element>
174 {
175 public:
176
177 /* Shorthand for the generator: */
178 typedef external<> storage_type;
179 typedef external<> generator_type;
180
181 /* Shorthand for the array type: */
182 typedef external_1D<Element> array_type;
183
184 /* Shorthand for the type of this vector: */
185 typedef vector<Element,generator_type> vector_type;
186
187 /* For integration into the expression template code: */
188 typedef vector_type expr_type;
189
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.
194 */
195
196 /* Standard: */
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;
200
201 /* For integration into the expression templates code: */
202 typedef vector_type& expr_reference;
203 typedef const vector_type& expr_const_reference;
204
205 /* For matching by storage type: */
206 typedef typename array_type::memory_tag memory_tag;
207
208 /* For matching by size type: */
209 typedef typename array_type::size_tag size_tag;
210
211 /* For matching by resizability: */
212 typedef typename array_type::resizing_tag resizing_tag;
213
214 /* For matching by result-type: */
215 typedef cml::et::vector_result_tag result_tag;
216
217 /* For matching by assignability: */
218 typedef cml::et::assignable_tag assignable_tag;
219
220
221 public:
222
223 /** Return square of the length. */
224 value_type length_squared() const {
225 return dot(*this,*this);
226 }
227
228 /** Return the length. */
229 value_type length() const {
230 return std::sqrt(length_squared());
231 }
232
233 /** Normalize the vector. */
234 vector_type& normalize() {
235 return (*this /= length());
236 }
237
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));
242 return *this;
243 }
244
245 /** Set this vector to a cardinal vector. */
246 vector_type& cardinal(size_t i) {
247 zero();
248 (*this)[i] = Element(1);
249 return *this;
250 }
251
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]);
258 }
259 }
260
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]);
267 }
268 }
269
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);
274 }
275 }
276
277
278 public:
279
280 /** Construct from an array of values and the size. */
281 vector(Element* const array, size_t size)
282 : array_type(array, size) {}
283
284
285 public:
286
287 /* Define class operators for external vectors. Note: external vectors
288 * cannot be copy-constructed, but they can be assigned to:
289 */
290 CML_ASSIGN_VEC_2
291 CML_ASSIGN_VEC_3
292 CML_ASSIGN_VEC_4
293
294 CML_VEC_ASSIGN_FROM_VECTYPE
295
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)
300
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)
304
305 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
306 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
307 };
308
309 } // namespace cml
310
311 #endif
312
313 // -------------------------------------------------------------------------
314 // vim:ft=cpp
This page took 0.051778 seconds and 5 git commands to generate.