MCPcopy Create free account
hub / github.com/FastLED/FastLED / swap_impl

Method swap_impl

src/fl/stl/basic_vector.cpp.hpp:360–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

358// ======= SWAP =======
359
360void vector_basic::swap_impl(vector_basic& other) {
361 // Swap all members. This works correctly even with inline buffers
362 // because we swap the offset too, and the inline data stays in place.
363 // HOWEVER: if either vector uses inline storage, we need to handle
364 // it carefully since inline buffers can't be swapped by pointer.
365
366 bool this_inline = isInline();
367 bool other_inline = other.isInline();
368
369 if (!this_inline && !other_inline) {
370 // Both on heap: just swap pointers and metadata
371 void* tmp_array = mArray;
372 mArray = other.mArray;
373 other.mArray = tmp_array;
374
375 fl::size tmp_cap = mCapacity;
376 mCapacity = other.mCapacity;
377 other.mCapacity = tmp_cap;
378 } else if (this_inline && other_inline) {
379 // Both inline: swap element data in-place
380 fl::size max_size = mSize > other.mSize ? mSize : other.mSize;
381 for (fl::size i = 0; i < max_size; ++i) {
382 if (i < mSize && i < other.mSize) {
383 if (mOps) {
384 mOps->swap_elements(element_ptr(i), other.element_ptr(i));
385 } else {
386 trivial_swap(element_ptr(i), other.element_ptr(i));
387 }
388 } else if (i < mSize) {
389 // Move this[i] to other[i], destroy this[i]
390 if (mOps) {
391 mOps->move_construct(other.element_ptr(i), element_ptr(i));
392 mOps->destroy(element_ptr(i));
393 } else {
394 fl::memcpy(other.element_ptr(i), element_ptr(i), mElementSize);
395 }
396 } else {
397 // Move other[i] to this[i], destroy other[i]
398 if (mOps) {
399 mOps->move_construct(element_ptr(i), other.element_ptr(i));
400 mOps->destroy(other.element_ptr(i));
401 } else {
402 fl::memcpy(element_ptr(i), other.element_ptr(i), mElementSize);
403 }
404 }
405 }
406 } else if (this_inline) {
407 // this is inline, other is on heap
408 // Move this's inline data to a temp, take other's heap pointer,
409 // move temp data to other's inline buffer
410 void* other_array = other.mArray;
411 fl::size other_cap = other.mCapacity;
412
413 // Move this's elements to other's inline buffer
414 if (other.hasInlineBuffer() && mSize <= other.mInlineCapacity) {
415 other.mArray = other.inlineBufferPtr();
416 other.mCapacity = other.mInlineCapacity;
417 if (mOps) {

Callers

nothing calls this directly

Calls 7

element_ptrMethod · 0.80
hasInlineBufferMethod · 0.80
memcpyFunction · 0.50
isInlineMethod · 0.45
destroyMethod · 0.45
inlineBufferPtrMethod · 0.45
allocateMethod · 0.45

Tested by

no test coverage detected