hoist the begin/end/capacity members and a few trivial methods

up into the non-templated SmallVectorBase class.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91426 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2009-12-15 08:29:22 +00:00
parent dc2e570411
commit 10aaf05c30

View File

@@ -49,24 +49,14 @@ namespace llvm {
/// SmallVectorBase - This is all the non-templated stuff common to all /// SmallVectorBase - This is all the non-templated stuff common to all
/// SmallVectors. /// SmallVectors.
class SmallVectorBase { class SmallVectorBase {
};
/// SmallVectorImpl - This class consists of common code factored out of the
/// SmallVector class to reduce code duplication based on the SmallVector 'N'
/// template parameter.
template <typename T>
class SmallVectorImpl {
protected: protected:
T *Begin, *End, *Capacity; void *BeginX, *EndX, *CapacityX;
// Allocate raw space for N elements of type T. If T has a ctor or dtor, we // Allocate raw space for N elements of type T. If T has a ctor or dtor, we
// don't want it to be automatically run, so we need to represent the space as // don't want it to be automatically run, so we need to represent the space as
// something else. An array of char would work great, but might not be // something else. An array of char would work great, but might not be
// aligned sufficiently. Instead, we either use GCC extensions, or some // aligned sufficiently. Instead, we either use GCC extensions, or some
// number of union instances for the space, which guarantee maximal alignment. // number of union instances for the space, which guarantee maximal alignment.
protected:
#ifdef __GNUC__ #ifdef __GNUC__
typedef char U; typedef char U;
U FirstEl __attribute__((aligned)); U FirstEl __attribute__((aligned));
@@ -79,46 +69,65 @@ protected:
} FirstEl; } FirstEl;
#endif #endif
// Space after 'FirstEl' is clobbered, do not add any instance vars after it. // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
protected:
SmallVectorBase(size_t Size)
: BeginX(&FirstEl), EndX(&FirstEl), CapacityX((char*)&FirstEl+Size) {}
/// isSmall - Return true if this is a smallvector which has not had dynamic
/// memory allocated for it.
bool isSmall() const {
return BeginX == static_cast<const void*>(&FirstEl);
}
public:
bool empty() const { return BeginX == EndX; }
};
/// SmallVectorImpl - This class consists of common code factored out of the
/// SmallVector class to reduce code duplication based on the SmallVector 'N'
/// template parameter.
template <typename T>
class SmallVectorImpl : public SmallVectorBase {
void setEnd(T *P) { EndX = P; }
public: public:
// Default ctor - Initialize to empty. // Default ctor - Initialize to empty.
explicit SmallVectorImpl(unsigned N) explicit SmallVectorImpl(unsigned N) : SmallVectorBase(N*sizeof(T)) {
: Begin(reinterpret_cast<T*>(&FirstEl)),
End(reinterpret_cast<T*>(&FirstEl)),
Capacity(reinterpret_cast<T*>(&FirstEl)+N) {
} }
~SmallVectorImpl() { ~SmallVectorImpl() {
// Destroy the constructed elements in the vector. // Destroy the constructed elements in the vector.
destroy_range(Begin, End); destroy_range(begin(), end());
// If this wasn't grown from the inline copy, deallocate the old space. // If this wasn't grown from the inline copy, deallocate the old space.
if (!isSmall()) if (!isSmall())
operator delete(Begin); operator delete(begin());
} }
typedef size_t size_type; typedef size_t size_type;
typedef ptrdiff_t difference_type; typedef ptrdiff_t difference_type;
typedef T value_type; typedef T value_type;
typedef T* iterator; typedef T *iterator;
typedef const T* const_iterator; typedef const T *const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator;
typedef T& reference; typedef T &reference;
typedef const T& const_reference; typedef const T &const_reference;
typedef T* pointer; typedef T *pointer;
typedef const T* const_pointer; typedef const T *const_pointer;
bool empty() const { return Begin == End; }
size_type size() const { return End-Begin; }
size_type max_size() const { return size_type(-1) / sizeof(T); }
// forward iterator creation methods. // forward iterator creation methods.
iterator begin() { return Begin; } iterator begin() { return (iterator)BeginX; }
const_iterator begin() const { return Begin; } const_iterator begin() const { return (const_iterator)BeginX; }
iterator end() { return End; } iterator end() { return (iterator)EndX; }
const_iterator end() const { return End; } const_iterator end() const { return (const_iterator)EndX; }
private:
iterator capacity_ptr() { return (iterator)CapacityX; }
const_iterator capacity_ptr() const { return (const_iterator)CapacityX; }
public:
// reverse iterator creation methods. // reverse iterator creation methods.
reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rbegin() { return reverse_iterator(end()); }
@@ -126,14 +135,25 @@ public:
reverse_iterator rend() { return reverse_iterator(begin()); } reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin());} const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
size_type size() const { return end()-begin(); }
size_type max_size() const { return size_type(-1) / sizeof(T); }
/// capacity - Return the total number of elements in the currently allocated
/// buffer.
size_t capacity() const { return capacity_ptr() - begin(); }
/// data - Return a pointer to the vector's buffer, even if empty().
pointer data() { return pointer(begin()); }
/// data - Return a pointer to the vector's buffer, even if empty().
const_pointer data() const { return const_pointer(begin()); }
reference operator[](unsigned idx) { reference operator[](unsigned idx) {
assert(Begin + idx < End); assert(begin() + idx < end());
return Begin[idx]; return begin()[idx];
} }
const_reference operator[](unsigned idx) const { const_reference operator[](unsigned idx) const {
assert(Begin + idx < End); assert(begin() + idx < end());
return Begin[idx]; return begin()[idx];
} }
reference front() { reference front() {
@@ -151,10 +171,10 @@ public:
} }
void push_back(const_reference Elt) { void push_back(const_reference Elt) {
if (End < Capacity) { if (EndX < CapacityX) {
Retry: Retry:
new (End) T(Elt); new (end()) T(Elt);
++End; setEnd(end()+1);
return; return;
} }
grow(); grow();
@@ -162,8 +182,8 @@ public:
} }
void pop_back() { void pop_back() {
--End; setEnd(end()-1);
End->~T(); end()->~T();
} }
T pop_back_val() { T pop_back_val() {
@@ -173,36 +193,36 @@ public:
} }
void clear() { void clear() {
destroy_range(Begin, End); destroy_range(begin(), end());
End = Begin; EndX = BeginX;
} }
void resize(unsigned N) { void resize(unsigned N) {
if (N < size()) { if (N < size()) {
destroy_range(Begin+N, End); destroy_range(begin()+N, end());
End = Begin+N; setEnd(begin()+N);
} else if (N > size()) { } else if (N > size()) {
if (unsigned(Capacity-Begin) < N) if (capacity() < N)
grow(N); grow(N);
construct_range(End, Begin+N, T()); construct_range(end(), begin()+N, T());
End = Begin+N; setEnd(begin()+N);
} }
} }
void resize(unsigned N, const T &NV) { void resize(unsigned N, const T &NV) {
if (N < size()) { if (N < size()) {
destroy_range(Begin+N, End); destroy_range(begin()+N, end());
End = Begin+N; setEnd(begin()+N);
} else if (N > size()) { } else if (N > size()) {
if (unsigned(Capacity-Begin) < N) if (capacity() < N)
grow(N); grow(N);
construct_range(End, Begin+N, NV); construct_range(end(), begin()+N, NV);
End = Begin+N; setEnd(begin()+N);
} }
} }
void reserve(unsigned N) { void reserve(unsigned N) {
if (unsigned(Capacity-Begin) < N) if (capacity() < N)
grow(N); grow(N);
} }
@@ -214,38 +234,38 @@ public:
void append(in_iter in_start, in_iter in_end) { void append(in_iter in_start, in_iter in_end) {
size_type NumInputs = std::distance(in_start, in_end); size_type NumInputs = std::distance(in_start, in_end);
// Grow allocated space if needed. // Grow allocated space if needed.
if (NumInputs > size_type(Capacity-End)) if (NumInputs > size_type(capacity_ptr()-end()))
grow(size()+NumInputs); grow(size()+NumInputs);
// Copy the new elements over. // Copy the new elements over.
std::uninitialized_copy(in_start, in_end, End); std::uninitialized_copy(in_start, in_end, end());
End += NumInputs; setEnd(end() + NumInputs);
} }
/// append - Add the specified range to the end of the SmallVector. /// append - Add the specified range to the end of the SmallVector.
/// ///
void append(size_type NumInputs, const T &Elt) { void append(size_type NumInputs, const T &Elt) {
// Grow allocated space if needed. // Grow allocated space if needed.
if (NumInputs > size_type(Capacity-End)) if (NumInputs > size_type(capacity_ptr()-end()))
grow(size()+NumInputs); grow(size()+NumInputs);
// Copy the new elements over. // Copy the new elements over.
std::uninitialized_fill_n(End, NumInputs, Elt); std::uninitialized_fill_n(end(), NumInputs, Elt);
End += NumInputs; setEnd(end() + NumInputs);
} }
void assign(unsigned NumElts, const T &Elt) { void assign(unsigned NumElts, const T &Elt) {
clear(); clear();
if (unsigned(Capacity-Begin) < NumElts) if (capacity() < NumElts)
grow(NumElts); grow(NumElts);
End = Begin+NumElts; setEnd(begin()+NumElts);
construct_range(Begin, End, Elt); construct_range(begin(), end(), Elt);
} }
iterator erase(iterator I) { iterator erase(iterator I) {
iterator N = I; iterator N = I;
// Shift all elts down one. // Shift all elts down one.
std::copy(I+1, End, I); std::copy(I+1, end(), I);
// Drop the last elt. // Drop the last elt.
pop_back(); pop_back();
return(N); return(N);
@@ -254,36 +274,36 @@ public:
iterator erase(iterator S, iterator E) { iterator erase(iterator S, iterator E) {
iterator N = S; iterator N = S;
// Shift all elts down. // Shift all elts down.
iterator I = std::copy(E, End, S); iterator I = std::copy(E, end(), S);
// Drop the last elts. // Drop the last elts.
destroy_range(I, End); destroy_range(I, end());
End = I; setEnd(I);
return(N); return(N);
} }
iterator insert(iterator I, const T &Elt) { iterator insert(iterator I, const T &Elt) {
if (I == End) { // Important special case for empty vector. if (I == end()) { // Important special case for empty vector.
push_back(Elt); push_back(Elt);
return end()-1; return end()-1;
} }
if (End < Capacity) { if (EndX < CapacityX) {
Retry: Retry:
new (End) T(back()); new (end()) T(back());
++End; setEnd(end()+1);
// Push everything else over. // Push everything else over.
std::copy_backward(I, End-1, End); std::copy_backward(I, end()-1, end());
*I = Elt; *I = Elt;
return I; return I;
} }
size_t EltNo = I-Begin; size_t EltNo = I-begin();
grow(); grow();
I = Begin+EltNo; I = begin()+EltNo;
goto Retry; goto Retry;
} }
iterator insert(iterator I, size_type NumToInsert, const T &Elt) { iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
if (I == End) { // Important special case for empty vector. if (I == end()) { // Important special case for empty vector.
append(NumToInsert, Elt); append(NumToInsert, Elt);
return end()-1; return end()-1;
} }
@@ -302,8 +322,8 @@ public:
// insertion. Since we already reserved space, we know that this won't // insertion. Since we already reserved space, we know that this won't
// reallocate the vector. // reallocate the vector.
if (size_t(end()-I) >= NumToInsert) { if (size_t(end()-I) >= NumToInsert) {
T *OldEnd = End; T *OldEnd = end();
append(End-NumToInsert, End); append(end()-NumToInsert, end());
// Copy the existing elements that get replaced. // Copy the existing elements that get replaced.
std::copy_backward(I, OldEnd-NumToInsert, OldEnd); std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
@@ -316,10 +336,10 @@ public:
// not inserting at the end. // not inserting at the end.
// Copy over the elements that we're about to overwrite. // Copy over the elements that we're about to overwrite.
T *OldEnd = End; T *OldEnd = end();
End += NumToInsert; setEnd(end() + NumToInsert);
size_t NumOverwritten = OldEnd-I; size_t NumOverwritten = OldEnd-I;
std::uninitialized_copy(I, OldEnd, End-NumOverwritten); std::uninitialized_copy(I, OldEnd, end()-NumOverwritten);
// Replace the overwritten part. // Replace the overwritten part.
std::fill_n(I, NumOverwritten, Elt); std::fill_n(I, NumOverwritten, Elt);
@@ -331,7 +351,7 @@ public:
template<typename ItTy> template<typename ItTy>
iterator insert(iterator I, ItTy From, ItTy To) { iterator insert(iterator I, ItTy From, ItTy To) {
if (I == End) { // Important special case for empty vector. if (I == end()) { // Important special case for empty vector.
append(From, To); append(From, To);
return end()-1; return end()-1;
} }
@@ -351,8 +371,8 @@ public:
// insertion. Since we already reserved space, we know that this won't // insertion. Since we already reserved space, we know that this won't
// reallocate the vector. // reallocate the vector.
if (size_t(end()-I) >= NumToInsert) { if (size_t(end()-I) >= NumToInsert) {
T *OldEnd = End; T *OldEnd = end();
append(End-NumToInsert, End); append(end()-NumToInsert, end());
// Copy the existing elements that get replaced. // Copy the existing elements that get replaced.
std::copy_backward(I, OldEnd-NumToInsert, OldEnd); std::copy_backward(I, OldEnd-NumToInsert, OldEnd);
@@ -365,10 +385,10 @@ public:
// not inserting at the end. // not inserting at the end.
// Copy over the elements that we're about to overwrite. // Copy over the elements that we're about to overwrite.
T *OldEnd = End; T *OldEnd = end();
End += NumToInsert; setEnd(end() + NumToInsert);
size_t NumOverwritten = OldEnd-I; size_t NumOverwritten = OldEnd-I;
std::uninitialized_copy(I, OldEnd, End-NumOverwritten); std::uninitialized_copy(I, OldEnd, end()-NumOverwritten);
// Replace the overwritten part. // Replace the overwritten part.
std::copy(From, From+NumOverwritten, I); std::copy(From, From+NumOverwritten, I);
@@ -378,21 +398,11 @@ public:
return I; return I;
} }
/// data - Return a pointer to the vector's buffer, even if empty().
pointer data() {
return pointer(Begin);
}
/// data - Return a pointer to the vector's buffer, even if empty().
const_pointer data() const {
return const_pointer(Begin);
}
const SmallVectorImpl &operator=(const SmallVectorImpl &RHS); const SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
bool operator==(const SmallVectorImpl &RHS) const { bool operator==(const SmallVectorImpl &RHS) const {
if (size() != RHS.size()) return false; if (size() != RHS.size()) return false;
for (T *This = Begin, *That = RHS.Begin, *E = Begin+size(); for (const T *This = begin(), *That = RHS.begin(), *E = end();
This != E; ++This, ++That) This != E; ++This, ++That)
if (*This != *That) if (*This != *That)
return false; return false;
@@ -405,10 +415,6 @@ public:
RHS.begin(), RHS.end()); RHS.begin(), RHS.end());
} }
/// capacity - Return the total number of elements in the currently allocated
/// buffer.
size_t capacity() const { return Capacity - Begin; }
/// set_size - Set the array size to \arg N, which the current array must have /// set_size - Set the array size to \arg N, which the current array must have
/// enough capacity for. /// enough capacity for.
/// ///
@@ -420,17 +426,10 @@ public:
/// which will only be overwritten. /// which will only be overwritten.
void set_size(unsigned N) { void set_size(unsigned N) {
assert(N <= capacity()); assert(N <= capacity());
End = Begin + N; setEnd(begin() + N);
} }
private: private:
/// isSmall - Return true if this is a smallvector which has not had dynamic
/// memory allocated for it.
bool isSmall() const {
return static_cast<const void*>(Begin) ==
static_cast<const void*>(&FirstEl);
}
/// grow - double the size of the allocated memory, guaranteeing space for at /// grow - double the size of the allocated memory, guaranteeing space for at
/// least one more element or MinSize if specified. /// least one more element or MinSize if specified.
void grow(size_type MinSize = 0); void grow(size_type MinSize = 0);
@@ -441,6 +440,7 @@ private:
} }
void destroy_range(T *S, T *E) { void destroy_range(T *S, T *E) {
// TODO: POD
while (S != E) { while (S != E) {
--E; --E;
E->~T(); E->~T();
@@ -451,7 +451,7 @@ private:
// Define this out-of-line to dissuade the C++ compiler from inlining it. // Define this out-of-line to dissuade the C++ compiler from inlining it.
template <typename T> template <typename T>
void SmallVectorImpl<T>::grow(size_t MinSize) { void SmallVectorImpl<T>::grow(size_t MinSize) {
size_t CurCapacity = Capacity-Begin; size_t CurCapacity = capacity();
size_t CurSize = size(); size_t CurSize = size();
size_t NewCapacity = 2*CurCapacity; size_t NewCapacity = 2*CurCapacity;
if (NewCapacity < MinSize) if (NewCapacity < MinSize)
@@ -460,21 +460,21 @@ void SmallVectorImpl<T>::grow(size_t MinSize) {
// Copy the elements over. // Copy the elements over.
if (is_class<T>::value) if (is_class<T>::value)
std::uninitialized_copy(Begin, End, NewElts); std::uninitialized_copy(begin(), end(), NewElts);
else else
// Use memcpy for PODs (std::uninitialized_copy optimizes to memmove). // Use memcpy for PODs (std::uninitialized_copy optimizes to memmove).
memcpy(NewElts, Begin, CurSize * sizeof(T)); memcpy(NewElts, begin(), CurSize * sizeof(T));
// Destroy the original elements. // Destroy the original elements.
destroy_range(Begin, End); destroy_range(begin(), end());
// If this wasn't grown from the inline copy, deallocate the old space. // If this wasn't grown from the inline copy, deallocate the old space.
if (!isSmall()) if (!isSmall())
operator delete(Begin); operator delete(begin());
Begin = NewElts; setEnd(NewElts+CurSize);
End = NewElts+CurSize; BeginX = NewElts;
Capacity = Begin+NewCapacity; CapacityX = begin()+NewCapacity;
} }
template <typename T> template <typename T>
@@ -483,35 +483,35 @@ void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
// We can only avoid copying elements if neither vector is small. // We can only avoid copying elements if neither vector is small.
if (!isSmall() && !RHS.isSmall()) { if (!isSmall() && !RHS.isSmall()) {
std::swap(Begin, RHS.Begin); std::swap(BeginX, RHS.BeginX);
std::swap(End, RHS.End); std::swap(EndX, RHS.EndX);
std::swap(Capacity, RHS.Capacity); std::swap(CapacityX, RHS.CapacityX);
return; return;
} }
if (RHS.size() > size_type(Capacity-Begin)) if (RHS.size() > capacity())
grow(RHS.size()); grow(RHS.size());
if (size() > size_type(RHS.Capacity-RHS.begin())) if (size() > RHS.capacity())
RHS.grow(size()); RHS.grow(size());
// Swap the shared elements. // Swap the shared elements.
size_t NumShared = size(); size_t NumShared = size();
if (NumShared > RHS.size()) NumShared = RHS.size(); if (NumShared > RHS.size()) NumShared = RHS.size();
for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i) for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
std::swap(Begin[i], RHS[i]); std::swap((*this)[i], RHS[i]);
// Copy over the extra elts. // Copy over the extra elts.
if (size() > RHS.size()) { if (size() > RHS.size()) {
size_t EltDiff = size() - RHS.size(); size_t EltDiff = size() - RHS.size();
std::uninitialized_copy(Begin+NumShared, End, RHS.End); std::uninitialized_copy(begin()+NumShared, end(), RHS.end());
RHS.End += EltDiff; RHS.setEnd(RHS.end()+EltDiff);
destroy_range(Begin+NumShared, End); destroy_range(begin()+NumShared, end());
End = Begin+NumShared; setEnd(begin()+NumShared);
} else if (RHS.size() > size()) { } else if (RHS.size() > size()) {
size_t EltDiff = RHS.size() - size(); size_t EltDiff = RHS.size() - size();
std::uninitialized_copy(RHS.Begin+NumShared, RHS.End, End); std::uninitialized_copy(RHS.begin()+NumShared, RHS.end(), end());
End += EltDiff; setEnd(end() + EltDiff);
destroy_range(RHS.Begin+NumShared, RHS.End); destroy_range(RHS.begin()+NumShared, RHS.end());
RHS.End = RHS.Begin+NumShared; RHS.setEnd(RHS.begin()+NumShared);
} }
} }
@@ -523,42 +523,42 @@ SmallVectorImpl<T>::operator=(const SmallVectorImpl<T> &RHS) {
// If we already have sufficient space, assign the common elements, then // If we already have sufficient space, assign the common elements, then
// destroy any excess. // destroy any excess.
unsigned RHSSize = unsigned(RHS.size()); size_t RHSSize = RHS.size();
unsigned CurSize = unsigned(size()); size_t CurSize = size();
if (CurSize >= RHSSize) { if (CurSize >= RHSSize) {
// Assign common elements. // Assign common elements.
iterator NewEnd; iterator NewEnd;
if (RHSSize) if (RHSSize)
NewEnd = std::copy(RHS.Begin, RHS.Begin+RHSSize, Begin); NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, begin());
else else
NewEnd = Begin; NewEnd = begin();
// Destroy excess elements. // Destroy excess elements.
destroy_range(NewEnd, End); destroy_range(NewEnd, end());
// Trim. // Trim.
End = NewEnd; setEnd(NewEnd);
return *this; return *this;
} }
// If we have to grow to have enough elements, destroy the current elements. // If we have to grow to have enough elements, destroy the current elements.
// This allows us to avoid copying them during the grow. // This allows us to avoid copying them during the grow.
if (unsigned(Capacity-Begin) < RHSSize) { if (capacity() < RHSSize) {
// Destroy current elements. // Destroy current elements.
destroy_range(Begin, End); destroy_range(begin(), end());
End = Begin; setEnd(begin());
CurSize = 0; CurSize = 0;
grow(RHSSize); grow(RHSSize);
} else if (CurSize) { } else if (CurSize) {
// Otherwise, use assignment for the already-constructed elements. // Otherwise, use assignment for the already-constructed elements.
std::copy(RHS.Begin, RHS.Begin+CurSize, Begin); std::copy(RHS.begin(), RHS.begin()+CurSize, begin());
} }
// Copy construct the new elements in place. // Copy construct the new elements in place.
std::uninitialized_copy(RHS.Begin+CurSize, RHS.End, Begin+CurSize); std::uninitialized_copy(RHS.begin()+CurSize, RHS.end(), begin()+CurSize);
// Set end. // Set end.
End = Begin+RHSSize; setEnd(begin()+RHSSize);
return *this; return *this;
} }