| 441 | } |
| 442 | |
| 443 | iterator insert(iterator it, T&& elm) { |
| 444 | if (it == this->end()) { // Important special case for empty vector. |
| 445 | this->push_back(std::move(elm)); |
| 446 | return this->end() - 1; |
| 447 | } |
| 448 | |
| 449 | if (this->m_end_ptr >= this->m_capacity_ptr) { |
| 450 | size_t elm_idx = it - this->begin(); |
| 451 | this->grow(); |
| 452 | it = this->begin() + elm_idx; |
| 453 | } |
| 454 | |
| 455 | new (static_cast<void*>(this->end())) T(std::move(this->back())); |
| 456 | // Push everything else over. |
| 457 | std::move_backward(it, this->end() - 1, this->end()); |
| 458 | this->set_end(this->end() + 1); |
| 459 | |
| 460 | // If we just moved the element we're inserting, be sure to update |
| 461 | // the reference. |
| 462 | T* elm_ptr = &elm; |
| 463 | if (it <= elm_ptr && elm_ptr < this->m_end_ptr) |
| 464 | ++elm_ptr; |
| 465 | |
| 466 | *it = std::move(*elm_ptr); |
| 467 | return it; |
| 468 | } |
| 469 | |
| 470 | iterator insert(iterator it, const T& _elm) { |
| 471 | if (it == this->end()) { // Important special case for empty vector. |