Vector.h 36.1 KB
Newer Older
mjs's avatar
mjs committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 *  Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
mjs's avatar
mjs committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

mjs's avatar
mjs committed
21 22
#ifndef WTF_Vector_h
#define WTF_Vector_h
mjs's avatar
mjs committed
23

24
#include <wtf/Alignment.h>
25
#include <wtf/CheckedArithmetic.h>
26
#include <wtf/FastMalloc.h>
27
#include <wtf/MallocPtr.h>
28 29
#include <wtf/Noncopyable.h>
#include <wtf/NotFound.h>
30
#include <wtf/OwnPtr.h>
31 32 33
#include <wtf/StdLibExtras.h>
#include <wtf/ValueCheck.h>
#include <wtf/VectorTraits.h>
darin's avatar
darin committed
34
#include <limits>
35
#include <string.h>
mjs's avatar
mjs committed
36 37
#include <utility>

mjs's avatar
mjs committed
38
namespace WTF {
mjs's avatar
mjs committed
39

andersca@apple.com's avatar
andersca@apple.com committed
40 41 42 43 44 45 46 47 48 49 50 51 52
template <bool needsDestruction, typename T>
struct VectorDestructor;

template<typename T>
struct VectorDestructor<false, T>
{
    static void destruct(T*, T*) {}
};

template<typename T>
struct VectorDestructor<true, T>
{
    static void destruct(T* begin, T* end) 
mjs's avatar
mjs committed
53
    {
andersca@apple.com's avatar
andersca@apple.com committed
54 55 56 57
        for (T* cur = begin; cur != end; ++cur)
            cur->~T();
    }
};
mjs's avatar
mjs committed
58

andersca@apple.com's avatar
andersca@apple.com committed
59 60
template <bool needsInitialization, bool canInitializeWithMemset, typename T>
struct VectorInitializer;
mjs's avatar
mjs committed
61

andersca@apple.com's avatar
andersca@apple.com committed
62 63 64 65 66
template<bool ignore, typename T>
struct VectorInitializer<false, ignore, T>
{
    static void initialize(T*, T*) {}
};
mjs's avatar
mjs committed
67

andersca@apple.com's avatar
andersca@apple.com committed
68 69 70 71
template<typename T>
struct VectorInitializer<true, false, T>
{
    static void initialize(T* begin, T* end) 
mjs's avatar
mjs committed
72
    {
andersca@apple.com's avatar
andersca@apple.com committed
73 74 75 76
        for (T* cur = begin; cur != end; ++cur)
            new (NotNull, cur) T;
    }
};
mjs's avatar
mjs committed
77

andersca@apple.com's avatar
andersca@apple.com committed
78 79 80 81
template<typename T>
struct VectorInitializer<true, true, T>
{
    static void initialize(T* begin, T* end) 
mjs's avatar
mjs committed
82
    {
andersca@apple.com's avatar
andersca@apple.com committed
83 84 85
        memset(begin, 0, reinterpret_cast<char*>(end) - reinterpret_cast<char*>(begin));
    }
};
mjs's avatar
mjs committed
86

andersca@apple.com's avatar
andersca@apple.com committed
87 88
template <bool canMoveWithMemcpy, typename T>
struct VectorMover;
mjs's avatar
mjs committed
89

andersca@apple.com's avatar
andersca@apple.com committed
90 91 92
template<typename T>
struct VectorMover<false, T>
{
93
    static void move(T* src, T* srcEnd, T* dst)
mjs's avatar
mjs committed
94
    {
andersca@apple.com's avatar
andersca@apple.com committed
95
        while (src != srcEnd) {
96
            new (NotNull, dst) T(std::move(*src));
andersca@apple.com's avatar
andersca@apple.com committed
97 98 99
            src->~T();
            ++dst;
            ++src;
mjs's avatar
mjs committed
100
        }
andersca@apple.com's avatar
andersca@apple.com committed
101
    }
102
    static void moveOverlapping(T* src, T* srcEnd, T* dst)
andersca@apple.com's avatar
andersca@apple.com committed
103 104 105 106 107 108 109 110
    {
        if (src > dst)
            move(src, srcEnd, dst);
        else {
            T* dstEnd = dst + (srcEnd - src);
            while (src != srcEnd) {
                --srcEnd;
                --dstEnd;
111
                new (NotNull, dstEnd) T(std::move(*srcEnd));
andersca@apple.com's avatar
andersca@apple.com committed
112
                srcEnd->~T();
darin's avatar
darin committed
113 114
            }
        }
andersca@apple.com's avatar
andersca@apple.com committed
115 116
    }
};
mjs's avatar
mjs committed
117

andersca@apple.com's avatar
andersca@apple.com committed
118 119 120 121
template<typename T>
struct VectorMover<true, T>
{
    static void move(const T* src, const T* srcEnd, T* dst) 
mjs's avatar
mjs committed
122
    {
andersca@apple.com's avatar
andersca@apple.com committed
123 124 125 126 127 128 129
        memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
    }
    static void moveOverlapping(const T* src, const T* srcEnd, T* dst) 
    {
        memmove(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
    }
};
mjs's avatar
mjs committed
130

andersca@apple.com's avatar
andersca@apple.com committed
131 132
template <bool canCopyWithMemcpy, typename T>
struct VectorCopier;
mjs's avatar
mjs committed
133

andersca@apple.com's avatar
andersca@apple.com committed
134 135 136 137
template<typename T>
struct VectorCopier<false, T>
{
    static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) 
mjs's avatar
mjs committed
138
    {
andersca@apple.com's avatar
andersca@apple.com committed
139 140 141 142
        while (src != srcEnd) {
            new (NotNull, dst) T(*src);
            ++dst;
            ++src;
mjs's avatar
mjs committed
143
        }
andersca@apple.com's avatar
andersca@apple.com committed
144 145
    }
};
mjs's avatar
mjs committed
146

andersca@apple.com's avatar
andersca@apple.com committed
147 148 149 150
template<typename T>
struct VectorCopier<true, T>
{
    static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) 
mjs's avatar
mjs committed
151
    {
andersca@apple.com's avatar
andersca@apple.com committed
152 153 154
        memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
    }
};
mjs's avatar
mjs committed
155

andersca@apple.com's avatar
andersca@apple.com committed
156 157
template <bool canFillWithMemset, typename T>
struct VectorFiller;
mjs's avatar
mjs committed
158

andersca@apple.com's avatar
andersca@apple.com committed
159 160 161 162
template<typename T>
struct VectorFiller<false, T>
{
    static void uninitializedFill(T* dst, T* dstEnd, const T& val) 
mjs's avatar
mjs committed
163
    {
andersca@apple.com's avatar
andersca@apple.com committed
164 165 166
        while (dst != dstEnd) {
            new (NotNull, dst) T(val);
            ++dst;
mjs's avatar
mjs committed
167
        }
andersca@apple.com's avatar
andersca@apple.com committed
168 169
    }
};
mjs's avatar
mjs committed
170

andersca@apple.com's avatar
andersca@apple.com committed
171 172 173 174
template<typename T>
struct VectorFiller<true, T>
{
    static void uninitializedFill(T* dst, T* dstEnd, const T& val) 
mjs's avatar
mjs committed
175
    {
andersca@apple.com's avatar
andersca@apple.com committed
176
        COMPILE_ASSERT(sizeof(T) == sizeof(char), Size_of_type_should_be_equal_to_one);
177
#if COMPILER(GCC) && defined(_FORTIFY_SOURCE)
andersca@apple.com's avatar
andersca@apple.com committed
178
        if (!__builtin_constant_p(dstEnd - dst) || (!(dstEnd - dst)))
179
#endif
andersca@apple.com's avatar
andersca@apple.com committed
180 181 182 183 184 185 186 187 188 189 190
            memset(dst, val, dstEnd - dst);
    }
};

template<bool canCompareWithMemcmp, typename T>
struct VectorComparer;

template<typename T>
struct VectorComparer<false, T>
{
    static bool compare(const T* a, const T* b, size_t size)
andersca's avatar
andersca committed
191
    {
andersca@apple.com's avatar
andersca@apple.com committed
192 193 194 195 196 197
        for (size_t i = 0; i < size; ++i)
            if (!(a[i] == b[i]))
                return false;
        return true;
    }
};
andersca's avatar
andersca committed
198

andersca@apple.com's avatar
andersca@apple.com committed
199 200 201 202
template<typename T>
struct VectorComparer<true, T>
{
    static bool compare(const T* a, const T* b, size_t size)
andersca's avatar
andersca committed
203
    {
andersca@apple.com's avatar
andersca@apple.com committed
204 205 206 207 208 209 210 211
        return memcmp(a, b, sizeof(T) * size) == 0;
    }
};

template<typename T>
struct VectorTypeOperations
{
    static void destruct(T* begin, T* end)
mjs's avatar
mjs committed
212
    {
andersca@apple.com's avatar
andersca@apple.com committed
213 214
        VectorDestructor<VectorTraits<T>::needsDestruction, T>::destruct(begin, end);
    }
mjs's avatar
mjs committed
215

andersca@apple.com's avatar
andersca@apple.com committed
216 217 218 219
    static void initialize(T* begin, T* end)
    {
        VectorInitializer<VectorTraits<T>::needsInitialization, VectorTraits<T>::canInitializeWithMemset, T>::initialize(begin, end);
    }
mjs's avatar
mjs committed
220

221
    static void move(T* src, T* srcEnd, T* dst)
andersca@apple.com's avatar
andersca@apple.com committed
222 223 224
    {
        VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::move(src, srcEnd, dst);
    }
mjs's avatar
mjs committed
225

226
    static void moveOverlapping(T* src, T* srcEnd, T* dst)
andersca@apple.com's avatar
andersca@apple.com committed
227 228 229
    {
        VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::moveOverlapping(src, srcEnd, dst);
    }
darin's avatar
darin committed
230

andersca@apple.com's avatar
andersca@apple.com committed
231 232 233 234
    static void uninitializedCopy(const T* src, const T* srcEnd, T* dst)
    {
        VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(src, srcEnd, dst);
    }
mjs's avatar
mjs committed
235

andersca@apple.com's avatar
andersca@apple.com committed
236 237 238 239 240 241 242 243 244 245
    static void uninitializedFill(T* dst, T* dstEnd, const T& val)
    {
        VectorFiller<VectorTraits<T>::canFillWithMemset, T>::uninitializedFill(dst, dstEnd, val);
    }
    
    static bool compare(const T* a, const T* b, size_t size)
    {
        return VectorComparer<VectorTraits<T>::canCompareWithMemcmp, T>::compare(a, b, size);
    }
};
mjs's avatar
mjs committed
246

andersca@apple.com's avatar
andersca@apple.com committed
247 248 249 250 251 252 253 254 255 256 257 258 259
template<typename T>
class VectorBufferBase {
    WTF_MAKE_NONCOPYABLE(VectorBufferBase);
public:
    void allocateBuffer(size_t newCapacity)
    {
        ASSERT(newCapacity);
        if (newCapacity > std::numeric_limits<unsigned>::max() / sizeof(T))
            CRASH();
        size_t sizeToAllocate = fastMallocGoodSize(newCapacity * sizeof(T));
        m_capacity = sizeToAllocate / sizeof(T);
        m_buffer = static_cast<T*>(fastMalloc(sizeToAllocate));
    }
260

andersca@apple.com's avatar
andersca@apple.com committed
261 262 263 264
    bool tryAllocateBuffer(size_t newCapacity)
    {
        ASSERT(newCapacity);
        if (newCapacity > std::numeric_limits<unsigned>::max() / sizeof(T))
265 266
            return false;

andersca@apple.com's avatar
andersca@apple.com committed
267 268 269
        size_t sizeToAllocate = fastMallocGoodSize(newCapacity * sizeof(T));
        T* newBuffer;
        if (tryFastMalloc(sizeToAllocate).getValue(newBuffer)) {
270
            m_capacity = sizeToAllocate / sizeof(T);
andersca@apple.com's avatar
andersca@apple.com committed
271
            m_buffer = newBuffer;
272 273
            return true;
        }
andersca@apple.com's avatar
andersca@apple.com committed
274 275
        return false;
    }
276

andersca@apple.com's avatar
andersca@apple.com committed
277
    bool shouldReallocateBuffer(size_t newCapacity) const
mjs's avatar
mjs committed
278
    {
andersca@apple.com's avatar
andersca@apple.com committed
279
        return VectorTraits<T>::canMoveWithMemcpy && m_capacity && newCapacity;
mjs's avatar
mjs committed
280 281
    }

andersca@apple.com's avatar
andersca@apple.com committed
282
    void reallocateBuffer(size_t newCapacity)
mjs's avatar
mjs committed
283
    {
andersca@apple.com's avatar
andersca@apple.com committed
284 285 286 287 288 289
        ASSERT(shouldReallocateBuffer(newCapacity));
        if (newCapacity > std::numeric_limits<size_t>::max() / sizeof(T))
            CRASH();
        size_t sizeToAllocate = fastMallocGoodSize(newCapacity * sizeof(T));
        m_capacity = sizeToAllocate / sizeof(T);
        m_buffer = static_cast<T*>(fastRealloc(m_buffer, sizeToAllocate));
mjs's avatar
mjs committed
290 291
    }

andersca@apple.com's avatar
andersca@apple.com committed
292
    void deallocateBuffer(T* bufferToDeallocate)
mjs's avatar
mjs committed
293
    {
andersca@apple.com's avatar
andersca@apple.com committed
294 295
        if (!bufferToDeallocate)
            return;
mjs's avatar
mjs committed
296
        
andersca@apple.com's avatar
andersca@apple.com committed
297 298 299
        if (m_buffer == bufferToDeallocate) {
            m_buffer = 0;
            m_capacity = 0;
mjs's avatar
mjs committed
300 301
        }

andersca@apple.com's avatar
andersca@apple.com committed
302
        fastFree(bufferToDeallocate);
mjs's avatar
mjs committed
303 304
    }

andersca@apple.com's avatar
andersca@apple.com committed
305 306 307
    T* buffer() { return m_buffer; }
    const T* buffer() const { return m_buffer; }
    size_t capacity() const { return m_capacity; }
308

309
    MallocPtr<T> releaseBuffer()
mjs's avatar
mjs committed
310
    {
andersca@apple.com's avatar
andersca@apple.com committed
311 312 313
        T* buffer = m_buffer;
        m_buffer = 0;
        m_capacity = 0;
314
        return adoptMallocPtr(buffer);
mjs's avatar
mjs committed
315 316
    }

andersca@apple.com's avatar
andersca@apple.com committed
317 318 319 320 321
protected:
    VectorBufferBase()
        : m_buffer(0)
        , m_capacity(0)
        , m_size(0)
322 323 324
    {
    }

andersca@apple.com's avatar
andersca@apple.com committed
325 326 327 328
    VectorBufferBase(T* buffer, size_t capacity, size_t size)
        : m_buffer(buffer)
        , m_capacity(capacity)
        , m_size(size)
329 330 331
    {
    }

andersca@apple.com's avatar
andersca@apple.com committed
332
    ~VectorBufferBase()
333
    {
andersca@apple.com's avatar
andersca@apple.com committed
334
        // FIXME: It would be nice to find a way to ASSERT that m_buffer hasn't leaked here.
335 336
    }

andersca@apple.com's avatar
andersca@apple.com committed
337 338 339 340 341 342 343
    T* m_buffer;
    unsigned m_capacity;
    unsigned m_size; // Only used by the Vector subclass, but placed here to avoid padding the struct.
};

template<typename T, size_t inlineCapacity>
class VectorBuffer;
344

andersca@apple.com's avatar
andersca@apple.com committed
345 346 347 348 349 350
template<typename T>
class VectorBuffer<T, 0> : private VectorBufferBase<T> {
private:
    typedef VectorBufferBase<T> Base;
public:
    VectorBuffer()
mjs's avatar
mjs committed
351 352 353
    {
    }

andersca@apple.com's avatar
andersca@apple.com committed
354
    VectorBuffer(size_t capacity, size_t size = 0)
mjs's avatar
mjs committed
355
    {
andersca@apple.com's avatar
andersca@apple.com committed
356 357 358 359 360
        m_size = size;
        // Calling malloc(0) might take a lock and may actually do an
        // allocation on some systems.
        if (capacity)
            allocateBuffer(capacity);
mjs's avatar
mjs committed
361 362
    }

andersca@apple.com's avatar
andersca@apple.com committed
363
    ~VectorBuffer()
mjs's avatar
mjs committed
364
    {
andersca@apple.com's avatar
andersca@apple.com committed
365
        deallocateBuffer(buffer());
mjs's avatar
mjs committed
366 367
    }
    
andersca@apple.com's avatar
andersca@apple.com committed
368
    void swap(VectorBuffer<T, 0>& other)
darin's avatar
darin committed
369
    {
andersca@apple.com's avatar
andersca@apple.com committed
370 371
        std::swap(m_buffer, other.m_buffer);
        std::swap(m_capacity, other.m_capacity);
darin's avatar
darin committed
372
    }
andersca@apple.com's avatar
andersca@apple.com committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    
    void restoreInlineBufferIfNeeded() { }

    using Base::allocateBuffer;
    using Base::tryAllocateBuffer;
    using Base::shouldReallocateBuffer;
    using Base::reallocateBuffer;
    using Base::deallocateBuffer;

    using Base::buffer;
    using Base::capacity;

    using Base::releaseBuffer;

protected:
    using Base::m_size;

private:
    using Base::m_buffer;
    using Base::m_capacity;
};

template<typename T, size_t inlineCapacity>
class VectorBuffer : private VectorBufferBase<T> {
    WTF_MAKE_NONCOPYABLE(VectorBuffer);
private:
    typedef VectorBufferBase<T> Base;
public:
    VectorBuffer()
        : Base(inlineBuffer(), inlineCapacity, 0)
403 404
    {
    }
andersca@apple.com's avatar
andersca@apple.com committed
405 406 407

    VectorBuffer(size_t capacity, size_t size = 0)
        : Base(inlineBuffer(), inlineCapacity, size)
408
    {
andersca@apple.com's avatar
andersca@apple.com committed
409 410
        if (capacity > inlineCapacity)
            Base::allocateBuffer(capacity);
411 412
    }

andersca@apple.com's avatar
andersca@apple.com committed
413
    ~VectorBuffer()
darin's avatar
darin committed
414
    {
andersca@apple.com's avatar
andersca@apple.com committed
415
        deallocateBuffer(buffer());
darin's avatar
darin committed
416 417
    }

andersca@apple.com's avatar
andersca@apple.com committed
418
    void allocateBuffer(size_t newCapacity)
mjs's avatar
mjs committed
419
    {
andersca@apple.com's avatar
andersca@apple.com committed
420 421 422
        // FIXME: This should ASSERT(!m_buffer) to catch misuse/leaks.
        if (newCapacity > inlineCapacity)
            Base::allocateBuffer(newCapacity);
mjs's avatar
mjs committed
423
        else {
andersca@apple.com's avatar
andersca@apple.com committed
424 425
            m_buffer = inlineBuffer();
            m_capacity = inlineCapacity;
mjs's avatar
mjs committed
426 427 428
        }
    }

andersca@apple.com's avatar
andersca@apple.com committed
429
    bool tryAllocateBuffer(size_t newCapacity)
430
    {
andersca@apple.com's avatar
andersca@apple.com committed
431 432 433 434 435
        if (newCapacity > inlineCapacity)
            return Base::tryAllocateBuffer(newCapacity);
        m_buffer = inlineBuffer();
        m_capacity = inlineCapacity;
        return true;
436 437
    }

andersca@apple.com's avatar
andersca@apple.com committed
438
    void deallocateBuffer(T* bufferToDeallocate)
ggaren's avatar
ggaren committed
439
    {
andersca@apple.com's avatar
andersca@apple.com committed
440 441 442
        if (bufferToDeallocate == inlineBuffer())
            return;
        Base::deallocateBuffer(bufferToDeallocate);
ggaren's avatar
ggaren committed
443 444
    }

andersca@apple.com's avatar
andersca@apple.com committed
445
    bool shouldReallocateBuffer(size_t newCapacity) const
ggaren@apple.com's avatar
ggaren@apple.com committed
446
    {
andersca@apple.com's avatar
andersca@apple.com committed
447 448
        // We cannot reallocate the inline buffer.
        return Base::shouldReallocateBuffer(newCapacity) && std::min(static_cast<size_t>(m_capacity), newCapacity) > inlineCapacity;
ggaren@apple.com's avatar
ggaren@apple.com committed
449 450
    }

andersca@apple.com's avatar
andersca@apple.com committed
451
    void reallocateBuffer(size_t newCapacity)
mjs's avatar
mjs committed
452
    {
andersca@apple.com's avatar
andersca@apple.com committed
453 454
        ASSERT(shouldReallocateBuffer(newCapacity));
        Base::reallocateBuffer(newCapacity);
mjs's avatar
mjs committed
455
    }
andersca@apple.com's avatar
andersca@apple.com committed
456 457

    void swap(VectorBuffer<T, inlineCapacity>& other)
458
    {
andersca@apple.com's avatar
andersca@apple.com committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        if (buffer() == inlineBuffer() && other.buffer() == other.inlineBuffer()) {
            WTF::swap(m_inlineBuffer, other.m_inlineBuffer);
            std::swap(m_capacity, other.m_capacity);
        } else if (buffer() == inlineBuffer()) {
            m_buffer = other.m_buffer;
            other.m_buffer = other.inlineBuffer();
            WTF::swap(m_inlineBuffer, other.m_inlineBuffer);
            std::swap(m_capacity, other.m_capacity);
        } else if (other.buffer() == other.inlineBuffer()) {
            other.m_buffer = m_buffer;
            m_buffer = inlineBuffer();
            WTF::swap(m_inlineBuffer, other.m_inlineBuffer);
            std::swap(m_capacity, other.m_capacity);
        } else {
            std::swap(m_buffer, other.m_buffer);
            std::swap(m_capacity, other.m_capacity);
        }
476
    }
andersca@apple.com's avatar
andersca@apple.com committed
477 478

    void restoreInlineBufferIfNeeded()
darin@apple.com's avatar
darin@apple.com committed
479
    {
andersca@apple.com's avatar
andersca@apple.com committed
480 481 482 483
        if (m_buffer)
            return;
        m_buffer = inlineBuffer();
        m_capacity = inlineCapacity;
darin@apple.com's avatar
darin@apple.com committed
484
    }
andersca@apple.com's avatar
andersca@apple.com committed
485 486 487 488

    using Base::buffer;
    using Base::capacity;

489
    MallocPtr<T> releaseBuffer()
bdakin@apple.com's avatar
bdakin@apple.com committed
490
    {
andersca@apple.com's avatar
andersca@apple.com committed
491
        if (buffer() == inlineBuffer())
492
            return nullptr;
andersca@apple.com's avatar
andersca@apple.com committed
493 494
        return Base::releaseBuffer();
    }
bdakin@apple.com's avatar
bdakin@apple.com committed
495

andersca@apple.com's avatar
andersca@apple.com committed
496 497
protected:
    using Base::m_size;
ggaren@apple.com's avatar
ggaren@apple.com committed
498

andersca@apple.com's avatar
andersca@apple.com committed
499 500 501
private:
    using Base::m_buffer;
    using Base::m_capacity;
502

andersca@apple.com's avatar
andersca@apple.com committed
503 504 505
    static const size_t m_inlineBufferSize = inlineCapacity * sizeof(T);
    T* inlineBuffer() { return reinterpret_cast_ptr<T*>(m_inlineBuffer.buffer); }
    const T* inlineBuffer() const { return reinterpret_cast_ptr<const T*>(m_inlineBuffer.buffer); }
ggaren@apple.com's avatar
ggaren@apple.com committed
506

andersca@apple.com's avatar
andersca@apple.com committed
507 508 509 510 511 512 513
    AlignedBuffer<m_inlineBufferSize, WTF_ALIGN_OF(T)> m_inlineBuffer;
};

struct UnsafeVectorOverflow {
    static NO_RETURN_DUE_TO_ASSERT void overflowed()
    {
        ASSERT_NOT_REACHED();
bdakin@apple.com's avatar
bdakin@apple.com committed
514
    }
andersca@apple.com's avatar
andersca@apple.com committed
515 516 517 518 519 520 521 522 523 524 525
};

template<typename T, size_t inlineCapacity = 0, typename OverflowHandler = CrashOnOverflow>
class Vector : private VectorBuffer<T, inlineCapacity> {
    WTF_MAKE_FAST_ALLOCATED;
private:
    typedef VectorBuffer<T, inlineCapacity> Base;
    typedef VectorTypeOperations<T> TypeOperations;

public:
    typedef T ValueType;
mjs's avatar
mjs committed
526

andersca@apple.com's avatar
andersca@apple.com committed
527 528 529 530
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
darin's avatar
darin committed
531

andersca@apple.com's avatar
andersca@apple.com committed
532
    Vector()
darin's avatar
darin committed
533 534
    {
    }
andersca@apple.com's avatar
andersca@apple.com committed
535 536 537
    
    explicit Vector(size_t size)
        : Base(size, size)
538
    {
andersca@apple.com's avatar
andersca@apple.com committed
539 540
        if (begin())
            TypeOperations::initialize(begin(), end());
541 542
    }

543 544 545 546 547 548 549
    Vector(size_t size, const T& val)
        : Base(size, size)
    {
        if (begin())
            TypeOperations::uninitializedFill(begin(), end(), val);
    }

andersca@apple.com's avatar
andersca@apple.com committed
550
    ~Vector()
mjs's avatar
mjs committed
551
    {
andersca@apple.com's avatar
andersca@apple.com committed
552 553
        if (m_size)
            shrink(0);
554 555
    }

andersca@apple.com's avatar
andersca@apple.com committed
556 557 558
    Vector(const Vector&);
    template<size_t otherCapacity, typename otherOverflowBehaviour>
    Vector(const Vector<T, otherCapacity, otherOverflowBehaviour>&);
559

andersca@apple.com's avatar
andersca@apple.com committed
560 561 562
    Vector& operator=(const Vector&);
    template<size_t otherCapacity, typename otherOverflowBehaviour>
    Vector& operator=(const Vector<T, otherCapacity, otherOverflowBehaviour>&);
563

andersca@apple.com's avatar
andersca@apple.com committed
564 565 566 567
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
    Vector(Vector&&);
    Vector& operator=(Vector&&);
#endif
darin's avatar
darin committed
568

andersca@apple.com's avatar
andersca@apple.com committed
569 570 571
    size_t size() const { return m_size; }
    size_t capacity() const { return Base::capacity(); }
    bool isEmpty() const { return !size(); }
ggaren's avatar
ggaren committed
572

andersca@apple.com's avatar
andersca@apple.com committed
573
    T& at(size_t i)
ggaren's avatar
ggaren committed
574
    {
andersca@apple.com's avatar
andersca@apple.com committed
575 576 577
        if (UNLIKELY(i >= size()))
            OverflowHandler::overflowed();
        return Base::buffer()[i];
ggaren's avatar
ggaren committed
578
    }
andersca@apple.com's avatar
andersca@apple.com committed
579
    const T& at(size_t i) const 
580
    {
andersca@apple.com's avatar
andersca@apple.com committed
581 582 583
        if (UNLIKELY(i >= size()))
            OverflowHandler::overflowed();
        return Base::buffer()[i];
584
    }
andersca@apple.com's avatar
andersca@apple.com committed
585
    T& at(Checked<size_t> i)
harrison's avatar
harrison committed
586
    {
andersca@apple.com's avatar
andersca@apple.com committed
587 588
        RELEASE_ASSERT(i < size());
        return Base::buffer()[i];
darin's avatar
darin committed
589
    }
andersca@apple.com's avatar
andersca@apple.com committed
590
    const T& at(Checked<size_t> i) const
harrison's avatar
harrison committed
591
    {
andersca@apple.com's avatar
andersca@apple.com committed
592 593
        RELEASE_ASSERT(i < size());
        return Base::buffer()[i];
harrison's avatar
harrison committed
594 595
    }

andersca@apple.com's avatar
andersca@apple.com committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    T& operator[](size_t i) { return at(i); }
    const T& operator[](size_t i) const { return at(i); }
    T& operator[](Checked<size_t> i) { return at(i); }
    const T& operator[](Checked<size_t> i) const { return at(i); }

    T* data() { return Base::buffer(); }
    const T* data() const { return Base::buffer(); }

    iterator begin() { return data(); }
    iterator end() { return begin() + m_size; }
    const_iterator begin() const { return data(); }
    const_iterator end() const { return begin() + m_size; }

    reverse_iterator rbegin() { return reverse_iterator(end()); }
    reverse_iterator rend() { return reverse_iterator(begin()); }
    const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
    const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }

    T& first() { return at(0); }
    const T& first() const { return at(0); }
    T& last() { return at(size() - 1); }
    const T& last() const { return at(size() - 1); }
    
    T takeLast()
darin's avatar
darin committed
620
    {
andersca@apple.com's avatar
andersca@apple.com committed
621 622 623
        T result = last();
        removeLast();
        return result;
darin's avatar
darin committed
624
    }
andersca@apple.com's avatar
andersca@apple.com committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    
    template<typename U> bool contains(const U&) const;
    template<typename U> size_t find(const U&) const;
    template<typename U> size_t reverseFind(const U&) const;

    void shrink(size_t size);
    void grow(size_t size);
    void resize(size_t size);
    void resizeToFit(size_t size);
    void reserveCapacity(size_t newCapacity);
    bool tryReserveCapacity(size_t newCapacity);
    void reserveInitialCapacity(size_t initialCapacity);
    void shrinkCapacity(size_t newCapacity);
    void shrinkToFit() { shrinkCapacity(size()); }

    void clear() { shrinkCapacity(0); }

    template<typename U> void append(const U*, size_t);
    template<typename U> void append(const U&);
644
    template<typename U> void uncheckedAppend(U&& val);
andersca@apple.com's avatar
andersca@apple.com committed
645 646 647 648 649 650 651 652 653 654 655
    template<typename U, size_t otherCapacity> void appendVector(const Vector<U, otherCapacity>&);
    template<typename U> bool tryAppend(const U*, size_t);

    template<typename U> void insert(size_t position, const U*, size_t);
    template<typename U> void insert(size_t position, const U&);
    template<typename U, size_t c> void insert(size_t position, const Vector<U, c>&);

    void remove(size_t position);
    void remove(size_t position, size_t length);

    void removeLast() 
weinig@apple.com's avatar
weinig@apple.com committed
656
    {
andersca@apple.com's avatar
andersca@apple.com committed
657 658 659
        if (UNLIKELY(isEmpty()))
            OverflowHandler::overflowed();
        shrink(size() - 1); 
weinig@apple.com's avatar
weinig@apple.com committed
660 661
    }

andersca@apple.com's avatar
andersca@apple.com committed
662 663 664 665 666
    void fill(const T&, size_t);
    void fill(const T& val) { fill(val, size()); }

    template<typename Iterator> void appendRange(Iterator start, Iterator end);

667
    MallocPtr<T> releaseBuffer();
andersca@apple.com's avatar
andersca@apple.com committed
668 669

    void swap(Vector<T, inlineCapacity, OverflowHandler>& other)
darin's avatar
darin committed
670
    {
andersca@apple.com's avatar
andersca@apple.com committed
671 672
        std::swap(m_size, other.m_size);
        Base::swap(other);
darin's avatar
darin committed
673 674
    }

andersca@apple.com's avatar
andersca@apple.com committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    void reverse();

    void checkConsistency();

private:
    void expandCapacity(size_t newMinCapacity);
    const T* expandCapacity(size_t newMinCapacity, const T*);
    bool tryExpandCapacity(size_t newMinCapacity);
    const T* tryExpandCapacity(size_t newMinCapacity, const T*);
    template<typename U> U* expandCapacity(size_t newMinCapacity, U*); 
    template<typename U> void appendSlowCase(const U&);

    using Base::m_size;
    using Base::buffer;
    using Base::capacity;
    using Base::swap;
    using Base::allocateBuffer;
    using Base::deallocateBuffer;
    using Base::tryAllocateBuffer;
    using Base::shouldReallocateBuffer;
    using Base::reallocateBuffer;
    using Base::restoreInlineBufferIfNeeded;
    using Base::releaseBuffer;
};

template<typename T, size_t inlineCapacity, typename OverflowHandler>
Vector<T, inlineCapacity, OverflowHandler>::Vector(const Vector& other)
    : Base(other.capacity(), other.size())
{
    if (begin())
        TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<size_t otherCapacity, typename otherOverflowBehaviour>
Vector<T, inlineCapacity, OverflowHandler>::Vector(const Vector<T, otherCapacity, otherOverflowBehaviour>& other)
    : Base(other.capacity(), other.size())
{
    if (begin())
        TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
Vector<T, inlineCapacity, OverflowHandler>& Vector<T, inlineCapacity, OverflowHandler>::operator=(const Vector<T, inlineCapacity, OverflowHandler>& other)
{
    if (&other == this)
        return *this;
    
    if (size() > other.size())
        shrink(other.size());
    else if (other.size() > capacity()) {
        clear();
        reserveCapacity(other.size());
        if (!begin())
            return *this;
    }
    
// Works around an assert in VS2010. See https://connect.microsoft.com/VisualStudio/feedback/details/558044/std-copy-should-not-check-dest-when-first-last
#if COMPILER(MSVC) && defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL
    if (!begin())
        return *this;
ap@apple.com's avatar
ap@apple.com committed
736
#endif
andersca@apple.com's avatar
andersca@apple.com committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

    std::copy(other.begin(), other.begin() + size(), begin());
    TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end());
    m_size = other.size();

    return *this;
}

inline bool typelessPointersAreEqual(const void* a, const void* b) { return a == b; }

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<size_t otherCapacity, typename otherOverflowBehaviour>
Vector<T, inlineCapacity, OverflowHandler>& Vector<T, inlineCapacity, OverflowHandler>::operator=(const Vector<T, otherCapacity, otherOverflowBehaviour>& other)
{
    // If the inline capacities match, we should call the more specific
    // template.  If the inline capacities don't match, the two objects
    // shouldn't be allocated the same address.
    ASSERT(!typelessPointersAreEqual(&other, this));

    if (size() > other.size())
        shrink(other.size());
    else if (other.size() > capacity()) {
        clear();
        reserveCapacity(other.size());
        if (!begin())
            return *this;
ap@apple.com's avatar
ap@apple.com committed
763
    }
andersca@apple.com's avatar
andersca@apple.com committed
764 765 766 767 768 769
    
// Works around an assert in VS2010. See https://connect.microsoft.com/VisualStudio/feedback/details/558044/std-copy-should-not-check-dest-when-first-last
#if COMPILER(MSVC) && defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL
    if (!begin())
        return *this;
#endif
ap@apple.com's avatar
ap@apple.com committed
770

andersca@apple.com's avatar
andersca@apple.com committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    std::copy(other.begin(), other.begin() + size(), begin());
    TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end());
    m_size = other.size();

    return *this;
}

#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
template<typename T, size_t inlineCapacity, typename OverflowHandler>
Vector<T, inlineCapacity, OverflowHandler>::Vector(Vector<T, inlineCapacity, OverflowHandler>&& other)
{
    // It's a little weird to implement a move constructor using swap but this way we
    // don't have to add a move constructor to VectorBuffer.
    swap(other);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
Vector<T, inlineCapacity, OverflowHandler>& Vector<T, inlineCapacity, OverflowHandler>::operator=(Vector<T, inlineCapacity, OverflowHandler>&& other)
{
    swap(other);
    return *this;
}
#endif

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<typename U>
bool Vector<T, inlineCapacity, OverflowHandler>::contains(const U& value) const
{
    return find(value) != notFound;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<typename U>
size_t Vector<T, inlineCapacity, OverflowHandler>::find(const U& value) const
{
    for (size_t i = 0; i < size(); ++i) {
        if (at(i) == value)
            return i;
    }
    return notFound;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<typename U>
size_t Vector<T, inlineCapacity, OverflowHandler>::reverseFind(const U& value) const
{
    for (size_t i = 1; i <= size(); ++i) {
        const size_t index = size() - i;
        if (at(index) == value)
            return index;
    }
    return notFound;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::fill(const T& val, size_t newSize)
{
    if (size() > newSize)
        shrink(newSize);
    else if (newSize > capacity()) {
        clear();
        reserveCapacity(newSize);
        if (!begin())
            return;
mjs's avatar
mjs committed
835
    }
andersca@apple.com's avatar
andersca@apple.com committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    
    std::fill(begin(), end(), val);
    TypeOperations::uninitializedFill(end(), begin() + newSize, val);
    m_size = newSize;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
template<typename Iterator>
void Vector<T, inlineCapacity, OverflowHandler>::appendRange(Iterator start, Iterator end)
{
    for (Iterator it = start; it != end; ++it)
        append(*it);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::expandCapacity(size_t newMinCapacity)
{
    reserveCapacity(std::max(newMinCapacity, std::max(static_cast<size_t>(16), capacity() + capacity() / 4 + 1)));
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
const T* Vector<T, inlineCapacity, OverflowHandler>::expandCapacity(size_t newMinCapacity, const T* ptr)
{
    if (ptr < begin() || ptr >= end()) {
        expandCapacity(newMinCapacity);
        return ptr;
    }
    size_t index = ptr - begin();
    expandCapacity(newMinCapacity);
    return begin() + index;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
bool Vector<T, inlineCapacity, OverflowHandler>::tryExpandCapacity(size_t newMinCapacity)
{
    return tryReserveCapacity(std::max(newMinCapacity, std::max(static_cast<size_t>(16), capacity() + capacity() / 4 + 1)));
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
const T* Vector<T, inlineCapacity, OverflowHandler>::tryExpandCapacity(size_t newMinCapacity, const T* ptr)
{
    if (ptr < begin() || ptr >= end()) {
        if (!tryExpandCapacity(newMinCapacity))
            return 0;
        return ptr;
    }
    size_t index = ptr - begin();
    if (!tryExpandCapacity(newMinCapacity))
        return 0;
    return begin() + index;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
inline U* Vector<T, inlineCapacity, OverflowHandler>::expandCapacity(size_t newMinCapacity, U* ptr)
{
    expandCapacity(newMinCapacity);
    return ptr;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::resize(size_t size)
{
    if (size <= m_size)
        TypeOperations::destruct(begin() + size, end());
    else {
        if (size > capacity())
            expandCapacity(size);
        if (begin())
            TypeOperations::initialize(end(), begin() + size);
    }
    
    m_size = size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::resizeToFit(size_t size)
{
    reserveCapacity(size);
    resize(size);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::shrink(size_t size)
{
    ASSERT(size <= m_size);
    TypeOperations::destruct(begin() + size, end());
    m_size = size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::grow(size_t size)
{
    ASSERT(size >= m_size);
    if (size > capacity())
        expandCapacity(size);
    if (begin())
        TypeOperations::initialize(end(), begin() + size);
    m_size = size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::reserveCapacity(size_t newCapacity)
{
    if (newCapacity <= capacity())
        return;
    T* oldBuffer = begin();
    T* oldEnd = end();
    Base::allocateBuffer(newCapacity);
    if (begin())
        TypeOperations::move(oldBuffer, oldEnd, begin());
    Base::deallocateBuffer(oldBuffer);
}
mjs's avatar
mjs committed
948

andersca@apple.com's avatar
andersca@apple.com committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
template<typename T, size_t inlineCapacity, typename OverflowHandler>
bool Vector<T, inlineCapacity, OverflowHandler>::tryReserveCapacity(size_t newCapacity)
{
    if (newCapacity <= capacity())
        return true;
    T* oldBuffer = begin();
    T* oldEnd = end();
    if (!Base::tryAllocateBuffer(newCapacity))
        return false;
    ASSERT(begin());
    TypeOperations::move(oldBuffer, oldEnd, begin());
    Base::deallocateBuffer(oldBuffer);
    return true;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::reserveInitialCapacity(size_t initialCapacity)
{
    ASSERT(!m_size);
    ASSERT(capacity() == inlineCapacity);
    if (initialCapacity > inlineCapacity)
        Base::allocateBuffer(initialCapacity);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void Vector<T, inlineCapacity, OverflowHandler>::shrinkCapacity(size_t newCapacity)
{
    if (newCapacity >= capacity())
        return;

    if (newCapacity < size()) 
        shrink(newCapacity);

    T* oldBuffer = begin();
    if (newCapacity > 0) {
        if (Base::shouldReallocateBuffer(newCapacity)) {
            Base::reallocateBuffer(newCapacity);
            return;
        }

        T* oldEnd = end();
        Base::allocateBuffer(newCapacity);
        if (begin() != oldBuffer)
            TypeOperations::move(oldBuffer, oldEnd, begin());
darin's avatar
darin committed
993 994
    }

andersca@apple.com's avatar
andersca@apple.com committed
995 996 997 998 999 1000 1001
    Base::deallocateBuffer(oldBuffer);
    Base::restoreInlineBufferIfNeeded();
}

// Templatizing these is better than just letting the conversion happen implicitly,
// because for instance it allows a PassRefPtr to be appended to a RefPtr vector
// without refcount thrash.
mjs's avatar
mjs committed
1002

andersca@apple.com's avatar
andersca@apple.com committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
void Vector<T, inlineCapacity, OverflowHandler>::append(const U* data, size_t dataSize)
{
    size_t newSize = m_size + dataSize;
    if (newSize > capacity()) {
        data = expandCapacity(newSize, data);
        if (!begin())
            return;
    }
    if (newSize < m_size)
        CRASH();
    T* dest = end();
    for (size_t i = 0; i < dataSize; ++i)
        new (NotNull, &dest[i]) T(data[i]);
    m_size = newSize;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
bool Vector<T, inlineCapacity, OverflowHandler>::tryAppend(const U* data, size_t dataSize)
{
    size_t newSize = m_size + dataSize;
    if (newSize > capacity()) {
        data = tryExpandCapacity(newSize, data);
        if (!data)
            return false;
        ASSERT(begin());
    }
    if (newSize < m_size)
        return false;
    T* dest = end();
    for (size_t i = 0; i < dataSize; ++i)
        new (NotNull, &dest[i]) T(data[i]);
    m_size = newSize;
    return true;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
ALWAYS_INLINE void Vector<T, inlineCapacity, OverflowHandler>::append(const U& val)
{
    if (size() != capacity()) {
        new (NotNull, end()) T(val);
        ++m_size;
        return;
mjs's avatar
mjs committed
1046 1047
    }

andersca@apple.com's avatar
andersca@apple.com committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    appendSlowCase(val);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
void Vector<T, inlineCapacity, OverflowHandler>::appendSlowCase(const U& val)
{
    ASSERT(size() == capacity());

    const U* ptr = &val;
    ptr = expandCapacity(size() + 1, ptr);
    if (!begin())
        return;

    new (NotNull, end()) T(*ptr);
    ++m_size;
}

// This version of append saves a branch in the case where you know that the
// vector's capacity is large enough for the append to succeed.

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
1069
inline void Vector<T, inlineCapacity, OverflowHandler>::uncheckedAppend(U&& val)
andersca@apple.com's avatar
andersca@apple.com committed
1070 1071
{
    ASSERT(size() < capacity());
1072 1073
    typename std::remove_reference<U>::type* ptr = &val;
    new (NotNull, end()) T(std::forward<U>(*ptr));
andersca@apple.com's avatar
andersca@apple.com committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    ++m_size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U, size_t otherCapacity>
inline void Vector<T, inlineCapacity, OverflowHandler>::appendVector(const Vector<U, otherCapacity>& val)
{
    append(val.begin(), val.size());
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
void Vector<T, inlineCapacity, OverflowHandler>::insert(size_t position, const U* data, size_t dataSize)
{
    ASSERT_WITH_SECURITY_IMPLICATION(position <= size());
    size_t newSize = m_size + dataSize;
    if (newSize > capacity()) {
        data = expandCapacity(newSize, data);
        if (!begin())
            return;
    }
    if (newSize < m_size)
        CRASH();
    T* spot = begin() + position;
    TypeOperations::moveOverlapping(spot, end(), spot + dataSize);
    for (size_t i = 0; i < dataSize; ++i)
        new (NotNull, &spot[i]) T(data[i]);
    m_size = newSize;
}
 
template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U>
inline void Vector<T, inlineCapacity, OverflowHandler>::insert(size_t position, const U& val)
{
    ASSERT_WITH_SECURITY_IMPLICATION(position <= size());
    const U* data = &val;
    if (size() == capacity()) {
        data = expandCapacity(size() + 1, data);
        if (!begin())
            return;
    }
    T* spot = begin() + position;
    TypeOperations::moveOverlapping(spot, end(), spot + 1);
    new (NotNull, spot) T(*data);
    ++m_size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler> template<typename U, size_t c>
inline void Vector<T, inlineCapacity, OverflowHandler>::insert(size_t position, const Vector<U, c>& val)
{
    insert(position, val.begin(), val.size());
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::remove(size_t position)
{
    ASSERT_WITH_SECURITY_IMPLICATION(position < size());
    T* spot = begin() + position;
    spot->~T();
    TypeOperations::moveOverlapping(spot + 1, end(), spot);
    --m_size;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::remove(size_t position, size_t length)
{
    ASSERT_WITH_SECURITY_IMPLICATION(position <= size());
    ASSERT_WITH_SECURITY_IMPLICATION(position + length <= size());
    T* beginSpot = begin() + position;
    T* endSpot = beginSpot + length;
    TypeOperations::destruct(beginSpot, endSpot); 
    TypeOperations::moveOverlapping(endSpot, end(), beginSpot);
    m_size -= length;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::reverse()
{
    for (size_t i = 0; i < m_size / 2; ++i)
        std::swap(at(i), at(m_size - 1 - i));
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
1154
inline MallocPtr<T> Vector<T, inlineCapacity, OverflowHandler>::releaseBuffer()
andersca@apple.com's avatar
andersca@apple.com committed
1155
{
1156
    auto buffer = Base::releaseBuffer();
andersca@apple.com's avatar
andersca@apple.com committed
1157 1158 1159 1160 1161
    if (inlineCapacity && !buffer && m_size) {
        // If the vector had some data, but no buffer to release,
        // that means it was using the inline buffer. In that case,
        // we create a brand new buffer so the caller always gets one.
        size_t bytes = m_size * sizeof(T);
1162
        buffer = adoptMallocPtr(static_cast<T*>(fastMalloc(bytes)));
1163
        memcpy(buffer.get(), data(), bytes);
mjs's avatar
mjs committed
1164
    }
andersca@apple.com's avatar
andersca@apple.com committed
1165
    m_size = 0;
1166
    return buffer;
andersca@apple.com's avatar
andersca@apple.com committed
1167
}
mjs's avatar
mjs committed
1168

andersca@apple.com's avatar
andersca@apple.com committed
1169 1170 1171
template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void Vector<T, inlineCapacity, OverflowHandler>::checkConsistency()
{
ap@apple.com's avatar
ap@apple.com committed
1172
#if !ASSERT_DISABLED
andersca@apple.com's avatar
andersca@apple.com committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    for (size_t i = 0; i < size(); ++i)
        ValueCheck<T>::checkConsistency(at(i));
#endif
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
void deleteAllValues(const Vector<T, inlineCapacity, OverflowHandler>& collection)
{
    typedef typename Vector<T, inlineCapacity, OverflowHandler>::const_iterator iterator;
    iterator end = collection.end();
    for (iterator it = collection.begin(); it != end; ++it)
        delete *it;
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline void swap(Vector<T, inlineCapacity, OverflowHandler>& a, Vector<T, inlineCapacity, OverflowHandler>& b)
{
    a.swap(b);
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
bool operator==(const Vector<T, inlineCapacity, OverflowHandler>& a, const Vector<T, inlineCapacity, OverflowHandler>& b)
{
    if (a.size() != b.size())
        return false;

    return VectorTypeOperations<T>::compare(a.data(), b.data(), a.size());
}

template<typename T, size_t inlineCapacity, typename OverflowHandler>
inline bool operator!=(const Vector<T, inlineCapacity, OverflowHandler>& a, const Vector<T, inlineCapacity, OverflowHandler>& b)
{
    return !(a == b);
}

#if !ASSERT_DISABLED