2006-07-26 08:22:30 +02:00
|
|
|
//===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-07-26 08:22:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SmallVector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SMALLVECTOR_H
|
|
|
|
#define LLVM_ADT_SMALLVECTOR_H
|
|
|
|
|
2014-03-10 14:43:46 +01:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2012-08-22 02:11:07 +02:00
|
|
|
#include "llvm/Support/AlignOf.h"
|
2012-05-02 07:39:15 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2013-03-29 23:07:12 +01:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2008-06-30 23:45:13 +02:00
|
|
|
#include "llvm/Support/type_traits.h"
|
2006-07-27 05:38:08 +02:00
|
|
|
#include <algorithm>
|
2009-02-20 23:20:18 +01:00
|
|
|
#include <cassert>
|
2010-06-10 12:13:58 +02:00
|
|
|
#include <cstddef>
|
2010-06-08 13:44:30 +02:00
|
|
|
#include <cstdlib>
|
2008-07-01 07:27:28 +02:00
|
|
|
#include <cstring>
|
2010-12-19 21:43:38 +01:00
|
|
|
#include <iterator>
|
2006-07-26 08:22:30 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This is all the non-templated stuff common to all SmallVectors.
|
2009-12-15 08:40:44 +01:00
|
|
|
class SmallVectorBase {
|
2006-10-30 04:39:20 +01:00
|
|
|
protected:
|
2009-12-15 09:29:22 +01:00
|
|
|
void *BeginX, *EndX, *CapacityX;
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2009-12-15 09:29:22 +01:00
|
|
|
protected:
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVectorBase(void *FirstEl, size_t Size)
|
|
|
|
: BeginX(FirstEl), EndX(FirstEl), CapacityX((char*)FirstEl+Size) {}
|
2012-05-02 07:39:15 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This is an implementation of the grow() method which only works
|
2011-07-27 20:40:45 +02:00
|
|
|
/// on POD-like data types and is out of line to reduce code duplication.
|
2012-08-22 02:11:07 +02:00
|
|
|
void grow_pod(void *FirstEl, size_t MinSizeInBytes, size_t TSize);
|
2011-07-27 20:40:45 +02:00
|
|
|
|
|
|
|
public:
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This returns size()*sizeof(T).
|
2009-12-16 09:34:40 +01:00
|
|
|
size_t size_in_bytes() const {
|
|
|
|
return size_t((char*)EndX - (char*)BeginX);
|
|
|
|
}
|
2012-08-22 02:11:07 +02:00
|
|
|
|
2009-12-16 09:34:40 +01: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 19:45:54 +02:00
|
|
|
|
2013-09-13 19:33:24 +02:00
|
|
|
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const { return BeginX == EndX; }
|
2009-12-15 09:29:22 +01:00
|
|
|
};
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-08-22 02:11:07 +02:00
|
|
|
template <typename T, unsigned N> struct SmallVectorStorage;
|
2009-12-16 07:55:45 +01:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This is the part of SmallVectorTemplateBase which does not depend on whether
|
|
|
|
/// the type T is a POD. The extra dummy template argument is used by ArrayRef
|
|
|
|
/// to avoid unnecessarily requiring T to be complete.
|
2012-08-22 02:11:07 +02:00
|
|
|
template <typename T, typename = void>
|
2009-12-16 07:55:45 +01:00
|
|
|
class SmallVectorTemplateCommon : public SmallVectorBase {
|
2012-08-22 02:11:07 +02:00
|
|
|
private:
|
|
|
|
template <typename, unsigned> friend struct SmallVectorStorage;
|
|
|
|
|
|
|
|
// 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. Use an array of char of sufficient alignment.
|
|
|
|
typedef llvm::AlignedCharArrayUnion<T> U;
|
|
|
|
U FirstEl;
|
|
|
|
// Space after 'FirstEl' is clobbered, do not add any instance vars after it.
|
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
protected:
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVectorTemplateCommon(size_t Size) : SmallVectorBase(&FirstEl, Size) {}
|
|
|
|
|
|
|
|
void grow_pod(size_t MinSizeInBytes, size_t TSize) {
|
|
|
|
SmallVectorBase::grow_pod(&FirstEl, MinSizeInBytes, TSize);
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Return true if this is a smallvector which has not had dynamic
|
2012-08-22 02:11:07 +02:00
|
|
|
/// memory allocated for it.
|
|
|
|
bool isSmall() const {
|
|
|
|
return BeginX == static_cast<const void*>(&FirstEl);
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Put this vector in a state of being small.
|
2012-08-22 02:11:07 +02:00
|
|
|
void resetToSmall() {
|
|
|
|
BeginX = EndX = CapacityX = &FirstEl;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-01-20 17:39:46 +01:00
|
|
|
void setEnd(T *P) { this->EndX = P; }
|
|
|
|
public:
|
2006-07-26 08:22:30 +02:00
|
|
|
typedef size_t size_type;
|
2008-10-16 02:15:24 +02:00
|
|
|
typedef ptrdiff_t difference_type;
|
2008-08-15 20:45:51 +02:00
|
|
|
typedef T value_type;
|
2009-12-15 09:29:22 +01:00
|
|
|
typedef T *iterator;
|
|
|
|
typedef const T *const_iterator;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-15 09:29:22 +01:00
|
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-15 09:29:22 +01:00
|
|
|
typedef T &reference;
|
|
|
|
typedef const T &const_reference;
|
|
|
|
typedef T *pointer;
|
|
|
|
typedef const T *const_pointer;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2007-08-20 23:22:48 +02:00
|
|
|
// forward iterator creation methods.
|
2009-12-16 07:55:45 +01: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 09:05:48 +01:00
|
|
|
protected:
|
2009-12-16 07:55:45 +01:00
|
|
|
iterator capacity_ptr() { return (iterator)this->CapacityX; }
|
|
|
|
const_iterator capacity_ptr() const { return (const_iterator)this->CapacityX;}
|
2009-12-15 09:29:22 +01:00
|
|
|
public:
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2007-08-20 23:22:48 +02: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 20:25:42 +01:00
|
|
|
|
2009-12-15 09:29:22 +01:00
|
|
|
size_type size() const { return end()-begin(); }
|
|
|
|
size_type max_size() const { return size_type(-1) / sizeof(T); }
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Return the total number of elements in the currently allocated buffer.
|
2009-12-15 09:29:22 +01:00
|
|
|
size_t capacity() const { return capacity_ptr() - begin(); }
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Return a pointer to the vector's buffer, even if empty().
|
2009-12-15 09:29:22 +01:00
|
|
|
pointer data() { return pointer(begin()); }
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Return a pointer to the vector's buffer, even if empty().
|
2009-12-15 09:29:22 +01:00
|
|
|
const_pointer data() const { return const_pointer(begin()); }
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-11-14 08:02:38 +01:00
|
|
|
reference operator[](size_type idx) {
|
2009-12-15 09:29:22 +01:00
|
|
|
assert(begin() + idx < end());
|
|
|
|
return begin()[idx];
|
2006-07-26 08:22:30 +02:00
|
|
|
}
|
2014-11-14 08:02:38 +01:00
|
|
|
const_reference operator[](size_type idx) const {
|
2009-12-15 09:29:22 +01:00
|
|
|
assert(begin() + idx < end());
|
|
|
|
return begin()[idx];
|
2006-07-26 08:22:30 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-22 08:27:16 +02:00
|
|
|
reference front() {
|
2013-01-24 05:29:24 +01:00
|
|
|
assert(!empty());
|
2006-08-22 08:27:16 +02:00
|
|
|
return begin()[0];
|
|
|
|
}
|
|
|
|
const_reference front() const {
|
2013-01-24 05:29:24 +01:00
|
|
|
assert(!empty());
|
2006-08-22 08:27:16 +02:00
|
|
|
return begin()[0];
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-07-26 08:22:30 +02:00
|
|
|
reference back() {
|
2013-01-24 05:29:24 +01:00
|
|
|
assert(!empty());
|
2006-07-26 08:22:30 +02:00
|
|
|
return end()[-1];
|
|
|
|
}
|
|
|
|
const_reference back() const {
|
2013-01-24 05:29:24 +01:00
|
|
|
assert(!empty());
|
2006-07-26 08:22:30 +02:00
|
|
|
return end()[-1];
|
|
|
|
}
|
2009-12-16 09:05:48 +01:00
|
|
|
};
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:09:23 +01: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 09:05:48 +01:00
|
|
|
template <typename T, bool isPodLike>
|
|
|
|
class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
|
2012-01-20 17:39:46 +01:00
|
|
|
protected:
|
2009-12-16 09:05:48 +01:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2009-12-16 09:09:23 +01:00
|
|
|
static void destroy_range(T *S, T *E) {
|
|
|
|
while (S != E) {
|
|
|
|
--E;
|
|
|
|
E->~T();
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Use move-assignment to move the range [I, E) onto the
|
2012-05-02 07:39:15 +02:00
|
|
|
/// 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) {
|
|
|
|
for (; I != E; ++I, ++Dest)
|
|
|
|
*Dest = ::std::move(*I);
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Use move-assignment to move the range
|
2012-05-02 07:39:15 +02:00
|
|
|
/// [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) {
|
|
|
|
while (I != E)
|
|
|
|
*--Dest = ::std::move(*--E);
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Move the range [I, E) into the uninitialized memory starting with "Dest",
|
|
|
|
/// constructing elements as needed.
|
2012-05-02 07:39:15 +02:00
|
|
|
template<typename It1, typename It2>
|
|
|
|
static void uninitialized_move(It1 I, It1 E, It2 Dest) {
|
|
|
|
for (; I != E; ++I, ++Dest)
|
|
|
|
::new ((void*) &*Dest) T(::std::move(*I));
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
|
|
|
|
/// constructing elements as needed.
|
2009-12-16 09:09:23 +01: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 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// 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 09:34:40 +01:00
|
|
|
void grow(size_t MinSize = 0);
|
2014-04-22 21:52:05 +02:00
|
|
|
|
2012-02-16 05:58:48 +01:00
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
2014-04-26 22:10:49 +02:00
|
|
|
if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
|
2014-04-22 21:07:14 +02:00
|
|
|
this->grow();
|
|
|
|
::new ((void*) this->end()) T(Elt);
|
|
|
|
this->setEnd(this->end()+1);
|
2012-05-02 07:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(T &&Elt) {
|
2014-04-26 22:10:49 +02:00
|
|
|
if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
|
2014-04-22 21:07:14 +02:00
|
|
|
this->grow();
|
|
|
|
::new ((void*) this->end()) T(::std::move(Elt));
|
|
|
|
this->setEnd(this->end()+1);
|
2012-02-16 05:58:48 +01:00
|
|
|
}
|
2014-03-01 10:27:28 +01:00
|
|
|
|
2012-02-16 05:58:48 +01:00
|
|
|
void pop_back() {
|
|
|
|
this->setEnd(this->end()-1);
|
|
|
|
this->end()->~T();
|
|
|
|
}
|
2009-12-16 09:05:48 +01:00
|
|
|
};
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2009-12-16 09:34:40 +01: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();
|
2014-04-22 21:52:05 +02:00
|
|
|
// Always grow, even from zero.
|
2013-03-29 23:07:12 +01:00
|
|
|
size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
|
2009-12-16 09:34:40 +01:00
|
|
|
if (NewCapacity < MinSize)
|
|
|
|
NewCapacity = MinSize;
|
2010-06-08 13:44:30 +02:00
|
|
|
T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-05-02 07:39:15 +02:00
|
|
|
// Move the elements over.
|
|
|
|
this->uninitialized_move(this->begin(), this->end(), NewElts);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:34:40 +01:00
|
|
|
// Destroy the original elements.
|
|
|
|
destroy_range(this->begin(), this->end());
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:34:40 +01:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 13:44:30 +02:00
|
|
|
free(this->begin());
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 12:38:03 +01:00
|
|
|
this->setEnd(NewElts+CurSize);
|
2009-12-16 09:34:40 +01:00
|
|
|
this->BeginX = NewElts;
|
|
|
|
this->CapacityX = this->begin()+NewCapacity;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
|
|
|
|
2009-12-16 09:09:23 +01:00
|
|
|
/// SmallVectorTemplateBase<isPodLike = true> - This is where we put method
|
|
|
|
/// implementations that are designed to work with POD-like T's.
|
2009-12-16 09:05:48 +01:00
|
|
|
template <typename T>
|
|
|
|
class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
|
2012-01-20 17:39:46 +01:00
|
|
|
protected:
|
2009-12-16 09:05:48 +01:00
|
|
|
SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:09:23 +01:00
|
|
|
// No need to do a destroy loop for POD's.
|
2009-12-17 20:07:19 +01:00
|
|
|
static void destroy_range(T *, T *) {}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Use move-assignment to move the range [I, E) onto the
|
2012-05-02 07:39:15 +02:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Use move-assignment to move the range [I, E) onto the objects ending at
|
|
|
|
/// "Dest", moving objects in reverse order.
|
2012-05-02 07:39:15 +02:00
|
|
|
template<typename It1, typename It2>
|
|
|
|
static It2 move_backward(It1 I, It1 E, It2 Dest) {
|
|
|
|
return ::std::copy_backward(I, E, Dest);
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Move the range [I, E) onto the uninitialized memory
|
2012-05-02 07:39:15 +02:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory
|
2009-12-16 09:09:23 +01:00
|
|
|
/// 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 19:53:37 +01:00
|
|
|
// Arbitrary iterator types; just use the basic implementation.
|
|
|
|
std::uninitialized_copy(I, E, Dest);
|
2009-12-16 09:09:23 +01:00
|
|
|
}
|
2010-03-26 19:53:37 +01:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Copy the range [I, E) onto the uninitialized memory
|
2010-03-26 19:53:37 +01:00
|
|
|
/// 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));
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Double the size of the allocated memory, guaranteeing space for at
|
2009-12-16 09:34:40 +01:00
|
|
|
/// 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 05:58:48 +01:00
|
|
|
public:
|
|
|
|
void push_back(const T &Elt) {
|
2014-04-26 22:10:49 +02:00
|
|
|
if (LLVM_UNLIKELY(this->EndX >= this->CapacityX))
|
2014-04-22 21:07:14 +02:00
|
|
|
this->grow();
|
|
|
|
memcpy(this->end(), &Elt, sizeof(T));
|
|
|
|
this->setEnd(this->end()+1);
|
2012-02-16 05:58:48 +01:00
|
|
|
}
|
2014-04-22 21:52:05 +02:00
|
|
|
|
2012-02-16 05:58:48 +01:00
|
|
|
void pop_back() {
|
|
|
|
this->setEnd(this->end()-1);
|
|
|
|
}
|
2009-12-16 09:05:48 +01:00
|
|
|
};
|
2010-06-02 19:45:54 +02:00
|
|
|
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This class consists of common code factored out of the SmallVector class to
|
|
|
|
/// reduce code duplication based on the SmallVector 'N' template parameter.
|
2009-12-16 09:05:48 +01:00
|
|
|
template <typename T>
|
|
|
|
class SmallVectorImpl : public SmallVectorTemplateBase<T, isPodLike<T>::value> {
|
2009-12-16 09:09:23 +01:00
|
|
|
typedef SmallVectorTemplateBase<T, isPodLike<T>::value > SuperClass;
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2012-11-15 01:25:36 +01:00
|
|
|
SmallVectorImpl(const SmallVectorImpl&) LLVM_DELETED_FUNCTION;
|
2009-12-16 09:05:48 +01:00
|
|
|
public:
|
2009-12-16 09:09:23 +01:00
|
|
|
typedef typename SuperClass::iterator iterator;
|
|
|
|
typedef typename SuperClass::size_type size_type;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-01-20 17:39:46 +01:00
|
|
|
protected:
|
2009-12-16 09:05:48 +01:00
|
|
|
// Default ctor - Initialize to empty.
|
|
|
|
explicit SmallVectorImpl(unsigned N)
|
|
|
|
: SmallVectorTemplateBase<T, isPodLike<T>::value>(N*sizeof(T)) {
|
2006-07-28 07:03:42 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-01-20 17:39:46 +01:00
|
|
|
public:
|
2009-12-16 09:05:48 +01:00
|
|
|
~SmallVectorImpl() {
|
|
|
|
// Destroy the constructed elements in the vector.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
// If this wasn't grown from the inline copy, deallocate the old space.
|
|
|
|
if (!this->isSmall())
|
2010-06-08 13:44:30 +02:00
|
|
|
free(this->begin());
|
2008-12-05 08:11:05 +01:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
|
|
|
|
2006-08-07 07:45:34 +02:00
|
|
|
void clear() {
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
2009-12-16 07:55:45 +01:00
|
|
|
this->EndX = this->BeginX;
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-22 19:28:57 +02:00
|
|
|
void resize(unsigned N) {
|
2009-12-16 09:05:48 +01: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 09:34:40 +01:00
|
|
|
this->grow(N);
|
2014-06-10 00:26:20 +02:00
|
|
|
for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
|
|
|
|
new (&*I) T();
|
2009-12-16 09:05:48 +01:00
|
|
|
this->setEnd(this->begin()+N);
|
2006-08-07 07:45:34 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-28 23:52:08 +02:00
|
|
|
void resize(unsigned N, const T &NV) {
|
2009-12-16 09:05:48 +01:00
|
|
|
if (N < this->size()) {
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(this->begin()+N, this->end());
|
|
|
|
this->setEnd(this->begin()+N);
|
2009-12-16 09:05:48 +01:00
|
|
|
} else if (N > this->size()) {
|
|
|
|
if (this->capacity() < N)
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(N);
|
2012-02-05 23:48:31 +01:00
|
|
|
std::uninitialized_fill(this->end(), this->begin()+N, NV);
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->begin()+N);
|
2006-08-28 23:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-09-01 08:08:16 +02:00
|
|
|
void reserve(unsigned N) {
|
2009-12-16 09:05:48 +01:00
|
|
|
if (this->capacity() < N)
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(N);
|
2006-09-01 08:08:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2013-08-20 00:12:00 +02:00
|
|
|
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
|
2012-05-02 07:39:15 +02:00
|
|
|
T Result = ::std::move(this->back());
|
2012-02-16 05:58:48 +01:00
|
|
|
this->pop_back();
|
2009-12-16 09:05:48 +01:00
|
|
|
return Result;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
void swap(SmallVectorImpl &RHS);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Add the specified range to the end of the SmallVector.
|
2006-07-27 05:38:08 +02:00
|
|
|
template<typename in_iter>
|
|
|
|
void append(in_iter in_start, in_iter in_end) {
|
2008-05-05 20:30:58 +02:00
|
|
|
size_type NumInputs = std::distance(in_start, in_end);
|
2006-07-27 05:38:08 +02:00
|
|
|
// Grow allocated space if needed.
|
2009-12-16 09:05:48 +01:00
|
|
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-07-27 05:38:08 +02:00
|
|
|
// Copy the new elements over.
|
2009-12-16 07:55:45 +01: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 09:05:48 +01:00
|
|
|
std::uninitialized_copy(in_start, in_end, this->end());
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->end() + NumInputs);
|
2006-07-27 05:38:08 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Add the specified range to the end of the SmallVector.
|
2008-10-16 02:15:24 +02:00
|
|
|
void append(size_type NumInputs, const T &Elt) {
|
|
|
|
// Grow allocated space if needed.
|
2009-12-16 09:05:48 +01:00
|
|
|
if (NumInputs > size_type(this->capacity_ptr()-this->end()))
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(this->size()+NumInputs);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Copy the new elements over.
|
2009-12-16 09:05:48 +01:00
|
|
|
std::uninitialized_fill_n(this->end(), NumInputs, Elt);
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->end() + NumInputs);
|
2008-10-16 02:15:24 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-08-08 03:44:16 +02:00
|
|
|
void assign(unsigned NumElts, const T &Elt) {
|
|
|
|
clear();
|
2014-03-09 07:22:58 +01:00
|
|
|
if (this->capacity() < NumElts)
|
|
|
|
this->grow(NumElts);
|
|
|
|
this->setEnd(this->begin()+NumElts);
|
|
|
|
std::uninitialized_fill(this->begin(), this->end(), Elt);
|
2006-08-08 03:44:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-04-02 20:24:46 +02:00
|
|
|
iterator erase(iterator I) {
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(I >= this->begin() && "Iterator to erase is out of bounds.");
|
|
|
|
assert(I < this->end() && "Erasing at past-the-end iterator.");
|
|
|
|
|
2008-04-02 20:24:46 +02:00
|
|
|
iterator N = I;
|
2006-08-22 08:27:16 +02:00
|
|
|
// Shift all elts down one.
|
2012-07-08 14:06:35 +02:00
|
|
|
this->move(I+1, this->end(), I);
|
2006-08-22 08:27:16 +02:00
|
|
|
// Drop the last elt.
|
2012-02-16 05:58:48 +01:00
|
|
|
this->pop_back();
|
2008-04-02 20:24:46 +02:00
|
|
|
return(N);
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-04-02 20:24:46 +02:00
|
|
|
iterator erase(iterator S, iterator E) {
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(S >= this->begin() && "Range to erase is out of bounds.");
|
|
|
|
assert(S <= E && "Trying to erase invalid range.");
|
|
|
|
assert(E <= this->end() && "Trying to erase past the end.");
|
|
|
|
|
2008-04-02 20:24:46 +02:00
|
|
|
iterator N = S;
|
2006-08-22 08:27:16 +02:00
|
|
|
// Shift all elts down.
|
2012-07-08 14:06:35 +02:00
|
|
|
iterator I = this->move(E, this->end(), S);
|
2006-08-22 08:27:16 +02:00
|
|
|
// Drop the last elts.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(I, this->end());
|
|
|
|
this->setEnd(I);
|
2008-04-02 20:24:46 +02:00
|
|
|
return(N);
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-05-02 07:39:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2014-04-22 21:47:26 +02:00
|
|
|
if (this->EndX >= this->CapacityX) {
|
2014-04-22 21:07:14 +02:00
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
2012-05-02 07:39:15 +02:00
|
|
|
}
|
2014-04-22 21:07:14 +02:00
|
|
|
|
|
|
|
::new ((void*) this->end()) T(::std::move(this->back()));
|
|
|
|
// Push everything else over.
|
|
|
|
this->move_backward(I, this->end()-1, this->end());
|
2014-06-08 18:00:02 +02:00
|
|
|
this->setEnd(this->end()+1);
|
2014-04-22 21:07:14 +02:00
|
|
|
|
|
|
|
// 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;
|
2012-05-02 07:39:15 +02:00
|
|
|
}
|
|
|
|
|
2006-08-22 08:27:16 +02:00
|
|
|
iterator insert(iterator I, const T &Elt) {
|
2009-12-16 09:05:48 +01:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2012-02-16 05:58:48 +01:00
|
|
|
this->push_back(Elt);
|
2009-12-16 09:05:48 +01:00
|
|
|
return this->end()-1;
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2014-04-22 21:47:26 +02:00
|
|
|
if (this->EndX >= this->CapacityX) {
|
2014-04-22 21:07:14 +02:00
|
|
|
size_t EltNo = I-this->begin();
|
|
|
|
this->grow();
|
|
|
|
I = this->begin()+EltNo;
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2014-06-08 18:55:13 +02:00
|
|
|
::new ((void*) this->end()) T(std::move(this->back()));
|
2014-04-22 21:07:14 +02:00
|
|
|
// Push everything else over.
|
|
|
|
this->move_backward(I, this->end()-1, this->end());
|
2014-06-08 18:55:13 +02:00
|
|
|
this->setEnd(this->end()+1);
|
2014-04-22 21:07:14 +02: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;
|
|
|
|
return I;
|
2006-08-22 08:27:16 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
iterator insert(iterator I, size_type NumToInsert, const T &Elt) {
|
2012-06-17 14:46:13 +02:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
|
|
|
size_t InsertElt = I - this->begin();
|
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2008-10-16 02:15:24 +02:00
|
|
|
append(NumToInsert, Elt);
|
2012-06-17 14:46:13 +02:00
|
|
|
return this->begin()+InsertElt;
|
2008-10-16 02:15:24 +02:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Ensure there is enough space.
|
2009-12-16 09:05:48 +01:00
|
|
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 09:05:48 +01:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-03-22 20:22:53 +01: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 09:05:48 +01:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
2014-06-08 21:12:28 +02:00
|
|
|
append(std::move_iterator<iterator>(this->end() - NumToInsert),
|
|
|
|
std::move_iterator<iterator>(this->end()));
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Copy the existing elements that get replaced.
|
2012-05-02 07:39:15 +02:00
|
|
|
this->move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
std::fill_n(I, NumToInsert, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-07-08 14:06:35 +02:00
|
|
|
// Move over the elements that we're about to overwrite.
|
2009-12-16 09:05:48 +01:00
|
|
|
T *OldEnd = this->end();
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->end() + NumToInsert);
|
2008-10-16 02:15:24 +02:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2012-07-08 14:06:35 +02:00
|
|
|
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Replace the overwritten part.
|
|
|
|
std::fill_n(I, NumOverwritten, Elt);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2008-10-16 02:15:24 +02:00
|
|
|
// Insert the non-overwritten middle part.
|
|
|
|
std::uninitialized_fill_n(OldEnd, NumToInsert-NumOverwritten, Elt);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
template<typename ItTy>
|
|
|
|
iterator insert(iterator I, ItTy From, ItTy To) {
|
2012-06-17 14:46:13 +02:00
|
|
|
// Convert iterator to elt# to avoid invalidating iterator when we reserve()
|
|
|
|
size_t InsertElt = I - this->begin();
|
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
if (I == this->end()) { // Important special case for empty vector.
|
2006-10-30 06:07:51 +01:00
|
|
|
append(From, To);
|
2012-06-17 14:46:13 +02:00
|
|
|
return this->begin()+InsertElt;
|
2006-10-30 06:07:51 +01:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-07-27 21:05:58 +02:00
|
|
|
assert(I >= this->begin() && "Insertion iterator is out of bounds.");
|
|
|
|
assert(I <= this->end() && "Inserting past the end of the vector.");
|
|
|
|
|
2008-05-05 20:30:58 +02:00
|
|
|
size_t NumToInsert = std::distance(From, To);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Ensure there is enough space.
|
2009-12-16 09:05:48 +01:00
|
|
|
reserve(static_cast<unsigned>(this->size() + NumToInsert));
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Uninvalidate the iterator.
|
2009-12-16 09:05:48 +01:00
|
|
|
I = this->begin()+InsertElt;
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-03-22 20:22:53 +01: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 09:05:48 +01:00
|
|
|
if (size_t(this->end()-I) >= NumToInsert) {
|
|
|
|
T *OldEnd = this->end();
|
2014-06-08 21:12:31 +02:00
|
|
|
append(std::move_iterator<iterator>(this->end() - NumToInsert),
|
|
|
|
std::move_iterator<iterator>(this->end()));
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Copy the existing elements that get replaced.
|
2012-05-02 07:39:15 +02:00
|
|
|
this->move_backward(I, OldEnd-NumToInsert, OldEnd);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
std::copy(From, To, I);
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Otherwise, we're inserting more elements than exist already, and we're
|
|
|
|
// not inserting at the end.
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-07-08 14:06:35 +02:00
|
|
|
// Move over the elements that we're about to overwrite.
|
2009-12-16 09:05:48 +01:00
|
|
|
T *OldEnd = this->end();
|
2009-12-17 22:23:46 +01:00
|
|
|
this->setEnd(this->end() + NumToInsert);
|
2008-05-05 20:30:58 +02:00
|
|
|
size_t NumOverwritten = OldEnd-I;
|
2012-07-08 14:06:35 +02:00
|
|
|
this->uninitialized_move(I, OldEnd, this->end()-NumOverwritten);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Replace the overwritten part.
|
2012-06-17 14:46:13 +02:00
|
|
|
for (T *J = I; NumOverwritten > 0; --NumOverwritten) {
|
|
|
|
*J = *From;
|
|
|
|
++J; ++From;
|
2010-03-26 19:53:37 +01:00
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-10-30 06:07:51 +01:00
|
|
|
// Insert the non-overwritten middle part.
|
2010-03-26 19:53:37 +01:00
|
|
|
this->uninitialized_copy(From, To, OldEnd);
|
2006-10-30 06:07:51 +01:00
|
|
|
return I;
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-05-02 07:39:15 +02:00
|
|
|
SmallVectorImpl &operator=(const SmallVectorImpl &RHS);
|
|
|
|
|
|
|
|
SmallVectorImpl &operator=(SmallVectorImpl &&RHS);
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:05:48 +01: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 19:42:02 +01:00
|
|
|
}
|
2009-12-16 09:05:48 +01:00
|
|
|
bool operator!=(const SmallVectorImpl &RHS) const {
|
2009-12-16 07:55:45 +01:00
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2009-12-16 09:05:48 +01:00
|
|
|
bool operator<(const SmallVectorImpl &RHS) const {
|
|
|
|
return std::lexicographical_compare(this->begin(), this->end(),
|
2008-05-31 00:37:47 +02:00
|
|
|
RHS.begin(), RHS.end());
|
|
|
|
}
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2012-09-13 14:34:29 +02:00
|
|
|
/// Set the array size to \p N, which the current array must have enough
|
|
|
|
/// capacity for.
|
2009-08-19 19:48:28 +02:00
|
|
|
///
|
|
|
|
/// 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 09:05:48 +01:00
|
|
|
assert(N <= this->capacity());
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->begin() + N);
|
2009-08-19 19:48:28 +02:00
|
|
|
}
|
2006-08-12 01:40:23 +02:00
|
|
|
};
|
2010-06-02 19:45:54 +02:00
|
|
|
|
2006-07-26 08:22:30 +02:00
|
|
|
|
2006-08-22 19:28:57 +02:00
|
|
|
template <typename T>
|
2009-12-16 09:05:48 +01:00
|
|
|
void SmallVectorImpl<T>::swap(SmallVectorImpl<T> &RHS) {
|
2006-08-22 19:28:57 +02:00
|
|
|
if (this == &RHS) return;
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-22 19:28:57 +02:00
|
|
|
// We can only avoid copying elements if neither vector is small.
|
2009-12-16 07:55:45 +01: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 19:28:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-12-16 09:05:48 +01:00
|
|
|
if (RHS.size() > this->capacity())
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(RHS.size());
|
2009-12-16 09:05:48 +01:00
|
|
|
if (this->size() > RHS.capacity())
|
|
|
|
RHS.grow(this->size());
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-22 19:28:57 +02:00
|
|
|
// Swap the shared elements.
|
2009-12-16 09:05:48 +01:00
|
|
|
size_t NumShared = this->size();
|
2006-08-22 19:28:57 +02:00
|
|
|
if (NumShared > RHS.size()) NumShared = RHS.size();
|
2008-05-05 20:30:58 +02:00
|
|
|
for (unsigned i = 0; i != static_cast<unsigned>(NumShared); ++i)
|
2009-12-15 09:29:22 +01:00
|
|
|
std::swap((*this)[i], RHS[i]);
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-22 19:28:57 +02:00
|
|
|
// Copy over the extra elts.
|
2009-12-16 09:05:48 +01:00
|
|
|
if (this->size() > RHS.size()) {
|
|
|
|
size_t EltDiff = this->size() - RHS.size();
|
2009-12-16 10:17:12 +01:00
|
|
|
this->uninitialized_copy(this->begin()+NumShared, this->end(), RHS.end());
|
2009-12-15 09:29:22 +01:00
|
|
|
RHS.setEnd(RHS.end()+EltDiff);
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(this->begin()+NumShared, this->end());
|
|
|
|
this->setEnd(this->begin()+NumShared);
|
2009-12-16 09:05:48 +01:00
|
|
|
} else if (RHS.size() > this->size()) {
|
|
|
|
size_t EltDiff = RHS.size() - this->size();
|
2009-12-16 10:17:12 +01: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 09:29:22 +01:00
|
|
|
RHS.setEnd(RHS.begin()+NumShared);
|
2006-08-22 19:28:57 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
template <typename T>
|
2012-05-02 07:39:15 +02:00
|
|
|
SmallVectorImpl<T> &SmallVectorImpl<T>::
|
2009-12-16 09:05:48 +01:00
|
|
|
operator=(const SmallVectorImpl<T> &RHS) {
|
2006-08-12 01:40:23 +02:00
|
|
|
// Avoid self-assignment.
|
|
|
|
if (this == &RHS) return *this;
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
// If we already have sufficient space, assign the common elements, then
|
|
|
|
// destroy any excess.
|
2009-12-15 09:29:22 +01:00
|
|
|
size_t RHSSize = RHS.size();
|
2009-12-16 09:05:48 +01:00
|
|
|
size_t CurSize = this->size();
|
2006-08-12 01:40:23 +02:00
|
|
|
if (CurSize >= RHSSize) {
|
|
|
|
// Assign common elements.
|
2007-08-10 09:02:50 +02:00
|
|
|
iterator NewEnd;
|
|
|
|
if (RHSSize)
|
2009-12-16 09:05:48 +01:00
|
|
|
NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
|
2007-08-10 09:02:50 +02:00
|
|
|
else
|
2009-12-16 09:05:48 +01:00
|
|
|
NewEnd = this->begin();
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
// Destroy excess elements.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(NewEnd, this->end());
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
// Trim.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(NewEnd);
|
2006-08-12 01:40:23 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02: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 07:39:15 +02:00
|
|
|
// FIXME: don't do this if they're efficiently moveable.
|
2009-12-16 09:05:48 +01:00
|
|
|
if (this->capacity() < RHSSize) {
|
2006-08-12 01:40:23 +02:00
|
|
|
// Destroy current elements.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->destroy_range(this->begin(), this->end());
|
|
|
|
this->setEnd(this->begin());
|
2006-08-12 01:40:23 +02:00
|
|
|
CurSize = 0;
|
2009-12-16 09:34:40 +01:00
|
|
|
this->grow(RHSSize);
|
2006-08-12 01:40:23 +02:00
|
|
|
} else if (CurSize) {
|
|
|
|
// Otherwise, use assignment for the already-constructed elements.
|
2009-12-16 09:05:48 +01:00
|
|
|
std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
|
2006-07-26 08:22:30 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
// Copy construct the new elements in place.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->uninitialized_copy(RHS.begin()+CurSize, RHS.end(),
|
|
|
|
this->begin()+CurSize);
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:40:23 +02:00
|
|
|
// Set end.
|
2009-12-16 10:17:12 +01:00
|
|
|
this->setEnd(this->begin()+RHSSize);
|
2006-08-17 00:09:24 +02:00
|
|
|
return *this;
|
2006-08-12 01:40:23 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2012-05-02 07:39:15 +02:00
|
|
|
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.
|
2014-04-30 17:49:06 +02:00
|
|
|
this->move(RHS.begin(), RHS.begin()+CurSize, this->begin());
|
2012-05-02 07:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-12-16 07:55:45 +01:00
|
|
|
|
2012-08-22 02:11:07 +02:00
|
|
|
/// Storage for the SmallVector elements which aren't contained in
|
|
|
|
/// SmallVectorTemplateCommon. There are 'N-1' elements here. The remaining '1'
|
|
|
|
/// element is in the base class. This is specialized for the N=1 and N=0 cases
|
|
|
|
/// to avoid allocating unnecessary storage.
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
struct SmallVectorStorage {
|
|
|
|
typename SmallVectorTemplateCommon<T>::U InlineElts[N - 1];
|
|
|
|
};
|
|
|
|
template <typename T> struct SmallVectorStorage<T, 1> {};
|
|
|
|
template <typename T> struct SmallVectorStorage<T, 0> {};
|
|
|
|
|
2014-09-03 22:08:51 +02:00
|
|
|
/// This is a 'vector' (really, a variable-sized array), optimized
|
2006-08-12 01:19:51 +02:00
|
|
|
/// 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> {
|
2014-09-03 22:08:51 +02:00
|
|
|
/// Inline space for elements which aren't stored in the base class.
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVectorStorage<T, N> Storage;
|
2009-01-09 20:25:42 +01:00
|
|
|
public:
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVector() : SmallVectorImpl<T>(N) {
|
2006-08-12 01:19:51 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-06-30 00:16:25 +02:00
|
|
|
explicit SmallVector(unsigned Size, const T &Value = T())
|
2012-08-22 02:11:07 +02:00
|
|
|
: SmallVectorImpl<T>(N) {
|
2012-02-05 23:14:48 +01:00
|
|
|
this->assign(Size, Value);
|
2007-01-31 21:08:34 +01:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2006-08-12 01:19:51 +02:00
|
|
|
template<typename ItTy>
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
|
2008-12-13 11:55:13 +01:00
|
|
|
this->append(S, E);
|
2006-08-12 01:19:51 +02:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2014-03-10 14:43:46 +01:00
|
|
|
template <typename RangeTy>
|
|
|
|
explicit SmallVector(const llvm::iterator_range<RangeTy> R)
|
|
|
|
: SmallVectorImpl<T>(N) {
|
|
|
|
this->append(R.begin(), R.end());
|
|
|
|
}
|
|
|
|
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVector(const SmallVector &RHS) : SmallVectorImpl<T>(N) {
|
2008-04-28 08:32:08 +02:00
|
|
|
if (!RHS.empty())
|
2008-12-13 11:55:13 +01:00
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
2008-04-28 08:32:08 +02:00
|
|
|
}
|
|
|
|
|
2006-08-22 08:27:16 +02:00
|
|
|
const SmallVector &operator=(const SmallVector &RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2012-08-22 02:11:07 +02:00
|
|
|
SmallVector(SmallVector &&RHS) : SmallVectorImpl<T>(N) {
|
2012-05-02 07:39:15 +02:00
|
|
|
if (!RHS.empty())
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
}
|
|
|
|
|
|
|
|
const SmallVector &operator=(SmallVector &&RHS) {
|
|
|
|
SmallVectorImpl<T>::operator=(::std::move(RHS));
|
|
|
|
return *this;
|
|
|
|
}
|
2006-08-12 01:19:51 +02:00
|
|
|
};
|
|
|
|
|
2011-07-27 20:40:45 +02: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 08:22:30 +02:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2006-08-22 08:27:16 +02: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 20:25:42 +01:00
|
|
|
|
2006-08-22 08:27:16 +02: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 08:22:30 +02:00
|
|
|
#endif
|