about summary refs log tree commit diff
path: root/absl/container
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-05-20T12·29-0700
committerDerek Mauro <dmauro@google.com>2019-05-20T21·12-0400
commita18fc7461e7409c2ad64e28537261db1e02e76fa (patch)
tree2ba71798e7362b173bef1872d9e6c14035fd8a8f /absl/container
parent8a394b19c149cab50534b04c5e21d42bc2217a7d (diff)
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
Diffstat (limited to 'absl/container')
-rw-r--r--absl/container/inlined_vector.h63
1 files changed, 23 insertions, 40 deletions
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h
index 61e0cfb4a3a4..6c2d08571785 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,