| 236 | // ======= INSERT ======= |
| 237 | |
| 238 | void vector_basic::insert_copy_impl(fl::size index, const void* element) { |
| 239 | if (index > mSize) index = mSize; |
| 240 | ensure_capacity(mSize + 1); |
| 241 | if (mSize >= mCapacity) return; // allocation failed |
| 242 | |
| 243 | if (mOps) { |
| 244 | // Shift elements right, starting from the end |
| 245 | if (mSize > index) { |
| 246 | // Move-construct the last element into uninitialized space |
| 247 | mOps->move_construct(element_ptr(mSize), element_ptr(mSize - 1)); |
| 248 | // Move-assign backwards for the rest |
| 249 | for (fl::size i = mSize - 1; i > index; --i) { |
| 250 | // Destroy dst, then move-construct from src |
| 251 | mOps->destroy(element_ptr(i)); |
| 252 | mOps->move_construct(element_ptr(i), element_ptr(i - 1)); |
| 253 | } |
| 254 | // Destroy the slot and copy-construct the new element |
| 255 | mOps->destroy(element_ptr(index)); |
| 256 | } |
| 257 | mOps->copy_construct(element_ptr(index), element); |
| 258 | } else { |
| 259 | // Trivial: memmove right, then memcpy |
| 260 | if (mSize > index) { |
| 261 | trivial_move_left(element_ptr(index + 1), element_ptr(index), |
| 262 | mSize - index); |
| 263 | } |
| 264 | fl::memcpy(element_ptr(index), element, mElementSize); |
| 265 | } |
| 266 | ++mSize; |
| 267 | } |
| 268 | |
| 269 | void vector_basic::insert_move_impl(fl::size index, void* element) { |
| 270 | if (index > mSize) index = mSize; |