44 template <
bool needsDestruction,
typename T>
58 for (T* cur = begin; cur != end; ++cur)
63 template <
bool needsInitialization,
bool canInitializeWithMemset,
typename T>
66 template<
bool ignore,
typename T>
77 for (T* cur = begin; cur != end; ++cur)
87 std::memset(begin, 0, reinterpret_cast<char *>(end) - reinterpret_cast<char *>(begin));
91 template <
bool canMoveWithMemcpy,
typename T>
97 static void move(
const T* src,
const T* srcEnd, T* dst)
99 while (src != srcEnd) {
101 const_cast<T*
>(src)->~T();
109 move(src, srcEnd, dst);
111 T* dstEnd = dst + (srcEnd - src);
112 while (src != srcEnd) {
115 new (dstEnd) T(*srcEnd);
116 const_cast<T*
>(srcEnd)->~T();
125 static void move(
const T* src,
const T* srcEnd, T* dst)
127 std::memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
131 std::memmove(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
135 template <
bool canCopyWithMemcpy,
typename T>
143 while (src != srcEnd) {
156 std::memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
160 template <
bool canFillWithMemset,
typename T>
168 while (dst != dstEnd) {
180 ASSERT(
sizeof(T) ==
sizeof(
char));
181 std::memset(dst, val, dstEnd - dst);
185 template<
bool canCompareWithMemcmp,
typename T>
191 static bool compare(
const T* a,
const T* b,
size_t size)
193 for (
size_t i = 0; i < size; ++i)
203 static bool compare(
const T* a,
const T* b,
size_t size)
205 return std::memcmp(a, b,
sizeof(T) * size) == 0;
222 static void move(
const T* src,
const T* srcEnd, T* dst)
242 static bool compare(
const T* a,
const T* b,
size_t size)
254 if (newCapacity > std::numeric_limits<size_t>::max() /
sizeof(T))
300 template<
typename T,
size_t inlineCapacity>
340 template<
typename T,
size_t inlineCapacity>
341 class VectorBuffer :
private VectorBufferBase<T> {
343 typedef VectorBufferBase<T> Base;
346 :
Base(inlineBuffer(), inlineCapacity)
351 :
Base(inlineBuffer(), inlineCapacity)
363 if (newCapacity > inlineCapacity)
369 if (bufferToDeallocate == inlineBuffer())
379 if (
buffer() == inlineBuffer())
388 static const size_t m_inlineBufferSize = inlineCapacity *
sizeof(T);
389 T* inlineBuffer() {
return reinterpret_cast<T*
>(&m_inlineBuffer); }
392 char m_inlineBuffer[m_inlineBufferSize];
395 template<
typename T,
size_t inlineCapacity = 0>
398 typedef VectorBuffer<T, inlineCapacity> Buffer;
399 typedef VectorTypeOperations<T> TypeOperations;
405 typedef const T* const_iterator;
425 template<
size_t otherCapacity>
429 template<
size_t otherCapacity>
432 size_t size()
const {
return m_size; }
433 size_t capacity()
const {
return m_buffer.capacity(); }
439 return m_buffer.buffer()[i];
441 const T&
at(
size_t i)
const
444 return m_buffer.buffer()[i];
450 T*
data() {
return m_buffer.buffer(); }
451 const T*
data()
const {
return m_buffer.buffer(); }
456 const_iterator
end()
const {
return begin() + m_size; }
471 template<
typename U>
void append(
const U*,
size_t);
472 template<
typename U>
void append(
const U&);
476 template<
typename U>
void insert(
size_t position,
const U*,
size_t);
477 template<
typename U>
void insert(
size_t position,
const U&);
480 template<
typename U>
void prepend(
const U*,
size_t);
481 template<
typename U>
void prepend(
const U&);
484 void remove(
size_t position);
485 void remove(
size_t position,
size_t length);
500 void fill(
const T&,
size_t);
503 template<
typename Iterator>
void appendRange(Iterator start, Iterator
end);
510 m_buffer.swap(other.m_buffer);
514 void expandCapacity(
size_t newMinCapacity);
515 const T* expandCapacity(
size_t newMinCapacity,
const T*);
516 template<
typename U> U* expandCapacity(
size_t newMinCapacity, U*);
522 template<
typename T,
size_t inlineCapacity>
524 : m_size(other.size())
525 , m_buffer(other.capacity())
530 template<
typename T,
size_t inlineCapacity>
531 template<
size_t otherCapacity>
533 : m_size(other.size())
534 , m_buffer(other.capacity())
539 template<
typename T,
size_t inlineCapacity>
545 if (size() > other.
size())
546 shrink(other.
size());
547 else if (other.
size() > capacity()) {
549 reserveCapacity(other.
size());
552 std::copy(other.
begin(), other.
begin() + size(), begin());
553 TypeOperations::uninitializedCopy(other.
begin() + size(), other.
end(), end());
554 m_size = other.
size();
559 template<
typename T,
size_t inlineCapacity>
560 template<
size_t otherCapacity>
566 if (size() > other.
size())
567 shrink(other.
size());
568 else if (other.
size() > capacity()) {
570 reserveCapacity(other.
size());
573 std::copy(other.
begin(), other.
begin() + size(), begin());
574 TypeOperations::uninitializedCopy(other.
begin() + size(), other.
end(), end());
575 m_size = other.
size();
580 template<
typename T,
size_t inlineCapacity>
583 if (size() > newSize)
585 else if (newSize > capacity()) {
587 reserveCapacity(newSize);
590 std::fill(begin(), end(), val);
591 TypeOperations::uninitializedFill(end(), begin() + newSize, val);
595 template<
typename T,
size_t inlineCapacity>
596 template<
typename Iterator>
599 for (Iterator it = start; it != end; ++it)
603 template<
typename T,
size_t inlineCapacity>
606 reserveCapacity(max(newMinCapacity, max(static_cast<size_t>(16), capacity() + capacity() / 4 + 1)));
609 template<
typename T,
size_t inlineCapacity>
612 if (ptr < begin() || ptr >= end()) {
613 expandCapacity(newMinCapacity);
616 size_t index = ptr - begin();
617 expandCapacity(newMinCapacity);
618 return begin() + index;
621 template<
typename T,
size_t inlineCapacity>
template<
typename U>
624 expandCapacity(newMinCapacity);
628 template<
typename T,
size_t inlineCapacity>
632 TypeOperations::destruct(begin() + size, end());
634 if (size > capacity())
635 expandCapacity(size);
637 TypeOperations::initialize(end(), begin() + size);
643 template<
typename T,
size_t inlineCapacity>
647 TypeOperations::destruct(begin() + size, end());
651 template<
typename T,
size_t inlineCapacity>
655 if (size > capacity())
656 expandCapacity(size);
658 TypeOperations::initialize(end(), begin() + size);
662 template<
typename T,
size_t inlineCapacity>
665 if (newCapacity <= capacity())
667 T* oldBuffer = begin();
669 m_buffer.allocateBuffer(newCapacity);
671 TypeOperations::move(oldBuffer, oldEnd, begin());
672 m_buffer.deallocateBuffer(oldBuffer);
675 template<
typename T,
size_t inlineCapacity>
678 if (newCapacity >= capacity())
681 resize(min(m_size, newCapacity));
683 T* oldBuffer = begin();
684 if (newCapacity > 0) {
686 m_buffer.allocateBuffer(newCapacity);
687 if (begin() != oldBuffer)
688 TypeOperations::move(oldBuffer, oldEnd, begin());
691 m_buffer.deallocateBuffer(oldBuffer);
698 template<
typename T,
size_t inlineCapacity>
template<
typename U>
701 size_t newSize = m_size + dataSize;
702 if (newSize > capacity()) {
703 data = expandCapacity(newSize, data);
708 for (
size_t i = 0; i < dataSize; ++i)
709 new (&dest[i]) T(data[i]);
713 template<
typename T,
size_t inlineCapacity>
template<
typename U>
717 if (size() == capacity()) {
718 ptr = expandCapacity(size() + 1, ptr);
730 new (end()) T(static_cast<T>(*ptr));
740 template<
typename T,
size_t inlineCapacity>
template<
typename U>
743 ASSERT(size() < capacity());
749 template<
typename T,
size_t inlineCapacity>
template<
typename U,
size_t c>
755 template<
typename T,
size_t inlineCapacity>
template<
typename U>
758 ASSERT(position <= size());
759 size_t newSize = m_size + dataSize;
760 if (newSize > capacity()) {
761 data = expandCapacity(newSize, data);
765 T* spot = begin() + position;
766 TypeOperations::moveOverlapping(spot, end(), spot + dataSize);
767 for (
size_t i = 0; i < dataSize; ++i)
768 new (&spot[i]) T(data[i]);
772 template<
typename T,
size_t inlineCapacity>
template<
typename U>
775 ASSERT(position <= size());
776 const U* data = &val;
777 if (size() == capacity()) {
778 data = expandCapacity(size() + 1, data);
782 T* spot = begin() + position;
783 TypeOperations::moveOverlapping(spot, end(), spot + 1);
788 template<
typename T,
size_t inlineCapacity>
template<
typename U,
size_t c>
791 insert(position, val.
begin(), val.
size());
794 template<
typename T,
size_t inlineCapacity>
template<
typename U>
797 insert(0, data, dataSize);
800 template<
typename T,
size_t inlineCapacity>
template<
typename U>
806 template<
typename T,
size_t inlineCapacity>
template<
typename U,
size_t c>
812 template<
typename T,
size_t inlineCapacity>
815 ASSERT(position < size());
816 T* spot = begin() + position;
818 TypeOperations::moveOverlapping(spot + 1, end(), spot);
822 template<
typename T,
size_t inlineCapacity>
825 ASSERT(position < size());
826 ASSERT(position + length < size());
827 T* beginSpot = begin() + position;
828 T* endSpot = beginSpot + length;
829 TypeOperations::destruct(beginSpot, endSpot);
830 TypeOperations::moveOverlapping(endSpot, end(), beginSpot);
834 template<
typename T,
size_t inlineCapacity>
837 T* buffer = m_buffer.releaseBuffer();
838 if (inlineCapacity && !buffer && m_size) {
842 size_t bytes = m_size *
sizeof(T);
844 memcpy(buffer, data(), bytes);
851 template<
typename T,
size_t inlineCapacity>
854 typedef typename Vector<T, inlineCapacity>::const_iterator iterator;
855 iterator end = collection.
end();
856 for (iterator it = collection.
begin(); it != end; ++it)
860 template<
typename T,
size_t inlineCapacity>
866 template<
typename T,
size_t inlineCapacity>
875 template<
typename T,
size_t inlineCapacity>
886 #endif // WTF_Vector_h
static bool compare(const T *a, const T *b, size_t size)
VectorBuffer(size_t capacity)
const_iterator begin() const
static void initialize(T *, T *)
static void destruct(T *, T *)
void append(const U *, size_t)
Vector(size_t size, const T &val)
static void uninitializedFill(T *dst, T *dstEnd, const T &val)
Vector & operator=(const Vector &)
const_iterator end() const
void remove(size_t position)
void allocateBuffer(size_t newCapacity)
void shrinkCapacity(size_t newCapacity)
static void initialize(T *begin, T *end)
void swap(OwnArrayPtr< T > &a, OwnArrayPtr< T > &b)
static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
void insert(size_t position, const U *, size_t)
void deleteAllValues(const HashMap< T, U, V, W, X > &collection)
void appendRange(Iterator start, Iterator end)
const T & at(size_t i) const
void prepend(const U *, size_t)
static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
void uncheckedAppend(const U &val)
VectorBuffer(size_t capacity)
static void destruct(T *begin, T *end)
static void initialize(T *begin, T *end)
void allocateBuffer(size_t newCapacity)
const T & operator[](size_t i) const
bool operator==(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
void swap(Vector< T, inlineCapacity > &a, Vector< T, inlineCapacity > &b)
static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
static void uninitializedFill(T *dst, T *dstEnd, const T &val)
static bool compare(const T *a, const T *b, size_t size)
void deallocateBuffer(T *bufferToDeallocate)
static bool compare(const T *a, const T *b, size_t size)
static void move(const T *src, const T *srcEnd, T *dst)
static void move(const T *src, const T *srcEnd, T *dst)
static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
VectorBufferBase(T *buffer, size_t capacity)
void * fastMalloc(size_t n)
static void initialize(T *begin, T *end)
static void move(const T *src, const T *srcEnd, T *dst)
bool operator!=(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
void swap(VectorBuffer< T, 0 > &other)
void fill(const T &, size_t)
static void destruct(T *begin, T *end)
void deallocateBuffer(T *bufferToDeallocate)
void swap(Vector< T, inlineCapacity > &other)
static void uninitializedFill(T *dst, T *dstEnd, const T &val)
void reserveCapacity(size_t newCapacity)