]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/vector/dynamic.h
bugfix: win32 packaging script temp directories
[chaz/yoink] / src / Moof / cml / vector / dynamic.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 resizeable, dynamic-memory vector.
11 */
12
13 #ifndef dynamic_vector_h
14 #define dynamic_vector_h
15
16 #include <cml/core/dynamic_1D.h>
17 #include <cml/vector/vector_expr.h>
18 #include <cml/vector/class_ops.h>
19 #include <cml/vector/vector_unroller.h>
20
21 namespace cml {
22
23 /** Resizeable, dynamic-memory vector. */
24 template<typename Element, typename Alloc>
25 class vector< Element, dynamic<Alloc> >
26 : public dynamic_1D<Element,Alloc>
27 {
28 public:
29
30 /* Shorthand for the generator: */
31 typedef dynamic<> storage_type;
32 typedef dynamic<Alloc> generator_type;
33
34 /* Shorthand for the array type: */
35 typedef dynamic_1D<Element,Alloc> array_type;
36
37 /* Shorthand for the type of this vector: */
38 typedef vector<Element,generator_type> vector_type;
39
40 /* The vector coordinate type: */
41 typedef Element coordinate_type;
42
43 /* For integration into the expression template code: */
44 typedef vector_type expr_type;
45
46 /* For integration into the expression template code: */
47 typedef vector_type temporary_type;
48
49 /* Standard: */
50 typedef typename array_type::value_type value_type;
51 typedef typename array_type::reference reference;
52 typedef typename array_type::const_reference const_reference;
53
54 /* For integration into the expression templates code: */
55 typedef vector_type& expr_reference;
56 typedef const vector_type& expr_const_reference;
57
58 /* For matching by storage type: */
59 typedef typename array_type::memory_tag memory_tag;
60
61 /* For matching by size type: */
62 typedef typename array_type::size_tag size_tag;
63
64 /* For matching by resizability: */
65 typedef typename array_type::resizing_tag resizing_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 /** Return square of the length. */
77 value_type length_squared() const {
78 return cml::dot(*this,*this);
79 }
80
81 /** Return the length. */
82 value_type length() const {
83 return std::sqrt(length_squared());
84 }
85
86 /** Normalize the vector. */
87 vector_type& normalize() {
88 return (*this /= length());
89 }
90
91 /** Set this vector to [0]. */
92 vector_type& zero() {
93 typedef cml::et::OpAssign<Element,Element> OpT;
94 cml::et::UnrollAssignment<OpT>(*this,Element(0));
95 return *this;
96 }
97
98 /** Set this vector to a cardinal vector. */
99 vector_type& cardinal(size_t i) {
100 zero();
101 (*this)[i] = Element(1);
102 return *this;
103 }
104
105 /** Pairwise minimum of this vector with another. */
106 template<typename E, class AT>
107 void minimize(const vector<E,AT>& v) {
108 /* XXX This should probably use ScalarPromote: */
109 for (size_t i = 0; i < this->size(); ++i) {
110 (*this)[i] = std::min((*this)[i],v[i]);
111 }
112 }
113
114 /** Pairwise maximum of this vector with another. */
115 template<typename E, class AT>
116 void maximize(const vector<E,AT>& v) {
117 /* XXX This should probably use ScalarPromote: */
118 for (size_t i = 0; i < this->size(); ++i) {
119 (*this)[i] = std::max((*this)[i],v[i]);
120 }
121 }
122
123 /** Fill vector with random elements. */
124 void random(value_type min, value_type max) {
125 for (size_t i = 0; i < this->size(); ++i) {
126 (*this)[i] = cml::random_real(min,max);
127 }
128 }
129
130
131 public:
132
133 /** Default constructor. */
134 vector() : array_type() {}
135
136 /** Construct given array size. */
137 vector(size_t N) : array_type(N) {}
138
139
140 public:
141
142 /* Define common class operators: */
143
144 CML_CONSTRUCT_VEC_2(: array_type())
145 CML_CONSTRUCT_VEC_3(: array_type())
146 CML_CONSTRUCT_VEC_4(: array_type())
147
148 CML_VEC_COPY_FROM_ARRAY(: array_type())
149 CML_VEC_COPY_FROM_VECTYPE(: array_type())
150 CML_VEC_COPY_FROM_VEC
151 CML_VEC_COPY_FROM_VECXPR
152
153 CML_ASSIGN_VEC_2
154 CML_ASSIGN_VEC_3
155 CML_ASSIGN_VEC_4
156
157 CML_VEC_ASSIGN_FROM_VECTYPE
158
159 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
160 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
161 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
162
163 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
164 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
165 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
166
167 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
168 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
169 };
170
171 } // namespace cml
172
173 #endif
174
175 // -------------------------------------------------------------------------
176 // vim:ft=cpp
This page took 0.03721 seconds and 4 git commands to generate.