This file is indexed.

/usr/include/KF5/wtf/Vector.h is in libkf5kjs-dev 5.28.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
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
736
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
763
764
765
766
767
768
769
770
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
835
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 *  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.
 *
 */

#ifndef WTF_Vector_h
#define WTF_Vector_h

#include <wtf/Assertions.h>
#include <wtf/FastMalloc.h>
#include <wtf/Noncopyable.h>
#include <wtf/VectorTraits.h>
#include <limits>
#include <stdlib.h>
#include <cstring>
#include <utility>

// Temporary workaround for Win32.
// We should use NOMINMAX instead.
#undef max

namespace WTF
{

using std::min;
using std::max;

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)
    {
        for (T *cur = begin; cur != end; ++cur) {
            cur->~T();
        }
    }
};

template <bool needsInitialization, bool canInitializeWithMemset, typename T>
struct VectorInitializer;

template<bool ignore, typename T>
struct VectorInitializer<false, ignore, T> {
    static void initialize(T *, T *) {}
};

template<typename T>
struct VectorInitializer<true, false, T> {
    static void initialize(T *begin, T *end)
    {
        for (T *cur = begin; cur != end; ++cur) {
            new(cur) T;
        }
    }
};

template<typename T>
struct VectorInitializer<true, true, T> {
    static void initialize(T *begin, T *end)
    {
        std::memset(begin, 0, reinterpret_cast<char *>(end) - reinterpret_cast<char *>(begin));
    }
};

template <bool canMoveWithMemcpy, typename T>
struct VectorMover;

template<typename T>
struct VectorMover<false, T> {
    static void move(const T *src, const T *srcEnd, T *dst)
    {
        while (src != srcEnd) {
            new(dst) T(*src);
            const_cast<T *>(src)->~T();
            ++dst;
            ++src;
        }
    }
    static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
    {
        if (src > dst) {
            move(src, srcEnd, dst);
        } else {
            T *dstEnd = dst + (srcEnd - src);
            while (src != srcEnd) {
                --srcEnd;
                --dstEnd;
                new(dstEnd) T(*srcEnd);
                const_cast<T *>(srcEnd)->~T();
            }
        }
    }
};

template<typename T>
struct VectorMover<true, T> {
    static void move(const T *src, const T *srcEnd, T *dst)
    {
        std::memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
    }
    static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
    {
        std::memmove(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
    }
};

template <bool canCopyWithMemcpy, typename T>
struct VectorCopier;

template<typename T>
struct VectorCopier<false, T> {
    static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
    {
        while (src != srcEnd) {
            new(dst) T(*src);
            ++dst;
            ++src;
        }
    }
};

template<typename T>
struct VectorCopier<true, T> {
    static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
    {
        std::memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
    }
};

template <bool canFillWithMemset, typename T>
struct VectorFiller;

template<typename T>
struct VectorFiller<false, T> {
    static void uninitializedFill(T *dst, T *dstEnd, const T &val)
    {
        while (dst != dstEnd) {
            new(dst) T(val);
            ++dst;
        }
    }
};

template<typename T>
struct VectorFiller<true, T> {
    static void uninitializedFill(T *dst, T *dstEnd, const T &val)
    {
        ASSERT(sizeof(T) == sizeof(char));
        std::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)
    {
        for (size_t i = 0; i < size; ++i)
            if (a[i] != b[i]) {
                return false;
            }
        return true;
    }
};

template<typename T>
struct VectorComparer<true, T> {
    static bool compare(const T *a, const T *b, size_t size)
    {
        return std::memcmp(a, b, sizeof(T) * size) == 0;
    }
};

template<typename T>
struct VectorTypeOperations {
    static void destruct(T *begin, T *end)
    {
        VectorDestructor<VectorTraits<T>::needsDestruction, T>::destruct(begin, end);
    }

    static void initialize(T *begin, T *end)
    {
        VectorInitializer<VectorTraits<T>::needsInitialization, VectorTraits<T>::canInitializeWithMemset, T>::initialize(begin, end);
    }

    static void move(const T *src, const T *srcEnd, T *dst)
    {
        VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::move(src, srcEnd, dst);
    }

    static void moveOverlapping(const T *src, const T *srcEnd, T *dst)
    {
        VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::moveOverlapping(src, srcEnd, dst);
    }

    static void uninitializedCopy(const T *src, const T *srcEnd, T *dst)
    {
        VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(src, srcEnd, dst);
    }

    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);
    }
};

template<typename T>
class VectorBufferBase : Noncopyable
{
public:
    void allocateBuffer(size_t newCapacity)
    {
        m_capacity = newCapacity;
        if (newCapacity > std::numeric_limits<size_t>::max() / sizeof(T)) {
            CRASH();
        }
        m_buffer = static_cast<T *>(fastMalloc(newCapacity * sizeof(T)));
    }

    void deallocateBuffer(T *bufferToDeallocate)
    {
        if (m_buffer == bufferToDeallocate) {
            m_buffer = 0;
        }
        fastFree(bufferToDeallocate);
    }

    T *buffer()
    {
        return m_buffer;
    }
    const T *buffer() const
    {
        return m_buffer;
    }
    size_t capacity() const
    {
        return m_capacity;
    }

    T *releaseBuffer()
    {
        T *buffer = m_buffer;
        m_buffer = 0;
        m_capacity = 0;
        return buffer;
    }

protected:
    VectorBufferBase()
        : m_buffer(0)
        , m_capacity(0)
    {
    }

    VectorBufferBase(T *buffer, size_t capacity)
        : m_buffer(buffer)
        , m_capacity(capacity)
    {
    }

    ~VectorBufferBase()
    {
        // FIXME: It would be nice to find a way to ASSERT that m_buffer hasn't leaked here.
    }

    T *m_buffer;
    size_t m_capacity;
};

template<typename T, size_t inlineCapacity>
class VectorBuffer;

template<typename T>
class VectorBuffer<T, 0> : private VectorBufferBase<T>
{
private:
    typedef VectorBufferBase<T> Base;
public:
    VectorBuffer()
    {
    }

    VectorBuffer(size_t capacity)
    {
        allocateBuffer(capacity);
    }

    ~VectorBuffer()
    {
        deallocateBuffer(buffer());
    }

    void swap(VectorBuffer<T, 0> &other)
    {
        std::swap(m_buffer, other.m_buffer);
        std::swap(m_capacity, other.m_capacity);
    }

    using Base::allocateBuffer;
    using Base::deallocateBuffer;

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

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

template<typename T, size_t inlineCapacity>
class VectorBuffer : private VectorBufferBase<T>
{
private:
    typedef VectorBufferBase<T> Base;
public:
    VectorBuffer()
        : Base(inlineBuffer(), inlineCapacity)
    {
    }

    VectorBuffer(size_t capacity)
        : Base(inlineBuffer(), inlineCapacity)
    {
        allocateBuffer(capacity);
    }

    ~VectorBuffer()
    {
        deallocateBuffer(buffer());
    }

    void allocateBuffer(size_t newCapacity)
    {
        if (newCapacity > inlineCapacity) {
            Base::allocateBuffer(newCapacity);
        }
    }

    void deallocateBuffer(T *bufferToDeallocate)
    {
        if (bufferToDeallocate == inlineBuffer()) {
            return;
        }
        Base::deallocateBuffer(bufferToDeallocate);
    }

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

    T *releaseBuffer()
    {
        if (buffer() == inlineBuffer()) {
            return 0;
        }
        return Base::releaseBuffer();
    }

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

    static const size_t m_inlineBufferSize = inlineCapacity *sizeof(T);
    T *inlineBuffer()
    {
        return reinterpret_cast<T *>(&m_inlineBuffer);
    }

    // FIXME: Nothing guarantees this buffer is appropriately aligned to hold objects of type T.
    char m_inlineBuffer[m_inlineBufferSize];
};

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

public:
    typedef T ValueType;

    typedef T *iterator;
    typedef const T *const_iterator;

    Vector()
        : m_size(0)
    {
    }

    explicit Vector(size_t size)
        : m_size(size)
        , m_buffer(size)
    {
        TypeOperations::initialize(begin(), end());
    }

    ~Vector()
    {
        clear();
    }

    Vector(const Vector &);
    template<size_t otherCapacity>
    Vector(const Vector<T, otherCapacity> &);

    Vector &operator=(const Vector &);
    template<size_t otherCapacity>
    Vector &operator=(const Vector<T, otherCapacity> &);

    size_t size() const
    {
        return m_size;
    }
    size_t capacity() const
    {
        return m_buffer.capacity();
    }
    bool isEmpty() const
    {
        return !size();
    }

    T &at(size_t i)
    {
        ASSERT(i < size());
        return m_buffer.buffer()[i];
    }
    const T &at(size_t i) const
    {
        ASSERT(i < size());
        return m_buffer.buffer()[i];
    }

    T &operator[](size_t i)
    {
        return at(i);
    }
    const T &operator[](size_t i) const
    {
        return at(i);
    }

    T *data()
    {
        return m_buffer.buffer();
    }
    const T *data() const
    {
        return m_buffer.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;
    }

    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);
    }

    void shrink(size_t size);
    void grow(size_t size);
    void resize(size_t size);
    void reserveCapacity(size_t newCapacity);
    void shrinkCapacity(size_t newCapacity);

    void clear()
    {
        if (m_size) {
            shrink(0);
        }
    }

    template<typename U> void append(const U *, size_t);
    template<typename U> void append(const U &);
    template<typename U> void uncheckedAppend(const U &val);
    template<typename U, size_t c> void append(const Vector<U, c> &);

    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> &);

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

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

    void removeLast()
    {
        ASSERT(!isEmpty());
        shrink(size() - 1);
    }

    Vector(size_t size, const T &val)
        : m_size(size)
        , m_buffer(size)
    {
        TypeOperations::uninitializedFill(begin(), end(), val);
    }

    void fill(const T &, size_t);
    void fill(const T &val)
    {
        fill(val, size());
    }

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

    T *releaseBuffer();

    void swap(Vector<T, inlineCapacity> &other)
    {
        std::swap(m_size, other.m_size);
        m_buffer.swap(other.m_buffer);
    }

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

    size_t m_size;
    Buffer m_buffer;
};

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

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

template<typename T, size_t inlineCapacity>
Vector<T, inlineCapacity> &Vector<T, inlineCapacity>::operator=(const Vector<T, inlineCapacity> &other)
{
    if (&other == this) {
        return *this;
    }

    if (size() > other.size()) {
        shrink(other.size());
    } else if (other.size() > capacity()) {
        clear();
        reserveCapacity(other.size());
    }

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

    return *this;
}

template<typename T, size_t inlineCapacity>
template<size_t otherCapacity>
Vector<T, inlineCapacity> &Vector<T, inlineCapacity>::operator=(const Vector<T, otherCapacity> &other)
{
    if (&other == this) {
        return *this;
    }

    if (size() > other.size()) {
        shrink(other.size());
    } else if (other.size() > capacity()) {
        clear();
        reserveCapacity(other.size());
    }

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

    return *this;
}

template<typename T, size_t inlineCapacity>
void Vector<T, inlineCapacity>::fill(const T &val, size_t newSize)
{
    if (size() > newSize) {
        shrink(newSize);
    } else if (newSize > capacity()) {
        clear();
        reserveCapacity(newSize);
    }

    std::fill(begin(), end(), val);
    TypeOperations::uninitializedFill(end(), begin() + newSize, val);
    m_size = newSize;
}

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

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

template<typename T, size_t inlineCapacity>
const T *Vector<T, inlineCapacity>::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> template<typename U>
inline U *Vector<T, inlineCapacity>::expandCapacity(size_t newMinCapacity, U *ptr)
{
    expandCapacity(newMinCapacity);
    return ptr;
}

template<typename T, size_t inlineCapacity>
void Vector<T, inlineCapacity>::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>
void Vector<T, inlineCapacity>::shrink(size_t size)
{
    ASSERT(size <= m_size);
    TypeOperations::destruct(begin() + size, end());
    m_size = size;
}

template<typename T, size_t inlineCapacity>
void Vector<T, inlineCapacity>::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>
void Vector<T, inlineCapacity>::reserveCapacity(size_t newCapacity)
{
    if (newCapacity <= capacity()) {
        return;
    }
    T *oldBuffer = begin();
    T *oldEnd = end();
    m_buffer.allocateBuffer(newCapacity);
    if (begin()) {
        TypeOperations::move(oldBuffer, oldEnd, begin());
    }
    m_buffer.deallocateBuffer(oldBuffer);
}

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

    resize(min(m_size, newCapacity));

    T *oldBuffer = begin();
    if (newCapacity > 0) {
        T *oldEnd = end();
        m_buffer.allocateBuffer(newCapacity);
        if (begin() != oldBuffer) {
            TypeOperations::move(oldBuffer, oldEnd, begin());
        }
    }

    m_buffer.deallocateBuffer(oldBuffer);
}

// 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.

template<typename T, size_t inlineCapacity> template<typename U>
void Vector<T, inlineCapacity>::append(const U *data, size_t dataSize)
{
    size_t newSize = m_size + dataSize;
    if (newSize > capacity()) {
        data = expandCapacity(newSize, data);
        if (!begin()) {
            return;
        }
    }
    T *dest = end();
    for (size_t i = 0; i < dataSize; ++i) {
        new(&dest[i]) T(data[i]);
    }
    m_size = newSize;
}

template<typename T, size_t inlineCapacity> template<typename U>
inline void Vector<T, inlineCapacity>::append(const U &val)
{
    const U *ptr = &val;
    if (size() == capacity()) {
        ptr = expandCapacity(size() + 1, ptr);
        if (!begin()) {
            return;
        }
    }

#if COMPILER(MSVC7)
    // FIXME: MSVC7 generates compilation errors when trying to assign
    // a pointer to a Vector of its base class (i.e. can't downcast). So far
    // I've been unable to determine any logical reason for this, so I can
    // only assume it is a bug with the compiler. Casting is a bad solution,
    // however, because it subverts implicit conversions, so a better
    // one is needed.
    new(end()) T(static_cast<T>(*ptr));
#else
    new(end()) T(*ptr);
#endif
    ++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> template<typename U>
inline void Vector<T, inlineCapacity>::uncheckedAppend(const U &val)
{
    ASSERT(size() < capacity());
    const U *ptr = &val;
    new(end()) T(*ptr);
    ++m_size;
}

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

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

template<typename T, size_t inlineCapacity> template<typename U>
inline void Vector<T, inlineCapacity>::insert(size_t position, const U &val)
{
    ASSERT(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(spot) T(*data);
    ++m_size;
}

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

template<typename T, size_t inlineCapacity> template<typename U>
void Vector<T, inlineCapacity>::prepend(const U *data, size_t dataSize)
{
    insert(0, data, dataSize);
}

template<typename T, size_t inlineCapacity> template<typename U>
inline void Vector<T, inlineCapacity>::prepend(const U &val)
{
    insert(0, val);
}

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

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

template<typename T, size_t inlineCapacity>
inline void Vector<T, inlineCapacity>::remove(size_t position, size_t length)
{
    ASSERT(position < size());
    ASSERT(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>
inline T *Vector<T, inlineCapacity>::releaseBuffer()
{
    T *buffer = m_buffer.releaseBuffer();
    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);
        buffer = static_cast<T *>(fastMalloc(bytes));
        memcpy(buffer, data(), bytes);
    }
    ASSERT(buffer);
    m_size = 0;
    return buffer;
}

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

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

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

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

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

} // namespace WTF

using WTF::Vector;

#endif // WTF_Vector_h