Export of internal Abseil changes.

--
b254aba40a65385deccd757480f8d7383f2f2f55 by CJ Johnson <johnsoncj@google.com>:

Updates some of the assignment operators to dispatch to the `assign(ForwardIt, ForwardIt)` member function (and removing `AssignForwardRange(...)` as a result.

PiperOrigin-RevId: 249030603
GitOrigin-RevId: b254aba40a65385deccd757480f8d7383f2f2f55
Change-Id: I6ba84c8e014d47de469691fa02f328df68066208
This commit is contained in:
Abseil Team 2019-05-20 05:29:52 -07:00 committed by Derek Mauro
parent 8a394b19c1
commit a18fc7461e

View file

@ -448,24 +448,16 @@ class InlinedVector {
// Replaces the contents of the inlined vector with copies of the elements in // Replaces the contents of the inlined vector with copies of the elements in
// the provided `std::initializer_list`. // the provided `std::initializer_list`.
InlinedVector& operator=(std::initializer_list<value_type> list) { InlinedVector& operator=(std::initializer_list<value_type> list) {
AssignForwardRange(list.begin(), list.end()); assign(list.begin(), list.end());
return *this; return *this;
} }
// Overload of `InlinedVector::operator=()` to replace the contents of the // Overload of `InlinedVector::operator=()` to replace the contents of the
// inlined vector with the contents of `other`. // inlined vector with the contents of `other`.
InlinedVector& operator=(const InlinedVector& other) { InlinedVector& operator=(const InlinedVector& other) {
if (ABSL_PREDICT_FALSE(this == std::addressof(other))) return *this; if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
const_pointer other_data = other.data();
// Optimized to avoid reallocation. assign(other_data, other_data + other.size());
// 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());
} }
return *this; return *this;
} }
@ -528,7 +520,7 @@ class InlinedVector {
// inlined vector with copies of the values in the provided // inlined vector with copies of the values in the provided
// `std::initializer_list`. // `std::initializer_list`.
void assign(std::initializer_list<value_type> 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 // Overload of `InlinedVector::assign()` to replace the contents of the
@ -536,7 +528,24 @@ class InlinedVector {
template <typename ForwardIterator, template <typename ForwardIterator,
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr> EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
void assign(ForwardIterator first, ForwardIterator last) { 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 // Overload of `InlinedVector::assign()` to replace the contents of the
@ -1057,32 +1066,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> template <typename ForwardIt>
void AppendForwardRange(ForwardIt first, ForwardIt last) { void AppendForwardRange(ForwardIt first, ForwardIt last) {
static_assert(absl::inlined_vector_internal::IsAtLeastForwardIterator< static_assert(absl::inlined_vector_internal::IsAtLeastForwardIterator<