diff options
Diffstat (limited to 'absl')
-rw-r--r-- | absl/container/inlined_vector.h | 63 |
1 files changed, 23 insertions, 40 deletions
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 61e0cfb4..6c2d0857 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -448,24 +448,16 @@ class InlinedVector { // Replaces the contents of the inlined vector with copies of the elements in // the provided `std::initializer_list`. InlinedVector& operator=(std::initializer_list<value_type> list) { - AssignForwardRange(list.begin(), list.end()); + assign(list.begin(), list.end()); return *this; } // Overload of `InlinedVector::operator=()` to replace the contents of the // inlined vector with the contents of `other`. InlinedVector& operator=(const InlinedVector& other) { - if (ABSL_PREDICT_FALSE(this == std::addressof(other))) return *this; - - // Optimized to avoid reallocation. - // Prefer reassignment to copy construction for elements. - if (size() < other.size()) { // grow - reserve(other.size()); - std::copy(other.begin(), other.begin() + size(), begin()); - std::copy(other.begin() + size(), other.end(), std::back_inserter(*this)); - } else { // maybe shrink - erase(begin() + other.size(), end()); - std::copy(other.begin(), other.end(), begin()); + if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { + const_pointer other_data = other.data(); + assign(other_data, other_data + other.size()); } return *this; } @@ -528,7 +520,7 @@ class InlinedVector { // inlined vector with copies of the values in the provided // `std::initializer_list`. void assign(std::initializer_list<value_type> list) { - AssignForwardRange(list.begin(), list.end()); + assign(list.begin(), list.end()); } // Overload of `InlinedVector::assign()` to replace the contents of the @@ -536,7 +528,24 @@ class InlinedVector { template <typename ForwardIterator, EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr> void assign(ForwardIterator first, ForwardIterator last) { - AssignForwardRange(first, last); + auto length = std::distance(first, last); + + // Prefer reassignment to copy construction for elements. + if (static_cast<size_type>(length) <= size()) { + erase(std::copy(first, last, begin()), end()); + return; + } + + reserve(length); + iterator out = begin(); + for (; out != end(); ++first, ++out) *out = *first; + if (storage_.GetIsAllocated()) { + UninitializedCopy(first, last, out); + storage_.SetAllocatedSize(length); + } else { + UninitializedCopy(first, last, out); + storage_.SetInlinedSize(length); + } } // Overload of `InlinedVector::assign()` to replace the contents of the @@ -1058,32 +1067,6 @@ class InlinedVector { } template <typename ForwardIt> - void AssignForwardRange(ForwardIt first, ForwardIt last) { - static_assert(absl::inlined_vector_internal::IsAtLeastForwardIterator< - ForwardIt>::value, - ""); - - auto length = std::distance(first, last); - - // Prefer reassignment to copy construction for elements. - if (static_cast<size_type>(length) <= size()) { - erase(std::copy(first, last, begin()), end()); - return; - } - - reserve(length); - iterator out = begin(); - for (; out != end(); ++first, ++out) *out = *first; - if (storage_.GetIsAllocated()) { - UninitializedCopy(first, last, out); - storage_.SetAllocatedSize(length); - } else { - UninitializedCopy(first, last, out); - storage_.SetInlinedSize(length); - } - } - - template <typename ForwardIt> void AppendForwardRange(ForwardIt first, ForwardIt last) { static_assert(absl::inlined_vector_internal::IsAtLeastForwardIterator< ForwardIt>::value, |