mirror of
https://github.com/RPCS3/llvm.git
synced 2025-01-12 23:40:43 +00:00
sink most of the meat in smallvector back from SmallVectorTemplateCommon
down into SmallVectorImpl. This requires sprinking a ton of this->'s in, but gives us a place to factor. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91522 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
0588bcef8d
commit
e6e55d7959
@ -86,19 +86,11 @@ public:
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class SmallVectorTemplateCommon : public SmallVectorBase {
|
class SmallVectorTemplateCommon : public SmallVectorBase {
|
||||||
|
protected:
|
||||||
void setEnd(T *P) { this->EndX = P; }
|
void setEnd(T *P) { this->EndX = P; }
|
||||||
public:
|
public:
|
||||||
SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(Size) {}
|
SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(Size) {}
|
||||||
|
|
||||||
~SmallVectorTemplateCommon() {
|
|
||||||
// Destroy the constructed elements in the vector.
|
|
||||||
destroy_range(begin(), end());
|
|
||||||
|
|
||||||
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
||||||
if (!this->isSmall())
|
|
||||||
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;
|
||||||
@ -118,7 +110,7 @@ public:
|
|||||||
const_iterator begin() const { return (const_iterator)this->BeginX; }
|
const_iterator begin() const { return (const_iterator)this->BeginX; }
|
||||||
iterator end() { return (iterator)this->EndX; }
|
iterator end() { return (iterator)this->EndX; }
|
||||||
const_iterator end() const { return (const_iterator)this->EndX; }
|
const_iterator end() const { return (const_iterator)this->EndX; }
|
||||||
private:
|
protected:
|
||||||
iterator capacity_ptr() { return (iterator)this->CapacityX; }
|
iterator capacity_ptr() { return (iterator)this->CapacityX; }
|
||||||
const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
|
const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
|
||||||
public:
|
public:
|
||||||
@ -163,64 +155,108 @@ public:
|
|||||||
const_reference back() const {
|
const_reference back() const {
|
||||||
return end()[-1];
|
return end()[-1];
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void push_back(const_reference Elt) {
|
|
||||||
if (this->EndX < this->CapacityX) {
|
template <typename T, bool isPodLike>
|
||||||
Retry:
|
class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
|
||||||
new (end()) T(Elt);
|
public:
|
||||||
setEnd(end()+1);
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
||||||
return;
|
|
||||||
}
|
};
|
||||||
grow();
|
|
||||||
goto Retry;
|
template <typename T>
|
||||||
|
class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
|
||||||
|
public:
|
||||||
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// 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 SmallVectorTemplateBase<T, isPodLike<T>::value> {
|
||||||
|
public:
|
||||||
|
typedef typename SmallVectorTemplateBase<T, isPodLike<T>::value >::iterator
|
||||||
|
iterator;
|
||||||
|
typedef typename SmallVectorTemplateBase<T, isPodLike<T>::value >::size_type
|
||||||
|
size_type;
|
||||||
|
|
||||||
|
// Default ctor - Initialize to empty.
|
||||||
|
explicit SmallVectorImpl(unsigned N)
|
||||||
|
: SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop_back() {
|
~SmallVectorImpl() {
|
||||||
setEnd(end()-1);
|
// Destroy the constructed elements in the vector.
|
||||||
end()->~T();
|
destroy_range(this->begin(), this->end());
|
||||||
|
|
||||||
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
||||||
|
if (!this->isSmall())
|
||||||
|
operator delete(this->begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
T pop_back_val() {
|
|
||||||
T Result = back();
|
|
||||||
pop_back();
|
|
||||||
return Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
destroy_range(begin(), end());
|
destroy_range(this->begin(), this->end());
|
||||||
this->EndX = this->BeginX;
|
this->EndX = this->BeginX;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize(unsigned N) {
|
void resize(unsigned N) {
|
||||||
if (N < size()) {
|
if (N < this->size()) {
|
||||||
destroy_range(begin()+N, end());
|
this->destroy_range(this->begin()+N, this->end());
|
||||||
setEnd(begin()+N);
|
this->setEnd(this->begin()+N);
|
||||||
} else if (N > size()) {
|
} else if (N > this->size()) {
|
||||||
if (capacity() < N)
|
if (this->capacity() < N)
|
||||||
grow(N);
|
grow(N);
|
||||||
construct_range(end(), begin()+N, T());
|
this->construct_range(this->end(), this->begin()+N, T());
|
||||||
setEnd(begin()+N);
|
this->setEnd(this->begin()+N);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize(unsigned N, const T &NV) {
|
void resize(unsigned N, const T &NV) {
|
||||||
if (N < size()) {
|
if (N < this->size()) {
|
||||||
destroy_range(begin()+N, end());
|
destroy_range(this->begin()+N, this->end());
|
||||||
setEnd(begin()+N);
|
setEnd(this->begin()+N);
|
||||||
} else if (N > size()) {
|
} else if (N > this->size()) {
|
||||||
if (capacity() < N)
|
if (this->capacity() < N)
|
||||||
grow(N);
|
grow(N);
|
||||||
construct_range(end(), begin()+N, NV);
|
construct_range(this->end(), this->begin()+N, NV);
|
||||||
setEnd(begin()+N);
|
setEnd(this->begin()+N);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reserve(unsigned N) {
|
void reserve(unsigned N) {
|
||||||
if (capacity() < N)
|
if (this->capacity() < N)
|
||||||
grow(N);
|
grow(N);
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(SmallVectorTemplateCommon &RHS);
|
void push_back(const T &Elt) {
|
||||||
|
if (this->EndX < this->CapacityX) {
|
||||||
|
Retry:
|
||||||
|
new (this->end()) T(Elt);
|
||||||
|
setEnd(this->end()+1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->grow();
|
||||||
|
goto Retry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop_back() {
|
||||||
|
setEnd(this->end()-1);
|
||||||
|
this->end()->~T();
|
||||||
|
}
|
||||||
|
|
||||||
|
T pop_back_val() {
|
||||||
|
T Result = this->back();
|
||||||
|
pop_back();
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void swap(SmallVectorImpl &RHS);
|
||||||
|
|
||||||
/// append - Add the specified range to the end of the SmallVector.
|
/// append - Add the specified range to the end of the SmallVector.
|
||||||
///
|
///
|
||||||
@ -228,40 +264,40 @@ 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_ptr()-end()))
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
||||||
grow(size()+NumInputs);
|
grow(this->size()+NumInputs);
|
||||||
|
|
||||||
// Copy the new elements over.
|
// Copy the new elements over.
|
||||||
// TODO: NEED To compile time dispatch on whether in_iter is a random access
|
// TODO: NEED To compile time dispatch on whether in_iter is a random access
|
||||||
// iterator to use the fast uninitialized_copy.
|
// iterator to use the fast uninitialized_copy.
|
||||||
std::uninitialized_copy(in_start, in_end, end());
|
std::uninitialized_copy(in_start, in_end, this->end());
|
||||||
setEnd(end() + NumInputs);
|
setEnd(this->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_ptr()-end()))
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
||||||
grow(size()+NumInputs);
|
grow(this->size()+NumInputs);
|
||||||
|
|
||||||
// Copy the new elements over.
|
// Copy the new elements over.
|
||||||
std::uninitialized_fill_n(end(), NumInputs, Elt);
|
std::uninitialized_fill_n(this->end(), NumInputs, Elt);
|
||||||
setEnd(end() + NumInputs);
|
setEnd(this->end() + NumInputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void assign(unsigned NumElts, const T &Elt) {
|
void assign(unsigned NumElts, const T &Elt) {
|
||||||
clear();
|
clear();
|
||||||
if (capacity() < NumElts)
|
if (this->capacity() < NumElts)
|
||||||
grow(NumElts);
|
grow(NumElts);
|
||||||
setEnd(begin()+NumElts);
|
setEnd(this->begin()+NumElts);
|
||||||
construct_range(begin(), end(), Elt);
|
construct_range(this->begin(), this->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, this->end(), I);
|
||||||
// Drop the last elt.
|
// Drop the last elt.
|
||||||
pop_back();
|
pop_back();
|
||||||
return(N);
|
return(N);
|
||||||
@ -270,56 +306,56 @@ 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, this->end(), S);
|
||||||
// Drop the last elts.
|
// Drop the last elts.
|
||||||
destroy_range(I, end());
|
destroy_range(I, this->end());
|
||||||
setEnd(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 == this->end()) { // Important special case for empty vector.
|
||||||
push_back(Elt);
|
push_back(Elt);
|
||||||
return end()-1;
|
return this->end()-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->EndX < this->CapacityX) {
|
if (this->EndX < this->CapacityX) {
|
||||||
Retry:
|
Retry:
|
||||||
new (end()) T(back());
|
new (this->end()) T(this->back());
|
||||||
setEnd(end()+1);
|
this->setEnd(this->end()+1);
|
||||||
// Push everything else over.
|
// Push everything else over.
|
||||||
std::copy_backward(I, end()-1, end());
|
std::copy_backward(I, this->end()-1, this->end());
|
||||||
*I = Elt;
|
*I = Elt;
|
||||||
return I;
|
return I;
|
||||||
}
|
}
|
||||||
size_t EltNo = I-begin();
|
size_t EltNo = I-this->begin();
|
||||||
grow();
|
this->grow();
|
||||||
I = begin()+EltNo;
|
I = this->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 == this->end()) { // Important special case for empty vector.
|
||||||
append(NumToInsert, Elt);
|
append(NumToInsert, Elt);
|
||||||
return end()-1;
|
return this->end()-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
||||||
size_t InsertElt = I-begin();
|
size_t InsertElt = I - this->begin();
|
||||||
|
|
||||||
// Ensure there is enough space.
|
// Ensure there is enough space.
|
||||||
reserve(static_cast<unsigned>(size() + NumToInsert));
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
||||||
|
|
||||||
// Uninvalidate the iterator.
|
// Uninvalidate the iterator.
|
||||||
I = begin()+InsertElt;
|
I = this->begin()+InsertElt;
|
||||||
|
|
||||||
// If there are more elements between the insertion point and the end of the
|
// If there are more elements between the insertion point and the end of the
|
||||||
// range than there are being inserted, we can use a simple approach to
|
// range than there are being inserted, we can use a simple approach to
|
||||||
// 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(this->end()-I) >= NumToInsert) {
|
||||||
T *OldEnd = end();
|
T *OldEnd = this->end();
|
||||||
append(end()-NumToInsert, end());
|
append(this->end()-NumToInsert, this->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);
|
||||||
@ -332,10 +368,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 = this->end();
|
||||||
setEnd(end() + NumToInsert);
|
setEnd(this->end() + NumToInsert);
|
||||||
size_t NumOverwritten = OldEnd-I;
|
size_t NumOverwritten = OldEnd-I;
|
||||||
uninitialized_copy(I, OldEnd, end()-NumOverwritten);
|
uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
|
||||||
|
|
||||||
// Replace the overwritten part.
|
// Replace the overwritten part.
|
||||||
std::fill_n(I, NumOverwritten, Elt);
|
std::fill_n(I, NumOverwritten, Elt);
|
||||||
@ -347,28 +383,28 @@ 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 == this->end()) { // Important special case for empty vector.
|
||||||
append(From, To);
|
append(From, To);
|
||||||
return end()-1;
|
return this->end()-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t NumToInsert = std::distance(From, To);
|
size_t NumToInsert = std::distance(From, To);
|
||||||
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
||||||
size_t InsertElt = I-begin();
|
size_t InsertElt = I - this->begin();
|
||||||
|
|
||||||
// Ensure there is enough space.
|
// Ensure there is enough space.
|
||||||
reserve(static_cast<unsigned>(size() + NumToInsert));
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
||||||
|
|
||||||
// Uninvalidate the iterator.
|
// Uninvalidate the iterator.
|
||||||
I = begin()+InsertElt;
|
I = this->begin()+InsertElt;
|
||||||
|
|
||||||
// If there are more elements between the insertion point and the end of the
|
// If there are more elements between the insertion point and the end of the
|
||||||
// range than there are being inserted, we can use a simple approach to
|
// range than there are being inserted, we can use a simple approach to
|
||||||
// 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(this->end()-I) >= NumToInsert) {
|
||||||
T *OldEnd = end();
|
T *OldEnd = this->end();
|
||||||
append(end()-NumToInsert, end());
|
append(this->end()-NumToInsert, this->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);
|
||||||
@ -381,10 +417,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 = this->end();
|
||||||
setEnd(end() + NumToInsert);
|
setEnd(this->end() + NumToInsert);
|
||||||
size_t NumOverwritten = OldEnd-I;
|
size_t NumOverwritten = OldEnd-I;
|
||||||
uninitialized_copy(I, OldEnd, end()-NumOverwritten);
|
uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
|
||||||
|
|
||||||
// Replace the overwritten part.
|
// Replace the overwritten part.
|
||||||
std::copy(From, From+NumOverwritten, I);
|
std::copy(From, From+NumOverwritten, I);
|
||||||
@ -394,19 +430,19 @@ public:
|
|||||||
return I;
|
return I;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SmallVectorTemplateCommon
|
const SmallVectorImpl
|
||||||
&operator=(const SmallVectorTemplateCommon &RHS);
|
&operator=(const SmallVectorImpl &RHS);
|
||||||
|
|
||||||
bool operator==(const SmallVectorTemplateCommon &RHS) const {
|
bool operator==(const SmallVectorImpl &RHS) const {
|
||||||
if (size() != RHS.size()) return false;
|
if (this->size() != RHS.size()) return false;
|
||||||
return std::equal(begin(), end(), RHS.begin());
|
return std::equal(this->begin(), this->end(), RHS.begin());
|
||||||
}
|
}
|
||||||
bool operator!=(const SmallVectorTemplateCommon &RHS) const {
|
bool operator!=(const SmallVectorImpl &RHS) const {
|
||||||
return !(*this == RHS);
|
return !(*this == RHS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const SmallVectorTemplateCommon &RHS) const {
|
bool operator<(const SmallVectorImpl &RHS) const {
|
||||||
return std::lexicographical_compare(begin(), end(),
|
return std::lexicographical_compare(this->begin(), this->end(),
|
||||||
RHS.begin(), RHS.end());
|
RHS.begin(), RHS.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,14 +456,14 @@ public:
|
|||||||
/// update the size later. This avoids the cost of value initializing elements
|
/// update the size later. This avoids the cost of value initializing elements
|
||||||
/// 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 <= this->capacity());
|
||||||
setEnd(begin() + N);
|
setEnd(this->begin() + N);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// 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_t MinSize = 0);
|
||||||
|
|
||||||
static void construct_range(T *S, T *E, const T &Elt) {
|
static void construct_range(T *S, T *E, const T &Elt) {
|
||||||
for (; S != E; ++S)
|
for (; S != E; ++S)
|
||||||
@ -459,31 +495,31 @@ 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 SmallVectorTemplateCommon<T>::grow(size_t MinSize) {
|
void SmallVectorImpl<T>::grow(size_t MinSize) {
|
||||||
size_t CurCapacity = capacity();
|
size_t CurCapacity = this->capacity();
|
||||||
size_t CurSize = size();
|
size_t CurSize = this->size();
|
||||||
size_t NewCapacity = 2*CurCapacity;
|
size_t NewCapacity = 2*CurCapacity;
|
||||||
if (NewCapacity < MinSize)
|
if (NewCapacity < MinSize)
|
||||||
NewCapacity = MinSize;
|
NewCapacity = MinSize;
|
||||||
T *NewElts = static_cast<T*>(operator new(NewCapacity*sizeof(T)));
|
T *NewElts = static_cast<T*>(operator new(NewCapacity*sizeof(T)));
|
||||||
|
|
||||||
// Copy the elements over.
|
// Copy the elements over.
|
||||||
uninitialized_copy(begin(), end(), NewElts);
|
uninitialized_copy(this->begin(), this->end(), NewElts);
|
||||||
|
|
||||||
// Destroy the original elements.
|
// Destroy the original elements.
|
||||||
destroy_range(begin(), end());
|
destroy_range(this->begin(), this->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 (!this->isSmall())
|
if (!this->isSmall())
|
||||||
operator delete(begin());
|
operator delete(this->begin());
|
||||||
|
|
||||||
setEnd(NewElts+CurSize);
|
setEnd(NewElts+CurSize);
|
||||||
this->BeginX = NewElts;
|
this->BeginX = NewElts;
|
||||||
this->CapacityX = begin()+NewCapacity;
|
this->CapacityX = this->begin()+NewCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void SmallVectorTemplateCommon<T>::swap(SmallVectorTemplateCommon<T> &RHS) {
|
void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
|
||||||
if (this == &RHS) return;
|
if (this == &RHS) return;
|
||||||
|
|
||||||
// We can only avoid copying elements if neither vector is small.
|
// We can only avoid copying elements if neither vector is small.
|
||||||
@ -493,54 +529,53 @@ void SmallVectorTemplateCommon<T>::swap(SmallVectorTemplateCommon<T> &RHS) {
|
|||||||
std::swap(this->CapacityX, RHS.CapacityX);
|
std::swap(this->CapacityX, RHS.CapacityX);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (RHS.size() > capacity())
|
if (RHS.size() > this->capacity())
|
||||||
grow(RHS.size());
|
grow(RHS.size());
|
||||||
if (size() > RHS.capacity())
|
if (this->size() > RHS.capacity())
|
||||||
RHS.grow(size());
|
RHS.grow(this->size());
|
||||||
|
|
||||||
// Swap the shared elements.
|
// Swap the shared elements.
|
||||||
size_t NumShared = size();
|
size_t NumShared = this->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((*this)[i], RHS[i]);
|
std::swap((*this)[i], RHS[i]);
|
||||||
|
|
||||||
// Copy over the extra elts.
|
// Copy over the extra elts.
|
||||||
if (size() > RHS.size()) {
|
if (this->size() > RHS.size()) {
|
||||||
size_t EltDiff = size() - RHS.size();
|
size_t EltDiff = this->size() - RHS.size();
|
||||||
uninitialized_copy(begin()+NumShared, end(), RHS.end());
|
uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
|
||||||
RHS.setEnd(RHS.end()+EltDiff);
|
RHS.setEnd(RHS.end()+EltDiff);
|
||||||
destroy_range(begin()+NumShared, end());
|
destroy_range(this->begin()+NumShared, this->end());
|
||||||
setEnd(begin()+NumShared);
|
setEnd(this->begin()+NumShared);
|
||||||
} else if (RHS.size() > size()) {
|
} else if (RHS.size() > this->size()) {
|
||||||
size_t EltDiff = RHS.size() - size();
|
size_t EltDiff = RHS.size() - this->size();
|
||||||
uninitialized_copy(RHS.begin()+NumShared, RHS.end(), end());
|
uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
|
||||||
setEnd(end() + EltDiff);
|
setEnd(this->end() + EltDiff);
|
||||||
destroy_range(RHS.begin()+NumShared, RHS.end());
|
destroy_range(RHS.begin()+NumShared, RHS.end());
|
||||||
RHS.setEnd(RHS.begin()+NumShared);
|
RHS.setEnd(RHS.begin()+NumShared);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const SmallVectorTemplateCommon<T> &
|
const SmallVectorImpl<T> &SmallVectorImpl<T>::
|
||||||
SmallVectorTemplateCommon<T>::
|
operator=(const SmallVectorImpl<T> &RHS) {
|
||||||
operator=(const SmallVectorTemplateCommon<T> &RHS) {
|
|
||||||
// Avoid self-assignment.
|
// Avoid self-assignment.
|
||||||
if (this == &RHS) return *this;
|
if (this == &RHS) return *this;
|
||||||
|
|
||||||
// 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.
|
||||||
size_t RHSSize = RHS.size();
|
size_t RHSSize = RHS.size();
|
||||||
size_t CurSize = size();
|
size_t CurSize = this->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, this->begin());
|
||||||
else
|
else
|
||||||
NewEnd = begin();
|
NewEnd = this->begin();
|
||||||
|
|
||||||
// Destroy excess elements.
|
// Destroy excess elements.
|
||||||
destroy_range(NewEnd, end());
|
destroy_range(NewEnd, this->end());
|
||||||
|
|
||||||
// Trim.
|
// Trim.
|
||||||
setEnd(NewEnd);
|
setEnd(NewEnd);
|
||||||
@ -549,53 +584,26 @@ SmallVectorTemplateCommon<T>::
|
|||||||
|
|
||||||
// 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 (capacity() < RHSSize) {
|
if (this->capacity() < RHSSize) {
|
||||||
// Destroy current elements.
|
// Destroy current elements.
|
||||||
destroy_range(begin(), end());
|
destroy_range(this->begin(), this->end());
|
||||||
setEnd(begin());
|
setEnd(this->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, this->begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy construct the new elements in place.
|
// Copy construct the new elements in place.
|
||||||
uninitialized_copy(RHS.begin()+CurSize, RHS.end(), begin()+CurSize);
|
uninitialized_copy(RHS.begin()+CurSize, RHS.end(), this->begin()+CurSize);
|
||||||
|
|
||||||
// Set end.
|
// Set end.
|
||||||
setEnd(begin()+RHSSize);
|
setEnd(this->begin()+RHSSize);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, bool isPodLike>
|
|
||||||
class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
|
|
||||||
public:
|
|
||||||
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
|
|
||||||
public:
|
|
||||||
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// 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 SmallVectorTemplateBase<T, isPodLike<T>::value> {
|
|
||||||
public:
|
|
||||||
// Default ctor - Initialize to empty.
|
|
||||||
explicit SmallVectorImpl(unsigned N)
|
|
||||||
: SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
|
/// SmallVector - This is a 'vector' (really, a variable-sized array), optimized
|
||||||
/// for the case when the array is small. It contains some number of elements
|
/// for the case when the array is small. It contains some number of elements
|
||||||
/// in-place, which allows it to avoid heap allocation when the actual number of
|
/// in-place, which allows it to avoid heap allocation when the actual number of
|
||||||
|
Loading…
x
Reference in New Issue
Block a user