FastLED 3.9.15
Loading...
Searching...
No Matches

◆ resize() [2/2]

template<typename T, typename Allocator = fl::allocator<T>>
void fl::HeapVector< T, Allocator >::resize ( fl::size n,
const T & value )
inline

Definition at line 479 of file vector.h.

479 {
480 if (n > mCapacity) {
481 // Need to allocate more space
482 T* new_array = mAlloc.allocate(n);
483
484 // Move existing elements
485 for (fl::size i = 0; i < mSize; ++i) {
486 mAlloc.construct(&new_array[i], fl::move(mArray[i]));
487 }
488
489 // Initialize new elements with value
490 for (fl::size i = mSize; i < n; ++i) {
491 mAlloc.construct(&new_array[i], value);
492 }
493
494 // Clean up old array
495 if (mArray) {
496 for (fl::size i = 0; i < mSize; ++i) {
497 mAlloc.destroy(&mArray[i]);
498 }
499 mAlloc.deallocate(mArray, mCapacity);
500 }
501
503 mCapacity = n;
504 mSize = n;
505 } else if (n > mSize) {
506 // Just need to add more elements
507 for (fl::size i = mSize; i < n; ++i) {
508 mAlloc.construct(&mArray[i], value);
509 }
510 mSize = n;
511 } else if (n < mSize) {
512 // Need to remove elements
513 for (fl::size i = n; i < mSize; ++i) {
514 mAlloc.destroy(&mArray[i]);
515 }
516 mSize = n;
517 }
518 }