| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef BT_OBJECT_ARRAY__ |
| 17 |
#define BT_OBJECT_ARRAY__ |
| 18 |
|
| 19 |
#include "btAlignedAllocator.h" |
| 20 |
#include "btScalar.h" |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#define BT_USE_PLACEMENT_NEW 1 |
| 29 |
|
| 30 |
#define BT_ALLOW_ARRAY_COPY_OPERATOR |
| 31 |
|
| 32 |
#ifdef BT_USE_MEMCPY |
| 33 |
#include <memory.h> |
| 34 |
#include <string.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef BT_USE_PLACEMENT_NEW |
| 38 |
#include <new> |
| 39 |
#endif |
| 40 |
|
| 41 |
namespace VHACD { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
template <typename T> |
| 46 |
|
| 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 |
|
| 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 |
| 64 |
private: |
| 65 |
SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T>& other); |
| 66 |
#endif |
| 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 |
| 82 |
} |
| 83 |
|
| 84 |
SIMD_FORCE_INLINE void init() |
| 85 |
{ |
| 86 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 191 |
|
| 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 |
| 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); |
| 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 |
| 252 |
|
| 253 |
m_size++; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 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 |
{ |
| 264 |
if (capacity() < _Count) { |
| 265 |
T* s = (T*)allocate(_Count); |
| 266 |
|
| 267 |
copy(0, size(), s); |
| 268 |
|
| 269 |
destroy(0, size()); |
| 270 |
|
| 271 |
deallocate(); |
| 272 |
|
| 273 |
|
| 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 |
|
| 294 |
|
| 295 |
int32_t i = lo, j = hi; |
| 296 |
T x = m_data[(lo + hi) / 2]; |
| 297 |
|
| 298 |
|
| 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 |
|
| 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 |
|
| 322 |
if (size() > 1) { |
| 323 |
quickSortInternal(CompareFunc, 0, size() - 1); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
template <typename L> |
| 329 |
void downHeap(T* pArr, int32_t k, int32_t n, const L& CompareFunc) |
| 330 |
{ |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
T temp = pArr[k - 1]; |
| 335 |
|
| 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 |
|
| 343 |
if (CompareFunc(temp, pArr[child - 1])) { |
| 344 |
|
| 345 |
pArr[k - 1] = pArr[child - 1]; |
| 346 |
k = child; |
| 347 |
} |
| 348 |
else { |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
pArr[k - 1] = temp; |
| 353 |
} |
| 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 |
| 367 |
} |
| 368 |
|
| 369 |
template <typename L> |
| 370 |
void heapSort(const L& CompareFunc) |
| 371 |
{ |
| 372 |
|
| 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 |
|
| 380 |
while (n >= 1) { |
| 381 |
swap(0, n - 1); |
| 382 |
|
| 383 |
n = n - 1; |
| 384 |
|
| 385 |
downHeap(m_data, 1, n, CompareFunc); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
int32_t findBinarySearch(const T& key) const |
| 391 |
{ |
| 392 |
int32_t first = 0; |
| 393 |
int32_t last = size() - 1; |
| 394 |
|
| 395 |
|
| 396 |
while (first <= last) { |
| 397 |
int32_t mid = (first + last) / 2; |
| 398 |
if (key > m_data[mid]) |
| 399 |
first = mid + 1; |
| 400 |
else if (key < m_data[mid]) |
| 401 |
last = mid - 1; |
| 402 |
else |
| 403 |
return mid; |
| 404 |
} |
| 405 |
return size(); |
| 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 |
|
| 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 |
}; |
| 451 |
|
| 452 |
#endif |