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:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
|
|
|
|
* June 12, 2009.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Corporation.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Luke Wagner <lw@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef jsvector_h_
|
|
|
|
#define jsvector_h_
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
#include "jstl.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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
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());
|
2009-09-01 18:46:19 -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;
|
2009-08-07 20:09:11 -07:00
|
|
|
for (T *dst = newbuf, *src = v.heapBegin(); src != v.heapEnd(); ++dst, ++src)
|
2009-06-30 17:19:42 -07:00
|
|
|
new(dst) T(*src);
|
2009-09-01 18:46:19 -07:00
|
|
|
VectorImpl::destroy(v.heapBegin(), v.heapEnd());
|
|
|
|
v.free(v.heapBegin());
|
2009-08-14 16:10:59 -07:00
|
|
|
v.heapEnd() = newbuf + v.heapLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
v.heapBegin() = newbuf;
|
|
|
|
v.heapCapacity() = 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;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-10-12 18:00:48 -07:00
|
|
|
T *newbuf = reinterpret_cast<T *>(v.realloc(v.heapBegin(), bytes));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!newbuf)
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
2009-08-14 16:10:59 -07:00
|
|
|
v.heapEnd() = newbuf + v.heapLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
v.heapBegin() = newbuf;
|
|
|
|
v.heapCapacity() = 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:
|
|
|
|
* - see "Allocation policies" in jstl.h (default ContextAllocPolicy)
|
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>
|
|
|
|
class Vector : AllocPolicy
|
2009-06-30 17:19:42 -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);
|
|
|
|
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 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointers to the heap-allocated buffer. Only [heapBegin(), heapEnd())
|
|
|
|
* hold valid constructed T objects. The range [heapEnd(), heapBegin() +
|
|
|
|
* heapCapacity()) holds uninitialized memory.
|
|
|
|
*/
|
|
|
|
struct BufferPtrs {
|
2009-09-10 16:44:01 -07:00
|
|
|
T *mBegin, *mEnd;
|
2009-08-07 20:09:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since a vector either stores elements inline or in a heap-allocated
|
2009-09-10 16:44:01 -07:00
|
|
|
* buffer, reuse the storage. mLengthOrCapacity serves as the union
|
2010-04-19 13:08:19 -07:00
|
|
|
* discriminator. In inline mode (when elements are stored in u.storage),
|
2009-09-10 16:44:01 -07:00
|
|
|
* mLengthOrCapacity holds the vector's length. In heap mode (when elements
|
|
|
|
* are stored in [u.ptrs.mBegin, u.ptrs.mEnd)), mLengthOrCapacity holds the
|
2009-08-07 20:09:11 -07:00
|
|
|
* vector's capacity.
|
|
|
|
*/
|
|
|
|
static const size_t sInlineCapacity =
|
2009-09-01 18:46:19 -07:00
|
|
|
tl::Clamp<N, sizeof(BufferPtrs) / sizeof(T),
|
|
|
|
sMaxInlineBytes / sizeof(T)>::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 =
|
|
|
|
tl::Max<1, sInlineCapacity * sizeof(T)>::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
|
|
|
|
2009-09-10 16:44:01 -07:00
|
|
|
size_t mLengthOrCapacity;
|
|
|
|
bool usingInlineStorage() const { return mLengthOrCapacity <= sInlineCapacity; }
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
union {
|
|
|
|
BufferPtrs ptrs;
|
2010-04-19 13:08:19 -07:00
|
|
|
AlignedStorage<sInlineBytes> storage;
|
2009-08-07 20:09:11 -07:00
|
|
|
} u;
|
|
|
|
|
|
|
|
/* Only valid when usingInlineStorage() */
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t &inlineLength() {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return mLengthOrCapacity;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t inlineLength() const {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return mLengthOrCapacity;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *inlineBegin() const {
|
|
|
|
JS_ASSERT(usingInlineStorage());
|
2010-04-19 13:08:19 -07:00
|
|
|
return (T *)u.storage.addr();
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *inlineEnd() const {
|
|
|
|
JS_ASSERT(usingInlineStorage());
|
2010-04-19 13:08:19 -07:00
|
|
|
return (T *)u.storage.addr() + mLengthOrCapacity;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only valid when !usingInlineStorage() */
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t heapLength() const {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-08-18 08:41:30 -07:00
|
|
|
/* Guaranteed by calculateNewCapacity. */
|
2009-09-10 16:44:01 -07:00
|
|
|
JS_ASSERT(size_t(u.ptrs.mEnd - u.ptrs.mBegin) ==
|
|
|
|
((size_t(u.ptrs.mEnd) - size_t(u.ptrs.mBegin)) / sizeof(T)));
|
|
|
|
return u.ptrs.mEnd - u.ptrs.mBegin;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t &heapCapacity() {
|
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return mLengthOrCapacity;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *&heapBegin() {
|
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return u.ptrs.mBegin;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *&heapEnd() {
|
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return u.ptrs.mEnd;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t heapCapacity() const {
|
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return mLengthOrCapacity;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
T *heapBegin() const {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return u.ptrs.mBegin;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 08:41:30 -07:00
|
|
|
T *heapEnd() const {
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(!usingInlineStorage());
|
2009-09-10 16:44:01 -07:00
|
|
|
return u.ptrs.mEnd;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
#ifdef DEBUG
|
2009-09-01 18:46:19 -07:00
|
|
|
friend class ReentrancyGuard;
|
2010-02-06 10:14:05 -08:00
|
|
|
bool entered;
|
2009-06-30 17:19:42 -07:00
|
|
|
#endif
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector(const Vector &);
|
|
|
|
Vector &operator=(const Vector &);
|
2009-08-07 20:09:11 -07:00
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
public:
|
2009-09-01 18:46:19 -07:00
|
|
|
Vector(AllocPolicy = AllocPolicy());
|
|
|
|
~Vector();
|
2009-06-30 17:19:42 -07:00
|
|
|
|
|
|
|
/* accessors */
|
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t length() const {
|
|
|
|
return usingInlineStorage() ? inlineLength() : heapLength();
|
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 {
|
2009-08-14 16:10:59 -07:00
|
|
|
return usingInlineStorage() ? inlineLength() == 0 : heapBegin() == heapEnd();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
size_t capacity() const {
|
|
|
|
return usingInlineStorage() ? sInlineCapacity : heapCapacity();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *begin() {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2009-08-07 20:09:11 -07:00
|
|
|
return usingInlineStorage() ? inlineBegin() : heapBegin();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const T *begin() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2009-08-07 20:09:11 -07:00
|
|
|
return usingInlineStorage() ? inlineBegin() : heapBegin();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
T *end() {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2009-08-07 20:09:11 -07:00
|
|
|
return usingInlineStorage() ? inlineEnd() : heapEnd();
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const T *end() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered);
|
2009-08-07 20:09:11 -07:00
|
|
|
return usingInlineStorage() ? inlineEnd() : heapEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
T &operator[](size_t i) {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered && i < length());
|
2009-08-07 20:09:11 -07:00
|
|
|
return begin()[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &operator[](size_t i) const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!entered && i < length());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* Destroy elements in the range [begin() + incr, end()). */
|
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);
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
void clear();
|
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
bool append(const T &t);
|
|
|
|
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
|
|
|
|
|
|
|
void popBack();
|
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);
|
2009-08-07 20:09:11 -07:00
|
|
|
};
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/* Helper functions */
|
2009-06-30 17:19:42 -07:00
|
|
|
|
2009-08-07 20:09:11 -07:00
|
|
|
/*
|
|
|
|
* This helper function is specialized for appending the characters of a string
|
|
|
|
* literal to a vector. This could not be done generically since one must take
|
|
|
|
* care not to append the terminating '\0'.
|
|
|
|
*/
|
2009-09-01 18:46:19 -07:00
|
|
|
template <class T, size_t N, class AP, size_t ArrayLength>
|
2010-07-25 17:17:44 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2009-09-01 18:46:19 -07:00
|
|
|
js_AppendLiteral(Vector<T,N,AP> &v, const char (&array)[ArrayLength])
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
2009-08-14 16:10:59 -07:00
|
|
|
return v.append(array, array + ArrayLength - 1);
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
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)
|
|
|
|
: AllocPolicy(ap), mLengthOrCapacity(0)
|
2009-09-01 18:46:19 -07:00
|
|
|
#ifdef DEBUG
|
2010-02-06 10:14:05 -08:00
|
|
|
, entered(false)
|
2009-09-01 18:46:19 -07:00
|
|
|
#endif
|
|
|
|
{}
|
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-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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
|
|
|
Impl::destroy(inlineBegin(), inlineEnd());
|
|
|
|
} else {
|
|
|
|
Impl::destroy(heapBegin(), heapEnd());
|
2009-09-01 18:46:19 -07:00
|
|
|
this->free(heapBegin());
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
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>
|
2009-06-30 17:19:42 -07:00
|
|
|
inline bool
|
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
|
|
|
|
|
|
|
/* Round up to next power of 2. */
|
2009-09-01 18:46:19 -07:00
|
|
|
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
|
2009-08-14 16:10:59 -07:00
|
|
|
* (heapLength() + 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
|
|
|
{
|
2009-08-18 08:41:30 -07:00
|
|
|
size_t newCap;
|
2009-08-14 16:10:59 -07:00
|
|
|
return calculateNewCapacity(heapLength(), 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
|
|
|
/*
|
2009-08-14 16:10:59 -07:00
|
|
|
* This function will create a new heap buffer with capacity (inlineLength() +
|
|
|
|
* 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
|
|
|
{
|
|
|
|
size_t newCap;
|
2009-08-14 16:10:59 -07:00
|
|
|
if (!calculateNewCapacity(inlineLength(), lengthInc, newCap))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Allocate buffer. */
|
2009-09-01 18:46:19 -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. */
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t length = inlineLength();
|
2009-08-18 08:41:30 -07:00
|
|
|
Impl::copyConstruct(newBuf, inlineBegin(), inlineEnd());
|
2009-08-07 20:09:11 -07:00
|
|
|
Impl::destroy(inlineBegin(), inlineEnd());
|
|
|
|
|
|
|
|
/* Switch in heap buffer. */
|
2009-09-10 16:44:01 -07:00
|
|
|
mLengthOrCapacity = newCap; /* marks us as !usingInlineStorage() */
|
2009-08-18 08:41:30 -07:00
|
|
|
heapBegin() = newBuf;
|
2009-08-14 16:10:59 -07:00
|
|
|
heapEnd() = newBuf + length;
|
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 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
|
|
|
{
|
2009-06-30 17:19:42 -07:00
|
|
|
ReentrancyGuard g(*this);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
|
|
|
if (request > sInlineCapacity)
|
2009-08-14 16:10:59 -07:00
|
|
|
return convertToHeapStorage(request - inlineLength());
|
2009-08-07 20:09:11 -07:00
|
|
|
} else {
|
|
|
|
if (request > heapCapacity())
|
2009-08-14 16:10:59 -07:00
|
|
|
return growHeapStorageBy(request - heapLength());
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2009-08-14 16:10:59 -07:00
|
|
|
JS_ASSERT(incr <= length());
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
|
|
|
Impl::destroy(inlineEnd() - incr, inlineEnd());
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() -= incr;
|
2009-08-07 20:09:11 -07:00
|
|
|
} else {
|
|
|
|
Impl::destroy(heapEnd() - incr, heapEnd());
|
|
|
|
heapEnd() -= 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
|
|
|
{
|
2009-08-07 20:09:11 -07:00
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
if (usingInlineStorage()) {
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = sInlineCapacity - inlineLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
if (incr <= freespace) {
|
|
|
|
T *newend = inlineEnd() + incr;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (InitNewElems)
|
|
|
|
Impl::initialize(inlineEnd(), newend);
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() += incr;
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!convertToHeapStorage(incr))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* grow if needed */
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = heapCapacity() - heapLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
if (incr > freespace) {
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!growHeapStorageBy(incr))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are !usingInlineStorage(). Initialize new elements. */
|
2009-08-14 16:10:59 -07:00
|
|
|
JS_ASSERT(heapCapacity() - heapLength() >= incr);
|
2009-08-07 20:09:11 -07:00
|
|
|
T *newend = heapEnd() + incr;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (InitNewElems)
|
|
|
|
Impl::initialize(heapEnd(), newend);
|
2009-08-07 20:09:11 -07:00
|
|
|
heapEnd() = newend;
|
|
|
|
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>
|
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
|
|
|
{
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t curLength = length();
|
|
|
|
if (newLength > curLength)
|
|
|
|
return growBy(newLength - curLength);
|
|
|
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
if (usingInlineStorage()) {
|
|
|
|
Impl::destroy(inlineBegin(), inlineEnd());
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() = 0;
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Impl::destroy(heapBegin(), heapEnd());
|
|
|
|
heapEnd() = heapBegin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>::append(const T &t)
|
2009-08-07 20:09:11 -07:00
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
if (usingInlineStorage()) {
|
2009-08-14 16:10:59 -07:00
|
|
|
if (inlineLength() < sInlineCapacity) {
|
2009-08-07 20:09:11 -07:00
|
|
|
new(inlineEnd()) T(t);
|
2009-08-14 16:10:59 -07:00
|
|
|
++inlineLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!convertToHeapStorage(1))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
} else {
|
2009-08-14 16:10:59 -07:00
|
|
|
if (heapLength() == heapCapacity() && !growHeapStorageBy(1))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are !usingInlineStorage(). Initialize new elements. */
|
2009-08-14 16:10:59 -07:00
|
|
|
JS_ASSERT(heapLength() <= heapCapacity() && heapCapacity() - heapLength() >= 1);
|
2009-08-07 20:09:11 -07:00
|
|
|
new(heapEnd()++) T(t);
|
|
|
|
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>
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = sInlineCapacity - inlineLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
if (needed <= freespace) {
|
|
|
|
Impl::copyConstructN(inlineEnd(), needed, t);
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() += needed;
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!convertToHeapStorage(needed))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
} else {
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = heapCapacity() - heapLength();
|
2009-08-18 08:41:30 -07:00
|
|
|
if (needed > freespace && !growHeapStorageBy(needed))
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* We are !usingInlineStorage(). Initialize new elements. */
|
2009-08-14 16:10:59 -07:00
|
|
|
JS_ASSERT(heapLength() <= heapCapacity() && heapCapacity() - heapLength() >= needed);
|
2009-08-07 20:09:11 -07:00
|
|
|
Impl::copyConstructN(heapEnd(), needed, t);
|
|
|
|
heapEnd() += needed;
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
JS_ASSERT(begin() <= p && p < end());
|
|
|
|
size_t pos = p - begin();
|
|
|
|
JS_ASSERT(pos <= length());
|
|
|
|
size_t oldLength = length();
|
|
|
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2009-09-01 18:46:19 -07:00
|
|
|
size_t needed = PointerRangeSize(insBegin, insEnd);
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = sInlineCapacity - inlineLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
if (needed <= freespace) {
|
|
|
|
Impl::copyConstruct(inlineEnd(), insBegin, insEnd);
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() += needed;
|
2009-08-07 20:09:11 -07:00
|
|
|
JS_ASSERT(usingInlineStorage());
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-18 08:41:30 -07:00
|
|
|
if (!convertToHeapStorage(needed))
|
2009-08-07 20:09:11 -07:00
|
|
|
return false;
|
|
|
|
} else {
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t freespace = heapCapacity() - heapLength();
|
2009-08-18 08:41:30 -07:00
|
|
|
if (needed > freespace && !growHeapStorageBy(needed))
|
2009-06-30 17:19:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* We are !usingInlineStorage(). Initialize new elements. */
|
2009-08-14 16:10:59 -07:00
|
|
|
JS_ASSERT(heapLength() <= heapCapacity() && heapCapacity() - heapLength() >= needed);
|
2009-08-07 20:09:11 -07:00
|
|
|
Impl::copyConstruct(heapEnd(), insBegin, insEnd);
|
|
|
|
heapEnd() += needed;
|
2009-06-30 17:19:42 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
JS_ASSERT(!empty());
|
|
|
|
if (usingInlineStorage()) {
|
2009-08-14 16:10:59 -07:00
|
|
|
--inlineLength();
|
2009-08-07 20:09:11 -07:00
|
|
|
inlineEnd()->~T();
|
|
|
|
} else {
|
|
|
|
--heapEnd();
|
|
|
|
heapEnd()->~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-08-07 20:09:11 -07:00
|
|
|
if (usingInlineStorage()) {
|
2009-09-01 18:46:19 -07:00
|
|
|
T *ret = reinterpret_cast<T *>(this->malloc(inlineLength() * sizeof(T)));
|
2009-08-07 20:09:11 -07:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
Impl::copyConstruct(ret, inlineBegin(), inlineEnd());
|
|
|
|
Impl::destroy(inlineBegin(), inlineEnd());
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() = 0;
|
2009-08-07 20:09:11 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *ret = heapBegin();
|
2009-09-10 16:44:01 -07:00
|
|
|
mLengthOrCapacity = 0; /* marks us as !usingInlineStorage() */
|
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
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2009-08-07 20:09:11 -07:00
|
|
|
|
|
|
|
/* Destroy what we have. */
|
|
|
|
if (usingInlineStorage()) {
|
|
|
|
Impl::destroy(inlineBegin(), inlineEnd());
|
2009-08-14 16:10:59 -07:00
|
|
|
inlineLength() = 0;
|
2009-08-07 20:09:11 -07:00
|
|
|
} else {
|
|
|
|
Impl::destroy(heapBegin(), heapEnd());
|
2009-09-01 18:46:19 -07:00
|
|
|
this->free(heapBegin());
|
2009-08-07 20:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Take in the new buffer. */
|
|
|
|
if (length <= sInlineCapacity) {
|
|
|
|
/*
|
2009-09-10 16:44:01 -07:00
|
|
|
* (mLengthOrCapacity <= sInlineCapacity) means inline storage, so we
|
2009-08-14 16:10:59 -07:00
|
|
|
* MUST use inline storage, even though p might otherwise be acceptable.
|
2009-08-07 20:09:11 -07:00
|
|
|
*/
|
2009-09-10 16:44:01 -07:00
|
|
|
mLengthOrCapacity = length; /* marks us as usingInlineStorage() */
|
2009-08-07 20:09:11 -07:00
|
|
|
Impl::copyConstruct(inlineBegin(), p, p + length);
|
|
|
|
Impl::destroy(p, p + length);
|
2009-09-01 18:46:19 -07:00
|
|
|
this->free(p);
|
2009-08-07 20:09:11 -07:00
|
|
|
} else {
|
2009-09-10 16:44:01 -07:00
|
|
|
mLengthOrCapacity = length; /* marks us as !usingInlineStorage() */
|
2009-08-07 20:09:11 -07:00
|
|
|
heapBegin() = p;
|
|
|
|
heapEnd() = heapBegin() + length;
|
|
|
|
}
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
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_ */
|