btAlignedObjectArray.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_OBJECT_ARRAY__
  14. #define BT_OBJECT_ARRAY__
  15. #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
  16. #include "btAlignedAllocator.h"
  17. ///If the platform doesn't support placement new, you can disable BT_USE_PLACEMENT_NEW
  18. ///then the btAlignedObjectArray doesn't support objects with virtual methods, and non-trivial constructors/destructors
  19. ///You can enable BT_USE_MEMCPY, then swapping elements in the array will use memcpy instead of operator=
  20. ///see discussion here: http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1231 and
  21. ///http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=1240
  22. #define BT_USE_PLACEMENT_NEW 1
  23. //#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...
  24. #ifdef BT_USE_MEMCPY
  25. #include <memory.h>
  26. #include <string.h>
  27. #endif //BT_USE_MEMCPY
  28. #ifdef BT_USE_PLACEMENT_NEW
  29. #include <new> //for placement new
  30. #endif //BT_USE_PLACEMENT_NEW
  31. ///The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods
  32. ///It is developed to replace stl::vector to avoid portability issues, including STL alignment issues to add SIMD/SSE data
  33. template <typename T>
  34. //template <class T>
  35. class btAlignedObjectArray
  36. {
  37. btAlignedAllocator<T , 16> m_allocator;
  38. int m_size;
  39. int m_capacity;
  40. T* m_data;
  41. //PCK: added this line
  42. bool m_ownsMemory;
  43. protected:
  44. SIMD_FORCE_INLINE int allocSize(int size)
  45. {
  46. return (size ? size*2 : 1);
  47. }
  48. SIMD_FORCE_INLINE void copy(int start,int end, T* dest) const
  49. {
  50. int i;
  51. for (i=start;i<end;++i)
  52. #ifdef BT_USE_PLACEMENT_NEW
  53. new (&dest[i]) T(m_data[i]);
  54. #else
  55. dest[i] = m_data[i];
  56. #endif //BT_USE_PLACEMENT_NEW
  57. }
  58. SIMD_FORCE_INLINE void init()
  59. {
  60. //PCK: added this line
  61. m_ownsMemory = true;
  62. m_data = 0;
  63. m_size = 0;
  64. m_capacity = 0;
  65. }
  66. SIMD_FORCE_INLINE void destroy(int first,int last)
  67. {
  68. int i;
  69. for (i=first; i<last;i++)
  70. {
  71. m_data[i].~T();
  72. }
  73. }
  74. SIMD_FORCE_INLINE void* allocate(int size)
  75. {
  76. if (size)
  77. return m_allocator.allocate(size);
  78. return 0;
  79. }
  80. SIMD_FORCE_INLINE void deallocate()
  81. {
  82. if(m_data) {
  83. //PCK: enclosed the deallocation in this block
  84. if (m_ownsMemory)
  85. {
  86. m_allocator.deallocate(m_data);
  87. }
  88. m_data = 0;
  89. }
  90. }
  91. public:
  92. btAlignedObjectArray()
  93. {
  94. init();
  95. }
  96. ~btAlignedObjectArray()
  97. {
  98. clear();
  99. }
  100. ///Generally it is best to avoid using the copy constructor of an btAlignedObjectArray, and use a (const) reference to the array instead.
  101. btAlignedObjectArray(const btAlignedObjectArray& otherArray)
  102. {
  103. init();
  104. int otherSize = otherArray.size();
  105. resize (otherSize);
  106. otherArray.copy(0, otherSize, m_data);
  107. }
  108. /// return the number of elements in the array
  109. SIMD_FORCE_INLINE int size() const
  110. {
  111. return m_size;
  112. }
  113. SIMD_FORCE_INLINE const T& at(int n) const
  114. {
  115. return m_data[n];
  116. }
  117. SIMD_FORCE_INLINE T& at(int n)
  118. {
  119. return m_data[n];
  120. }
  121. SIMD_FORCE_INLINE const T& operator[](int n) const
  122. {
  123. return m_data[n];
  124. }
  125. SIMD_FORCE_INLINE T& operator[](int n)
  126. {
  127. return m_data[n];
  128. }
  129. ///clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
  130. SIMD_FORCE_INLINE void clear()
  131. {
  132. destroy(0,size());
  133. deallocate();
  134. init();
  135. }
  136. SIMD_FORCE_INLINE void pop_back()
  137. {
  138. m_size--;
  139. m_data[m_size].~T();
  140. }
  141. ///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.
  142. ///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.
  143. SIMD_FORCE_INLINE void resize(int newsize, const T& fillData=T())
  144. {
  145. int curSize = size();
  146. if (newsize < curSize)
  147. {
  148. for(int i = newsize; i < curSize; i++)
  149. {
  150. m_data[i].~T();
  151. }
  152. } else
  153. {
  154. if (newsize > size())
  155. {
  156. reserve(newsize);
  157. }
  158. #ifdef BT_USE_PLACEMENT_NEW
  159. for (int i=curSize;i<newsize;i++)
  160. {
  161. new ( &m_data[i]) T(fillData);
  162. }
  163. #endif //BT_USE_PLACEMENT_NEW
  164. }
  165. m_size = newsize;
  166. }
  167. SIMD_FORCE_INLINE T& expandNonInitializing( )
  168. {
  169. int sz = size();
  170. if( sz == capacity() )
  171. {
  172. reserve( allocSize(size()) );
  173. }
  174. m_size++;
  175. return m_data[sz];
  176. }
  177. SIMD_FORCE_INLINE T& expand( const T& fillValue=T())
  178. {
  179. int sz = size();
  180. if( sz == capacity() )
  181. {
  182. reserve( allocSize(size()) );
  183. }
  184. m_size++;
  185. #ifdef BT_USE_PLACEMENT_NEW
  186. new (&m_data[sz]) T(fillValue); //use the in-place new (not really allocating heap memory)
  187. #endif
  188. return m_data[sz];
  189. }
  190. SIMD_FORCE_INLINE void push_back(const T& _Val)
  191. {
  192. int sz = size();
  193. if( sz == capacity() )
  194. {
  195. reserve( allocSize(size()) );
  196. }
  197. #ifdef BT_USE_PLACEMENT_NEW
  198. new ( &m_data[m_size] ) T(_Val);
  199. #else
  200. m_data[size()] = _Val;
  201. #endif //BT_USE_PLACEMENT_NEW
  202. m_size++;
  203. }
  204. /// return the pre-allocated (reserved) elements, this is at least as large as the total number of elements,see size() and reserve()
  205. SIMD_FORCE_INLINE int capacity() const
  206. {
  207. return m_capacity;
  208. }
  209. SIMD_FORCE_INLINE void reserve(int _Count)
  210. { // determine new minimum length of allocated storage
  211. if (capacity() < _Count)
  212. { // not enough room, reallocate
  213. T* s = (T*)allocate(_Count);
  214. copy(0, size(), s);
  215. destroy(0,size());
  216. deallocate();
  217. //PCK: added this line
  218. m_ownsMemory = true;
  219. m_data = s;
  220. m_capacity = _Count;
  221. }
  222. }
  223. class less
  224. {
  225. public:
  226. bool operator() ( const T& a, const T& b )
  227. {
  228. return ( a < b );
  229. }
  230. };
  231. template <typename L>
  232. void quickSortInternal(L CompareFunc,int lo, int hi)
  233. {
  234. // lo is the lower index, hi is the upper index
  235. // of the region of array a that is to be sorted
  236. int i=lo, j=hi;
  237. T x=m_data[(lo+hi)/2];
  238. // partition
  239. do
  240. {
  241. while (CompareFunc(m_data[i],x))
  242. i++;
  243. while (CompareFunc(x,m_data[j]))
  244. j--;
  245. if (i<=j)
  246. {
  247. swap(i,j);
  248. i++; j--;
  249. }
  250. } while (i<=j);
  251. // recursion
  252. if (lo<j)
  253. quickSortInternal( CompareFunc, lo, j);
  254. if (i<hi)
  255. quickSortInternal( CompareFunc, i, hi);
  256. }
  257. template <typename L>
  258. void quickSort(L CompareFunc)
  259. {
  260. //don't sort 0 or 1 elements
  261. if (size()>1)
  262. {
  263. quickSortInternal(CompareFunc,0,size()-1);
  264. }
  265. }
  266. ///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
  267. template <typename L>
  268. void downHeap(T *pArr, int k, int n,L CompareFunc)
  269. {
  270. /* PRE: a[k+1..N] is a heap */
  271. /* POST: a[k..N] is a heap */
  272. T temp = pArr[k - 1];
  273. /* k has child(s) */
  274. while (k <= n/2)
  275. {
  276. int child = 2*k;
  277. if ((child < n) && CompareFunc(pArr[child - 1] , pArr[child]))
  278. {
  279. child++;
  280. }
  281. /* pick larger child */
  282. if (CompareFunc(temp , pArr[child - 1]))
  283. {
  284. /* move child up */
  285. pArr[k - 1] = pArr[child - 1];
  286. k = child;
  287. }
  288. else
  289. {
  290. break;
  291. }
  292. }
  293. pArr[k - 1] = temp;
  294. } /*downHeap*/
  295. void swap(int index0,int index1)
  296. {
  297. #ifdef BT_USE_MEMCPY
  298. char temp[sizeof(T)];
  299. memcpy(temp,&m_data[index0],sizeof(T));
  300. memcpy(&m_data[index0],&m_data[index1],sizeof(T));
  301. memcpy(&m_data[index1],temp,sizeof(T));
  302. #else
  303. T temp = m_data[index0];
  304. m_data[index0] = m_data[index1];
  305. m_data[index1] = temp;
  306. #endif //BT_USE_PLACEMENT_NEW
  307. }
  308. template <typename L>
  309. void heapSort(L CompareFunc)
  310. {
  311. /* sort a[0..N-1], N.B. 0 to N-1 */
  312. int k;
  313. int n = m_size;
  314. for (k = n/2; k > 0; k--)
  315. {
  316. downHeap(m_data, k, n, CompareFunc);
  317. }
  318. /* a[1..N] is now a heap */
  319. while ( n>=1 )
  320. {
  321. swap(0,n-1); /* largest of a[0..n-1] */
  322. n = n - 1;
  323. /* restore a[1..i-1] heap */
  324. downHeap(m_data, 1, n, CompareFunc);
  325. }
  326. }
  327. ///non-recursive binary search, assumes sorted array
  328. int findBinarySearch(const T& key) const
  329. {
  330. int first = 0;
  331. int last = size();
  332. //assume sorted array
  333. while (first <= last) {
  334. int mid = (first + last) / 2; // compute mid point.
  335. if (key > m_data[mid])
  336. first = mid + 1; // repeat search in top half.
  337. else if (key < m_data[mid])
  338. last = mid - 1; // repeat search in bottom half.
  339. else
  340. return mid; // found it. return position /////
  341. }
  342. return size(); // failed to find key
  343. }
  344. int findLinearSearch(const T& key) const
  345. {
  346. int index=size();
  347. int i;
  348. for (i=0;i<size();i++)
  349. {
  350. if (m_data[i] == key)
  351. {
  352. index = i;
  353. break;
  354. }
  355. }
  356. return index;
  357. }
  358. void remove(const T& key)
  359. {
  360. int findIndex = findLinearSearch(key);
  361. if (findIndex<size())
  362. {
  363. swap( findIndex,size()-1);
  364. pop_back();
  365. }
  366. }
  367. //PCK: whole function
  368. void initializeFromBuffer(void *buffer, int size, int capacity)
  369. {
  370. clear();
  371. m_ownsMemory = false;
  372. m_data = (T*)buffer;
  373. m_size = size;
  374. m_capacity = capacity;
  375. }
  376. void copyFromArray(const btAlignedObjectArray& otherArray)
  377. {
  378. int otherSize = otherArray.size();
  379. resize (otherSize);
  380. otherArray.copy(0, otherSize, m_data);
  381. }
  382. };
  383. #endif //BT_OBJECT_ARRAY__