From 57b79795b37b367d69d58ad10f25e997b4f55ce9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 22 Aug 2006 17:28:57 +0000 Subject: [PATCH] add resize, move swap out of line git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29823 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/SmallVector.h | 92 +++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/include/llvm/ADT/SmallVector.h b/include/llvm/ADT/SmallVector.h index ac76944f267..ad08db33eef 100644 --- a/include/llvm/ADT/SmallVector.h +++ b/include/llvm/ADT/SmallVector.h @@ -112,43 +112,20 @@ public: End = Begin; } - void swap(SmallVectorImpl &RHS) { - if (this == &RHS) return; - - // We can only avoid copying elements if neither vector is small. - if (!isSmall() && !RHS.isSmall()) { - std::swap(Begin, RHS.Begin); - std::swap(End, RHS.End); - std::swap(Capacity, RHS.Capacity); - return; - } - if (Begin+RHS.size() > Capacity) - grow(RHS.size()); - if (RHS.begin()+size() > RHS.Capacity) - RHS.grow(size()); - - // Swap the shared elements. - unsigned NumShared = size(); - if (NumShared > RHS.size()) NumShared = RHS.size(); - for (unsigned i = 0; i != NumShared; ++i) - std::swap(Begin[i], RHS[i]); - - // Copy over the extra elts. - if (size() > RHS.size()) { - unsigned EltDiff = size() - RHS.size(); - std::uninitialized_copy(Begin+NumShared, End, RHS.End); - RHS.End += EltDiff; - destroy_range(Begin+NumShared, End); - End = Begin+NumShared; - } else if (RHS.size() > size()) { - unsigned EltDiff = RHS.size() - size(); - std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End); - End += EltDiff; - destroy_range(RHS.Begin+NumShared, RHS.End); - RHS.End = RHS.Begin+NumShared; + void resize(unsigned N) { + if (N < size()) { + destroy_range(Begin+N, End); + End = Begin+N; + } else if (N > size()) { + if (Begin+N > Capacity) + grow(N); + construct_range(End, Begin+N, T()); + End = Begin+N; } } + void swap(SmallVectorImpl &RHS); + /// append - Add the specified range to the end of the SmallVector. /// template @@ -168,8 +145,7 @@ public: if (Begin+NumElts > Capacity) grow(NumElts); End = Begin+NumElts; - for (; NumElts; --NumElts) - new (Begin+NumElts-1) T(Elt); + construct_range(Begin, End, Elt); } void erase(iterator I) { @@ -220,6 +196,12 @@ private: /// grow - double the size of the allocated memory, guaranteeing space for at /// least one more element or MinSize if specified. void grow(unsigned MinSize = 0); + + void construct_range(T *S, T *E, const T &Elt) { + for (; S != E; ++S) + new (S) T(Elt); + } + void destroy_range(T *S, T *E) { while (S != E) { @@ -253,6 +235,44 @@ void SmallVectorImpl::grow(unsigned MinSize) { End = NewElts+CurSize; Capacity = Begin+NewCapacity; } + +template +void SmallVectorImpl::swap(SmallVectorImpl &RHS) { + if (this == &RHS) return; + + // We can only avoid copying elements if neither vector is small. + if (!isSmall() && !RHS.isSmall()) { + std::swap(Begin, RHS.Begin); + std::swap(End, RHS.End); + std::swap(Capacity, RHS.Capacity); + return; + } + if (Begin+RHS.size() > Capacity) + grow(RHS.size()); + if (RHS.begin()+size() > RHS.Capacity) + RHS.grow(size()); + + // Swap the shared elements. + unsigned NumShared = size(); + if (NumShared > RHS.size()) NumShared = RHS.size(); + for (unsigned i = 0; i != NumShared; ++i) + std::swap(Begin[i], RHS[i]); + + // Copy over the extra elts. + if (size() > RHS.size()) { + unsigned EltDiff = size() - RHS.size(); + std::uninitialized_copy(Begin+NumShared, End, RHS.End); + RHS.End += EltDiff; + destroy_range(Begin+NumShared, End); + End = Begin+NumShared; + } else if (RHS.size() > size()) { + unsigned EltDiff = RHS.size() - size(); + std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End); + End += EltDiff; + destroy_range(RHS.Begin+NumShared, RHS.End); + RHS.End = RHS.Begin+NumShared; + } +} template const SmallVectorImpl & -- 2.34.1