2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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.
16 #ifndef BT_OBJECT_ARRAY__
17 #define BT_OBJECT_ARRAY__
19 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
20 #include "btAlignedAllocator.h"
22 ///If the platform doesn't support placement new, you can disable BT_USE_PLACEMENT_NEW
23 ///then the btAlignedObjectArray doesn't support objects with virtual methods, and non-trivial constructors/destructors
24 ///You can enable BT_USE_MEMCPY, then swapping elements in the array will use memcpy instead of operator=
25 ///see discussion here: https://bulletphysics.orgphpBB2/viewtopic.php?t=1231 and
26 ///http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1240
28 #define BT_USE_PLACEMENT_NEW 1
29 //#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...
30 #define BT_ALLOW_ARRAY_COPY_OPERATOR // enabling this can accidently perform deep copies of data if you are not careful
35 #endif //BT_USE_MEMCPY
37 #ifdef BT_USE_PLACEMENT_NEW
38 #include <new> //for placement new
39 #endif //BT_USE_PLACEMENT_NEW
41 ///The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods
42 ///It is developed to replace stl::vector to avoid portability issues, including STL alignment issues to add SIMD/SSE data
45 class btAlignedObjectArray
47 btAlignedAllocator<T, 16> m_allocator;
52 //PCK: added this line
55 #ifdef BT_ALLOW_ARRAY_COPY_OPERATOR
57 SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T>& other)
62 #else //BT_ALLOW_ARRAY_COPY_OPERATOR
64 SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T>& other);
65 #endif //BT_ALLOW_ARRAY_COPY_OPERATOR
68 SIMD_FORCE_INLINE int allocSize(int size)
70 return (size ? size * 2 : 1);
72 SIMD_FORCE_INLINE void copy(int start, int end, T* dest) const
75 for (i = start; i < end; ++i)
76 #ifdef BT_USE_PLACEMENT_NEW
77 new (&dest[i]) T(m_data[i]);
80 #endif //BT_USE_PLACEMENT_NEW
83 SIMD_FORCE_INLINE void init()
85 //PCK: added this line
91 SIMD_FORCE_INLINE void destroy(int first, int last)
94 for (i = first; i < last; i++)
100 SIMD_FORCE_INLINE void* allocate(int size)
103 return m_allocator.allocate(size);
107 SIMD_FORCE_INLINE void deallocate()
111 //PCK: enclosed the deallocation in this block
114 m_allocator.deallocate(m_data);
121 btAlignedObjectArray()
126 ~btAlignedObjectArray()
131 ///Generally it is best to avoid using the copy constructor of an btAlignedObjectArray, and use a (const) reference to the array instead.
132 btAlignedObjectArray(const btAlignedObjectArray& otherArray)
136 int otherSize = otherArray.size();
138 otherArray.copy(0, otherSize, m_data);
141 /// return the number of elements in the array
142 SIMD_FORCE_INLINE int size() const
147 SIMD_FORCE_INLINE const T& at(int n) const
150 btAssert(n < size());
154 SIMD_FORCE_INLINE T& at(int n)
157 btAssert(n < size());
161 SIMD_FORCE_INLINE const T& operator[](int n) const
164 btAssert(n < size());
168 SIMD_FORCE_INLINE T& operator[](int n)
171 btAssert(n < size());
175 ///clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
176 SIMD_FORCE_INLINE void clear()
185 SIMD_FORCE_INLINE void pop_back()
187 btAssert(m_size > 0);
192 ///resize changes the number of elements in the array. If the new size is larger, the new elements will be constructed using the optional second argument.
193 ///when the new number of elements is smaller, the destructor will be called, but memory will not be freed, to reduce performance overhead of run-time memory (de)allocations.
194 SIMD_FORCE_INLINE void resizeNoInitialize(int newsize)
196 if (newsize > size())
203 SIMD_FORCE_INLINE void resize(int newsize, const T& fillData = T())
205 const int curSize = size();
207 if (newsize < curSize)
209 for (int i = newsize; i < curSize; i++)
216 if (newsize > curSize)
220 #ifdef BT_USE_PLACEMENT_NEW
221 for (int i = curSize; i < newsize; i++)
223 new (&m_data[i]) T(fillData);
225 #endif //BT_USE_PLACEMENT_NEW
230 SIMD_FORCE_INLINE T& expandNonInitializing()
232 const int sz = size();
233 if (sz == capacity())
235 reserve(allocSize(size()));
242 SIMD_FORCE_INLINE T& expand(const T& fillValue = T())
244 const int sz = size();
245 if (sz == capacity())
247 reserve(allocSize(size()));
250 #ifdef BT_USE_PLACEMENT_NEW
251 new (&m_data[sz]) T(fillValue); //use the in-place new (not really allocating heap memory)
257 SIMD_FORCE_INLINE void push_back(const T& _Val)
259 const int sz = size();
260 if (sz == capacity())
262 reserve(allocSize(size()));
265 #ifdef BT_USE_PLACEMENT_NEW
266 new (&m_data[m_size]) T(_Val);
268 m_data[size()] = _Val;
269 #endif //BT_USE_PLACEMENT_NEW
274 /// return the pre-allocated (reserved) elements, this is at least as large as the total number of elements,see size() and reserve()
275 SIMD_FORCE_INLINE int capacity() const
280 SIMD_FORCE_INLINE void reserve(int _Count)
281 { // determine new minimum length of allocated storage
282 if (capacity() < _Count)
283 { // not enough room, reallocate
284 T* s = (T*)allocate(_Count);
292 //PCK: added this line
304 bool operator()(const T& a, const T& b) const
310 template <typename L>
311 void quickSortInternal(const L& CompareFunc, int lo, int hi)
313 // lo is the lower index, hi is the upper index
314 // of the region of array a that is to be sorted
316 T x = m_data[(lo + hi) / 2];
321 while (CompareFunc(m_data[i], x))
323 while (CompareFunc(x, m_data[j]))
335 quickSortInternal(CompareFunc, lo, j);
337 quickSortInternal(CompareFunc, i, hi);
340 template <typename L>
341 void quickSort(const L& CompareFunc)
343 //don't sort 0 or 1 elements
346 quickSortInternal(CompareFunc, 0, size() - 1);
350 ///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
351 template <typename L>
352 void downHeap(T* pArr, int k, int n, const L& CompareFunc)
354 /* PRE: a[k+1..N] is a heap */
355 /* POST: a[k..N] is a heap */
357 T temp = pArr[k - 1];
363 if ((child < n) && CompareFunc(pArr[child - 1], pArr[child]))
367 /* pick larger child */
368 if (CompareFunc(temp, pArr[child - 1]))
371 pArr[k - 1] = pArr[child - 1];
382 void swap(int index0, int index1)
385 char temp[sizeof(T)];
386 memcpy(temp, &m_data[index0], sizeof(T));
387 memcpy(&m_data[index0], &m_data[index1], sizeof(T));
388 memcpy(&m_data[index1], temp, sizeof(T));
390 T temp = m_data[index0];
391 m_data[index0] = m_data[index1];
392 m_data[index1] = temp;
393 #endif //BT_USE_PLACEMENT_NEW
396 template <typename L>
397 void heapSort(const L& CompareFunc)
399 /* sort a[0..N-1], N.B. 0 to N-1 */
402 for (k = n / 2; k > 0; k--)
404 downHeap(m_data, k, n, CompareFunc);
407 /* a[1..N] is now a heap */
410 swap(0, n - 1); /* largest of a[0..n-1] */
413 /* restore a[1..i-1] heap */
414 downHeap(m_data, 1, n, CompareFunc);
418 ///non-recursive binary search, assumes sorted array
419 int findBinarySearch(const T& key) const
422 int last = size() - 1;
424 //assume sorted array
425 while (first <= last)
427 int mid = (first + last) / 2; // compute mid point.
428 if (key > m_data[mid])
429 first = mid + 1; // repeat search in top half.
430 else if (key < m_data[mid])
431 last = mid - 1; // repeat search in bottom half.
433 return mid; // found it. return position /////
435 return size(); // failed to find key
438 int findLinearSearch(const T& key) const
443 for (i = 0; i < size(); i++)
445 if (m_data[i] == key)
454 // If the key is not in the array, return -1 instead of 0,
455 // since 0 also means the first element in the array.
456 int findLinearSearch2(const T& key) const
461 for (i = 0; i < size(); i++)
463 if (m_data[i] == key)
472 void removeAtIndex(int index)
476 swap(index, size() - 1);
480 void remove(const T& key)
482 int findIndex = findLinearSearch(key);
483 removeAtIndex(findIndex);
486 //PCK: whole function
487 void initializeFromBuffer(void* buffer, int size, int capacity)
490 m_ownsMemory = false;
493 m_capacity = capacity;
496 void copyFromArray(const btAlignedObjectArray& otherArray)
498 int otherSize = otherArray.size();
500 otherArray.copy(0, otherSize, m_data);
504 #endif //BT_OBJECT_ARRAY__