aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-07-27 03:38:08 +0000
committerChris Lattner <sabre@nondot.org>2006-07-27 03:38:08 +0000
commit4da479b02bd2734bb19a802b22621ad5b195ee81 (patch)
tree9d76f7007e01ac58ef743a305cdec96a41dec4ee
parent3b4866e194d32c47fd64160aa9c9caaa48d85d01 (diff)
downloadllvm-4da479b02bd2734bb19a802b22621ad5b195ee81.zip
llvm-4da479b02bd2734bb19a802b22621ad5b195ee81.tar.gz
llvm-4da479b02bd2734bb19a802b22621ad5b195ee81.tar.bz2
Use std::copy instead of custom loops to take advantage of STL optimizations.
Add a new append method for appending a range. llvm-svn: 29323
-rw-r--r--llvm/include/llvm/ADT/SmallVector.h24
1 files changed, 19 insertions, 5 deletions
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index c75453f..e542f4c 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -14,7 +14,9 @@
#ifndef LLVM_ADT_SMALLVECTOR_H
#define LLVM_ADT_SMALLVECTOR_H
+#include <algorithm>
#include <cassert>
+#include <iterator>
#include <memory>
namespace llvm {
@@ -113,6 +115,20 @@ public:
goto Retry;
}
+ /// append - Add the specified range to the end of the SmallVector.
+ ///
+ template<typename in_iter>
+ void append(in_iter in_start, in_iter in_end) {
+ unsigned NumInputs = std::distance(in_start, in_end);
+ // Grow allocated space if needed.
+ if (End+NumInputs > Capacity)
+ grow(size()+NumInputs);
+
+ // Copy the new elements over.
+ std::uninitialized_copy(in_start, in_end, End);
+ End += NumInputs;
+ }
+
const SmallVector &operator=(const SmallVector &RHS) {
// Avoid self-assignment.
if (this == &RHS) return *this;
@@ -123,8 +139,7 @@ public:
unsigned CurSize = size();
if (CurSize >= RHSSize) {
// Assign common elements.
- for (unsigned i = 0; i != RHSSize; ++i)
- Begin[i] = RHS.Begin[i];
+ std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin);
// Destroy excess elements.
for (unsigned i = RHSSize; i != CurSize; ++i)
@@ -144,10 +159,9 @@ public:
End = Begin;
CurSize = 0;
grow(RHSSize);
- } else {
+ } else if (CurSize) {
// Otherwise, use assignment for the already-constructed elements.
- for (unsigned i = 0; i != CurSize; ++i)
- Begin[i] = RHS.Begin[i];
+ std::copy(RHS.Begin, RHS.Begin+CurSize, Begin);
}
// Copy construct the new elements in place.