diff options
author | Chris Lattner <sabre@nondot.org> | 2006-08-22 17:28:57 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2006-08-22 17:28:57 +0000 |
commit | 57b79795b37b367d69d58ad10f25e997b4f55ce9 (patch) | |
tree | edcc927cc7638a0b021e9e5f8627e53d94115b41 /include/llvm/ADT | |
parent | d520dd7a241e45ce11125b6a378610fdfb87647d (diff) | |
download | llvm-57b79795b37b367d69d58ad10f25e997b4f55ce9.tar.gz llvm-57b79795b37b367d69d58ad10f25e997b4f55ce9.tar.bz2 llvm-57b79795b37b367d69d58ad10f25e997b4f55ce9.tar.xz |
add resize, move swap out of line
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29823 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT')
-rw-r--r-- | include/llvm/ADT/SmallVector.h | 92 |
1 files changed, 56 insertions, 36 deletions
diff --git a/include/llvm/ADT/SmallVector.h b/include/llvm/ADT/SmallVector.h index ac76944f26..ad08db33ee 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<typename in_iter> @@ -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<T>::grow(unsigned MinSize) { End = NewElts+CurSize; Capacity = Begin+NewCapacity; } + +template <typename T> +void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &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 <typename T> const SmallVectorImpl<T> & |