| 639 | |
| 640 | template <typename T> |
| 641 | void SmallVectorImpl<T>::swap(SmallVectorImpl<T>& rhs) { |
| 642 | if (this == &rhs) |
| 643 | return; |
| 644 | |
| 645 | // We can only avoid copying elements if neither vector is small. |
| 646 | if (!this->is_small() && !rhs.is_small()) { |
| 647 | std::swap(this->m_begin_ptr, rhs.m_begin_ptr); |
| 648 | std::swap(this->m_end_ptr, rhs.m_end_ptr); |
| 649 | std::swap(this->m_capacity_ptr, rhs.m_capacity_ptr); |
| 650 | return; |
| 651 | } |
| 652 | if (rhs.size() > this->capacity()) |
| 653 | this->grow(rhs.size()); |
| 654 | if (this->size() > rhs.capacity()) |
| 655 | rhs.grow(this->size()); |
| 656 | |
| 657 | // Swap the shared elements. |
| 658 | size_t num_shared = this->size(); |
| 659 | if (num_shared > rhs.size()) |
| 660 | num_shared = rhs.size(); |
| 661 | for (size_type i = 0; i != num_shared; ++i) |
| 662 | std::swap((*this)[i], rhs[i]); |
| 663 | |
| 664 | // Copy over the extra elms. |
| 665 | if (this->size() > rhs.size()) { |
| 666 | size_t elm_diff = this->size() - rhs.size(); |
| 667 | this->uninitialized_move(this->begin() + num_shared, this->end(), rhs.end()); |
| 668 | rhs.set_end(rhs.end() + elm_diff); |
| 669 | this->destroy_range(this->begin() + num_shared, this->end()); |
| 670 | this->set_end(this->begin() + num_shared); |
| 671 | } else if (rhs.size() > this->size()) { |
| 672 | size_t elm_diff = rhs.size() - this->size(); |
| 673 | this->uninitialized_move(rhs.begin() + num_shared, rhs.end(), this->end()); |
| 674 | this->set_end(this->end() + elm_diff); |
| 675 | this->destroy_range(rhs.begin() + num_shared, rhs.end()); |
| 676 | rhs.set_end(rhs.begin() + num_shared); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | template <typename T> |
| 681 | SmallVectorImpl<T>& SmallVectorImpl<T>::operator=(const SmallVectorImpl<T>& rhs) { |