Debian Codegraph @

thirdparty/vhacd/inc/btAlignedObjectArray.h

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