2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5
This software is provided 'as-is', without any express or implied warranty.
6
In no event will the authors be held liable for any damages arising from the use of this software.
7
Permission is granted to anyone to use this software for any purpose,
8
including commercial applications, and to alter it and redistribute it freely,
9
subject to the following restrictions:
11
1. 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.
12
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13
3. This notice may not be removed or altered from any source distribution.
17
#ifndef BT_OBJECT_ARRAY__
18
#define BT_OBJECT_ARRAY__
20
#include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
21
#include "btAlignedAllocator.h"
23
///If the platform doesn't support placement new, you can disable BT_USE_PLACEMENT_NEW
24
///then the btAlignedObjectArray doesn't support objects with virtual methods, and non-trivial constructors/destructors
25
///You can enable BT_USE_MEMCPY, then swapping elements in the array will use memcpy instead of operator=
26
///see discussion here: http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1231 and
27
///http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1240
29
#define BT_USE_PLACEMENT_NEW 1
30
//#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...
35
#endif //BT_USE_MEMCPY
37
#ifdef BT_USE_PLACEMENT_NEW
38
#include <new> //for placement new
39
#endif //BT_USE_PLACEMENT_NEW
42
///btAlignedObjectArray uses a subset of the stl::vector interface for its methods
43
///It is developed to replace stl::vector to avoid STL alignment issues to add SIMD/SSE data
46
class btAlignedObjectArray
48
btAlignedAllocator<T , 16> m_allocator;
55
SIMD_FORCE_INLINE int allocSize(int size)
57
return (size ? size*2 : 1);
59
SIMD_FORCE_INLINE void copy(int start,int end, T* dest)
62
for (i=start;i<end;++i)
63
#ifdef BT_USE_PLACEMENT_NEW
64
new (&dest[i]) T(m_data[i]);
67
#endif //BT_USE_PLACEMENT_NEW
70
SIMD_FORCE_INLINE void init()
76
SIMD_FORCE_INLINE void destroy(int first,int last)
79
for (i=first; i<last;i++)
85
SIMD_FORCE_INLINE void* allocate(int size)
88
return m_allocator.allocate(size);
92
SIMD_FORCE_INLINE void deallocate()
95
m_allocator.deallocate(m_data);
105
btAlignedObjectArray()
110
~btAlignedObjectArray()
115
SIMD_FORCE_INLINE int capacity() const
116
{ // return current length of allocated storage
120
SIMD_FORCE_INLINE int size() const
121
{ // return length of sequence
125
SIMD_FORCE_INLINE const T& operator[](int n) const
130
SIMD_FORCE_INLINE T& operator[](int n)
136
SIMD_FORCE_INLINE void clear()
145
SIMD_FORCE_INLINE void pop_back()
151
SIMD_FORCE_INLINE void resize(int newsize, const T& fillData=T())
153
int curSize = size();
155
if (newsize < size())
157
for(int i = curSize; i < newsize; i++)
163
if (newsize > size())
167
#ifdef BT_USE_PLACEMENT_NEW
168
for (int i=curSize;i<newsize;i++)
170
new ( &m_data[i]) T(fillData);
172
#endif //BT_USE_PLACEMENT_NEW
180
SIMD_FORCE_INLINE T& expand( const T& fillValue=T())
183
if( sz == capacity() )
185
reserve( allocSize(size()) );
188
#ifdef BT_USE_PLACEMENT_NEW
189
new (&m_data[sz]) T(fillValue); //use the in-place new (not really allocating heap memory)
196
SIMD_FORCE_INLINE void push_back(const T& _Val)
199
if( sz == capacity() )
201
reserve( allocSize(size()) );
204
#ifdef BT_USE_PLACEMENT_NEW
205
new ( &m_data[m_size] ) T(_Val);
207
m_data[size()] = _Val;
208
#endif //BT_USE_PLACEMENT_NEW
215
SIMD_FORCE_INLINE void reserve(int _Count)
216
{ // determine new minimum length of allocated storage
217
if (capacity() < _Count)
218
{ // not enough room, reallocate
219
T* s = (T*)allocate(_Count);
239
bool operator() ( const T& a, const T& b )
246
///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
247
template <typename L>
248
void downHeap(T *pArr, int k, int n,L CompareFunc)
250
/* PRE: a[k+1..N] is a heap */
251
/* POST: a[k..N] is a heap */
253
T temp = pArr[k - 1];
259
if ((child < n) && CompareFunc(pArr[child - 1] , pArr[child]))
263
/* pick larger child */
264
if (CompareFunc(temp , pArr[child - 1]))
267
pArr[k - 1] = pArr[child - 1];
278
void swap(int index0,int index1)
281
char temp[sizeof(T)];
282
memcpy(temp,&m_data[index0],sizeof(T));
283
memcpy(&m_data[index0],&m_data[index1],sizeof(T));
284
memcpy(&m_data[index1],temp,sizeof(T));
286
T temp = m_data[index0];
287
m_data[index0] = m_data[index1];
288
m_data[index1] = temp;
289
#endif //BT_USE_PLACEMENT_NEW
293
template <typename L>
294
void heapSort(L CompareFunc)
296
/* sort a[0..N-1], N.B. 0 to N-1 */
299
for (k = n/2; k > 0; k--)
301
downHeap(m_data, k, n, CompareFunc);
304
/* a[1..N] is now a heap */
307
swap(0,n-1); /* largest of a[0..n-1] */
311
/* restore a[1..i-1] heap */
312
downHeap(m_data, 1, n, CompareFunc);
316
///non-recursive binary search, assumes sorted array
317
int findBinarySearch(const T& key) const
322
//assume sorted array
323
while (first <= last) {
324
int mid = (first + last) / 2; // compute mid point.
325
if (key > m_data[mid])
326
first = mid + 1; // repeat search in top half.
327
else if (key < m_data[mid])
328
last = mid - 1; // repeat search in bottom half.
330
return mid; // found it. return position /////
332
return size(); // failed to find key
336
int findLinearSearch(const T& key) const
341
for (i=0;i<size();i++)
343
if (m_data[i] == key)
352
void remove(const T& key)
355
int findIndex = findLinearSearch(key);
356
if (findIndex<size())
358
swap( findIndex,size()-1);
365
#endif //BT_OBJECT_ARRAY__