1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef VECTOR_H |
4 | #define VECTOR_H |
5 | |
6 | #include <vector> |
7 | #include <wtf/Assertions.h> |
8 | #include <wtf/NotFound.h> |
9 | #include <qalgorithms.h> |
10 | |
11 | enum WTF_UnusedOverflowMode { |
12 | UnsafeVectorOverflow |
13 | }; |
14 | |
15 | namespace WTF { |
16 | |
17 | template <typename T, int capacity = 1, int overflowMode = UnsafeVectorOverflow> |
18 | class Vector : public std::vector<T> { |
19 | public: |
20 | Vector() {} |
21 | Vector(int initialSize) : std::vector<T>(initialSize) {} |
22 | Vector(std::initializer_list<T> list) : std::vector<T>(list) {} |
23 | |
24 | inline void append(const T& value) |
25 | { this->push_back(value); } |
26 | |
27 | template <typename OtherType> |
28 | inline void append(const OtherType& other) |
29 | { this->push_back(T(other)); } |
30 | |
31 | inline void append(T&& other) |
32 | { this->push_back(std::move(other)); } |
33 | |
34 | inline void append(const Vector<T>& vector) |
35 | { |
36 | this->insert(this->end(), vector.begin(), vector.end()); |
37 | } |
38 | |
39 | inline void append(const T* ptr, size_t count) |
40 | { |
41 | for (size_t i = 0; i < count; ++i) |
42 | this->push_back(T(ptr[i])); |
43 | } |
44 | |
45 | inline void append(typename std::vector<T>::const_iterator it, size_t count) |
46 | { |
47 | for (size_t i = 0; i < count; ++i, ++it) |
48 | this->push_back(*it); |
49 | } |
50 | |
51 | unsigned size() const { return static_cast<unsigned>(std::vector<T>::size()); } |
52 | |
53 | using std::vector<T>::insert; |
54 | |
55 | inline void reserveInitialCapacity(size_t size) { this->reserve(size); } |
56 | |
57 | inline void insert(size_t position, T value) |
58 | { this->insert(this->begin() + position, value); } |
59 | |
60 | inline void grow(size_t size) |
61 | { this->resize(size); } |
62 | |
63 | inline void shrink(size_t size) |
64 | { this->erase(this->begin() + size, this->end()); } |
65 | |
66 | inline void shrinkToFit() |
67 | { this->shrink(this->size()); } |
68 | |
69 | inline void remove(size_t position) |
70 | { this->erase(this->begin() + position); } |
71 | |
72 | inline bool isEmpty() const { return this->empty(); } |
73 | |
74 | inline T &last() { return *(this->begin() + this->size() - 1); } |
75 | |
76 | bool contains(const T &value) const |
77 | { |
78 | for (const T &inVector : *this) { |
79 | if (inVector == value) |
80 | return true; |
81 | } |
82 | return false; |
83 | } |
84 | }; |
85 | |
86 | template <typename T, int capacity> |
87 | void deleteAllValues(const Vector<T, capacity> &vector) |
88 | { |
89 | qDeleteAll(vector); |
90 | } |
91 | |
92 | } |
93 | |
94 | using WTF::Vector; |
95 | using WTF::deleteAllValues; |
96 | |
97 | #endif // VECTOR_H |
98 | |