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 ( size_t n,
const T & value )
inline

Definition at line 421 of file vector.h.

421 {
422 if (n > mCapacity) {
423 // Need to allocate more space
424 T* new_array = mAlloc.allocate(n);
425
426 // Copy existing elements
427 for (size_t i = 0; i < mSize; ++i) {
428 mAlloc.construct(&new_array[i], mArray[i]);
429 }
430
431 // Initialize new elements with value
432 for (size_t i = mSize; i < n; ++i) {
433 mAlloc.construct(&new_array[i], value);
434 }
435
436 // Clean up old array
437 if (mArray) {
438 for (size_t i = 0; i < mSize; ++i) {
439 mAlloc.destroy(&mArray[i]);
440 }
441 mAlloc.deallocate(mArray, mCapacity);
442 }
443
445 mCapacity = n;
446 mSize = n;
447 } else if (n > mSize) {
448 // Just need to add more elements
449 for (size_t i = mSize; i < n; ++i) {
450 mAlloc.construct(&mArray[i], value);
451 }
452 mSize = n;
453 } else if (n < mSize) {
454 // Need to remove elements
455 for (size_t i = n; i < mSize; ++i) {
456 mAlloc.destroy(&mArray[i]);
457 }
458 mSize = n;
459 }
460 }