btAlignedObjectArray.h 12 KB

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