My Project: dynamics.mod/NewtonDynamics/source/core/dgGeneralVector.h Source File

My Project

dgGeneralVector.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #ifndef __dgGeneralVector__
23 #define __dgGeneralVector__
24
25 #include "dgStdafx.h"
26 #include "dgDebug.h"
27
28 #define DG_COUNT_FLOAT_OPS
29
30 template<class T> class dgSPDMatrix;
31 template<class T> class dgGeneralMatrix;
32
33 template<class T>
34 class dgGeneralVector
35 {
36 friend class dgSPDMatrix<T>;
37 friend class dgGeneralMatrix<T>;
38
39 public:
40 dgGeneralVector (dgInt32 size);
41 dgGeneralVector (dgInt32 size, T *mem);
42 dgGeneralVector (const dgGeneralVector<T> &src);
43 dgGeneralVector (const dgGeneralVector<T> &src, T *mem);
44
45 ~dgGeneralVector ();
46
47 T& operator[] (dgInt32 i);
48 const T& operator[] (dgInt32 i) const;
49
50 dgInt32 GetRowCount() const;
51
52 void Clear (T val);
53 void Copy (const dgGeneralVector<T> &src);
54 T DotProduct (const dgGeneralVector &b) const;
55 T Norm () const;
56 T Norm2 () const;
57
58 void operator += (const dgGeneralVector &A);
59 void operator -= (const dgGeneralVector &A);
60
61 void Scale (T scale);
62 void LinearCombine (T scale, const dgGeneralVector &A, const dgGeneralVector &B);
63
64
65 static dgInt32 GetFloatOps();
66 static dgInt32 GetMemWrites();
67
68 static void SetFloatOps(dgInt32 initialCount = 0);
69 static void SetMemWrites(dgInt32 initialCount = 0);
70
71 void Trace () const;
72
73
74 protected:
75 bool m_ownMemory;
76 dgInt32 m_colCount;
77 T* m_columns;
78
79 static dgInt32 m_floatsOp;
80 static dgInt32 m_memoryWrite;
81
82
83 dgGeneralVector();
84 };
85
86
87 // ***********************************************************************************************
88 //
89 // vector
90 //
91 // ***********************************************************************************************
92 template<class T>
93 dgGeneralVector<T>::dgGeneralVector()
94 {
95 m_ownMemory = false;
96 m_colCount = 0;
97 m_columns = NULL;
98 }
99
100 template<class T>
101 dgGeneralVector<T>::dgGeneralVector (dgInt32 size)
102 {
103 dgAssert (size > 0);
104 m_ownMemory = true;
105 m_colCount = size;
106 m_columns = new T[size];
107 dgAssert ((((dgUnsigned32) m_columns) & 0x0f) == 0);
108 }
109
110 template<class T>
111 dgGeneralVector<T>::dgGeneralVector (dgInt32 size, T *mem)
112 {
113 m_ownMemory = false;
114 m_colCount = size;
115 m_columns = mem;
116 dgAssert ((((dgUnsigned32) m_columns) & 0x0f) == 0);
117 }
118
119 template<class T>
120 dgGeneralVector<T>::dgGeneralVector (const dgGeneralVector<T> &src)
121 {
122 m_ownMemory = true;
123 m_colCount = src.m_colCount;
124 m_columns = new T[m_colCount];
125 dgAssert ((((dgUnsigned32) m_columns) & 0x0f) == 0);
126
127 Copy (src);
128 }
129
130 template<class T>
131 dgGeneralVector<T>::dgGeneralVector (const dgGeneralVector<T> &src, T *mem)
132 {
133 m_ownMemory = false;
134 m_colCount = src.m_colCount;
135 m_columns = mem;
136 dgAssert ((((dgUnsigned32) m_columns) & 0x0f) == 0);
137
138 Copy (src);
139 }
140
141
142 template<class T>
143 dgInt32 dgGeneralVector<T>::GetFloatOps()
144 {
145 return m_floatsOp;
146 }
147
148 template<class T>
149 void dgGeneralVector<T>::SetFloatOps(dgInt32 initialCount)
150 {
151 m_floatsOp = initialCount;
152 }
153
154
155 template<class T>
156 dgInt32 dgGeneralVector<T>::GetMemWrites()
157 {
158 return m_memoryWrite;
159 }
160
161
162 template<class T>
163 void dgGeneralVector<T>::SetMemWrites(dgInt32 initialCount)
164 {
165 m_memoryWrite = initialCount;
166 }
167
168
169
170 template<class T>
171 dgGeneralVector<T>::~dgGeneralVector ()
172 {
173 if (m_ownMemory) {
174 delete[] m_columns;
175 }
176 }
177
178 template<class T>
179 void dgGeneralVector<T>::Trace () const
180 {
181 dgInt32 i;
182 for (i = 0; i < m_colCount; i ++) {
183 dgTrace (("%f ", m_columns[i]));
184
185 }
186 dgTrace (("\n"));
187 }
188
189
190
191 template<class T>
192 T& dgGeneralVector<T>::operator[] (dgInt32 i)
193 {
194 dgAssert (i < m_colCount);
195 dgAssert (i >= 0);
196 return m_columns[i];
197 }
198
199 template<class T>
200 const T& dgGeneralVector<T>::operator[] (dgInt32 i) const
201 {
202 dgAssert (i < m_colCount);
203 dgAssert (i >= 0);
204 return m_columns[i];
205 }
206
207 template<class T>
208 dgInt32 dgGeneralVector<T>::GetRowCount() const
209 {
210 return m_colCount;
211 }
212
213
214 // return dot product
215 template<class T>
216 T dgGeneralVector<T>::DotProduct (const dgGeneralVector<T> &A) const
217 {
218 dgInt32 i;
219
220 dgAssert (m_colCount == A.m_colCount);
221 T val (0.0);
222 for (i = 0; i < m_colCount; i ++) {
223 val = val + m_columns[i] * A.m_columns[i];
224 }
225
226 #ifdef DG_COUNT_FLOAT_OPS
227 m_floatsOp += 2 * m_colCount;
228 m_memoryWrite += m_colCount;
229 #endif
230
231 return val;
232 }
233
234
235
236
237 template<class T>
238 void dgGeneralVector<T>::Clear (T val)
239 {
240 dgInt32 i;
241 for (i = 0; i < m_colCount; i ++ ) {
242 m_columns[i] = val;
243 }
244
245 #ifdef DG_COUNT_FLOAT_OPS
246 m_memoryWrite += m_colCount;
247 #endif
248 }
249
250 template<class T>
251 void dgGeneralVector<T>::Copy (const dgGeneralVector<T> &src)
252 {
253 dgInt32 i;
254
255 dgAssert (m_colCount == src.m_colCount);
256 for (i = 0; i < m_colCount; i ++ ) {
257 m_columns[i] = src.m_columns[i];
258 }
259
260 #ifdef DG_COUNT_FLOAT_OPS
261 m_memoryWrite += m_colCount;
262 #endif
263 }
264
265
266
267 template<class T>
268 void dgGeneralVector<T>::Scale (T scale)
269 {
270 dgInt32 i;
271 for (i = 0; i < m_colCount; i ++ ) {
272 m_columns[i] = m_columns[i] * scale;
273 }
274
275 #ifdef DG_COUNT_FLOAT_OPS
276 m_floatsOp += m_colCount;
277 m_memoryWrite += m_colCount;
278 #endif
279 }
280
281
282 template<class T>
283 T dgGeneralVector<T>::Norm2 () const
284 {
285 dgInt32 i;
286 T max;
287
288 max = 0;
289 for (i = 0; i < m_colCount; i ++ ) {
290 max = dgMax (m_columns[i] * m_columns[i], max);
291 }
292
293 #ifdef DG_COUNT_FLOAT_OPS
294 m_floatsOp += m_colCount;
295 m_memoryWrite += m_colCount;
296 #endif
297
298 return max;
299 }
300
301 template<class T>
302 T dgGeneralVector<T>::Norm () const
303 {
304 return T (sqrt (Norm2 ()));
305 }
306
307
308 template<class T>
309 void dgGeneralVector<T>::LinearCombine (T scale, const dgGeneralVector<T> &A, const dgGeneralVector<T> &B)
310 {
311 dgInt32 i;
312
313 dgAssert (A.m_colCount == m_colCount);
314 dgAssert (B.m_colCount == m_colCount);
315 for (i = 0; i < m_colCount; i ++ ) {
316 m_columns[i] = A.m_columns[i] * scale + B.m_columns[i];
317 }
318
319 #ifdef DG_COUNT_FLOAT_OPS
320 m_floatsOp += 3 * m_colCount;
321 m_memoryWrite += m_colCount;
322 #endif
323 }
324
325
326 template<class T>
327 void dgGeneralVector<T>::operator+= (const dgGeneralVector<T> &A)
328 {
329 dgInt32 i;
330
331 dgAssert (A.m_colCount == m_colCount);
332 for (i = 0; i < m_colCount; i ++ ) {
333 m_columns[i] += A.m_columns[i];
334 }
335
336 #ifdef DG_COUNT_FLOAT_OPS
337 m_floatsOp += m_colCount;
338 m_memoryWrite += m_colCount;
339 #endif
340 }
341
342 template<class T>
343 void dgGeneralVector<T>::operator-= (const dgGeneralVector<T> &A)
344 {
345 dgInt32 i;
346
347 dgAssert (A.m_colCount == m_colCount);
348 for (i = 0; i < m_colCount; i ++ ) {
349 m_columns[i] -= A.m_columns[i];
350 }
351
352 #ifdef DG_COUNT_FLOAT_OPS
353 m_floatsOp += m_colCount;
354 m_memoryWrite += m_colCount;
355 #endif
356 }
357
358
359 template<class T> dgInt32 dgGeneralVector <T>::m_floatsOp = 0;
360 template<class T> dgInt32 dgGeneralVector <T>::m_memoryWrite = 0;
361
362
363 #endif
364
dgGeneralVector::operator-=
void operator-=(const dgGeneralVector &A)
Definition: dgGeneralVector.h:343
dgGeneralVector
Definition: dgGeneralVector.h:34
dgDebug.h
dgGeneralVector::~dgGeneralVector
~dgGeneralVector()
Definition: dgGeneralVector.h:171
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgGeneralVector::LinearCombine
void LinearCombine(T scale, const dgGeneralVector &A, const dgGeneralVector &B)
Definition: dgGeneralVector.h:309
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgMax
DG_INLINE T dgMax(T A, T B)
Definition: dgTypes.h:299
dgGeneralVector::m_memoryWrite
static dgInt32 m_memoryWrite
Definition: dgGeneralVector.h:80
dgGeneralVector::dgGeneralVector
dgGeneralVector()
Definition: dgGeneralVector.h:93
dgTrace
#define dgTrace(x)
Definition: dgDebug.h:36
dgGeneralVector::operator[]
T & operator[](dgInt32 i)
Definition: dgGeneralVector.h:192
dgGeneralVector::Clear
void Clear(T val)
Definition: dgGeneralVector.h:238
dgGeneralVector::m_colCount
dgInt32 m_colCount
Definition: dgGeneralVector.h:76
dgGeneralVector::GetMemWrites
static dgInt32 GetMemWrites()
Definition: dgGeneralVector.h:156
dgStdafx.h
dgGeneralVector::Trace
void Trace() const
Definition: dgGeneralVector.h:179
dgGeneralVector::Copy
void Copy(const dgGeneralVector< T > &src)
Definition: dgGeneralVector.h:251
dgGeneralVector::GetRowCount
dgInt32 GetRowCount() const
Definition: dgGeneralVector.h:208
dgUnsigned32
unsigned dgUnsigned32
Definition: dgTypes.h:219
dgGeneralVector::Norm2
T Norm2() const
Definition: dgGeneralVector.h:283
dgSPDMatrix
Definition: dgGeneralVector.h:30
dgGeneralVector::operator+=
void operator+=(const dgGeneralVector &A)
Definition: dgGeneralVector.h:327
dgGeneralVector::SetFloatOps
static void SetFloatOps(dgInt32 initialCount=0)
Definition: dgGeneralVector.h:149
dgGeneralMatrix
Definition: dgGeneralMatrix.h:41
dgGeneralVector::m_ownMemory
bool m_ownMemory
Definition: dgGeneralVector.h:75
dgGeneralVector::Norm
T Norm() const
Definition: dgGeneralVector.h:302
dgGeneralVector::m_floatsOp
static dgInt32 m_floatsOp
Definition: dgGeneralVector.h:79
dgGeneralVector::m_columns
T * m_columns
Definition: dgGeneralVector.h:77
dgGeneralVector::Scale
void Scale(T scale)
Definition: dgGeneralVector.h:268
dgGeneralVector::DotProduct
T DotProduct(const dgGeneralVector &b) const
Definition: dgGeneralVector.h:216
dgGeneralVector::SetMemWrites
static void SetMemWrites(dgInt32 initialCount=0)
Definition: dgGeneralVector.h:163
dgGeneralVector::GetFloatOps
static dgInt32 GetFloatOps()
Definition: dgGeneralVector.h:143