OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
spursAlignedObjectArray.h
1// Gamespy Technology
2// NOTE: this code has been provided by Sony for usage in Speex SPURS Manager
3
4/*
5Bullet Continuous Collision Detection and Physics Library
6Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
7
8This software is provided 'as-is', without any express or implied warranty.
9In no event will the authors be held liable for any damages arising from the use of this software.
10Permission is granted to anyone to use this software for any purpose,
11including commercial applications, and to alter it and redistribute it freely,
12subject to the following restrictions:
13
141. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
152. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
163. This notice may not be removed or altered from any source distribution.
17*/
18
19
20#ifndef BT_OBJECT_ARRAY__
21#define BT_OBJECT_ARRAY__
22
23#include "spursScalar.h" // has definitions like SIMD_FORCE_INLINE
24#include "spursAlignedAllocator.h"
25
31
32#define BT_USE_PLACEMENT_NEW 1
33//#define BT_USE_MEMCPY 1 //disable, because it is cumbersome to find out for each platform where memcpy is defined. It can be in <memory.h> or <string.h> or otherwise...
34
35#ifdef BT_USE_MEMCPY
36#include <memory.h>
37#include <string.h>
38#endif //BT_USE_MEMCPY
39
40#ifdef BT_USE_PLACEMENT_NEW
41#include <new> //for placement new
42#endif //BT_USE_PLACEMENT_NEW
43
44
47template <typename T>
48//template <class T>
49class spursAlignedObjectArray
50{
52
53 int m_size;
54 int m_capacity;
55 T* m_data;
56
57 protected:
58 SIMD_FORCE_INLINE int allocSize(int size)
59 {
60 return (size ? size*2 : 1);
61 }
62 SIMD_FORCE_INLINE void copy(int start,int end, T* dest)
63 {
64 int i;
65 for (i=start;i<end;++i)
66#ifdef BT_USE_PLACEMENT_NEW
67 new (&dest[i]) T(m_data[i]);
68#else
69 dest[i] = m_data[i];
70#endif //BT_USE_PLACEMENT_NEW
71 }
72
73 SIMD_FORCE_INLINE void init()
74 {
75 m_data = 0;
76 m_size = 0;
77 m_capacity = 0;
78 }
79 SIMD_FORCE_INLINE void destroy(int first,int last)
80 {
81 int i;
82 for (i=first; i<last;i++)
83 {
84 m_data[i].~T();
85 }
86 }
87
88 SIMD_FORCE_INLINE void* allocate(int size)
89 {
90 if (size)
91 return m_allocator.allocate(size);
92 return 0;
93 }
94
95 SIMD_FORCE_INLINE void deallocate()
96 {
97 if(m_data) {
98 m_allocator.deallocate(m_data);
99 m_data = 0;
100 }
101 }
102
103
104
105
106 public:
107
108 spursAlignedObjectArray()
109 {
110 init();
111 }
112
113 ~spursAlignedObjectArray()
114 {
115 clear();
116 }
117
118 SIMD_FORCE_INLINE int capacity() const
119 { // return current length of allocated storage
120 return m_capacity;
121 }
122
123 SIMD_FORCE_INLINE int size() const
124 { // return length of sequence
125 return m_size;
126 }
127
128 SIMD_FORCE_INLINE const T& operator[](int n) const
129 {
130 return m_data[n];
131 }
132
133 SIMD_FORCE_INLINE T& operator[](int n)
134 {
135 return m_data[n];
136 }
137
138
139 SIMD_FORCE_INLINE void clear()
140 {
141 destroy(0,size());
142
143 deallocate();
144
145 init();
146 }
147
148 SIMD_FORCE_INLINE void pop_back()
149 {
150 m_size--;
151 m_data[m_size].~T();
152 }
153
154 SIMD_FORCE_INLINE void resize(int newsize, const T& fillData=T())
155 {
156 int curSize = size();
157
158 if (newsize < size())
159 {
160 for(int i = curSize; i < newsize; i++)
161 {
162 m_data[i].~T();
163 }
164 } else
165 {
166 if (newsize > size())
167 {
168 reserve(newsize);
169 }
170#ifdef BT_USE_PLACEMENT_NEW
171 for (int i=curSize;i<newsize;i++)
172 {
173 new ( &m_data[i]) T(fillData);
174 }
175#endif //BT_USE_PLACEMENT_NEW
176
177 }
178
179 m_size = newsize;
180 }
181
182
183 SIMD_FORCE_INLINE T& expand( const T& fillValue=T())
184 {
185 int sz = size();
186 if( sz == capacity() )
187 {
188 reserve( allocSize(size()) );
189 }
190 m_size++;
191#ifdef BT_USE_PLACEMENT_NEW
192 new (&m_data[sz]) T(fillValue); //use the in-place new (not really allocating heap memory)
193#endif
194
195 return m_data[sz];
196 }
197
198
199 SIMD_FORCE_INLINE void push_back(const T& _Val)
200 {
201 int sz = size();
202 if( sz == capacity() )
203 {
204 reserve( allocSize(size()) );
205 }
206
207#ifdef BT_USE_PLACEMENT_NEW
208 new ( &m_data[m_size] ) T(_Val);
209#else
210 m_data[size()] = _Val;
211#endif //BT_USE_PLACEMENT_NEW
212
213 m_size++;
214 }
215
216
217
218 SIMD_FORCE_INLINE void reserve(int _Count)
219 { // determine new minimum length of allocated storage
220 if (capacity() < _Count)
221 { // not enough room, reallocate
222 T* s = (T*)allocate(_Count);
223
224 copy(0, size(), s);
225
226 destroy(0,size());
227
228 deallocate();
229
230 m_data = s;
231
232 m_capacity = _Count;
233
234 }
235 }
236
237
238 class less
239 {
240 public:
241
242 bool operator() ( const T& a, const T& b )
243 {
244 return ( a < b );
245 }
246 };
247
248
250 template <typename L>
251 void downHeap(T *pArr, int k, int n,L CompareFunc)
252 {
253 /* PRE: a[k+1..N] is a heap */
254 /* POST: a[k..N] is a heap */
255
256 T temp = pArr[k - 1];
257 /* k has child(s) */
258 while (k <= n/2)
259 {
260 int child = 2*k;
261
262 if ((child < n) && CompareFunc(pArr[child - 1] , pArr[child]))
263 {
264 child++;
265 }
266 /* pick larger child */
267 if (CompareFunc(temp , pArr[child - 1]))
268 {
269 /* move child up */
270 pArr[k - 1] = pArr[child - 1];
271 k = child;
272 }
273 else
274 {
275 break;
276 }
277 }
278 pArr[k - 1] = temp;
279 } /*downHeap*/
280
281 void swap(int index0,int index1)
282 {
283#ifdef BT_USE_MEMCPY
284 char temp[sizeof(T)];
285 memcpy(temp,&m_data[index0],sizeof(T));
286 memcpy(&m_data[index0],&m_data[index1],sizeof(T));
287 memcpy(&m_data[index1],temp,sizeof(T));
288#else
289 T temp = m_data[index0];
290 m_data[index0] = m_data[index1];
291 m_data[index1] = temp;
292#endif //BT_USE_PLACEMENT_NEW
293
294 }
295
296 template <typename L>
297 void heapSort(L CompareFunc)
298 {
299 /* sort a[0..N-1], N.B. 0 to N-1 */
300 int k;
301 int n = m_size;
302 for (k = n/2; k > 0; k--)
303 {
304 downHeap(m_data, k, n, CompareFunc);
305 }
306
307 /* a[1..N] is now a heap */
308 while ( n>=1 )
309 {
310 swap(0,n-1); /* largest of a[0..n-1] */
311
312
313 n = n - 1;
314 /* restore a[1..i-1] heap */
315 downHeap(m_data, 1, n, CompareFunc);
316 }
317 }
318
320 int findBinarySearch(const T& key) const
321 {
322 int first = 0;
323 int last = size();
324
325 //assume sorted array
326 while (first <= last) {
327 int mid = (first + last) / 2; // compute mid point.
328 if (key > m_data[mid])
329 first = mid + 1; // repeat search in top half.
330 else if (key < m_data[mid])
331 last = mid - 1; // repeat search in bottom half.
332 else
333 return mid; // found it. return position /////
334 }
335 return size(); // failed to find key
336 }
337
338
339 int findLinearSearch(const T& key) const
340 {
341 int index=size();
342 int i;
343
344 for (i=0;i<size();i++)
345 {
346 if (m_data[i] == key)
347 {
348 index = i;
349 break;
350 }
351 }
352 return index;
353 }
354
355 void remove(const T& key)
356 {
357
358 int findIndex = findLinearSearch(key);
359 if (findIndex<size())
360 {
361 swap( findIndex,size()-1);
362 pop_back();
363 }
364 }
365
366};
367
368#endif //BT_OBJECT_ARRAY__
369
370
Definition spursAlignedAllocator.h:35
Definition spursAlignedObjectArray.h:239
int findBinarySearch(const T &key) const
non-recursive binary search, assumes sorted array
Definition spursAlignedObjectArray.h:320
void downHeap(T *pArr, int k, int n, L CompareFunc)
heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
Definition spursAlignedObjectArray.h:251