Bring TinyPtrVector under test. Somehow we never picked up unit tests
[oota-llvm.git] / include / llvm / ADT / TinyPtrVector.h
index f9b7d559c39efa6e49af0ad2914ac778533b05a6..ca624c6c5412bc0f047b044a61720bb641a2b6f1 100644 (file)
@@ -27,6 +27,8 @@ template <typename EltTy>
 class TinyPtrVector {
 public:
   typedef llvm::SmallVector<EltTy, 4> VecTy;
+  typedef typename VecTy::value_type value_type;
+
   llvm::PointerUnion<EltTy, VecTy*> Val;
   
   TinyPtrVector() {}
@@ -74,9 +76,6 @@ public:
   typedef EltTy *iterator;
 
   iterator begin() {
-    if (empty())
-      return 0;
-    
     if (Val.template is<EltTy>())
       return Val.getAddrOfPtr1();
     
@@ -84,11 +83,8 @@ public:
 
   }
   iterator end() {
-    if (empty())
-      return 0;
-    
     if (Val.template is<EltTy>())
-      return begin() + 1;
+      return begin() + (Val.isNull() ? 0 : 1);
     
     return Val.template get<VecTy *>()->end();
   }
@@ -120,6 +116,14 @@ public:
     return Val.template get<VecTy*>()->front();
   }
   
+  EltTy back() const {
+    assert(!empty() && "vector empty");
+    if (EltTy V = Val.template dyn_cast<EltTy>())
+      return V;
+    return Val.template get<VecTy*>()->back();
+  }
+
+  
   void push_back(EltTy NewVal) {
     assert(NewVal != 0 && "Can't add a null value");
     
@@ -139,6 +143,15 @@ public:
     Val.template get<VecTy*>()->push_back(NewVal);
   }
   
+  void pop_back() {
+    // If we have a single value, convert to empty.
+    if (Val.template is<EltTy>())
+      Val = (EltTy)0;
+    else if (VecTy *Vec = Val.template get<VecTy*>())
+      Vec->pop_back();
+  }
+
+  
   void clear() {
     // If we have a single value, convert to empty.
     if (Val.template is<EltTy>()) {
@@ -160,8 +173,7 @@ public:
       // benefit to collapsing back to a pointer
       return Vec->erase(I);
     }
-
-    return 0;
+    return end();
   }
   
 private: