Skip to content

Commit 6ed3083

Browse files
committed
ADT: Reduce code duplication in SmallVector by calling reserve and clear, NFC
1 parent 53b3460 commit 6ed3083

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,7 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
603603
void append(in_iter in_start, in_iter in_end) {
604604
this->assertSafeToAddRange(in_start, in_end);
605605
size_type NumInputs = std::distance(in_start, in_end);
606-
if (NumInputs > this->capacity() - this->size())
607-
this->grow(this->size()+NumInputs);
608-
606+
this->reserve(this->size() + NumInputs);
609607
this->uninitialized_copy(in_start, in_end, this->end());
610608
this->set_size(this->size() + NumInputs);
611609
}
@@ -888,10 +886,8 @@ void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
888886
std::swap(this->Capacity, RHS.Capacity);
889887
return;
890888
}
891-
if (RHS.size() > this->capacity())
892-
this->grow(RHS.size());
893-
if (this->size() > RHS.capacity())
894-
RHS.grow(this->size());
889+
this->reserve(RHS.size());
890+
RHS.reserve(this->size());
895891

896892
// Swap the shared elements.
897893
size_t NumShared = this->size();
@@ -946,8 +942,7 @@ SmallVectorImpl<T> &SmallVectorImpl<T>::
946942
// FIXME: don't do this if they're efficiently moveable.
947943
if (this->capacity() < RHSSize) {
948944
// Destroy current elements.
949-
this->destroy_range(this->begin(), this->end());
950-
this->set_size(0);
945+
this->clear();
951946
CurSize = 0;
952947
this->grow(RHSSize);
953948
} else if (CurSize) {
@@ -1006,8 +1001,7 @@ SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
10061001
// elements.
10071002
if (this->capacity() < RHSSize) {
10081003
// Destroy current elements.
1009-
this->destroy_range(this->begin(), this->end());
1010-
this->set_size(0);
1004+
this->clear();
10111005
CurSize = 0;
10121006
this->grow(RHSSize);
10131007
} else if (CurSize) {

0 commit comments

Comments
 (0)