]> Dogcows Code - chaz/yoink/blob - src/moof/cml/vector/fixed.h
bugfix: win32 packaging script temp directories
[chaz/yoink] / src / moof / cml / vector / fixed.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 Specialization for fixed-size, fixed-memory vectors.
11 */
12
13 #ifndef fixed_vector_h
14 #define fixed_vector_h
15
16 #include <cml/core/fixed_1D.h>
17 #include <cml/vector/vector_expr.h>
18 #include <cml/vector/class_ops.h>
19 #include <cml/vector/vector_unroller.h>
20 #include <cml/vector/external.h>
21 #include <cml/util.h>
22
23 namespace cml {
24
25 /** Fixed-size, fixed-memory vector. */
26 template<typename Element, int Size>
27 class vector< Element, fixed<Size> >
28 : public fixed_1D<Element,Size>
29 {
30 public:
31
32 /* Shorthand for the generator: */
33 typedef fixed<> storage_type;
34 typedef fixed<Size> generator_type;
35
36 /* Shorthand for the array type: */
37 typedef fixed_1D<Element,Size> array_type;
38
39 /* Shorthand for the type of this vector: */
40 typedef vector<Element,generator_type> vector_type;
41
42 /* The vector coordinate type: */
43 typedef Element coordinate_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_type temporary_type;
50 typedef vector< Element, fixed<Size-1> > subvector_type;
51
52 /* Standard: */
53 typedef typename array_type::value_type value_type;
54 typedef typename array_type::reference reference;
55 typedef typename array_type::const_reference const_reference;
56
57 /* For integration into the expression templates code: */
58 typedef vector_type& expr_reference;
59 typedef const vector_type& expr_const_reference;
60
61 /* For matching by storage type: */
62 typedef typename array_type::memory_tag memory_tag;
63
64 /* For matching by size type: */
65 typedef typename array_type::size_tag size_tag;
66
67 /* For matching by result-type: */
68 typedef cml::et::vector_result_tag result_tag;
69
70 /* For matching by assignability: */
71 typedef cml::et::assignable_tag assignable_tag;
72
73
74 public:
75
76 /** Static constant containing the vector's space dimension. */
77 enum { dimension = Size };
78
79
80 public:
81
82 /** Return square of the length. */
83 value_type length_squared() const {
84 return cml::dot(*this,*this);
85 }
86
87 /** Return the length. */
88 value_type length() const {
89 return std::sqrt(length_squared());
90 }
91
92 /** Normalize the vector. */
93 vector_type& normalize() {
94 return (*this /= length());
95 }
96
97 /** Set this vector to [0]. */
98 vector_type& zero() {
99 typedef cml::et::OpAssign<Element,Element> OpT;
100 cml::et::UnrollAssignment<OpT>(*this,Element(0));
101 return *this;
102 }
103
104 /** Set this vector to a cardinal vector. */
105 vector_type& cardinal(size_t i) {
106 zero();
107 (*this)[i] = Element(1);
108 return *this;
109 }
110
111 /** Pairwise minimum of this vector with another. */
112 template<typename E, class AT>
113 void minimize(const vector<E,AT>& v) {
114 /* XXX This should probably use ScalarPromote: */
115 for (size_t i = 0; i < this->size(); ++i) {
116 (*this)[i] = std::min((*this)[i],v[i]);
117 }
118 }
119
120 /** Pairwise maximum of this vector with another. */
121 template<typename E, class AT>
122 void maximize(const vector<E,AT>& v) {
123 /* XXX This should probably use ScalarPromote: */
124 for (size_t i = 0; i < this->size(); ++i) {
125 (*this)[i] = std::max((*this)[i],v[i]);
126 }
127 }
128
129 /** Fill vector with random elements. */
130 void random(value_type min, value_type max) {
131 for (size_t i = 0; i < this->size(); ++i) {
132 (*this)[i] = cml::random_real(min,max);
133 }
134 }
135
136 /** Return a subvector by removing element i.
137 *
138 * @internal This is horribly inefficient...
139 */
140 subvector_type subvector(size_t i) const {
141 subvector_type s;
142 for(size_t m = 0, n = 0; m < this->size(); ++ m)
143 if(m != i) s[n++] = (*this)[m];
144 return s;
145 };
146
147
148 public:
149
150 vector() : array_type() {}
151
152
153 public:
154
155 /* Define common class operators: */
156
157 CML_CONSTRUCT_VEC_2(/**/)
158 CML_CONSTRUCT_VEC_3(/**/)
159 CML_CONSTRUCT_VEC_4(/**/)
160
161 CML_CONSTRUCT_FROM_SUBVEC(/**/)
162
163 CML_VEC_COPY_FROM_FIXED_ARRAY(array_type::array_size,/**/)
164 CML_VEC_COPY_FROM_VECTYPE(: array_type())
165 CML_VEC_COPY_FROM_VEC
166 CML_VEC_COPY_FROM_VECXPR
167
168 CML_ASSIGN_VEC_2
169 CML_ASSIGN_VEC_3
170 CML_ASSIGN_VEC_4
171
172 CML_VEC_ASSIGN_FROM_VECTYPE
173
174 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
175 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
176 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
177
178 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
179 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
180 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
181
182 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
183 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
184 };
185
186 } // namespace cml
187
188 #endif
189
190 // -------------------------------------------------------------------------
191 // vim:ft=cpp
This page took 0.037459 seconds and 4 git commands to generate.