Bring TinyPtrVector under test. Somehow we never picked up unit tests
[oota-llvm.git] / include / llvm / ADT / TinyPtrVector.h
index ee86d8bdf70f529e895a82986aa32dfe95160db3..ca624c6c5412bc0f047b044a61720bb641a2b6f1 100644 (file)
 #ifndef LLVM_ADT_TINYPTRVECTOR_H
 #define LLVM_ADT_TINYPTRVECTOR_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/PointerUnion.h"
+#include "llvm/Support/Compiler.h"
 
 namespace llvm {
   
@@ -25,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() {}
@@ -32,11 +36,25 @@ public:
     if (VecTy *V = Val.template dyn_cast<VecTy*>())
       Val = new VecTy(*V);
   }
+#if LLVM_USE_RVALUE_REFERENCES
+  TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
+    RHS.Val = (EltTy)0;
+  }
+#endif
   ~TinyPtrVector() {
     if (VecTy *V = Val.template dyn_cast<VecTy*>())
       delete V;
   }
   
+  // implicit conversion operator to ArrayRef.
+  operator ArrayRef<EltTy>() const {
+    if (Val.isNull())
+      return ArrayRef<EltTy>();
+    if (Val.template is<EltTy>())
+      return *Val.getAddrOfPtr1();
+    return *Val.template get<VecTy*>();
+  }
+  
   bool empty() const {
     // This vector can be empty if it contains no element, or if it
     // contains a pointer to an empty vector.
@@ -54,28 +72,31 @@ public:
     return Val.template get<VecTy*>()->size();
   }
   
-  typedef const EltTy *iterator;
-  iterator begin() const {
-    if (empty())
-      return 0;
-    
+  typedef const EltTy *const_iterator;
+  typedef EltTy *iterator;
+
+  iterator begin() {
     if (Val.template is<EltTy>())
-      return Val.template getAddrOf<EltTy>();
+      return Val.getAddrOfPtr1();
     
     return Val.template get<VecTy *>()->begin();
 
   }
-  iterator end() const {
-    if (empty())
-      return 0;
-    
+  iterator end() {
     if (Val.template is<EltTy>())
-      return begin() + 1;
+      return begin() + (Val.isNull() ? 0 : 1);
     
     return Val.template get<VecTy *>()->end();
   }
 
-  
+  const_iterator begin() const {
+    return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
+  }
+
+  const_iterator end() const {
+    return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
+  }
+
   EltTy operator[](unsigned i) const {
     assert(!Val.isNull() && "can't index into an empty vector");
     if (EltTy V = Val.template dyn_cast<EltTy>()) {
@@ -95,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");
     
@@ -114,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>()) {
@@ -124,9 +162,25 @@ public:
     }
     // Otherwise, we're already empty.
   }
+
+  iterator erase(iterator I) {
+    // If we have a single value, convert to empty.
+    if (Val.template is<EltTy>()) {
+      if (I == begin())
+        Val = (EltTy)0;
+    } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
+      // multiple items in a vector; just do the erase, there is no
+      // benefit to collapsing back to a pointer
+      return Vec->erase(I);
+    }
+    return end();
+  }
   
 private:
   void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
+#if LLVM_USE_RVALUE_REFERENCES
+  void operator=(TinyPtrVector&&); // NOT IMPLEMENTED YET.
+#endif
 };
 } // end namespace llvm