//===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #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 { /// TinyPtrVector - This class is specialized for cases where there are /// normally 0 or 1 element in a vector, but is general enough to go beyond that /// when required. /// /// NOTE: This container doesn't allow you to store a null pointer into it. /// template class TinyPtrVector { public: typedef llvm::SmallVector VecTy; llvm::PointerUnion Val; TinyPtrVector() {} TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) { if (VecTy *V = Val.template dyn_cast()) 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()) delete V; } // implicit conversion operator to ArrayRef. operator ArrayRef() const { if (Val.isNull()) return ArrayRef(); if (Val.template is()) return *Val.getAddrOfPtr1(); return *Val.template get(); } bool empty() const { // This vector can be empty if it contains no element, or if it // contains a pointer to an empty vector. if (Val.isNull()) return true; if (VecTy *Vec = Val.template dyn_cast()) return Vec->empty(); return false; } unsigned size() const { if (empty()) return 0; if (Val.template is()) return 1; return Val.template get()->size(); } typedef const EltTy *const_iterator; typedef EltTy *iterator; iterator begin() { if (empty()) return 0; if (Val.template is()) return Val.getAddrOfPtr1(); return Val.template get()->begin(); } iterator end() { if (empty()) return 0; if (Val.template is()) return begin() + 1; return Val.template get()->end(); } const_iterator begin() const { return (const_iterator)const_cast(this)->begin(); } const_iterator end() const { return (const_iterator)const_cast(this)->end(); } EltTy operator[](unsigned i) const { assert(!Val.isNull() && "can't index into an empty vector"); if (EltTy V = Val.template dyn_cast()) { assert(i == 0 && "tinyvector index out of range"); return V; } assert(i < Val.template get()->size() && "tinyvector index out of range"); return (*Val.template get())[i]; } EltTy front() const { assert(!empty() && "vector empty"); if (EltTy V = Val.template dyn_cast()) return V; return Val.template get()->front(); } EltTy back() const { assert(!empty() && "vector empty"); if (EltTy V = Val.template dyn_cast()) return V; return Val.template get()->back(); } void push_back(EltTy NewVal) { assert(NewVal != 0 && "Can't add a null value"); // If we have nothing, add something. if (Val.isNull()) { Val = NewVal; return; } // If we have a single value, convert to a vector. if (EltTy V = Val.template dyn_cast()) { Val = new VecTy(); Val.template get()->push_back(V); } // Add the new value, we know we have a vector. Val.template get()->push_back(NewVal); } void pop_back() { // If we have a single value, convert to empty. if (Val.template is()) Val = (EltTy)0; else if (VecTy *Vec = Val.template get()) Vec->pop_back(); } void clear() { // If we have a single value, convert to empty. if (Val.template is()) { Val = (EltTy)0; } else if (VecTy *Vec = Val.template dyn_cast()) { // If we have a vector form, just clear it. Vec->clear(); } // Otherwise, we're already empty. } iterator erase(iterator I) { // If we have a single value, convert to empty. if (Val.template is()) { if (I == begin()) Val = (EltTy)0; } else if (VecTy *Vec = Val.template dyn_cast()) { // multiple items in a vector; just do the erase, there is no // benefit to collapsing back to a pointer return Vec->erase(I); } return 0; } private: void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET. #if LLVM_USE_RVALUE_REFERENCES void operator=(TinyPtrVector&&); // NOT IMPLEMENTED YET. #endif }; } // end namespace llvm #endif