PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Dependencies/include/Bullet/LinearMath/btAlignedObjectArray.h

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