2006-07-26 06:22:30 +00:00
|
|
|
//===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-07-26 06:22:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SmallVector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SMALLVECTOR_H
|
|
|
|
#define LLVM_ADT_SMALLVECTOR_H
|
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-06-30 21:45:13 +00:00
|
|
|
#include "llvm/Support/type_traits.h"
|
2006-07-27 03:38:08 +00:00
|
|
|
#include <algorithm>
|
2009-02-20 22:20:18 +00:00
|
|
|
#include <cassert>
|
2010-06-10 10:13:58 +00:00
|
|
|
#include <cstddef>
|
2010-06-08 11:44:30 +00:00
|
|
|
#include <cstdlib>
|
2008-07-01 05:27:28 +00:00
|
|
|
#include <cstring>
|
2010-12-19 20:43:38 +00:00
|
|
|
#include <iterator>
|
2006-07-26 06:22:30 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-12-15 07:40:44 +00:00
|
|
|
/// SmallVectorBase - This is all the non-templated stuff common to all
|
|
|
|
/// SmallVectors.
|
|
|
|
class SmallVectorBase {
|
2006-10-30 03:39:20 +00:00
|
|
|
protected:
|
2009-12-15 08:29:22 +00:00
|
|
|
void *BeginX, *EndX, *CapacityX;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
// 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
|
|
|
|
// something else. An array of char would work great, but might not be
|
2010-10-03 15:31:50 +00:00
|
|
|
// aligned sufficiently. Instead we use some number of union instances for
|
|
|
|
// the space, which guarantee maximal alignment.
|
2010-10-03 15:15:19 +00:00
|
|
|
union U {
|
|
|
|
double D;
|
|
|
|
long double LD;
|
|
|
|
long long L;
|
|
|
|
void *P;
|
2010-03-18 18:40:47 +00:00
|
|
|
} FirstEl;
|
2006-08-11 23:19:51 +00:00
|
|
|
// Space after 'FirstEl' is clobbered, do not add any instance vars after it.
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
protected:
|
|
|
|
SmallVectorBase(size_t Size)
|
|
|
|
: BeginX(&FirstEl), EndX(&FirstEl), CapacityX((char*)&FirstEl+Size) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
/// 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);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
/// resetToSmall - Put this vector in a state of being small.
|
|
|
|
void resetToSmall() {
|
|
|
|
BeginX = EndX = CapacityX = &FirstEl;
|
|
|
|
}
|
|
|
|
|
2011-07-27 18:40:45 +00:00
|
|
|
/// grow_pod - This is an implementation of the grow() method which only works
|
|
|
|
/// on POD-like data types and is out of line to reduce code duplication.
|
|
|
|
void grow_pod(size_t MinSizeInBytes, size_t TSize);
|
|
|
|
|
|
|
|
public:
|
2009-12-16 08:34:40 +00:00
|
|
|
/// size_in_bytes - This returns size()*sizeof(T).
|
|
|
|
size_t size_in_bytes() const {
|
|
|
|
return size_t((char*)EndX - (char*)BeginX);
|
|
|
|
}
|
2011-07-27 18:40:45 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
/// capacity_in_bytes - This returns capacity()*sizeof(T).
|
|
|
|
size_t capacity_in_bytes() const {
|
|
|
|
return size_t((char*)CapacityX - (char*)BeginX);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
bool empty() const { return BeginX == EndX; }
|
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 06:55:45 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
template <typename T>
|
2009-12-16 06:55:45 +00:00
|
|
|
class SmallVectorTemplateCommon : public SmallVectorBase {
|
2009-12-16 08:05:48 +00:00
|
|
|
protected:
|
2009-12-16 06:55:45 +00:00
|
|
|
SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(Size) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
void setEnd(T *P) { this->EndX = P; }
|
|
|
|
public:
|
2006-07-26 06:22:30 +00:00
|
|
|
typedef size_t size_type;
|
2008-10-16 00:15:24 +00:00
|
|
|
typedef ptrdiff_t difference_type;
|
2008-08-15 18:45:51 +00:00
|
|
|
typedef T value_type;
|
2009-12-15 08:29:22 +00:00
|
|
|
typedef T *iterator;
|
|
|
|
typedef const T *const_iterator;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
typedef T &reference;
|
|
|
|
typedef const T &const_reference;
|
|
|
|
typedef T *pointer;
|
|
|
|
typedef const T *const_pointer;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2007-08-20 21:22:48 +00:00
|
|
|
// forward iterator creation methods.
|
2009-12-16 06:55:45 +00:00
|
|
|
iterator begin() { return (iterator)this->BeginX; }
|
|
|
|
const_iterator begin() const { return (const_iterator)this->BeginX; }
|
|
|
|
iterator end() { return (iterator)this->EndX; }
|
|
|
|
const_iterator end() const { return (const_iterator)this->EndX; }
|
2009-12-16 08:05:48 +00:00
|
|
|
protected:
|
2009-12-16 06:55:45 +00:00
|
|
|
iterator capacity_ptr() { return (iterator)this->CapacityX; }
|
|
|
|
const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
|
2009-12-15 08:29:22 +00:00
|
|
|
public:
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2007-08-20 21:22:48 +00:00
|
|
|
// reverse iterator creation methods.
|
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
|
|
|
const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
|
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
|
|
|
const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
size_type size() const { return end()-begin(); }
|
|
|
|
size_type max_size() const { return size_type(-1) / sizeof(T); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
/// capacity - Return the total number of elements in the currently allocated
|
|
|
|
/// buffer.
|
|
|
|
size_t capacity() const { return capacity_ptr() - begin(); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-15 08:29:22 +00:00
|
|
|
/// 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()); }
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
reference operator[](unsigned idx) {
|
2009-12-15 08:29:22 +00:00
|
|
|
assert(begin() + idx < end());
|
|
|
|
return begin()[idx];
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
|
|
|
const_reference operator[](unsigned idx) const {
|
2009-12-15 08:29:22 +00:00
|
|
|
assert(begin() + idx < end());
|
|
|
|
return begin()[idx];
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
reference front() {
|
|
|
|
return begin()[0];
|
|
|
|
}
|
|
|
|
const_reference front() const {
|
|
|
|
return begin()[0];
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
reference back() {
|
|
|
|
return end()[-1];
|
|
|
|
}
|
|
|
|
const_reference back() const {
|
|
|
|
return end()[-1];
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
/// SmallVectorTemplateBase<isPodLike = false> - This is where we put method
|
|
|
|
/// implementations that are designed to work with non-POD-like T's.
|
2009-12-16 08:05:48 +00:00
|
|
|
template <typename T, bool isPodLike>
|
|
|
|
class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
static void destroy_range(T *S, T *E) {
|
|
|
|
while (S != E) {
|
|
|
|
--E;
|
|
|
|
E->~T();
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
/// move - Use move-assignment to move the range [I, E) onto the
|
|
|
|
/// objects starting with "Dest". This is just <memory>'s
|
|
|
|
/// std::move, but not all stdlibs actually provide that.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static It2 move(It1 I, It1 E, It2 Dest) {
|
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
for (; I != E; ++I, ++Dest)
|
|
|
|
*Dest = ::std::move(*I);
|
|
|
|
return Dest;
|
|
|
|
#else
|
|
|
|
return ::std::copy(I, E, Dest);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// move_backward - Use move-assignment to move the range
|
|
|
|
/// [I, E) onto the objects ending at "Dest", moving objects
|
|
|
|
/// in reverse order. This is just <algorithm>'s
|
|
|
|
/// std::move_backward, but not all stdlibs actually provide that.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static It2 move_backward(It1 I, It1 E, It2 Dest) {
|
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
while (I != E)
|
|
|
|
*--Dest = ::std::move(*--E);
|
|
|
|
return Dest;
|
|
|
|
#else
|
|
|
|
return ::std::copy_backward(I, E, Dest);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// uninitialized_move - Move the range [I, E) into the uninitialized
|
|
|
|
/// memory starting with "Dest", constructing elements as needed.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_move(It1 I, It1 E, It2 Dest) {
|
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
for (; I != E; ++I, ++Dest)
|
|
|
|
::new ((void*) &*Dest) T(::std::move(*I));
|
|
|
|
#else
|
|
|
|
::std::uninitialized_copy(I, E, Dest);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// uninitialized_copy - Copy the range [I, E) onto the uninitialized
|
|
|
|
/// memory starting with "Dest", constructing elements as needed.
|
2009-12-16 08:09:23 +00:00
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
|
|
|
|
std::uninitialized_copy(I, E, Dest);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
/// grow - Grow the allocated memory (without initializing new
|
|
|
|
/// elements), doubling the size of the allocated memory.
|
|
|
|
/// Guarantees space for at least one more element, or MinSize more
|
|
|
|
/// elements if specified.
|
2009-12-16 08:34:40 +00:00
|
|
|
void grow(size_t MinSize = 0);
|
2012-02-16 04:58:48 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
|
|
|
if (this->EndX < this->CapacityX) {
|
|
|
|
Retry:
|
2012-05-02 05:39:15 +00:00
|
|
|
::new ((void*) this->end()) T(Elt);
|
|
|
|
this->setEnd(this->end()+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->grow();
|
|
|
|
goto Retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
void push_back(T &&Elt) {
|
|
|
|
if (this->EndX < this->CapacityX) {
|
|
|
|
Retry:
|
|
|
|
::new ((void*) this->end()) T(::std::move(Elt));
|
2012-02-16 04:58:48 +00:00
|
|
|
this->setEnd(this->end()+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->grow();
|
|
|
|
goto Retry;
|
|
|
|
}
|
2012-05-02 05:39:15 +00:00
|
|
|
#endif
|
2012-02-16 04:58:48 +00:00
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
this->setEnd(this->end()-1);
|
|
|
|
this->end()->~T();
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// Define this out-of-line to dissuade the C++ compiler from inlining it.
|
|
|
|
template <typename T, bool isPodLike>
|
|
|
|
void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
|
|
|
|
size_t CurCapacity = this->capacity();
|
|
|
|
size_t CurSize = this->size();
|
2010-09-02 21:55:03 +00:00
|
|
|
size_t NewCapacity = 2*CurCapacity + 1; // Always grow, even from zero.
|
2009-12-16 08:34:40 +00:00
|
|
|
if (NewCapacity < MinSize)
|
|
|
|
NewCapacity = MinSize;
|
2010-06-08 11:44:30 +00:00
|
|
|
T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
// Move the elements over.
|
|
|
|
this->uninitialized_move(this->begin(), this->end(), NewElts);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// Destroy the original elements.
|
|
|
|
destroy_range(this->begin(), this->end());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 11:44:30 +00:00
|
|
|
free(this->begin());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 11:38:03 +00:00
|
|
|
this->setEnd(NewElts+CurSize);
|
2009-12-16 08:34:40 +00:00
|
|
|
this->BeginX = NewElts;
|
|
|
|
this->CapacityX = this->begin()+NewCapacity;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
/// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
|
|
|
|
/// implementations that are designed to work with POD-like T's.
|
2009-12-16 08:05:48 +00:00
|
|
|
template <typename T>
|
|
|
|
class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
// No need to do a destroy loop for POD's.
|
2009-12-17 19:07:19 +00:00
|
|
|
static void destroy_range(T *, T *) {}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
/// move - Use move-assignment to move the range [I, E) onto the
|
|
|
|
/// objects starting with "Dest". For PODs, this is just memcpy.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static It2 move(It1 I, It1 E, It2 Dest) {
|
|
|
|
return ::std::copy(I, E, Dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// move_backward - Use move-assignment to move the range
|
|
|
|
/// [I, E) onto the objects ending at "Dest", moving objects
|
|
|
|
/// in reverse order.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static It2 move_backward(It1 I, It1 E, It2 Dest) {
|
|
|
|
return ::std::copy_backward(I, E, Dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// uninitialized_move - Move the range [I, E) onto the uninitialized memory
|
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_move(It1 I, It1 E, It2 Dest) {
|
|
|
|
// Just do a copy.
|
|
|
|
uninitialized_copy(I, E, Dest);
|
|
|
|
}
|
|
|
|
|
2009-12-16 08:09:23 +00:00
|
|
|
/// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
|
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
|
2010-03-26 18:53:37 +00:00
|
|
|
// Arbitrary iterator types; just use the basic implementation.
|
|
|
|
std::uninitialized_copy(I, E, Dest);
|
2009-12-16 08:09:23 +00:00
|
|
|
}
|
2010-03-26 18:53:37 +00:00
|
|
|
|
|
|
|
/// uninitialized_copy - Copy the range [I, E) onto the uninitialized memory
|
|
|
|
/// starting with "Dest", constructing elements into it as needed.
|
|
|
|
template<typename T1, typename T2>
|
|
|
|
static void uninitialized_copy(T1 *I, T1 *E, T2 *Dest) {
|
|
|
|
// Use memcpy for PODs iterated by pointers (which includes SmallVector
|
|
|
|
// iterators): std::uninitialized_copy optimizes to memmove, but we can
|
|
|
|
// use memcpy here.
|
|
|
|
memcpy(Dest, I, (E-I)*sizeof(T));
|
|
|
|
}
|
|
|
|
|
2009-12-16 08:34:40 +00:00
|
|
|
/// grow - double the size of the allocated memory, guaranteeing space for at
|
|
|
|
/// least one more element or MinSize if specified.
|
|
|
|
void grow(size_t MinSize = 0) {
|
|
|
|
this->grow_pod(MinSize*sizeof(T), sizeof(T));
|
|
|
|
}
|
2012-02-16 04:58:48 +00:00
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
|
|
|
if (this->EndX < this->CapacityX) {
|
|
|
|
Retry:
|
2012-04-29 10:53:29 +00:00
|
|
|
memcpy(this->end(), &Elt, sizeof(T));
|
2012-02-16 04:58:48 +00:00
|
|
|
this->setEnd(this->end()+1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->grow();
|
|
|
|
goto Retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
this->setEnd(this->end()-1);
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
/// 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> {
|
2009-12-16 08:09:23 +00:00
|
|
|
typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
|
2010-11-26 04:16:08 +00:00
|
|
|
|
2010-06-15 18:59:43 +00:00
|
|
|
SmallVectorImpl(const SmallVectorImpl&); // DISABLED.
|
2009-12-16 08:05:48 +00:00
|
|
|
public:
|
2009-12-16 08:09:23 +00:00
|
|
|
typedef typename SuperClass::iterator iterator;
|
|
|
|
typedef typename SuperClass::size_type size_type;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
protected:
|
2009-12-16 08:05:48 +00:00
|
|
|
// Default ctor - Initialize to empty.
|
|
|
|
explicit SmallVectorImpl(unsigned N)
|
|
|
|
: SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
|
2006-07-28 05:03:42 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-01-20 16:39:46 +00:00
|
|
|
public:
|
2009-12-16 08:05:48 +00:00
|
|
|
~SmallVectorImpl() {
|
|
|
|
// Destroy the constructed elements in the vector.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 11:44:30 +00:00
|
|
|
free(this->begin());
|
2008-12-05 07:11:05 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
|
|
|
|
2006-08-07 05:45:34 +00:00
|
|
|
void clear() {
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2009-12-16 06:55:45 +00:00
|
|
|
this->EndX = this->BeginX;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
void resize(unsigned N) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (N < this->size()) {
|
|
|
|
this->destroy_range(this->begin()+N, this->end());
|
|
|
|
this->setEnd(this->begin()+N);
|
|
|
|
} else if (N > this->size()) {
|
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2012-02-05 22:48:31 +00:00
|
|
|
std::uninitialized_fill(this->end(), this->begin()+N, T());
|
2009-12-16 08:05:48 +00:00
|
|
|
this->setEnd(this->begin()+N);
|
2006-08-07 05:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-28 21:52:08 +00:00
|
|
|
void resize(unsigned N, const T &NV) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (N < this->size()) {
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin()+N, this->end());
|
|
|
|
this->setEnd(this->begin()+N);
|
2009-12-16 08:05:48 +00:00
|
|
|
} else if (N > this->size()) {
|
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2012-02-05 22:48:31 +00:00
|
|
|
std::uninitialized_fill(this->end(), this->begin()+N, NV);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->begin()+N);
|
2006-08-28 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-09-01 06:08:16 +00:00
|
|
|
void reserve(unsigned N) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->capacity() < N)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(N);
|
2006-09-01 06:08:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
T pop_back_val() {
|
2012-05-02 05:39:15 +00:00
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
T Result = ::std::move(this->back());
|
|
|
|
#else
|
2009-12-16 08:05:48 +00:00
|
|
|
T Result = this->back();
|
2012-05-02 05:39:15 +00:00
|
|
|
#endif
|
2012-02-16 04:58:48 +00:00
|
|
|
this->pop_back();
|
2009-12-16 08:05:48 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
void swap(SmallVectorImpl &RHS);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-07-27 03:38:08 +00:00
|
|
|
/// append - Add the specified range to the end of the SmallVector.
|
|
|
|
///
|
|
|
|
template<typename in_iter>
|
|
|
|
void append(in_iter in_start, in_iter in_end) {
|
2008-05-05 18:30:58 +00:00
|
|
|
size_type NumInputs = std::distance(in_start, in_end);
|
2006-07-27 03:38:08 +00:00
|
|
|
// Grow allocated space if needed.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-07-27 03:38:08 +00:00
|
|
|
// Copy the new elements over.
|
2009-12-16 06:55:45 +00:00
|
|
|
// TODO: NEED To compile time dispatch on whether in_iter is a random access
|
|
|
|
// iterator to use the fast uninitialized_copy.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::uninitialized_copy(in_start, in_end, this->end());
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->end() + NumInputs);
|
2006-07-27 03:38:08 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
/// append - Add the specified range to the end of the SmallVector.
|
|
|
|
///
|
|
|
|
void append(size_type NumInputs, const T &Elt) {
|
|
|
|
// Grow allocated space if needed.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Copy the new elements over.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::uninitialized_fill_n(this->end(), NumInputs, Elt);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->end() + NumInputs);
|
2008-10-16 00:15:24 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-08-08 01:44:16 +00:00
|
|
|
void assign(unsigned NumElts, const T &Elt) {
|
|
|
|
clear();
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->capacity() < NumElts)
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(NumElts);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->begin()+NumElts);
|
2012-02-05 22:48:31 +00:00
|
|
|
std::uninitialized_fill(this->begin(), this->end(), Elt);
|
2006-08-08 01:44:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-04-02 18:24:46 +00:00
|
|
|
iterator erase(iterator I) {
|
|
|
|
iterator N = I;
|
2006-08-22 06:27:16 +00:00
|
|
|
// Shift all elts down one.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::copy(I+1, this->end(), I);
|
2006-08-22 06:27:16 +00:00
|
|
|
// Drop the last elt.
|
2012-02-16 04:58:48 +00:00
|
|
|
this->pop_back();
|
2008-04-02 18:24:46 +00:00
|
|
|
return(N);
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-04-02 18:24:46 +00:00
|
|
|
iterator erase(iterator S, iterator E) {
|
|
|
|
iterator N = S;
|
2006-08-22 06:27:16 +00:00
|
|
|
// Shift all elts down.
|
2009-12-16 08:05:48 +00:00
|
|
|
iterator I = std::copy(E, this->end(), S);
|
2006-08-22 06:27:16 +00:00
|
|
|
// Drop the last elts.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(I, this->end());
|
|
|
|
this->setEnd(I);
|
2008-04-02 18:24:46 +00:00
|
|
|
return(N);
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
iterator insert(iterator I, T &&Elt) {
|
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
|
|
|
this->push_back(::std::move(Elt));
|
|
|
|
return this->end()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->EndX < this->CapacityX) {
|
|
|
|
Retry:
|
|
|
|
::new ((void*) this->end()) T(::std::move(this->back()));
|
|
|
|
this->setEnd(this->end()+1);
|
|
|
|
// Push everything else over.
|
|
|
|
this->move_backward(I, this->end()-1, this->end());
|
|
|
|
|
|
|
|
// If we just moved the element we're inserting, be sure to update
|
|
|
|
// the reference.
|
|
|
|
T *EltPtr = &Elt;
|
|
|
|
if (I <= EltPtr && EltPtr < this->EndX)
|
|
|
|
++EltPtr;
|
|
|
|
|
|
|
|
*I = ::std::move(*EltPtr);
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
|
|
|
goto Retry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
iterator insert(iterator I, const T &Elt) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2012-02-16 04:58:48 +00:00
|
|
|
this->push_back(Elt);
|
2009-12-16 08:05:48 +00:00
|
|
|
return this->end()-1;
|
2006-08-22 06:27:16 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 06:55:45 +00:00
|
|
|
if (this->EndX < this->CapacityX) {
|
2009-12-16 08:05:48 +00:00
|
|
|
Retry:
|
2012-05-02 05:39:15 +00:00
|
|
|
::new ((void*) this->end()) T(this->back());
|
2009-12-16 08:05:48 +00:00
|
|
|
this->setEnd(this->end()+1);
|
2006-08-22 06:27:16 +00:00
|
|
|
// Push everything else over.
|
2012-05-02 05:39:15 +00:00
|
|
|
this->move_backward(I, this->end()-1, this->end());
|
2011-07-06 22:36:59 +00:00
|
|
|
|
|
|
|
// If we just moved the element we're inserting, be sure to update
|
|
|
|
// the reference.
|
|
|
|
const T *EltPtr = &Elt;
|
|
|
|
if (I <= EltPtr && EltPtr < this->EndX)
|
|
|
|
++EltPtr;
|
|
|
|
|
|
|
|
*I = *EltPtr;
|
2006-08-22 06:27:16 +00:00
|
|
|
return I;
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
2006-08-22 06:27:16 +00:00
|
|
|
goto Retry;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2008-10-16 00:15:24 +00:00
|
|
|
append(NumToInsert, Elt);
|
2009-12-16 08:05:48 +00:00
|
|
|
return this->end()-1;
|
2008-10-16 00:15:24 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t InsertElt = I - this->begin();
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Ensure there is enough space.
|
2009-12-16 08:05:48 +00:00
|
|
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 08:05:48 +00:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-03-22 19:22:53 +00:00
|
|
|
// 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
|
|
|
|
// insertion. Since we already reserved space, we know that this won't
|
|
|
|
// reallocate the vector.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
|
|
|
append(this->end()-NumToInsert, this->end());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Copy the existing elements that get replaced.
|
2012-05-02 05:39:15 +00:00
|
|
|
this->move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
std::fill_n(I, NumToInsert, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Copy over the elements that we're about to overwrite.
|
2009-12-16 08:05:48 +00:00
|
|
|
T *OldEnd = this->end();
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->end() + NumToInsert);
|
2008-10-16 00:15:24 +00:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Replace the overwritten part.
|
|
|
|
std::fill_n(I, NumOverwritten, Elt);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-10-16 00:15:24 +00:00
|
|
|
// Insert the non-overwritten middle part.
|
|
|
|
std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
template<typename ItTy>
|
|
|
|
iterator insert(iterator I, ItTy From, ItTy To) {
|
2009-12-16 08:05:48 +00:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2006-10-30 05:07:51 +00:00
|
|
|
append(From, To);
|
2009-12-16 08:05:48 +00:00
|
|
|
return this->end()-1;
|
2006-10-30 05:07:51 +00:00
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t NumToInsert = std::distance(From, To);
|
2006-10-30 05:07:51 +00:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t InsertElt = I - this->begin();
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Ensure there is enough space.
|
2009-12-16 08:05:48 +00:00
|
|
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 08:05:48 +00:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-03-22 19:22:53 +00:00
|
|
|
// 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
|
|
|
|
// insertion. Since we already reserved space, we know that this won't
|
|
|
|
// reallocate the vector.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
|
|
|
append(this->end()-NumToInsert, this->end());
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Copy the existing elements that get replaced.
|
2012-05-02 05:39:15 +00:00
|
|
|
this->move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
std::copy(From, To, I);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Copy over the elements that we're about to overwrite.
|
2009-12-16 08:05:48 +00:00
|
|
|
T *OldEnd = this->end();
|
2009-12-17 21:23:46 +00:00
|
|
|
this->setEnd(this->end() + NumToInsert);
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2009-12-17 21:23:46 +00:00
|
|
|
this->uninitialized_copy(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Replace the overwritten part.
|
2010-03-26 18:53:37 +00:00
|
|
|
for (; NumOverwritten > 0; --NumOverwritten) {
|
|
|
|
*I = *From;
|
|
|
|
++I; ++From;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-10-30 05:07:51 +00:00
|
|
|
// Insert the non-overwritten middle part.
|
2010-03-26 18:53:37 +00:00
|
|
|
this->uninitialized_copy(From, To, OldEnd);
|
2006-10-30 05:07:51 +00:00
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
|
|
|
|
|
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
|
|
|
|
#endif
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator==(const SmallVectorImpl &RHS) const {
|
|
|
|
if (this->size() != RHS.size()) return false;
|
|
|
|
return std::equal(this->begin(), this->end(), RHS.begin());
|
2008-01-11 18:42:02 +00:00
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator!=(const SmallVectorImpl &RHS) const {
|
2009-12-16 06:55:45 +00:00
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-12-16 08:05:48 +00:00
|
|
|
bool operator<(const SmallVectorImpl &RHS) const {
|
|
|
|
return std::lexicographical_compare(this->begin(), this->end(),
|
2008-05-30 22:37:47 +00:00
|
|
|
RHS.begin(), RHS.end());
|
|
|
|
}
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2009-08-19 17:48:28 +00:00
|
|
|
/// set_size - Set the array size to \arg N, which the current array must have
|
|
|
|
/// enough capacity for.
|
|
|
|
///
|
|
|
|
/// This does not construct or destroy any elements in the vector.
|
|
|
|
///
|
|
|
|
/// Clients can use this in conjunction with capacity() to write past the end
|
|
|
|
/// of the buffer when they know that more elements are available, and only
|
|
|
|
/// update the size later. This avoids the cost of value initializing elements
|
|
|
|
/// which will only be overwritten.
|
|
|
|
void set_size(unsigned N) {
|
2009-12-16 08:05:48 +00:00
|
|
|
assert(N <= this->capacity());
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->begin() + N);
|
2009-08-19 17:48:28 +00:00
|
|
|
}
|
2006-08-11 23:40:23 +00:00
|
|
|
};
|
2010-06-02 17:45:54 +00:00
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
template <typename T>
|
2009-12-16 08:05:48 +00:00
|
|
|
void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
|
2006-08-22 17:28:57 +00:00
|
|
|
if (this == &RHS) return;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// We can only avoid copying elements if neither vector is small.
|
2009-12-16 06:55:45 +00:00
|
|
|
if (!this->isSmall() && !RHS.isSmall()) {
|
|
|
|
std::swap(this->BeginX, RHS.BeginX);
|
|
|
|
std::swap(this->EndX, RHS.EndX);
|
|
|
|
std::swap(this->CapacityX, RHS.CapacityX);
|
2006-08-22 17:28:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-16 08:05:48 +00:00
|
|
|
if (RHS.size() > this->capacity())
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(RHS.size());
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->size() > RHS.capacity())
|
|
|
|
RHS.grow(this->size());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// Swap the shared elements.
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t NumShared = this->size();
|
2006-08-22 17:28:57 +00:00
|
|
|
if (NumShared > RHS.size()) NumShared = RHS.size();
|
2008-05-05 18:30:58 +00:00
|
|
|
for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
|
2009-12-15 08:29:22 +00:00
|
|
|
std::swap((*this)[i], RHS[i]);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 17:28:57 +00:00
|
|
|
// Copy over the extra elts.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->size() > RHS.size()) {
|
|
|
|
size_t EltDiff = this->size() - RHS.size();
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
|
2009-12-15 08:29:22 +00:00
|
|
|
RHS.setEnd(RHS.end()+EltDiff);
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin()+NumShared, this->end());
|
|
|
|
this->setEnd(this->begin()+NumShared);
|
2009-12-16 08:05:48 +00:00
|
|
|
} else if (RHS.size() > this->size()) {
|
|
|
|
size_t EltDiff = RHS.size() - this->size();
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(RHS.begin()+NumShared, RHS.end(), this->end());
|
|
|
|
this->setEnd(this->end() + EltDiff);
|
|
|
|
this->destroy_range(RHS.begin()+NumShared, RHS.end());
|
2009-12-15 08:29:22 +00:00
|
|
|
RHS.setEnd(RHS.begin()+NumShared);
|
2006-08-22 17:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
template <typename T>
|
2012-05-02 05:39:15 +00:00
|
|
|
SmallVectorImpl<T> &SmallVectorImpl<T>::
|
2009-12-16 08:05:48 +00:00
|
|
|
operator=(const SmallVectorImpl<T> &RHS) {
|
2006-08-11 23:40:23 +00:00
|
|
|
// Avoid self-assignment.
|
|
|
|
if (this == &RHS) return *this;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// If we already have sufficient space, assign the common elements, then
|
|
|
|
// destroy any excess.
|
2009-12-15 08:29:22 +00:00
|
|
|
size_t RHSSize = RHS.size();
|
2009-12-16 08:05:48 +00:00
|
|
|
size_t CurSize = this->size();
|
2006-08-11 23:40:23 +00:00
|
|
|
if (CurSize >= RHSSize) {
|
|
|
|
// Assign common elements.
|
2007-08-10 07:02:50 +00:00
|
|
|
iterator NewEnd;
|
|
|
|
if (RHSSize)
|
2009-12-16 08:05:48 +00:00
|
|
|
NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
|
2007-08-10 07:02:50 +00:00
|
|
|
else
|
2009-12-16 08:05:48 +00:00
|
|
|
NewEnd = this->begin();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Destroy excess elements.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(NewEnd, this->end());
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Trim.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(NewEnd);
|
2006-08-11 23:40:23 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// If we have to grow to have enough elements, destroy the current elements.
|
|
|
|
// This allows us to avoid copying them during the grow.
|
2012-05-02 05:39:15 +00:00
|
|
|
// FIXME: don't do this if they're efficiently moveable.
|
2009-12-16 08:05:48 +00:00
|
|
|
if (this->capacity() < RHSSize) {
|
2006-08-11 23:40:23 +00:00
|
|
|
// Destroy current elements.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
this->setEnd(this->begin());
|
2006-08-11 23:40:23 +00:00
|
|
|
CurSize = 0;
|
2009-12-16 08:34:40 +00:00
|
|
|
this->grow(RHSSize);
|
2006-08-11 23:40:23 +00:00
|
|
|
} else if (CurSize) {
|
|
|
|
// Otherwise, use assignment for the already-constructed elements.
|
2009-12-16 08:05:48 +00:00
|
|
|
std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
|
2006-07-26 06:22:30 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Copy construct the new elements in place.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
|
|
|
|
this->begin()+CurSize);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:40:23 +00:00
|
|
|
// Set end.
|
2009-12-16 09:17:12 +00:00
|
|
|
this->setEnd(this->begin()+RHSSize);
|
2006-08-16 22:09:24 +00:00
|
|
|
return *this;
|
2006-08-11 23:40:23 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
template <typename T>
|
|
|
|
SmallVectorImpl<T> &SmallVectorImpl<T>::operator=(SmallVectorImpl<T> &&RHS) {
|
|
|
|
// Avoid self-assignment.
|
|
|
|
if (this == &RHS) return *this;
|
|
|
|
|
|
|
|
// If the RHS isn't small, clear this vector and then steal its buffer.
|
|
|
|
if (!RHS.isSmall()) {
|
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
if (!this->isSmall()) free(this->begin());
|
|
|
|
this->BeginX = RHS.BeginX;
|
|
|
|
this->EndX = RHS.EndX;
|
|
|
|
this->CapacityX = RHS.CapacityX;
|
|
|
|
RHS.resetToSmall();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have sufficient space, assign the common elements, then
|
|
|
|
// destroy any excess.
|
|
|
|
size_t RHSSize = RHS.size();
|
|
|
|
size_t CurSize = this->size();
|
|
|
|
if (CurSize >= RHSSize) {
|
|
|
|
// Assign common elements.
|
|
|
|
iterator NewEnd = this->begin();
|
|
|
|
if (RHSSize)
|
|
|
|
NewEnd = this->move(RHS.begin(), RHS.end(), NewEnd);
|
|
|
|
|
|
|
|
// Destroy excess elements and trim the bounds.
|
|
|
|
this->destroy_range(NewEnd, this->end());
|
|
|
|
this->setEnd(NewEnd);
|
|
|
|
|
|
|
|
// Clear the RHS.
|
|
|
|
RHS.clear();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have to grow to have enough elements, destroy the current elements.
|
|
|
|
// This allows us to avoid copying them during the grow.
|
|
|
|
// FIXME: this may not actually make any sense if we can efficiently move
|
|
|
|
// elements.
|
|
|
|
if (this->capacity() < RHSSize) {
|
|
|
|
// Destroy current elements.
|
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
this->setEnd(this->begin());
|
|
|
|
CurSize = 0;
|
|
|
|
this->grow(RHSSize);
|
|
|
|
} else if (CurSize) {
|
|
|
|
// Otherwise, use assignment for the already-constructed elements.
|
|
|
|
this->move(RHS.begin(), RHS.end(), this->begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move-construct the new elements in place.
|
|
|
|
this->uninitialized_move(RHS.begin()+CurSize, RHS.end(),
|
|
|
|
this->begin()+CurSize);
|
|
|
|
|
|
|
|
// Set end.
|
|
|
|
this->setEnd(this->begin()+RHSSize);
|
|
|
|
|
|
|
|
RHS.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-16 06:55:45 +00:00
|
|
|
|
2006-08-11 23:19:51 +00:00
|
|
|
/// 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
|
|
|
|
/// in-place, which allows it to avoid heap allocation when the actual number of
|
|
|
|
/// elements is below that threshold. This allows normal "small" cases to be
|
|
|
|
/// fast without losing generality for large inputs.
|
|
|
|
///
|
|
|
|
/// Note that this does not attempt to be exception safe.
|
|
|
|
///
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
class SmallVector : public SmallVectorImpl<T> {
|
|
|
|
/// InlineElts - These are 'N-1' elements that are stored inline in the body
|
|
|
|
/// of the vector. The extra '1' element is stored in SmallVectorImpl.
|
|
|
|
typedef typename SmallVectorImpl<T>::U U;
|
2006-08-16 01:23:31 +00:00
|
|
|
enum {
|
|
|
|
// MinUs - The number of U's require to cover N T's.
|
2008-05-05 18:30:58 +00:00
|
|
|
MinUs = (static_cast<unsigned int>(sizeof(T))*N +
|
2009-01-09 19:25:42 +00:00
|
|
|
static_cast<unsigned int>(sizeof(U)) - 1) /
|
2008-05-05 18:30:58 +00:00
|
|
|
static_cast<unsigned int>(sizeof(U)),
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-16 01:23:31 +00:00
|
|
|
// NumInlineEltsElts - The number of elements actually in this array. There
|
|
|
|
// is already one in the parent class, and we have to round up to avoid
|
|
|
|
// having a zero-element array.
|
2008-08-22 16:07:55 +00:00
|
|
|
NumInlineEltsElts = MinUs > 1 ? (MinUs - 1) : 1,
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-16 01:23:31 +00:00
|
|
|
// NumTsAvailable - The number of T's we actually have space for, which may
|
|
|
|
// be more than N due to rounding.
|
2008-05-05 18:30:58 +00:00
|
|
|
NumTsAvailable = (NumInlineEltsElts+1)*static_cast<unsigned int>(sizeof(U))/
|
|
|
|
static_cast<unsigned int>(sizeof(T))
|
2006-08-16 01:23:31 +00:00
|
|
|
};
|
|
|
|
U InlineElts[NumInlineEltsElts];
|
2009-01-09 19:25:42 +00:00
|
|
|
public:
|
2006-08-16 01:23:31 +00:00
|
|
|
SmallVector() : SmallVectorImpl<T>(NumTsAvailable) {
|
2006-08-11 23:19:51 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-06-29 22:16:25 +00:00
|
|
|
explicit SmallVector(unsigned Size, const T &Value = T())
|
2007-01-31 20:08:34 +00:00
|
|
|
: SmallVectorImpl<T>(NumTsAvailable) {
|
2012-02-05 22:14:48 +00:00
|
|
|
this->assign(Size, Value);
|
2007-01-31 20:08:34 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-11 23:19:51 +00:00
|
|
|
template<typename ItTy>
|
2006-08-16 01:23:31 +00:00
|
|
|
SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(NumTsAvailable) {
|
2008-12-13 10:55:13 +00:00
|
|
|
this->append(S, E);
|
2006-08-11 23:19:51 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-04-28 06:32:08 +00:00
|
|
|
SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(NumTsAvailable) {
|
|
|
|
if (!RHS.empty())
|
2008-12-13 10:55:13 +00:00
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
2008-04-28 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
const SmallVector &operator=(const SmallVector &RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-05-02 05:39:15 +00:00
|
|
|
#if LLVM_USE_RVALUE_REFERENCES
|
|
|
|
SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(NumTsAvailable) {
|
|
|
|
if (!RHS.empty())
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
}
|
|
|
|
|
|
|
|
const SmallVector &operator=(SmallVector &&RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-11 23:19:51 +00:00
|
|
|
};
|
|
|
|
|
2010-08-25 23:11:24 +00:00
|
|
|
/// Specialize SmallVector at N=0. This specialization guarantees
|
|
|
|
/// that it can be instantiated at an incomplete T if none of its
|
|
|
|
/// members are required.
|
|
|
|
template <typename T>
|
|
|
|
class SmallVector<T,0> : public SmallVectorImpl<T> {
|
|
|
|
public:
|
2010-09-02 21:55:03 +00:00
|
|
|
SmallVector() : SmallVectorImpl<T>(0) {}
|
2010-08-25 23:11:24 +00:00
|
|
|
|
|
|
|
explicit SmallVector(unsigned Size, const T &Value = T())
|
|
|
|
: SmallVectorImpl<T>(0) {
|
2012-02-05 22:14:48 +00:00
|
|
|
this->assign(Size, Value);
|
2010-08-25 23:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ItTy>
|
|
|
|
SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(0) {
|
|
|
|
this->append(S, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(0) {
|
2010-09-02 21:55:03 +00:00
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
2010-08-25 23:11:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-02 21:55:03 +00:00
|
|
|
SmallVector &operator=(const SmallVectorImpl<T> &RHS) {
|
|
|
|
return SmallVectorImpl<T>::operator=(RHS);
|
2010-08-25 23:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-07-27 18:40:45 +00:00
|
|
|
template<typename T, unsigned N>
|
|
|
|
static inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
|
|
|
|
return X.capacity_in_bytes();
|
|
|
|
}
|
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
namespace std {
|
|
|
|
/// Implement std::swap in terms of SmallVector swap.
|
|
|
|
template<typename T>
|
|
|
|
inline void
|
|
|
|
swap(llvm::SmallVectorImpl<T> &LHS, llvm::SmallVectorImpl<T> &RHS) {
|
|
|
|
LHS.swap(RHS);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-08-22 06:27:16 +00:00
|
|
|
/// Implement std::swap in terms of SmallVector swap.
|
|
|
|
template<typename T, unsigned N>
|
|
|
|
inline void
|
|
|
|
swap(llvm::SmallVector<T, N> &LHS, llvm::SmallVector<T, N> &RHS) {
|
|
|
|
LHS.swap(RHS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-26 06:22:30 +00:00
|
|
|
#endif
|