You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

501 line
11 KiB

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