]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/vector/external.h
bc9bc3fe9d29cda003b989736fae7af0fc4e836d
[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 /* For integration into the expression template code: */
46 typedef vector_type expr_type;
47
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.
53 */
54
55 /* Standard: */
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;
59
60 /* For integration into the expression templates code: */
61 typedef vector_type& expr_reference;
62 typedef const vector_type& expr_const_reference;
63
64 /* For matching by storage type: */
65 typedef typename array_type::memory_tag memory_tag;
66
67 /* For matching by size type: */
68 typedef typename array_type::size_tag size_tag;
69
70 /* For matching by result-type: */
71 typedef cml::et::vector_result_tag result_tag;
72
73 /* For matching by assignability: */
74 typedef cml::et::assignable_tag assignable_tag;
75
76
77 public:
78
79 /** Return square of the length. */
80 value_type length_squared() const {
81 return cml::dot(*this,*this);
82 }
83
84 /** Return the length. */
85 value_type length() const {
86 return std::sqrt(length_squared());
87 }
88
89 /** Normalize the vector. */
90 vector_type& normalize() {
91 return (*this /= length());
92 }
93
94 /** Set this vector to [0]. */
95 vector_type& zero() {
96 typedef cml::et::OpAssign<Element,Element> OpT;
97 cml::et::UnrollAssignment<OpT>(*this,Element(0));
98 return *this;
99 }
100
101 /** Set this vector to a cardinal vector. */
102 vector_type& cardinal(size_t i) {
103 zero();
104 (*this)[i] = Element(1);
105 return *this;
106 }
107
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]);
114 }
115 }
116
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]);
123 }
124 }
125
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);
130 }
131 }
132
133
134 public:
135
136 /** Construct from an array of values. */
137 vector(Element* const array) : array_type(array) {}
138
139
140 public:
141
142 CML_ASSIGN_VEC_2
143 CML_ASSIGN_VEC_3
144 CML_ASSIGN_VEC_4
145
146 CML_VEC_ASSIGN_FROM_VECTYPE
147
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)
152
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)
156
157 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
158 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
159 };
160
161 /** Run-time sized vector. */
162 template<typename Element>
163 class vector< Element, external<> >
164 : public external_1D<Element>
165 {
166 public:
167
168 /* Shorthand for the generator: */
169 typedef external<> storage_type;
170 typedef external<> generator_type;
171
172 /* Shorthand for the array type: */
173 typedef external_1D<Element> array_type;
174
175 /* Shorthand for the type of this vector: */
176 typedef vector<Element,generator_type> vector_type;
177
178 /* For integration into the expression template code: */
179 typedef vector_type expr_type;
180
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.
185 */
186
187 /* Standard: */
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;
191
192 /* For integration into the expression templates code: */
193 typedef vector_type& expr_reference;
194 typedef const vector_type& expr_const_reference;
195
196 /* For matching by storage type: */
197 typedef typename array_type::memory_tag memory_tag;
198
199 /* For matching by size type: */
200 typedef typename array_type::size_tag size_tag;
201
202 /* For matching by resizability: */
203 typedef typename array_type::resizing_tag resizing_tag;
204
205 /* For matching by result-type: */
206 typedef cml::et::vector_result_tag result_tag;
207
208 /* For matching by assignability: */
209 typedef cml::et::assignable_tag assignable_tag;
210
211
212 public:
213
214 /** Return square of the length. */
215 value_type length_squared() const {
216 return dot(*this,*this);
217 }
218
219 /** Return the length. */
220 value_type length() const {
221 return std::sqrt(length_squared());
222 }
223
224 /** Normalize the vector. */
225 vector_type& normalize() {
226 return (*this /= length());
227 }
228
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));
233 return *this;
234 }
235
236 /** Set this vector to a cardinal vector. */
237 vector_type& cardinal(size_t i) {
238 zero();
239 (*this)[i] = Element(1);
240 return *this;
241 }
242
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]);
249 }
250 }
251
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]);
258 }
259 }
260
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);
265 }
266 }
267
268
269 public:
270
271 /** Construct from an array of values and the size. */
272 vector(Element* const array, size_t size)
273 : array_type(array, size) {}
274
275
276 public:
277
278 /* Define class operators for external vectors. Note: external vectors
279 * cannot be copy-constructed, but they can be assigned to:
280 */
281 CML_ASSIGN_VEC_2
282 CML_ASSIGN_VEC_3
283 CML_ASSIGN_VEC_4
284
285 CML_VEC_ASSIGN_FROM_VECTYPE
286
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)
291
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)
295
296 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
297 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
298 };
299
300 } // namespace cml
301
302 #endif
303
304 // -------------------------------------------------------------------------
305 // vim:ft=cpp
This page took 0.046078 seconds and 3 git commands to generate.