b3AlignedObjectArray.h 11 KB

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