]> Dogcows Code - chaz/yoink/blob - src/cml/vector/dynamic.h
now using cml for vectors and math stuff
[chaz/yoink] / src / 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 /* For integration into the expression template code: */
41 typedef vector_type expr_type;
42
43 /* For integration into the expression template code: */
44 typedef vector_type temporary_type;
45
46 /* Standard: */
47 typedef typename array_type::value_type value_type;
48 typedef typename array_type::reference reference;
49 typedef typename array_type::const_reference const_reference;
50
51 /* For integration into the expression templates code: */
52 typedef vector_type& expr_reference;
53 typedef const vector_type& expr_const_reference;
54
55 /* For matching by storage type: */
56 typedef typename array_type::memory_tag memory_tag;
57
58 /* For matching by size type: */
59 typedef typename array_type::size_tag size_tag;
60
61 /* For matching by resizability: */
62 typedef typename array_type::resizing_tag resizing_tag;
63
64 /* For matching by result-type: */
65 typedef cml::et::vector_result_tag result_tag;
66
67 /* For matching by assignability: */
68 typedef cml::et::assignable_tag assignable_tag;
69
70
71 public:
72
73 /** Return square of the length. */
74 value_type length_squared() const {
75 return cml::dot(*this,*this);
76 }
77
78 /** Return the length. */
79 value_type length() const {
80 return std::sqrt(length_squared());
81 }
82
83 /** Normalize the vector. */
84 vector_type& normalize() {
85 return (*this /= length());
86 }
87
88 /** Set this vector to [0]. */
89 vector_type& zero() {
90 typedef cml::et::OpAssign<Element,Element> OpT;
91 cml::et::UnrollAssignment<OpT>(*this,Element(0));
92 return *this;
93 }
94
95 /** Set this vector to a cardinal vector. */
96 vector_type& cardinal(size_t i) {
97 zero();
98 (*this)[i] = Element(1);
99 return *this;
100 }
101
102 /** Pairwise minimum of this vector with another. */
103 template<typename E, class AT>
104 void minimize(const vector<E,AT>& v) {
105 /* XXX This should probably use ScalarPromote: */
106 for (size_t i = 0; i < this->size(); ++i) {
107 (*this)[i] = std::min((*this)[i],v[i]);
108 }
109 }
110
111 /** Pairwise maximum of this vector with another. */
112 template<typename E, class AT>
113 void maximize(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::max((*this)[i],v[i]);
117 }
118 }
119
120 /** Fill vector with random elements. */
121 void random(value_type min, value_type max) {
122 for (size_t i = 0; i < this->size(); ++i) {
123 (*this)[i] = cml::random_real(min,max);
124 }
125 }
126
127
128 public:
129
130 /** Default constructor. */
131 vector() : array_type() {}
132
133 /** Construct given array size. */
134 vector(size_t N) : array_type(N) {}
135
136
137 public:
138
139 /* Define common class operators: */
140
141 CML_CONSTRUCT_VEC_2(: array_type())
142 CML_CONSTRUCT_VEC_3(: array_type())
143 CML_CONSTRUCT_VEC_4(: array_type())
144
145 CML_VEC_COPY_FROM_ARRAY(: array_type())
146 CML_VEC_COPY_FROM_VECTYPE(: array_type())
147 CML_VEC_COPY_FROM_VEC
148 CML_VEC_COPY_FROM_VECXPR
149
150 CML_ASSIGN_VEC_2
151 CML_ASSIGN_VEC_3
152 CML_ASSIGN_VEC_4
153
154 CML_VEC_ASSIGN_FROM_VECTYPE
155
156 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
157 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
158 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
159
160 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
161 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
162 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
163
164 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
165 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
166 };
167
168 } // namespace cml
169
170 #endif
171
172 // -------------------------------------------------------------------------
173 // vim:ft=cpp
This page took 0.036129 seconds and 4 git commands to generate.