2009-06-30 17:19:42 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=99 ft=cpp:
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-06-30 17:19:42 -07:00
|
|
|
|
|
|
|
#ifndef jsvector_h_
|
|
|
|
#define jsvector_h_
|
|
|
|
|
2011-12-16 11:42:07 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2011-10-04 15:33:00 -07:00
|
|
|
#include "TemplateLib.h"
|
|
|
|
#include "Utility.h"
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-08-02 07:44:24 -07:00
|
|
|
/* Silence dire "bugs in previous versions of MSVC have been fixed" warnings */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4345)
|
|
|
|
#endif
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
namespace js {
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2012-01-02 14:34:25 -08:00
|
|
|
class TempAllocPolicy;
|
|
|
|
|
|
|
|
template <class T,
|
|
|
|
size_t MinInlineCapacity = 0,
|
|
|
|
class AllocPolicy = TempAllocPolicy>
|
2011-10-04 15:33:00 -07:00
|
|
|
class Vector;
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
/*
|
|
|
|
* This template class provides a default implementation for vector operations
|
|
|
|
* when the element type is not known to be a POD, as judged by IsPodType.
|
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP, bool IsPod>
|
|
|
|
struct VectorImpl
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
|
|
|
/* Destroys constructed objects in the range [begin, end). */
|
|
|
|
static inline void destroy(T *begin, T *end) {
|
|
|
|
for (T *p = begin; p != end; ++p)
|
|
|
|
p->~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Constructs objects in the uninitialized range [begin, end). */
|
|
|
|
static inline void initialize(T *begin, T *end) {
|
|
|
|
for (T *p = begin; p != end; ++p)
|
|
|
|
new(p) T();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy-constructs objects in the uninitialized range
|
|
|
|
* [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
|
|
|
|
*/
|
|
|
|
template <class U>
|
2009-08-07 20:09:11 -07:00
|
|
|
static inline void copyConstruct(T *dst, const U *srcbeg, const U *srcend) {
|
2009-06-30 17:19:42 -07:00
|
|
|
for (const U *p = srcbeg; p != srcend; ++p, ++dst)
|
|
|
|
new(dst) T(*p);
|
|
|
|
}
|
|
|
|
|
2011-08-01 17:52:53 -07:00
|
|
|
/*
|
|
|
|
* Move-constructs objects in the uninitialized range
|
|
|
|
* [dst, dst+(srcend-srcbeg)) from the range [srcbeg, srcend).
|
|
|
|
*/
|
|
|
|
template <class U>
|
|
|
|
static inline void moveConstruct(T *dst, const U *srcbeg, const U *srcend) {
|
|
|
|
for (const U *p = srcbeg; p != srcend; ++p, ++dst)
|
|
|
|
new(dst) T(Move(*p));
|
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/*
|
|
|
|
* Copy-constructs objects in the uninitialized range [dst, dst+n) from the
|
|
|
|
* same object u.
|
|
|
|
*/
|
|
|
|
template <class U>
|
|
|
|
static inline void copyConstructN(T *dst, size_t n, const U &u) {
|
|
|
|
for (T *end = dst + n; dst != end; ++dst)
|
|
|
|
new(dst) T(u);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
/*
|
|
|
|
* Grows the given buffer to have capacity newcap, preserving the objects
|
2009-08-07 20:09:11 -07:00
|
|
|
* constructed in the range [begin, end) and updating v. Assumes that (1)
|
|
|
|
* newcap has not overflowed, and (2) multiplying newcap by sizeof(T) will
|
|
|
|
* not overflow.
|
2009-06-30 17:19:42 -07:00
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
static inline bool growTo(Vector<T,N,AP> &v, size_t newcap) {
|
2009-08-18 08:41:30 -07:00
|
|
|
JS_ASSERT(!v.usingInlineStorage());
|
2011-03-31 01:14:12 -07:00
|
|
|
T *newbuf = reinterpret_cast<T *>(v.malloc_(newcap * sizeof(T)));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!newbuf)
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
2010-11-15 17:15:01 -08:00
|
|
|
for (T *dst = newbuf, *src = v.beginNoCheck(); src != v.endNoCheck(); ++dst, ++src)
|
2011-08-01 17:52:53 -07:00
|
|
|
new(dst) T(Move(*src));
|
2010-11-15 17:15:01 -08:00
|
|
|
VectorImpl::destroy(v.beginNoCheck(), v.endNoCheck());
|
2011-03-31 01:14:12 -07:00
|
|
|
v.free_(v.mBegin);
|
2010-11-15 17:15:01 -08:00
|
|
|
v.mBegin = newbuf;
|
|
|
|
/* v.mLength is unchanged. */
|
|
|
|
v.mCapacity = newcap;
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This partial template specialization provides a default implementation for
|
|
|
|
* vector operations when the element type is known to be a POD, as judged by
|
|
|
|
* IsPodType.
|
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
struct VectorImpl<T, N, AP, true>
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
|
|
|
static inline void destroy(T *, T *) {}
|
|
|
|
|
|
|
|
static inline void initialize(T *begin, T *end) {
|
2009-08-07 20:09:11 -07:00
|
|
|
/*
|
|
|
|
* You would think that memset would be a big win (or even break even)
|
|
|
|
* when we know T is a POD. But currently it's not. This is probably
|
|
|
|
* because |append| tends to be given small ranges and memset requires
|
|
|
|
* a function call that doesn't get inlined.
|
|
|
|
*
|
|
|
|
* memset(begin, 0, sizeof(T) * (end-begin));
|
|
|
|
*/
|
2009-06-30 17:19:42 -07:00
|
|
|
for (T *p = begin; p != end; ++p)
|
2009-08-07 20:09:11 -07:00
|
|
|
new(p) T();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
template <class U>
|
|
|
|
static inline void copyConstruct(T *dst, const U *srcbeg, const U *srcend) {
|
|
|
|
/*
|
|
|
|
* See above memset comment. Also, notice that copyConstruct is
|
|
|
|
* currently templated (T != U), so memcpy won't work without
|
|
|
|
* requiring T == U.
|
|
|
|
*
|
|
|
|
* memcpy(dst, srcbeg, sizeof(T) * (srcend - srcbeg));
|
|
|
|
*/
|
|
|
|
for (const U *p = srcbeg; p != srcend; ++p, ++dst)
|
2009-06-30 17:19:42 -07:00
|
|
|
*dst = *p;
|
|
|
|
}
|
|
|
|
|
2011-08-01 17:52:53 -07:00
|
|
|
template <class U>
|
|
|
|
static inline void moveConstruct(T *dst, const U *srcbeg, const U *srcend) {
|
|
|
|
copyConstruct(dst, srcbeg, srcend);
|
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
static inline void copyConstructN(T *dst, size_t n, const T &t) {
|
2009-11-19 10:34:28 -08:00
|
|
|
for (T *p = dst, *end = dst + n; p != end; ++p)
|
|
|
|
*p = t;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
static inline bool growTo(Vector<T,N,AP> &v, size_t newcap) {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(!v.usingInlineStorage());
|
2009-06-30 17:19:42 -07:00
|
|
|
size_t bytes = sizeof(T) * newcap;
|
2011-06-17 10:14:30 -07:00
|
|
|
size_t oldBytes = sizeof(T) * v.mCapacity;
|
2011-06-15 18:11:59 -07:00
|
|
|
T *newbuf = reinterpret_cast<T *>(v.realloc_(v.mBegin, oldBytes, bytes));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!newbuf)
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
2010-11-15 17:15:01 -08:00
|
|
|
v.mBegin = newbuf;
|
|
|
|
/* v.mLength is unchanged. */
|
|
|
|
v.mCapacity = newcap;
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS-friendly, STL-like container providing a short-lived, dynamic buffer.
|
2009-09-01 18:46:19 -07:00
|
|
|
* Vector calls the constructors/destructors of all elements stored in
|
2009-08-07 20:09:11 -07:00
|
|
|
* its internal buffer, so non-PODs may be safely used. Additionally,
|
2009-09-01 18:46:19 -07:00
|
|
|
* Vector will store the first N elements in-place before resorting to
|
|
|
|
* dynamic allocation.
|
2009-06-30 17:19:42 -07:00
|
|
|
*
|
|
|
|
* T requirements:
|
|
|
|
* - default and copy constructible, assignable, destructible
|
|
|
|
* - operations do not throw
|
2009-09-01 18:46:19 -07:00
|
|
|
* N requirements:
|
|
|
|
* - any value, however, N is clamped to min/max values
|
|
|
|
* AllocPolicy:
|
2011-06-17 10:11:33 -07:00
|
|
|
* - see "Allocation policies" in jsalloc.h (default js::TempAllocPolicy)
|
2009-06-30 17:19:42 -07:00
|
|
|
*
|
2009-09-01 18:46:19 -07:00
|
|
|
* N.B: Vector is not reentrant: T member functions called during Vector member
|
|
|
|
* functions must not call back into the same object.
|
2009-06-30 17:19:42 -07:00
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AllocPolicy>
|
2011-03-22 14:19:10 -07:00
|
|
|
class Vector : private AllocPolicy
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2012-05-15 17:21:23 -07:00
|
|
|
typedef typename tl::StaticAssert<tl::IsRelocatableHeapType<T>::result>::result _;
|
2012-04-11 17:14:11 -07:00
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/* utilities */
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
static const bool sElemIsPod = tl::IsPodType<T>::result;
|
|
|
|
typedef VectorImpl<T, N, AllocPolicy, sElemIsPod> Impl;
|
|
|
|
friend struct VectorImpl<T, N, AllocPolicy, sElemIsPod>;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
bool calculateNewCapacity(size_t curLength, size_t lengthInc, size_t &newCap);
|
2010-11-15 17:15:01 -08:00
|
|
|
bool growStorageBy(size_t lengthInc);
|
2009-08-14 16:10:59 -07:00
|
|
|
bool growHeapStorageBy(size_t lengthInc);
|
|
|
|
bool convertToHeapStorage(size_t lengthInc);
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
template <bool InitNewElems> inline bool growByImpl(size_t inc);
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/* magic constants */
|
|
|
|
|
|
|
|
static const int sMaxInlineBytes = 1024;
|
|
|
|
|
|
|
|
/* compute constants */
|
|
|
|
|
2011-05-12 18:39:47 -07:00
|
|
|
/*
|
|
|
|
* Consider element size to be 1 for buffer sizing if there are
|
|
|
|
* 0 inline elements. This allows us to compile when the definition
|
|
|
|
* of the element type is not visible here.
|
|
|
|
*
|
|
|
|
* Explicit specialization is only allowed at namespace scope, so
|
|
|
|
* in order to keep everything here, we use a dummy template
|
|
|
|
* parameter with partial specialization.
|
|
|
|
*/
|
|
|
|
template <int M, int Dummy>
|
|
|
|
struct ElemSize {
|
|
|
|
static const size_t result = sizeof(T);
|
|
|
|
};
|
|
|
|
template <int Dummy>
|
|
|
|
struct ElemSize<0, Dummy> {
|
|
|
|
static const size_t result = 1;
|
|
|
|
};
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
static const size_t sInlineCapacity =
|
2011-05-12 18:39:47 -07:00
|
|
|
tl::Min<N, sMaxInlineBytes / ElemSize<N, 0>::result>::result;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
/* Calculate inline buffer size; avoid 0-sized array. */
|
|
|
|
static const size_t sInlineBytes =
|
2011-05-12 18:39:47 -07:00
|
|
|
tl::Max<1, sInlineCapacity * ElemSize<N, 0>::result>::result;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
/* member data */
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
/*
|
|
|
|
* Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
|
|
|
|
* mBegin + mLength) hold valid constructed T objects. The range [mBegin +
|
2011-02-18 18:32:29 -08:00
|
|
|
* mLength, mBegin + mCapacity) holds uninitialized memory. The range
|
|
|
|
* [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
|
|
|
|
* previously allocated by a call to reserve().
|
2010-11-15 17:15:01 -08:00
|
|
|
*/
|
|
|
|
T *mBegin;
|
|
|
|
size_t mLength; /* Number of elements in the Vector. */
|
|
|
|
size_t mCapacity; /* Max number of elements storable in the Vector without resizing. */
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
size_t mReserved; /* Max elements of reserved or used space in this vector. */
|
|
|
|
#endif
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
AlignedStorage<sInlineBytes> storage;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
friend class ReentrancyGuard;
|
|
|
|
bool entered;
|
|
|
|
#endif
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-12-16 11:42:07 -08:00
|
|
|
Vector(const Vector &) MOZ_DELETE;
|
|
|
|
Vector &operator=(const Vector &) MOZ_DELETE;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
/* private accessors */
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
bool usingInlineStorage() const {
|
|
|
|
return mBegin == (T *)storage.addr();
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
T *beginNoCheck() const {
|
|
|
|
return mBegin;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
T *endNoCheck() {
|
|
|
|
return mBegin + mLength;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
const T *endNoCheck() const {
|
|
|
|
return mBegin + mLength;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
size_t reserved() const {
|
|
|
|
JS_ASSERT(mReserved <= mCapacity);
|
|
|
|
JS_ASSERT(mLength <= mReserved);
|
|
|
|
return mReserved;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-08 12:44:42 -08:00
|
|
|
/* Append operations guaranteed to succeed due to pre-reserved space. */
|
2011-08-01 17:52:53 -07:00
|
|
|
template <class U> void internalAppend(U t);
|
2011-03-08 12:44:42 -08:00
|
|
|
void internalAppendN(const T &t, size_t n);
|
|
|
|
template <class U> void internalAppend(const U *begin, size_t length);
|
|
|
|
template <class U, size_t O, class BP> void internalAppend(const Vector<U,O,BP> &other);
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
public:
|
2011-04-01 16:33:41 -07:00
|
|
|
static const size_t sMaxInlineStorage = N;
|
|
|
|
|
2011-01-11 15:19:57 -08:00
|
|
|
typedef T ElementType;
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector(AllocPolicy = AllocPolicy());
|
2011-08-01 17:52:53 -07:00
|
|
|
Vector(MoveRef<Vector>); /* Move constructor. */
|
|
|
|
Vector &operator=(MoveRef<Vector>); /* Move assignment. */
|
2009-09-01 18:46:19 -07:00
|
|
|
~Vector();
|
2009-06-30 17:19:42 -07:00
|
|
|
|
|
|
|
/* accessors */
|
|
|
|
|
2010-12-06 10:26:58 -08:00
|
|
|
const AllocPolicy &allocPolicy() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-07-13 16:05:41 -07:00
|
|
|
AllocPolicy &allocPolicy() {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-10-26 16:20:21 -07:00
|
|
|
enum { InlineLength = N };
|
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t length() const {
|
2010-11-15 17:15:01 -08:00
|
|
|
return mLength;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
bool empty() const {
|
2010-11-15 17:15:01 -08:00
|
|
|
return mLength == 0;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
size_t capacity() const {
|
2010-11-15 17:15:01 -08:00
|
|
|
return mCapacity;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2012-01-20 04:11:42 -08:00
|
|
|
T *begin() {
|
|
|
|
JS_ASSERT(!entered);
|
|
|
|
return mBegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *begin() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2010-11-15 17:15:01 -08:00
|
|
|
return mBegin;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *end() {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2010-11-15 17:15:01 -08:00
|
|
|
return mBegin + mLength;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const T *end() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2010-11-15 17:15:01 -08:00
|
|
|
return mBegin + mLength;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T &operator[](size_t i) {
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(!entered && i < mLength);
|
2009-08-07 20:09:11 -07:00
|
|
|
return begin()[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &operator[](size_t i) const {
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(!entered && i < mLength);
|
2009-08-07 20:09:11 -07:00
|
|
|
return begin()[i];
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T &back() {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered && !empty());
|
2009-08-07 20:09:11 -07:00
|
|
|
return *(end() - 1);
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const T &back() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered && !empty());
|
2009-08-07 20:09:11 -07:00
|
|
|
return *(end() - 1);
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2011-10-07 12:02:50 -07:00
|
|
|
class Range {
|
|
|
|
friend class Vector;
|
|
|
|
T *cur, *end;
|
|
|
|
Range(T *cur, T *end) : cur(cur), end(end) {}
|
|
|
|
public:
|
|
|
|
Range() {}
|
|
|
|
bool empty() const { return cur == end; }
|
|
|
|
size_t remain() const { return end - cur; }
|
|
|
|
T &front() const { return *cur; }
|
|
|
|
void popFront() { JS_ASSERT(!empty()); ++cur; }
|
|
|
|
T popCopyFront() { JS_ASSERT(!empty()); return *cur++; }
|
|
|
|
};
|
|
|
|
|
|
|
|
Range all() {
|
|
|
|
return Range(begin(), end());
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
/* mutators */
|
|
|
|
|
2010-08-05 05:16:56 -07:00
|
|
|
/* If reserve(length() + N) succeeds, the N next appends are guaranteed to succeed. */
|
2009-08-07 20:09:11 -07:00
|
|
|
bool reserve(size_t capacity);
|
2009-08-11 15:45:32 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
/*
|
|
|
|
* Destroy elements in the range [end() - incr, end()). Does not deallocate
|
|
|
|
* or unreserve storage for those elements.
|
|
|
|
*/
|
2009-08-07 20:09:11 -07:00
|
|
|
void shrinkBy(size_t incr);
|
2009-08-11 15:45:32 -07:00
|
|
|
|
2010-04-06 16:40:49 -07:00
|
|
|
/* Grow the vector by incr elements. */
|
2009-08-07 20:09:11 -07:00
|
|
|
bool growBy(size_t incr);
|
2009-08-11 15:45:32 -07:00
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
/* Call shrinkBy or growBy based on whether newSize > length(). */
|
|
|
|
bool resize(size_t newLength);
|
2009-08-11 15:45:32 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
/* Leave new elements as uninitialized memory. */
|
|
|
|
bool growByUninitialized(size_t incr);
|
2010-10-04 11:45:34 -07:00
|
|
|
bool resizeUninitialized(size_t newLength);
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
/* Shorthand for shrinkBy(length()). */
|
2009-06-30 17:19:42 -07:00
|
|
|
void clear();
|
|
|
|
|
2011-06-24 14:22:30 -07:00
|
|
|
/* Clears and releases any heap-allocated storage. */
|
|
|
|
void clearAndFree();
|
|
|
|
|
2011-08-01 17:52:53 -07:00
|
|
|
/*
|
|
|
|
* Potentially fallible append operations.
|
|
|
|
*
|
|
|
|
* The function templates that take an unspecified type U require a
|
|
|
|
* const T & or a MoveRef<T>. The MoveRef<T> variants move their
|
|
|
|
* operands into the vector, instead of copying them. If they fail, the
|
|
|
|
* operand is left unmoved.
|
|
|
|
*/
|
|
|
|
template <class U> bool append(U t);
|
2009-08-07 20:09:11 -07:00
|
|
|
bool appendN(const T &t, size_t n);
|
|
|
|
template <class U> bool append(const U *begin, const U *end);
|
|
|
|
template <class U> bool append(const U *begin, size_t length);
|
2010-08-11 13:30:07 -07:00
|
|
|
template <class U, size_t O, class BP> bool append(const Vector<U,O,BP> &other);
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
/*
|
|
|
|
* Guaranteed-infallible append operations for use upon vectors whose
|
|
|
|
* memory has been pre-reserved.
|
|
|
|
*/
|
|
|
|
void infallibleAppend(const T &t) {
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(t);
|
2011-02-18 18:32:29 -08:00
|
|
|
}
|
|
|
|
void infallibleAppendN(const T &t, size_t n) {
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppendN(t, n);
|
2011-02-18 18:32:29 -08:00
|
|
|
}
|
|
|
|
template <class U> void infallibleAppend(const U *begin, const U *end) {
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(begin, PointerRangeSize(begin, end));
|
2011-02-18 18:32:29 -08:00
|
|
|
}
|
|
|
|
template <class U> void infallibleAppend(const U *begin, size_t length) {
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(begin, length);
|
2011-02-18 18:32:29 -08:00
|
|
|
}
|
|
|
|
template <class U, size_t O, class BP> void infallibleAppend(const Vector<U,O,BP> &other) {
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(other);
|
2011-02-18 18:32:29 -08:00
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
void popBack();
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2010-11-30 18:41:32 -08:00
|
|
|
T popCopy();
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
/*
|
2009-09-01 18:46:19 -07:00
|
|
|
* Transfers ownership of the internal buffer used by Vector to the caller.
|
|
|
|
* After this call, the Vector is empty. Since the returned buffer may need
|
|
|
|
* to be allocated (if the elements are currently stored in-place), the
|
|
|
|
* call can fail, returning NULL.
|
2009-08-07 20:09:11 -07:00
|
|
|
*
|
2009-08-14 16:10:59 -07:00
|
|
|
* N.B. Although a T*, only the range [0, length()) is constructed.
|
2009-06-30 17:19:42 -07:00
|
|
|
*/
|
|
|
|
T *extractRawBuffer();
|
|
|
|
|
|
|
|
/*
|
2009-09-01 18:46:19 -07:00
|
|
|
* Transfer ownership of an array of objects into the Vector.
|
2009-06-30 17:19:42 -07:00
|
|
|
* N.B. This call assumes that there are no uninitialized elements in the
|
|
|
|
* passed array.
|
|
|
|
*/
|
2009-08-07 20:09:11 -07:00
|
|
|
void replaceRawBuffer(T *p, size_t length);
|
2010-08-11 13:30:07 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Places |val| at position |p|, shifting existing elements
|
|
|
|
* from |p| onward one position higher.
|
|
|
|
*/
|
|
|
|
bool insert(T *p, const T &val);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes the element |t|, which must fall in the bounds [begin, end),
|
|
|
|
* shifting existing elements from |t + 1| onward one position lower.
|
|
|
|
*/
|
|
|
|
void erase(T *t);
|
2012-02-26 20:18:13 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Measure the size of the Vector's heap-allocated storage.
|
|
|
|
*/
|
|
|
|
size_t sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const;
|
|
|
|
|
2012-04-11 17:14:11 -07:00
|
|
|
/*
|
2012-02-26 20:18:13 -08:00
|
|
|
* Like sizeOfExcludingThis, but also measures the size of the Vector
|
|
|
|
* object (which must be heap-allocated) itself.
|
|
|
|
*/
|
|
|
|
size_t sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const;
|
2009-08-07 20:09:11 -07:00
|
|
|
};
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
/* This does the re-entrancy check plus several other sanity checks. */
|
|
|
|
#define REENTRANCY_GUARD_ET_AL \
|
|
|
|
ReentrancyGuard g(*this); \
|
|
|
|
JS_ASSERT_IF(usingInlineStorage(), mCapacity == sInlineCapacity); \
|
2011-02-18 18:32:29 -08:00
|
|
|
JS_ASSERT(reserved() <= mCapacity); \
|
|
|
|
JS_ASSERT(mLength <= reserved()); \
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(mLength <= mCapacity)
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
/* Vector Implementation */
|
|
|
|
|
2009-11-04 01:03:00 -08:00
|
|
|
template <class T, size_t N, class AllocPolicy>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE
|
2009-11-04 01:03:00 -08:00
|
|
|
Vector<T,N,AllocPolicy>::Vector(AllocPolicy ap)
|
2010-11-15 17:15:01 -08:00
|
|
|
: AllocPolicy(ap), mBegin((T *)storage.addr()), mLength(0),
|
|
|
|
mCapacity(sInlineCapacity)
|
2009-09-01 18:46:19 -07:00
|
|
|
#ifdef DEBUG
|
2011-02-18 18:32:29 -08:00
|
|
|
, mReserved(0), entered(false)
|
2009-09-01 18:46:19 -07:00
|
|
|
#endif
|
2012-04-10 17:03:25 -07:00
|
|
|
{}
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2011-08-01 17:52:53 -07:00
|
|
|
/* Move constructor. */
|
|
|
|
template <class T, size_t N, class AllocPolicy>
|
|
|
|
JS_ALWAYS_INLINE
|
|
|
|
Vector<T, N, AllocPolicy>::Vector(MoveRef<Vector> rhs)
|
|
|
|
: AllocPolicy(rhs)
|
|
|
|
{
|
|
|
|
mLength = rhs->mLength;
|
|
|
|
mCapacity = rhs->mCapacity;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mReserved = rhs->mReserved;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rhs->usingInlineStorage()) {
|
|
|
|
/* We can't move the buffer over in this case, so copy elements. */
|
2011-09-19 10:01:06 -07:00
|
|
|
mBegin = (T *)storage.addr();
|
2011-08-01 17:52:53 -07:00
|
|
|
Impl::moveConstruct(mBegin, rhs->beginNoCheck(), rhs->endNoCheck());
|
|
|
|
/*
|
|
|
|
* Leave rhs's mLength, mBegin, mCapacity, and mReserved as they are.
|
|
|
|
* The elements in its in-line storage still need to be destroyed.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Take src's buffer, and turn src into an empty vector using
|
|
|
|
* in-line storage.
|
|
|
|
*/
|
|
|
|
mBegin = rhs->mBegin;
|
|
|
|
rhs->mBegin = (T *) rhs->storage.addr();
|
|
|
|
rhs->mCapacity = sInlineCapacity;
|
|
|
|
rhs->mLength = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
rhs->mReserved = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move assignment. */
|
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
JS_ALWAYS_INLINE
|
|
|
|
Vector<T, N, AP> &
|
|
|
|
Vector<T, N, AP>::operator=(MoveRef<Vector> rhs)
|
|
|
|
{
|
|
|
|
this->~Vector();
|
|
|
|
new(this) Vector(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::~Vector()
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
if (!usingInlineStorage())
|
2011-03-31 01:14:12 -07:00
|
|
|
this->free_(beginNoCheck());
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
/*
|
2009-08-14 16:10:59 -07:00
|
|
|
* Calculate a new capacity that is at least lengthInc greater than
|
|
|
|
* curLength and check for overflow.
|
2009-08-18 08:41:30 -07:00
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-10-01 21:00:55 -07:00
|
|
|
STATIC_POSTCONDITION(!return || newCap >= curLength + lengthInc)
|
2012-06-11 23:21:49 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
/* gcc (ARM, x86) compiler bug workaround - See bug 694694 */
|
|
|
|
JS_NEVER_INLINE bool
|
|
|
|
#else
|
2009-06-30 17:19:42 -07:00
|
|
|
inline bool
|
2012-06-11 23:21:49 -07:00
|
|
|
#endif
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::calculateNewCapacity(size_t curLength, size_t lengthInc,
|
|
|
|
size_t &newCap)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t newMinCap = curLength + lengthInc;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
/*
|
|
|
|
* Check for overflow in the above addition, below CEILING_LOG2, and later
|
|
|
|
* multiplication by sizeof(T).
|
|
|
|
*/
|
2009-08-14 16:10:59 -07:00
|
|
|
if (newMinCap < curLength ||
|
2009-09-01 18:46:19 -07:00
|
|
|
newMinCap & tl::MulOverflowMask<2 * sizeof(T)>::result) {
|
|
|
|
this->reportAllocOverflow();
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-10-20 15:47:39 -07:00
|
|
|
/* Round up to next power of 2. */
|
|
|
|
newCap = RoundUpPow2(newMinCap);
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
/*
|
|
|
|
* Do not allow a buffer large enough that the expression ((char *)end() -
|
|
|
|
* (char *)begin()) overflows ptrdiff_t. See Bug 510319.
|
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
if (newCap & tl::UnsafeRangeSizeMask<T>::result) {
|
|
|
|
this->reportAllocOverflow();
|
2009-08-18 08:41:30 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
/*
|
|
|
|
* This function will grow the current heap capacity to have capacity
|
2010-11-15 17:15:01 -08:00
|
|
|
* (mLength + lengthInc) and fail on OOM or integer overflow.
|
2009-08-18 08:41:30 -07:00
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::growHeapStorageBy(size_t lengthInc)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-08-18 08:41:30 -07:00
|
|
|
size_t newCap;
|
2010-11-15 17:15:01 -08:00
|
|
|
return calculateNewCapacity(mLength, lengthInc, newCap) &&
|
2009-08-18 08:41:30 -07:00
|
|
|
Impl::growTo(*this, newCap);
|
|
|
|
}
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
/*
|
2010-11-15 17:15:01 -08:00
|
|
|
* This function will create a new heap buffer with capacity (mLength +
|
2009-08-14 16:10:59 -07:00
|
|
|
* lengthInc()), move all elements in the inline buffer to this new buffer,
|
2009-08-18 08:41:30 -07:00
|
|
|
* and fail on OOM or integer overflow.
|
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-08-18 08:41:30 -07:00
|
|
|
inline bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::convertToHeapStorage(size_t lengthInc)
|
2009-08-18 08:41:30 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
2009-08-18 08:41:30 -07:00
|
|
|
size_t newCap;
|
2010-11-15 17:15:01 -08:00
|
|
|
if (!calculateNewCapacity(mLength, lengthInc, newCap))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Allocate buffer. */
|
2011-03-31 01:14:12 -07:00
|
|
|
T *newBuf = reinterpret_cast<T *>(this->malloc_(newCap * sizeof(T)));
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!newBuf)
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/* Copy inline elements into heap buffer. */
|
2011-08-01 17:52:53 -07:00
|
|
|
Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
|
2010-11-15 17:15:01 -08:00
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* Switch in heap buffer. */
|
2010-11-15 17:15:01 -08:00
|
|
|
mBegin = newBuf;
|
|
|
|
/* mLength is unchanged. */
|
|
|
|
mCapacity = newCap;
|
2009-08-07 20:09:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
template <class T, size_t N, class AP>
|
2010-11-16 15:37:18 -08:00
|
|
|
JS_NEVER_INLINE bool
|
2010-11-15 17:15:01 -08:00
|
|
|
Vector<T,N,AP>::growStorageBy(size_t incr)
|
|
|
|
{
|
|
|
|
JS_ASSERT(mLength + incr > mCapacity);
|
|
|
|
return usingInlineStorage()
|
|
|
|
? convertToHeapStorage(incr)
|
|
|
|
: growHeapStorageBy(incr);
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-08-07 20:09:11 -07:00
|
|
|
inline bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::reserve(size_t request)
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
2011-08-01 17:52:53 -07:00
|
|
|
if (request > mCapacity && !growStorageBy(request - mLength))
|
|
|
|
return false;
|
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
2011-08-01 17:52:53 -07:00
|
|
|
if (request > mReserved)
|
|
|
|
mReserved = request;
|
|
|
|
JS_ASSERT(mLength <= mReserved);
|
|
|
|
JS_ASSERT(mReserved <= mCapacity);
|
2011-02-18 18:32:29 -08:00
|
|
|
#endif
|
2011-08-01 17:52:53 -07:00
|
|
|
return true;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-06-30 17:19:42 -07:00
|
|
|
inline void
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::shrinkBy(size_t incr)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
JS_ASSERT(incr <= mLength);
|
|
|
|
Impl::destroy(endNoCheck() - incr, endNoCheck());
|
|
|
|
mLength -= incr;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-14 23:19:36 -07:00
|
|
|
template <bool InitNewElems>
|
|
|
|
JS_ALWAYS_INLINE bool
|
|
|
|
Vector<T,N,AP>::growByImpl(size_t incr)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
if (incr > mCapacity - mLength && !growStorageBy(incr))
|
|
|
|
return false;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(mLength + incr <= mCapacity);
|
|
|
|
T *newend = endNoCheck() + incr;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (InitNewElems)
|
2010-11-15 17:15:01 -08:00
|
|
|
Impl::initialize(endNoCheck(), newend);
|
|
|
|
mLength += incr;
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (mLength > mReserved)
|
|
|
|
mReserved = mLength;
|
|
|
|
#endif
|
2009-08-07 20:09:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2010-07-14 23:19:36 -07:00
|
|
|
Vector<T,N,AP>::growBy(size_t incr)
|
|
|
|
{
|
|
|
|
return growByImpl<true>(incr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2010-07-14 23:19:36 -07:00
|
|
|
Vector<T,N,AP>::growByUninitialized(size_t incr)
|
|
|
|
{
|
|
|
|
return growByImpl<false>(incr);
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-10-01 21:00:55 -07:00
|
|
|
STATIC_POSTCONDITION(!return || ubound(this->begin()) >= newLength)
|
2009-08-07 20:09:11 -07:00
|
|
|
inline bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::resize(size_t newLength)
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
size_t curLength = mLength;
|
2009-08-14 16:10:59 -07:00
|
|
|
if (newLength > curLength)
|
|
|
|
return growBy(newLength - curLength);
|
2010-10-04 11:45:34 -07:00
|
|
|
shrinkBy(curLength - newLength);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
JS_ALWAYS_INLINE bool
|
|
|
|
Vector<T,N,AP>::resizeUninitialized(size_t newLength)
|
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
size_t curLength = mLength;
|
2010-10-04 11:45:34 -07:00
|
|
|
if (newLength > curLength)
|
|
|
|
return growByUninitialized(newLength - curLength);
|
2009-08-14 16:10:59 -07:00
|
|
|
shrinkBy(curLength - newLength);
|
2009-08-07 20:09:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-08-07 20:09:11 -07:00
|
|
|
inline void
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::clear()
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
mLength = 0;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2011-06-24 14:22:30 -07:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
inline void
|
|
|
|
Vector<T,N,AP>::clearAndFree()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
if (usingInlineStorage())
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->free_(beginNoCheck());
|
|
|
|
mBegin = (T *)storage.addr();
|
|
|
|
mCapacity = sInlineCapacity;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mReserved = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2011-08-01 17:52:53 -07:00
|
|
|
template <class U>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2011-08-01 17:52:53 -07:00
|
|
|
Vector<T,N,AP>::append(U t)
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
if (mLength == mCapacity && !growStorageBy(1))
|
|
|
|
return false;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
2011-03-08 12:44:42 -08:00
|
|
|
if (mLength + 1 > mReserved)
|
|
|
|
mReserved = mLength + 1;
|
2011-02-18 18:32:29 -08:00
|
|
|
#endif
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(t);
|
2009-08-07 20:09:11 -07:00
|
|
|
return true;
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2011-03-08 12:44:42 -08:00
|
|
|
template <class T, size_t N, class AP>
|
2011-08-01 17:52:53 -07:00
|
|
|
template <class U>
|
2011-03-08 12:44:42 -08:00
|
|
|
JS_ALWAYS_INLINE void
|
2011-08-01 17:52:53 -07:00
|
|
|
Vector<T,N,AP>::internalAppend(U t)
|
2011-03-08 12:44:42 -08:00
|
|
|
{
|
|
|
|
JS_ASSERT(mLength + 1 <= mReserved);
|
|
|
|
JS_ASSERT(mReserved <= mCapacity);
|
|
|
|
new(endNoCheck()) T(t);
|
|
|
|
++mLength;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::appendN(const T &t, size_t needed)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
|
|
|
if (mLength + needed > mCapacity && !growStorageBy(needed))
|
|
|
|
return false;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
2011-03-08 12:44:42 -08:00
|
|
|
if (mLength + needed > mReserved)
|
|
|
|
mReserved = mLength + needed;
|
2011-02-18 18:32:29 -08:00
|
|
|
#endif
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppendN(t, needed);
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-08 12:44:42 -08:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
JS_ALWAYS_INLINE void
|
|
|
|
Vector<T,N,AP>::internalAppendN(const T &t, size_t needed)
|
|
|
|
{
|
|
|
|
JS_ASSERT(mLength + needed <= mReserved);
|
|
|
|
JS_ASSERT(mReserved <= mCapacity);
|
|
|
|
Impl::copyConstructN(endNoCheck(), needed, t);
|
|
|
|
mLength += needed;
|
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
inline bool
|
|
|
|
Vector<T,N,AP>::insert(T *p, const T &val)
|
|
|
|
{
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(begin() <= p && p <= end());
|
2010-08-11 13:30:07 -07:00
|
|
|
size_t pos = p - begin();
|
2010-11-15 17:15:01 -08:00
|
|
|
JS_ASSERT(pos <= mLength);
|
|
|
|
size_t oldLength = mLength;
|
2010-08-11 13:30:07 -07:00
|
|
|
if (pos == oldLength)
|
|
|
|
return append(val);
|
|
|
|
{
|
|
|
|
T oldBack = back();
|
|
|
|
if (!append(oldBack)) /* Dup the last element. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = oldLength; i > pos; --i)
|
|
|
|
(*this)[i] = (*this)[i - 1];
|
|
|
|
(*this)[pos] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP>
|
|
|
|
inline void
|
|
|
|
Vector<T,N,AP>::erase(T *it)
|
|
|
|
{
|
|
|
|
JS_ASSERT(begin() <= it && it < end());
|
|
|
|
while (it + 1 != end()) {
|
|
|
|
*it = *(it + 1);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
popBack();
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-06-30 17:19:42 -07:00
|
|
|
template <class U>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::append(const U *insBegin, const U *insEnd)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
2009-09-01 18:46:19 -07:00
|
|
|
size_t needed = PointerRangeSize(insBegin, insEnd);
|
2010-11-15 17:15:01 -08:00
|
|
|
if (mLength + needed > mCapacity && !growStorageBy(needed))
|
|
|
|
return false;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
2011-03-08 12:44:42 -08:00
|
|
|
if (mLength + needed > mReserved)
|
|
|
|
mReserved = mLength + needed;
|
2011-02-18 18:32:29 -08:00
|
|
|
#endif
|
2011-03-08 12:44:42 -08:00
|
|
|
internalAppend(insBegin, needed);
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-08 12:44:42 -08:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
template <class U>
|
|
|
|
JS_ALWAYS_INLINE void
|
|
|
|
Vector<T,N,AP>::internalAppend(const U *insBegin, size_t length)
|
|
|
|
{
|
|
|
|
JS_ASSERT(mLength + length <= mReserved);
|
|
|
|
JS_ASSERT(mReserved <= mCapacity);
|
|
|
|
Impl::copyConstruct(endNoCheck(), insBegin, insBegin + length);
|
|
|
|
mLength += length;
|
|
|
|
}
|
|
|
|
|
2010-08-11 13:30:07 -07:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
template <class U, size_t O, class BP>
|
|
|
|
inline bool
|
|
|
|
Vector<T,N,AP>::append(const Vector<U,O,BP> &other)
|
|
|
|
{
|
|
|
|
return append(other.begin(), other.end());
|
|
|
|
}
|
|
|
|
|
2011-03-08 12:44:42 -08:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
template <class U, size_t O, class BP>
|
|
|
|
inline void
|
|
|
|
Vector<T,N,AP>::internalAppend(const Vector<U,O,BP> &other)
|
|
|
|
{
|
|
|
|
internalAppend(other.begin(), other.length());
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-08-07 20:09:11 -07:00
|
|
|
template <class U>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::append(const U *insBegin, size_t length)
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
|
|
|
return this->append(insBegin, insBegin + length);
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE void
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::popBack()
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(!empty());
|
2010-11-15 17:15:01 -08:00
|
|
|
--mLength;
|
|
|
|
endNoCheck()->~T();
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2010-11-30 18:41:32 -08:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
JS_ALWAYS_INLINE T
|
|
|
|
Vector<T,N,AP>::popCopy()
|
|
|
|
{
|
|
|
|
T ret = back();
|
|
|
|
popBack();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-06-30 17:19:42 -07:00
|
|
|
inline T *
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::extractRawBuffer()
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
T *ret;
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
2011-03-31 01:14:12 -07:00
|
|
|
ret = reinterpret_cast<T *>(this->malloc_(mLength * sizeof(T)));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
2010-11-15 17:15:01 -08:00
|
|
|
Impl::copyConstruct(ret, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
/* mBegin, mCapacity are unchanged. */
|
|
|
|
mLength = 0;
|
|
|
|
} else {
|
|
|
|
ret = mBegin;
|
|
|
|
mBegin = (T *)storage.addr();
|
|
|
|
mLength = 0;
|
|
|
|
mCapacity = sInlineCapacity;
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
mReserved = 0;
|
|
|
|
#endif
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
2009-06-30 17:19:42 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP>
|
2009-06-30 17:19:42 -07:00
|
|
|
inline void
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector<T,N,AP>::replaceRawBuffer(T *p, size_t length)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2010-11-15 17:15:01 -08:00
|
|
|
REENTRANCY_GUARD_ET_AL;
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* Destroy what we have. */
|
2010-11-15 17:15:01 -08:00
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
if (!usingInlineStorage())
|
2011-03-31 01:14:12 -07:00
|
|
|
this->free_(beginNoCheck());
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* Take in the new buffer. */
|
|
|
|
if (length <= sInlineCapacity) {
|
|
|
|
/*
|
2010-11-15 17:15:01 -08:00
|
|
|
* We convert to inline storage if possible, even though p might
|
|
|
|
* otherwise be acceptable. Maybe this behaviour should be
|
|
|
|
* specifiable with an argument to this function.
|
2009-08-07 20:09:11 -07:00
|
|
|
*/
|
2010-11-15 17:15:01 -08:00
|
|
|
mBegin = (T *)storage.addr();
|
|
|
|
mLength = length;
|
|
|
|
mCapacity = sInlineCapacity;
|
2011-08-01 17:52:53 -07:00
|
|
|
Impl::moveConstruct(mBegin, p, p + length);
|
2009-08-07 20:09:11 -07:00
|
|
|
Impl::destroy(p, p + length);
|
2011-03-31 01:14:12 -07:00
|
|
|
this->free_(p);
|
2009-08-07 20:09:11 -07:00
|
|
|
} else {
|
2010-11-15 17:15:01 -08:00
|
|
|
mBegin = p;
|
|
|
|
mLength = length;
|
|
|
|
mCapacity = length;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
2011-02-18 18:32:29 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
mReserved = length;
|
|
|
|
#endif
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2012-02-26 20:18:13 -08:00
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
inline size_t
|
|
|
|
Vector<T,N,AP>::sizeOfExcludingThis(JSMallocSizeOfFun mallocSizeOf) const
|
|
|
|
{
|
|
|
|
return usingInlineStorage() ? 0 : mallocSizeOf(beginNoCheck());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, size_t N, class AP>
|
|
|
|
inline size_t
|
|
|
|
Vector<T,N,AP>::sizeOfIncludingThis(JSMallocSizeOfFun mallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2010-08-02 07:44:24 -07:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
#endif /* jsvector_h_ */
|