2011-07-18 03:40:02 +02:00
|
|
|
//===- 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/SmallVector.h"
|
|
|
|
#include "llvm/ADT/PointerUnion.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 <typename EltTy>
|
|
|
|
class TinyPtrVector {
|
|
|
|
public:
|
|
|
|
typedef llvm::SmallVector<EltTy, 4> VecTy;
|
|
|
|
llvm::PointerUnion<EltTy, VecTy*> Val;
|
|
|
|
|
|
|
|
TinyPtrVector() {}
|
|
|
|
TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
|
|
|
|
if (VecTy *V = Val.template dyn_cast<VecTy*>())
|
|
|
|
Val = new VecTy(*V);
|
|
|
|
}
|
|
|
|
~TinyPtrVector() {
|
|
|
|
if (VecTy *V = Val.template dyn_cast<VecTy*>())
|
|
|
|
delete V;
|
|
|
|
}
|
|
|
|
|
2011-12-06 03:00:33 +01:00
|
|
|
// implicit conversion operator to ArrayRef.
|
|
|
|
operator ArrayRef<EltTy>() const {
|
|
|
|
if (Val.isNull())
|
|
|
|
return ArrayRef<EltTy>();
|
|
|
|
if (Val.template is<EltTy>())
|
2012-03-07 04:37:32 +01:00
|
|
|
return *Val.getAddrOfPtr1();
|
2011-12-06 03:00:33 +01:00
|
|
|
return *Val.template get<VecTy*>();
|
|
|
|
}
|
|
|
|
|
2011-07-18 03:40:02 +02:00
|
|
|
bool empty() const {
|
2011-07-18 18:35:57 +02:00
|
|
|
// This vector can be empty if it contains no element, or if it
|
|
|
|
// contains a pointer to an empty vector.
|
2011-07-18 03:40:02 +02:00
|
|
|
if (Val.isNull()) return true;
|
|
|
|
if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
|
|
|
|
return Vec->empty();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned size() const {
|
|
|
|
if (empty())
|
|
|
|
return 0;
|
2011-07-18 18:35:57 +02:00
|
|
|
if (Val.template is<EltTy>())
|
2011-07-18 03:40:02 +02:00
|
|
|
return 1;
|
2011-07-18 18:35:57 +02:00
|
|
|
return Val.template get<VecTy*>()->size();
|
2011-07-18 03:40:02 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 14:32:36 +01:00
|
|
|
typedef const EltTy *const_iterator;
|
2012-03-06 08:14:58 +01:00
|
|
|
typedef EltTy *iterator;
|
|
|
|
|
|
|
|
iterator begin() {
|
2011-07-18 03:53:11 +02:00
|
|
|
if (empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Val.template is<EltTy>())
|
2012-03-06 08:14:54 +01:00
|
|
|
return Val.getAddrOfPtr1();
|
2011-07-18 03:53:11 +02:00
|
|
|
|
|
|
|
return Val.template get<VecTy *>()->begin();
|
|
|
|
|
|
|
|
}
|
2012-03-06 08:14:58 +01:00
|
|
|
iterator end() {
|
2011-07-18 03:53:11 +02:00
|
|
|
if (empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Val.template is<EltTy>())
|
|
|
|
return begin() + 1;
|
|
|
|
|
|
|
|
return Val.template get<VecTy *>()->end();
|
|
|
|
}
|
|
|
|
|
2012-03-06 08:14:58 +01:00
|
|
|
const_iterator begin() const {
|
|
|
|
return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator end() const {
|
|
|
|
return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
|
|
|
|
}
|
|
|
|
|
2011-07-18 03:40:02 +02:00
|
|
|
EltTy operator[](unsigned i) const {
|
|
|
|
assert(!Val.isNull() && "can't index into an empty vector");
|
|
|
|
if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
|
|
|
assert(i == 0 && "tinyvector index out of range");
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2011-07-18 18:35:57 +02:00
|
|
|
assert(i < Val.template get<VecTy*>()->size() &&
|
2011-07-18 03:40:02 +02:00
|
|
|
"tinyvector index out of range");
|
2011-07-18 18:35:57 +02:00
|
|
|
return (*Val.template get<VecTy*>())[i];
|
2011-07-18 03:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EltTy front() const {
|
|
|
|
assert(!empty() && "vector empty");
|
|
|
|
if (EltTy V = Val.template dyn_cast<EltTy>())
|
|
|
|
return V;
|
|
|
|
return Val.template get<VecTy*>()->front();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-07-18 18:35:57 +02:00
|
|
|
if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
2011-07-18 03:40:02 +02:00
|
|
|
Val = new VecTy();
|
|
|
|
Val.template get<VecTy*>()->push_back(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new value, we know we have a vector.
|
|
|
|
Val.template get<VecTy*>()->push_back(NewVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
// If we have a single value, convert to empty.
|
2011-07-18 03:43:58 +02:00
|
|
|
if (Val.template is<EltTy>()) {
|
2011-07-18 03:40:02 +02:00
|
|
|
Val = (EltTy)0;
|
|
|
|
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
|
|
|
// If we have a vector form, just clear it.
|
|
|
|
Vec->clear();
|
|
|
|
}
|
|
|
|
// Otherwise, we're already empty.
|
|
|
|
}
|
2012-03-06 08:14:58 +01:00
|
|
|
|
|
|
|
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 0;
|
|
|
|
}
|
2011-07-18 03:40:02 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|