Use reserve for linked_hash_map/set copy and assignment.

That would allocate only necessary number of slots and saving RAM.

PiperOrigin-RevId: 936473458
Change-Id: Ibd103ba2613bd778a3d2a9496b72d63ab384f073
diff --git a/absl/container/linked_hash_map.h b/absl/container/linked_hash_map.h
index 61720d6..efc9686 100644
--- a/absl/container/linked_hash_map.h
+++ b/absl/container/linked_hash_map.h
@@ -219,14 +219,15 @@
                         alloc) {}
 
   linked_hash_map(const linked_hash_map& other)
-      : linked_hash_map(other.bucket_count(), other.hash_function(),
-                        other.key_eq(), other.get_allocator()) {
+      : linked_hash_map(0, other.hash_function(), other.key_eq(),
+                        other.get_allocator()) {
+    reserve(other.size());
     CopyFrom(other);
   }
 
   linked_hash_map(const linked_hash_map& other, const allocator_type& alloc)
-      : linked_hash_map(other.bucket_count(), other.hash_function(),
-                        other.key_eq(), alloc) {
+      : linked_hash_map(0, other.hash_function(), other.key_eq(), alloc) {
+    reserve(other.size());
     CopyFrom(other);
   }
 
@@ -250,8 +251,9 @@
   linked_hash_map& operator=(const linked_hash_map& other) {
     if (this != &other) {
       // Make a new set, with other's hash/eq/alloc.
-      set_ = SetType(other.bucket_count(), other.set_.hash_function(),
-                     other.set_.key_eq(), other.get_allocator());
+      set_ = SetType(0, other.set_.hash_function(), other.set_.key_eq(),
+                     other.get_allocator());
+      set_.reserve(other.size());
       // Copy the list, with other's allocator.
       list_ = ListType(other.get_allocator());
       CopyFrom(other);
@@ -272,6 +274,7 @@
 
   linked_hash_map& operator=(std::initializer_list<value_type> values) {
     clear();
+    reserve(values.size());
     insert(values.begin(), values.end());
     return *this;
   }
diff --git a/absl/container/linked_hash_set.h b/absl/container/linked_hash_set.h
index fe207c8..ae7819b 100644
--- a/absl/container/linked_hash_set.h
+++ b/absl/container/linked_hash_set.h
@@ -209,14 +209,15 @@
                         alloc) {}
 
   linked_hash_set(const linked_hash_set& other)
-      : linked_hash_set(other.bucket_count(), other.hash_function(),
-                        other.key_eq(), other.get_allocator()) {
+      : linked_hash_set(0, other.hash_function(), other.key_eq(),
+                        other.get_allocator()) {
+    reserve(other.size());
     CopyFrom(other);
   }
 
   linked_hash_set(const linked_hash_set& other, const allocator_type& alloc)
-      : linked_hash_set(other.bucket_count(), other.hash_function(),
-                        other.key_eq(), alloc) {
+      : linked_hash_set(0, other.hash_function(), other.key_eq(), alloc) {
+    reserve(other.size());
     CopyFrom(other);
   }
 
@@ -240,8 +241,9 @@
   linked_hash_set& operator=(const linked_hash_set& other) {
     if (this != &other) {
       // Make a new set, with other's hash/eq/alloc.
-      set_ = SetType(other.bucket_count(), other.set_.hash_function(),
+      set_ = SetType(0, other.set_.hash_function(),
                      other.set_.key_eq(), other.get_allocator());
+      set_.reserve(other.size());
       // Copy the list, with other's allocator.
       list_ = ListType(other.get_allocator());
       CopyFrom(other);
@@ -261,6 +263,7 @@
 
   linked_hash_set& operator=(std::initializer_list<key_type> values) {
     clear();
+    reserve(values.size());
     insert(values.begin(), values.end());
     return *this;
   }