btAlignedObjectArray.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #define BT_ALLOW_ARRAY_COPY_OPERATOR // enabling this can accidently perform deep copies of data if you are not careful
  25. #ifdef BT_USE_MEMCPY
  26. #include <memory.h>
  27. #include <string.h>
  28. #endif //BT_USE_MEMCPY
  29. #ifdef BT_USE_PLACEMENT_NEW
  30. #include <new> //for placement new
  31. #endif //BT_USE_PLACEMENT_NEW
  32. // The register keyword is deprecated in C++11 so don't use it.
  33. #if __cplusplus > 199711L
  34. #define BT_REGISTER
  35. #else
  36. #define BT_REGISTER register
  37. #endif
  38. ///The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods
  39. ///It is developed to replace stl::vector to avoid portability issues, including STL alignment issues to add SIMD/SSE data
  40. template <typename T>
  41. //template <class T>
  42. class btAlignedObjectArray
  43. {
  44. btAlignedAllocator<T , 16> m_allocator;
  45. int m_size;
  46. int m_capacity;
  47. T* m_data;
  48. //PCK: added this line
  49. bool m_ownsMemory;
  50. #ifdef BT_ALLOW_ARRAY_COPY_OPERATOR
  51. public:
  52. SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T> &other)
  53. {
  54. copyFromArray(other);
  55. return *this;
  56. }
  57. #else//BT_ALLOW_ARRAY_COPY_OPERATOR
  58. private:
  59. SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T> &other);
  60. #endif//BT_ALLOW_ARRAY_COPY_OPERATOR
  61. protected:
  62. SIMD_FORCE_INLINE int allocSize(int size)
  63. {
  64. return (size ? size*2 : 1);
  65. }
  66. SIMD_FORCE_INLINE void copy(int start,int end, T* dest) const
  67. {
  68. int i;
  69. for (i=start;i<end;++i)
  70. #ifdef BT_USE_PLACEMENT_NEW
  71. new (&dest[i]) T(m_data[i]);
  72. #else
  73. dest[i] = m_data[i];
  74. #endif //BT_USE_PLACEMENT_NEW
  75. }
  76. SIMD_FORCE_INLINE void init()
  77. {
  78. //PCK: added this line
  79. m_ownsMemory = true;
  80. m_data = 0;
  81. m_size = 0;
  82. m_capacity = 0;
  83. }
  84. SIMD_FORCE_INLINE void destroy(int first,int last)
  85. {
  86. int i;
  87. for (i=first; i<last;i++)
  88. {
  89. m_data[i].~T();
  90. }
  91. }
  92. SIMD_FORCE_INLINE void* allocate(int size)
  93. {
  94. if (size)
  95. return m_allocator.allocate(size);
  96. return 0;
  97. }
  98. SIMD_FORCE_INLINE void deallocate()
  99. {
  100. if(m_data) {
  101. //PCK: enclosed the deallocation in this block
  102. if (m_ownsMemory)
  103. {
  104. m_allocator.deallocate(m_data);
  105. }
  106. m_data = 0;
  107. }
  108. }
  109. public:
  110. btAlignedObjectArray()
  111. {
  112. init();
  113. }
  114. ~btAlignedObjectArray()
  115. {
  116. clear();
  117. }
  118. ///Generally it is best to avoid using the copy constructor of an btAlignedObjectArray, and use a (const) reference to the array instead.
  119. btAlignedObjectArray(const btAlignedObjectArray& otherArray)
  120. {
  121. init();
  122. int otherSize = otherArray.size();
  123. resize (otherSize);
  124. otherArray.copy(0, otherSize, m_data);
  125. }
  126. /// return the number of elements in the array
  127. SIMD_FORCE_INLINE int size() const
  128. {
  129. return m_size;
  130. }
  131. SIMD_FORCE_INLINE const T& at(int n) const
  132. {
  133. btAssert(n>=0);
  134. btAssert(n<size());
  135. return m_data[n];
  136. }
  137. SIMD_FORCE_INLINE T& at(int n)
  138. {
  139. btAssert(n>=0);
  140. btAssert(n<size());
  141. return m_data[n];
  142. }
  143. SIMD_FORCE_INLINE const T& operator[](int n) const
  144. {
  145. btAssert(n>=0);
  146. btAssert(n<size());
  147. return m_data[n];
  148. }
  149. SIMD_FORCE_INLINE T& operator[](int n)
  150. {
  151. btAssert(n>=0);
  152. btAssert(n<size());
  153. return m_data[n];
  154. }
  155. ///clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
  156. SIMD_FORCE_INLINE void clear()
  157. {
  158. destroy(0,size());
  159. deallocate();
  160. init();
  161. }
  162. SIMD_FORCE_INLINE void pop_back()
  163. {
  164. btAssert(m_size>0);
  165. m_size--;
  166. m_data[m_size].~T();
  167. }
  168. ///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.
  169. ///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.
  170. SIMD_FORCE_INLINE void resizeNoInitialize(int newsize)
  171. {
  172. if (newsize > size())
  173. {
  174. reserve(newsize);
  175. }
  176. m_size = newsize;
  177. }
  178. SIMD_FORCE_INLINE void resize(int newsize, const T& fillData=T())
  179. {
  180. const BT_REGISTER int curSize = size();
  181. if (newsize < curSize)
  182. {
  183. for(int i = newsize; i < curSize; i++)
  184. {
  185. m_data[i].~T();
  186. }
  187. } else
  188. {
  189. if (newsize > curSize)
  190. {
  191. reserve(newsize);
  192. }
  193. #ifdef BT_USE_PLACEMENT_NEW
  194. for (int i=curSize;i<newsize;i++)
  195. {
  196. new ( &m_data[i]) T(fillData);
  197. }
  198. #endif //BT_USE_PLACEMENT_NEW
  199. }
  200. m_size = newsize;
  201. }
  202. SIMD_FORCE_INLINE T& expandNonInitializing( )
  203. {
  204. const BT_REGISTER int sz = size();
  205. if( sz == capacity() )
  206. {
  207. reserve( allocSize(size()) );
  208. }
  209. m_size++;
  210. return m_data[sz];
  211. }
  212. SIMD_FORCE_INLINE T& expand( const T& fillValue=T())
  213. {
  214. const BT_REGISTER int sz = size();
  215. if( sz == capacity() )
  216. {
  217. reserve( allocSize(size()) );
  218. }
  219. m_size++;
  220. #ifdef BT_USE_PLACEMENT_NEW
  221. new (&m_data[sz]) T(fillValue); //use the in-place new (not really allocating heap memory)
  222. #endif
  223. return m_data[sz];
  224. }
  225. SIMD_FORCE_INLINE void push_back(const T& _Val)
  226. {
  227. const BT_REGISTER int sz = size();
  228. if( sz == capacity() )
  229. {
  230. reserve( allocSize(size()) );
  231. }
  232. #ifdef BT_USE_PLACEMENT_NEW
  233. new ( &m_data[m_size] ) T(_Val);
  234. #else
  235. m_data[size()] = _Val;
  236. #endif //BT_USE_PLACEMENT_NEW
  237. m_size++;
  238. }
  239. /// return the pre-allocated (reserved) elements, this is at least as large as the total number of elements,see size() and reserve()
  240. SIMD_FORCE_INLINE int capacity() const
  241. {
  242. return m_capacity;
  243. }
  244. SIMD_FORCE_INLINE void reserve(int _Count)
  245. { // determine new minimum length of allocated storage
  246. if (capacity() < _Count)
  247. { // not enough room, reallocate
  248. T* s = (T*)allocate(_Count);
  249. copy(0, size(), s);
  250. destroy(0,size());
  251. deallocate();
  252. //PCK: added this line
  253. m_ownsMemory = true;
  254. m_data = s;
  255. m_capacity = _Count;
  256. }
  257. }
  258. class less
  259. {
  260. public:
  261. bool operator() ( const T& a, const T& b ) const
  262. {
  263. return ( a < b );
  264. }
  265. };
  266. template <typename L>
  267. void quickSortInternal(const L& CompareFunc,int lo, int hi)
  268. {
  269. // lo is the lower index, hi is the upper index
  270. // of the region of array a that is to be sorted
  271. int i=lo, j=hi;
  272. T x=m_data[(lo+hi)/2];
  273. // partition
  274. do
  275. {
  276. while (CompareFunc(m_data[i],x))
  277. i++;
  278. while (CompareFunc(x,m_data[j]))
  279. j--;
  280. if (i<=j)
  281. {
  282. swap(i,j);
  283. i++; j--;
  284. }
  285. } while (i<=j);
  286. // recursion
  287. if (lo<j)
  288. quickSortInternal( CompareFunc, lo, j);
  289. if (i<hi)
  290. quickSortInternal( CompareFunc, i, hi);
  291. }
  292. template <typename L>
  293. void quickSort(const L& CompareFunc)
  294. {
  295. //don't sort 0 or 1 elements
  296. if (size()>1)
  297. {
  298. quickSortInternal(CompareFunc,0,size()-1);
  299. }
  300. }
  301. ///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
  302. template <typename L>
  303. void downHeap(T *pArr, int k, int n, const L& CompareFunc)
  304. {
  305. /* PRE: a[k+1..N] is a heap */
  306. /* POST: a[k..N] is a heap */
  307. T temp = pArr[k - 1];
  308. /* k has child(s) */
  309. while (k <= n/2)
  310. {
  311. int child = 2*k;
  312. if ((child < n) && CompareFunc(pArr[child - 1] , pArr[child]))
  313. {
  314. child++;
  315. }
  316. /* pick larger child */
  317. if (CompareFunc(temp , pArr[child - 1]))
  318. {
  319. /* move child up */
  320. pArr[k - 1] = pArr[child - 1];
  321. k = child;
  322. }
  323. else
  324. {
  325. break;
  326. }
  327. }
  328. pArr[k - 1] = temp;
  329. } /*downHeap*/
  330. void swap(int index0,int index1)
  331. {
  332. #ifdef BT_USE_MEMCPY
  333. char temp[sizeof(T)];
  334. memcpy(temp,&m_data[index0],sizeof(T));
  335. memcpy(&m_data[index0],&m_data[index1],sizeof(T));
  336. memcpy(&m_data[index1],temp,sizeof(T));
  337. #else
  338. T temp = m_data[index0];
  339. m_data[index0] = m_data[index1];
  340. m_data[index1] = temp;
  341. #endif //BT_USE_PLACEMENT_NEW
  342. }
  343. template <typename L>
  344. void heapSort(const L& CompareFunc)
  345. {
  346. /* sort a[0..N-1], N.B. 0 to N-1 */
  347. int k;
  348. int n = m_size;
  349. for (k = n/2; k > 0; k--)
  350. {
  351. downHeap(m_data, k, n, CompareFunc);
  352. }
  353. /* a[1..N] is now a heap */
  354. while ( n>=1 )
  355. {
  356. swap(0,n-1); /* largest of a[0..n-1] */
  357. n = n - 1;
  358. /* restore a[1..i-1] heap */
  359. downHeap(m_data, 1, n, CompareFunc);
  360. }
  361. }
  362. ///non-recursive binary search, assumes sorted array
  363. int findBinarySearch(const T& key) const
  364. {
  365. int first = 0;
  366. int last = size()-1;
  367. //assume sorted array
  368. while (first <= last) {
  369. int mid = (first + last) / 2; // compute mid point.
  370. if (key > m_data[mid])
  371. first = mid + 1; // repeat search in top half.
  372. else if (key < m_data[mid])
  373. last = mid - 1; // repeat search in bottom half.
  374. else
  375. return mid; // found it. return position /////
  376. }
  377. return size(); // failed to find key
  378. }
  379. int findLinearSearch(const T& key) const
  380. {
  381. int index=size();
  382. int i;
  383. for (i=0;i<size();i++)
  384. {
  385. if (m_data[i] == key)
  386. {
  387. index = i;
  388. break;
  389. }
  390. }
  391. return index;
  392. }
  393. // If the key is not in the array, return -1 instead of 0,
  394. // since 0 also means the first element in the array.
  395. int findLinearSearch2(const T& key) const
  396. {
  397. int index=-1;
  398. int i;
  399. for (i=0;i<size();i++)
  400. {
  401. if (m_data[i] == key)
  402. {
  403. index = i;
  404. break;
  405. }
  406. }
  407. return index;
  408. }
  409. void removeAtIndex(int index)
  410. {
  411. if (index<size())
  412. {
  413. swap( index,size()-1);
  414. pop_back();
  415. }
  416. }
  417. void remove(const T& key)
  418. {
  419. int findIndex = findLinearSearch(key);
  420. removeAtIndex(findIndex);
  421. }
  422. //PCK: whole function
  423. void initializeFromBuffer(void *buffer, int size, int capacity)
  424. {
  425. clear();
  426. m_ownsMemory = false;
  427. m_data = (T*)buffer;
  428. m_size = size;
  429. m_capacity = capacity;
  430. }
  431. void copyFromArray(const btAlignedObjectArray& otherArray)
  432. {
  433. int otherSize = otherArray.size();
  434. resize (otherSize);
  435. otherArray.copy(0, otherSize, m_data);
  436. }
  437. };
  438. #endif //BT_OBJECT_ARRAY__