[llvm-commits] CVS: llvm/include/llvm/ADT/SmallVector.h

Chris Lattner lattner at cs.uiuc.edu
Tue Aug 22 10:29:11 PDT 2006



Changes in directory llvm/include/llvm/ADT:

SmallVector.h updated: 1.15 -> 1.16
---
Log message:

add resize, move swap out of line


---
Diffs of the changes:  (+56 -36)

 SmallVector.h |   92 +++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 56 insertions(+), 36 deletions(-)


Index: llvm/include/llvm/ADT/SmallVector.h
diff -u llvm/include/llvm/ADT/SmallVector.h:1.15 llvm/include/llvm/ADT/SmallVector.h:1.16
--- llvm/include/llvm/ADT/SmallVector.h:1.15	Tue Aug 22 01:27:16 2006
+++ llvm/include/llvm/ADT/SmallVector.h	Tue Aug 22 12:28:57 2006
@@ -112,43 +112,20 @@
     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 @@
     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 @@
   /// 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 @@
   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> &






More information about the llvm-commits mailing list