MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / swap

Method swap

dnn/include/megdnn/thin/small_vector.h:641–678  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

639
640template <typename T>
641void 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
680template <typename T>
681SmallVectorImpl<T>& SmallVectorImpl<T>::operator=(const SmallVectorImpl<T>& rhs) {

Callers 3

TESTFunction · 0.45
swapFunction · 0.45

Calls 10

swapFunction · 0.85
is_smallMethod · 0.80
capacityMethod · 0.80
set_endMethod · 0.80
sizeMethod · 0.45
growMethod · 0.45
uninitialized_moveMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
destroy_rangeMethod · 0.45

Tested by 1

TESTFunction · 0.36