b3AlignedObjectArray.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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: http://continuousphysics.com/Bullet/phpBB2/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. //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. b3AlignedObjectArray()
  105. {
  106. init();
  107. }
  108. ~b3AlignedObjectArray()
  109. {
  110. clear();
  111. }
  112. ///Generally it is best to avoid using the copy constructor of an b3AlignedObjectArray, and use a (const) reference to the array instead.
  113. b3AlignedObjectArray(const b3AlignedObjectArray& 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. B3_FORCE_INLINE int size() const
  122. {
  123. return m_size;
  124. }
  125. B3_FORCE_INLINE const T& at(int n) const
  126. {
  127. b3Assert(n>=0);
  128. b3Assert(n<size());
  129. return m_data[n];
  130. }
  131. B3_FORCE_INLINE T& at(int n)
  132. {
  133. b3Assert(n>=0);
  134. b3Assert(n<size());
  135. return m_data[n];
  136. }
  137. B3_FORCE_INLINE const T& operator[](int n) const
  138. {
  139. b3Assert(n>=0);
  140. b3Assert(n<size());
  141. return m_data[n];
  142. }
  143. B3_FORCE_INLINE T& operator[](int n)
  144. {
  145. b3Assert(n>=0);
  146. b3Assert(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. B3_FORCE_INLINE void clear()
  151. {
  152. destroy(0,size());
  153. deallocate();
  154. init();
  155. }
  156. B3_FORCE_INLINE void pop_back()
  157. {
  158. b3Assert(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. B3_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. B3_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 B3_USE_PLACEMENT_NEW
  195. for (int i=curSize;i<newsize;i++)
  196. {
  197. new ( &m_data[i]) T(fillData);
  198. }
  199. #endif //B3_USE_PLACEMENT_NEW
  200. }
  201. m_size = newsize;
  202. }
  203. B3_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. B3_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 B3_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. B3_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 B3_USE_PLACEMENT_NEW
  234. new ( &m_data[m_size] ) T(_Val);
  235. #else
  236. m_data[size()] = _Val;
  237. #endif //B3_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. B3_FORCE_INLINE int capacity() const
  242. {
  243. return m_capacity;
  244. }
  245. B3_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. b3Assert(s);
  251. if (s==0)
  252. {
  253. b3Error("b3AlignedObjectArray reserve out-of-memory\n");
  254. _Count=0;
  255. m_size=0;
  256. }
  257. copy(0, size(), s);
  258. destroy(0,size());
  259. deallocate();
  260. //PCK: added this line
  261. m_ownsMemory = true;
  262. m_data = s;
  263. m_capacity = _Count;
  264. }
  265. }
  266. class less
  267. {
  268. public:
  269. bool operator() ( const T& a, const T& b )
  270. {
  271. return ( a < b );
  272. }
  273. };
  274. template <typename L>
  275. void quickSortInternal(const L& CompareFunc,int lo, int hi)
  276. {
  277. // lo is the lower index, hi is the upper index
  278. // of the region of array a that is to be sorted
  279. int i=lo, j=hi;
  280. T x=m_data[(lo+hi)/2];
  281. // partition
  282. do
  283. {
  284. while (CompareFunc(m_data[i],x))
  285. i++;
  286. while (CompareFunc(x,m_data[j]))
  287. j--;
  288. if (i<=j)
  289. {
  290. swap(i,j);
  291. i++; j--;
  292. }
  293. } while (i<=j);
  294. // recursion
  295. if (lo<j)
  296. quickSortInternal( CompareFunc, lo, j);
  297. if (i<hi)
  298. quickSortInternal( CompareFunc, i, hi);
  299. }
  300. template <typename L>
  301. void quickSort(const L& CompareFunc)
  302. {
  303. //don't sort 0 or 1 elements
  304. if (size()>1)
  305. {
  306. quickSortInternal(CompareFunc,0,size()-1);
  307. }
  308. }
  309. ///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/
  310. template <typename L>
  311. void downHeap(T *pArr, int k, int n, const L& CompareFunc)
  312. {
  313. /* PRE: a[k+1..N] is a heap */
  314. /* POST: a[k..N] is a heap */
  315. T temp = pArr[k - 1];
  316. /* k has child(s) */
  317. while (k <= n/2)
  318. {
  319. int child = 2*k;
  320. if ((child < n) && CompareFunc(pArr[child - 1] , pArr[child]))
  321. {
  322. child++;
  323. }
  324. /* pick larger child */
  325. if (CompareFunc(temp , pArr[child - 1]))
  326. {
  327. /* move child up */
  328. pArr[k - 1] = pArr[child - 1];
  329. k = child;
  330. }
  331. else
  332. {
  333. break;
  334. }
  335. }
  336. pArr[k - 1] = temp;
  337. } /*downHeap*/
  338. void swap(int index0,int index1)
  339. {
  340. #ifdef B3_USE_MEMCPY
  341. char temp[sizeof(T)];
  342. memcpy(temp,&m_data[index0],sizeof(T));
  343. memcpy(&m_data[index0],&m_data[index1],sizeof(T));
  344. memcpy(&m_data[index1],temp,sizeof(T));
  345. #else
  346. T temp = m_data[index0];
  347. m_data[index0] = m_data[index1];
  348. m_data[index1] = temp;
  349. #endif //B3_USE_PLACEMENT_NEW
  350. }
  351. template <typename L>
  352. void heapSort(const L& CompareFunc)
  353. {
  354. /* sort a[0..N-1], N.B. 0 to N-1 */
  355. int k;
  356. int n = m_size;
  357. for (k = n/2; k > 0; k--)
  358. {
  359. downHeap(m_data, k, n, CompareFunc);
  360. }
  361. /* a[1..N] is now a heap */
  362. while ( n>=1 )
  363. {
  364. swap(0,n-1); /* largest of a[0..n-1] */
  365. n = n - 1;
  366. /* restore a[1..i-1] heap */
  367. downHeap(m_data, 1, n, CompareFunc);
  368. }
  369. }
  370. ///non-recursive binary search, assumes sorted array
  371. int findBinarySearch(const T& key) const
  372. {
  373. int first = 0;
  374. int last = size()-1;
  375. //assume sorted array
  376. while (first <= last) {
  377. int mid = (first + last) / 2; // compute mid point.
  378. if (key > m_data[mid])
  379. first = mid + 1; // repeat search in top half.
  380. else if (key < m_data[mid])
  381. last = mid - 1; // repeat search in bottom half.
  382. else
  383. return mid; // found it. return position /////
  384. }
  385. return size(); // failed to find key
  386. }
  387. int findLinearSearch(const T& key) const
  388. {
  389. int index=size();
  390. int i;
  391. for (i=0;i<size();i++)
  392. {
  393. if (m_data[i] == key)
  394. {
  395. index = i;
  396. break;
  397. }
  398. }
  399. return index;
  400. }
  401. int findLinearSearch2(const T& key) const
  402. {
  403. int index=-1;
  404. int i;
  405. for (i=0;i<size();i++)
  406. {
  407. if (m_data[i] == key)
  408. {
  409. index = i;
  410. break;
  411. }
  412. }
  413. return index;
  414. }
  415. void remove(const T& key)
  416. {
  417. int findIndex = findLinearSearch(key);
  418. if (findIndex<size())
  419. {
  420. swap( findIndex,size()-1);
  421. pop_back();
  422. }
  423. }
  424. //PCK: whole function
  425. void initializeFromBuffer(void *buffer, int size, int capacity)
  426. {
  427. clear();
  428. m_ownsMemory = false;
  429. m_data = (T*)buffer;
  430. m_size = size;
  431. m_capacity = capacity;
  432. }
  433. void copyFromArray(const b3AlignedObjectArray& otherArray)
  434. {
  435. int otherSize = otherArray.size();
  436. resize (otherSize);
  437. otherArray.copy(0, otherSize, m_data);
  438. }
  439. void removeAtIndex(int index)
  440. {
  441. if (index<size())
  442. {
  443. swap( index,size()-1);
  444. pop_back();
  445. }
  446. }
  447. };
  448. #endif //B3_OBJECT_ARRAY__