ScummVM API documentation
dgGeneralVector.h
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>
35  friend class dgSPDMatrix<T>;
36  friend class dgGeneralMatrix<T>;
37 
38 public:
39  dgGeneralVector(dgInt32 size);
40  dgGeneralVector(dgInt32 size, T *mem);
42  dgGeneralVector(const dgGeneralVector<T> &src, T *mem);
43 
44  ~dgGeneralVector();
45 
46  T &operator[](dgInt32 i);
47  const T &operator[](dgInt32 i) const;
48 
49  dgInt32 GetRowCount() const;
50 
51  void Clear(T val);
52  void Copy(const dgGeneralVector<T> &src);
53  T DotProduct(const dgGeneralVector &b) const;
54  T Norm() const;
55  T Norm2() const;
56 
57  void operator += (const dgGeneralVector &A);
58  void operator -= (const dgGeneralVector &A);
59 
60  void Scale(T scale);
61  void LinearCombine(T scale, const dgGeneralVector &A, const dgGeneralVector &B);
62 
63 
64  static dgInt32 GetFloatOps();
65  static dgInt32 GetMemWrites();
66 
67  static void SetFloatOps(dgInt32 initialCount = 0);
68  static void SetMemWrites(dgInt32 initialCount = 0);
69 
70  void Trace() const;
71 
72 
73 protected:
74  bool m_ownMemory;
75  dgInt32 m_colCount;
76  T *m_columns;
77 
78  static dgInt32 m_floatsOp;
79  static dgInt32 m_memoryWrite;
80 
81 
83 };
84 
85 
86 // ***********************************************************************************************
87 //
88 // vector
89 //
90 // ***********************************************************************************************
91 template<class T>
93  m_ownMemory = false;
94  m_colCount = 0;
95  m_columns = NULL;
96 }
97 
98 template<class T>
100  NEWTON_ASSERT(size > 0);
101  m_ownMemory = true;
102  m_colCount = size;
103  m_columns = new T[size];
104  NEWTON_ASSERT((((dgUnsigned32) m_columns) & 0x0f) == 0);
105 }
106 
107 template<class T>
108 dgGeneralVector<T>::dgGeneralVector(dgInt32 size, T *mem) {
109  m_ownMemory = false;
110  m_colCount = size;
111  m_columns = mem;
112  NEWTON_ASSERT((((dgUnsigned32) m_columns) & 0x0f) == 0);
113 }
114 
115 template<class T>
117  m_ownMemory = true;
118  m_colCount = src.m_colCount;
119  m_columns = new T[m_colCount];
120  NEWTON_ASSERT((((dgUnsigned32) m_columns) & 0x0f) == 0);
121 
122  Copy(src);
123 }
124 
125 template<class T>
127  m_ownMemory = false;
128  m_colCount = src.m_colCount;
129  m_columns = mem;
130  NEWTON_ASSERT((((dgUnsigned32) m_columns) & 0x0f) == 0);
131 
132  Copy(src);
133 }
134 
135 
136 template<class T>
138  return m_floatsOp;
139 }
140 
141 template<class T>
142 void dgGeneralVector<T>::SetFloatOps(dgInt32 initialCount) {
143  m_floatsOp = initialCount;
144 }
145 
146 
147 template<class T>
149  return m_memoryWrite;
150 }
151 
152 
153 template<class T>
154 void dgGeneralVector<T>::SetMemWrites(dgInt32 initialCount) {
155  m_memoryWrite = initialCount;
156 }
157 
158 
159 
160 template<class T>
162  if (m_ownMemory) {
163  delete[] m_columns;
164  }
165 }
166 
167 template<class T>
168 void dgGeneralVector<T>::Trace() const {
169  dgInt32 i;
170  for (i = 0; i < m_colCount; i ++) {
171  dgTrace(("%f ", m_columns[i]));
172 
173  }
174  dgTrace(("\n"));
175 }
176 
177 
178 
179 template<class T>
180 T &dgGeneralVector<T>::operator[](dgInt32 i) {
181  NEWTON_ASSERT(i < m_colCount);
182  NEWTON_ASSERT(i >= 0);
183  return m_columns[i];
184 }
185 
186 template<class T>
187 const T &dgGeneralVector<T>::operator[](dgInt32 i) const {
188  NEWTON_ASSERT(i < m_colCount);
189  NEWTON_ASSERT(i >= 0);
190  return m_columns[i];
191 }
192 
193 template<class T>
194 dgInt32 dgGeneralVector<T>::GetRowCount() const {
195  return m_colCount;
196 }
197 
198 
199 // return dot product
200 template<class T>
202  dgInt32 i;
203 
204  NEWTON_ASSERT(m_colCount == A.m_colCount);
205  T val(0.0);
206  for (i = 0; i < m_colCount; i ++) {
207  val = val + m_columns[i] * A.m_columns[i];
208  }
209 
210 #ifdef DG_COUNT_FLOAT_OPS
211  m_floatsOp += 2 * m_colCount;
212  m_memoryWrite += m_colCount;
213 #endif
214 
215  return val;
216 }
217 
218 
219 
220 
221 template<class T>
222 void dgGeneralVector<T>::Clear(T val) {
223  dgInt32 i;
224  for (i = 0; i < m_colCount; i ++) {
225  m_columns[i] = val;
226  }
227 
228 #ifdef DG_COUNT_FLOAT_OPS
229  m_memoryWrite += m_colCount;
230 #endif
231 }
232 
233 template<class T>
235  dgInt32 i;
236 
237  NEWTON_ASSERT(m_colCount == src.m_colCount);
238  for (i = 0; i < m_colCount; i ++) {
239  m_columns[i] = src.m_columns[i];
240  }
241 
242 #ifdef DG_COUNT_FLOAT_OPS
243  m_memoryWrite += m_colCount;
244 #endif
245 }
246 
247 
248 
249 template<class T>
251  dgInt32 i;
252  for (i = 0; i < m_colCount; i ++) {
253  m_columns[i] = m_columns[i] * scale;
254  }
255 
256 #ifdef DG_COUNT_FLOAT_OPS
257  m_floatsOp += m_colCount;
258  m_memoryWrite += m_colCount;
259 #endif
260 }
261 
262 
263 template<class T>
264 T dgGeneralVector<T>::Norm2() const {
265  dgInt32 i;
266  T max;
267 
268  max = 0;
269  for (i = 0; i < m_colCount; i ++) {
270  max = GetMax(m_columns[i] * m_columns[i], max);
271  }
272 
273 #ifdef DG_COUNT_FLOAT_OPS
274  m_floatsOp += m_colCount;
275  m_memoryWrite += m_colCount;
276 #endif
277 
278  return max;
279 }
280 
281 template<class T>
282 T dgGeneralVector<T>::Norm() const {
283  return T(sqrt(Norm2()));
284 }
285 
286 
287 template<class T>
289  dgInt32 i;
290 
291  NEWTON_ASSERT(A.m_colCount == m_colCount);
292  NEWTON_ASSERT(B.m_colCount == m_colCount);
293  for (i = 0; i < m_colCount; i ++) {
294  m_columns[i] = A.m_columns[i] * scale + B.m_columns[i];
295  }
296 
297 #ifdef DG_COUNT_FLOAT_OPS
298  m_floatsOp += 3 * m_colCount;
299  m_memoryWrite += m_colCount;
300 #endif
301 }
302 
303 
304 template<class T>
306  dgInt32 i;
307 
308  NEWTON_ASSERT(A.m_colCount == m_colCount);
309  for (i = 0; i < m_colCount; i ++) {
310  m_columns[i] += A.m_columns[i];
311  }
312 
313 #ifdef DG_COUNT_FLOAT_OPS
314  m_floatsOp += m_colCount;
315  m_memoryWrite += m_colCount;
316 #endif
317 }
318 
319 template<class T>
321  dgInt32 i;
322 
323  NEWTON_ASSERT(A.m_colCount == m_colCount);
324  for (i = 0; i < m_colCount; i ++) {
325  m_columns[i] -= A.m_columns[i];
326  }
327 
328 #ifdef DG_COUNT_FLOAT_OPS
329  m_floatsOp += m_colCount;
330  m_memoryWrite += m_colCount;
331 #endif
332 }
333 
334 
335 template<class T> dgInt32 dgGeneralVector <T>::m_floatsOp = 0;
336 template<class T> dgInt32 dgGeneralVector <T>::m_memoryWrite = 0;
337 
338 
339 #endif
340 
Definition: dgGeneralVector.h:34
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: dgGeneralVector.h:30
Definition: dgGeneralMatrix.h:40