Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkTDArray.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkTDArray_DEFINED
9#define SkTDArray_DEFINED
10
15
16#include <algorithm>
17#include <cstddef>
18#include <initializer_list>
19
21public:
22 explicit SkTDStorage(int sizeOfT);
23 SkTDStorage(const void* src, int size, int sizeOfT);
24
25 // Copy
26 SkTDStorage(const SkTDStorage& that);
27 SkTDStorage& operator= (const SkTDStorage& that);
28
29 // Move
31 SkTDStorage& operator= (SkTDStorage&& that);
32
34
35 void reset();
36 void swap(SkTDStorage& that);
37
38 // Size routines
39 bool empty() const { return fSize == 0; }
40 void clear() { fSize = 0; }
41 int size() const { return fSize; }
42 void resize(int newSize);
43 size_t size_bytes() const { return this->bytes(fSize); }
44
45 // Capacity routines
46 int capacity() const { return fCapacity; }
47 void reserve(int newCapacity);
48 void shrink_to_fit();
49
50 void* data() { return fStorage; }
51 const void* data() const { return fStorage; }
52
53 // Deletion routines
54 void erase(int index, int count);
55 // Removes the entry at 'index' and replaces it with the last array element
56 void removeShuffle(int index);
57
58 // Insertion routines
59 void* prepend();
60
61 void append();
62 void append(int count);
63 void* append(const void* src, int count);
64
65 void* insert(int index);
66 void* insert(int index, int count, const void* src);
67
68 void pop_back() {
69 SkASSERT(fSize > 0);
70 fSize--;
71 }
72
73 friend bool operator==(const SkTDStorage& a, const SkTDStorage& b);
74 friend bool operator!=(const SkTDStorage& a, const SkTDStorage& b) {
75 return !(a == b);
76 }
77
78private:
79 size_t bytes(int n) const { return SkToSizeT(n * fSizeOfT); }
80 void* address(int n) { return fStorage + this->bytes(n); }
81
82 // Adds delta to fSize. Crash if outside [0, INT_MAX]
83 int calculateSizeOrDie(int delta);
84
85 // Move the tail of the array defined by the indexes tailStart and tailEnd to dstIndex. The
86 // elements at dstIndex are overwritten by the tail.
87 void moveTail(int dstIndex, int tailStart, int tailEnd);
88
89 // Copy src into the array at dstIndex.
90 void copySrc(int dstIndex, const void* src, int count);
91
92 const int fSizeOfT;
93 std::byte* fStorage{nullptr};
94 int fCapacity{0}; // size of the allocation in fArray (#elements)
95 int fSize{0}; // logical number of elements (fSize <= fCapacity)
96};
97
98static inline void swap(SkTDStorage& a, SkTDStorage& b) {
99 a.swap(b);
100}
101
102// SkTDArray<T> implements a std::vector-like array for raw data-only objects that do not require
103// construction or destruction. The constructor and destructor for T will not be called; T objects
104// will always be moved via raw memcpy. Newly created T objects will contain uninitialized memory.
105template <typename T> class SkTDArray {
106public:
107 SkTDArray() : fStorage{sizeof(T)} {}
108 SkTDArray(const T src[], int count) : fStorage{src, count, sizeof(T)} { }
109 SkTDArray(const std::initializer_list<T>& list) : SkTDArray(list.begin(), list.size()) {}
110
111 // Copy
112 SkTDArray(const SkTDArray<T>& src) : SkTDArray(src.data(), src.size()) {}
114 fStorage = src.fStorage;
115 return *this;
116 }
117
118 // Move
119 SkTDArray(SkTDArray<T>&& src) : fStorage{std::move(src.fStorage)} {}
121 fStorage = std::move(src.fStorage);
122 return *this;
123 }
124
125 friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
126 return a.fStorage == b.fStorage;
127 }
128 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) { return !(a == b); }
129
130 void swap(SkTDArray<T>& that) {
131 using std::swap;
132 swap(fStorage, that.fStorage);
133 }
134
135 bool empty() const { return fStorage.empty(); }
136
137 // Return the number of elements in the array
138 int size() const { return fStorage.size(); }
139
140 // Return the total number of elements allocated.
141 // Note: capacity() - size() gives you the number of elements you can add without causing an
142 // allocation.
143 int capacity() const { return fStorage.capacity(); }
144
145 // return the number of bytes in the array: count * sizeof(T)
146 size_t size_bytes() const { return fStorage.size_bytes(); }
147
148 T* data() { return static_cast<T*>(fStorage.data()); }
149 const T* data() const { return static_cast<const T*>(fStorage.data()); }
150 T* begin() { return this->data(); }
151 const T* begin() const { return this->data(); }
152 T* end() { return this->data() + this->size(); }
153 const T* end() const { return this->data() + this->size(); }
154
155 T& operator[](int index) {
156 return this->data()[sk_collection_check_bounds(index, this->size())];
157 }
158 const T& operator[](int index) const {
159 return this->data()[sk_collection_check_bounds(index, this->size())];
160 }
161
162 const T& back() const {
164 return this->data()[this->size() - 1];
165 }
166 T& back() {
168 return this->data()[this->size() - 1];
169 }
170
171 void reset() {
172 fStorage.reset();
173 }
174
175 void clear() {
176 fStorage.clear();
177 }
178
179 // Sets the number of elements in the array.
180 // If the array does not have space for count elements, it will increase
181 // the storage allocated to some amount greater than that required.
182 // It will never shrink the storage.
183 void resize(int count) {
184 fStorage.resize(count);
185 }
186
187 void reserve(int n) {
188 fStorage.reserve(n);
189 }
190
192 fStorage.append();
193 return this->end() - 1;
194 }
195 T* append(int count) {
196 fStorage.append(count);
197 return this->end() - count;
198 }
199 T* append(int count, const T* src) {
200 return static_cast<T*>(fStorage.append(src, count));
201 }
202
203 T* insert(int index) {
204 return static_cast<T*>(fStorage.insert(index));
205 }
206 T* insert(int index, int count, const T* src = nullptr) {
207 return static_cast<T*>(fStorage.insert(index, count, src));
208 }
209
210 void remove(int index, int count = 1) {
211 fStorage.erase(index, count);
212 }
213
214 void removeShuffle(int index) {
215 fStorage.removeShuffle(index);
216 }
217
218 // routines to treat the array like a stack
219 void push_back(const T& v) {
220 this->append();
221 this->back() = v;
222 }
223 void pop_back() { fStorage.pop_back(); }
224
226 fStorage.shrink_to_fit();
227 }
228
229private:
230 SkTDStorage fStorage;
231};
232
233template <typename T> static inline void swap(SkTDArray<T>& a, SkTDArray<T>& b) { a.swap(b); }
234
235#endif
m reset()
static void erase(SkSurface *surface)
int count
#define SK_SPI
Definition SkAPI.h:41
SK_API void sk_collection_not_empty(bool empty)
Definition SkAssert.h:175
SK_API T sk_collection_check_bounds(T i, T size)
Definition SkAssert.h:143
#define SkASSERT(cond)
Definition SkAssert.h:116
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition SkRefCnt.h:341
static void swap(SkTDStorage &a, SkTDStorage &b)
Definition SkTDArray.h:98
constexpr size_t SkToSizeT(S x)
Definition SkTo.h:31
T * end()
Definition SkTDArray.h:152
const T & back() const
Definition SkTDArray.h:162
SkTDArray< T > & operator=(SkTDArray< T > &&src)
Definition SkTDArray.h:120
int size() const
Definition SkTDArray.h:138
SkTDArray(const SkTDArray< T > &src)
Definition SkTDArray.h:112
bool empty() const
Definition SkTDArray.h:135
T * data()
Definition SkTDArray.h:148
int capacity() const
Definition SkTDArray.h:143
const T * begin() const
Definition SkTDArray.h:151
SkTDArray(const T src[], int count)
Definition SkTDArray.h:108
friend bool operator==(const SkTDArray< T > &a, const SkTDArray< T > &b)
Definition SkTDArray.h:125
T * insert(int index, int count, const T *src=nullptr)
Definition SkTDArray.h:206
void push_back(const T &v)
Definition SkTDArray.h:219
void reset()
Definition SkTDArray.h:171
SkTDArray< T > & operator=(const SkTDArray< T > &src)
Definition SkTDArray.h:113
T * append(int count, const T *src)
Definition SkTDArray.h:199
const T * data() const
Definition SkTDArray.h:149
void reserve(int n)
Definition SkTDArray.h:187
T * begin()
Definition SkTDArray.h:150
T * append()
Definition SkTDArray.h:191
SkTDArray(SkTDArray< T > &&src)
Definition SkTDArray.h:119
const T & operator[](int index) const
Definition SkTDArray.h:158
void resize(int count)
Definition SkTDArray.h:183
const T * end() const
Definition SkTDArray.h:153
void clear()
Definition SkTDArray.h:175
friend bool operator!=(const SkTDArray< T > &a, const SkTDArray< T > &b)
Definition SkTDArray.h:128
void remove(int index, int count=1)
Definition SkTDArray.h:210
T & back()
Definition SkTDArray.h:166
T & operator[](int index)
Definition SkTDArray.h:155
T * append(int count)
Definition SkTDArray.h:195
T * insert(int index)
Definition SkTDArray.h:203
size_t size_bytes() const
Definition SkTDArray.h:146
SkTDArray(const std::initializer_list< T > &list)
Definition SkTDArray.h:109
void removeShuffle(int index)
Definition SkTDArray.h:214
void swap(SkTDArray< T > &that)
Definition SkTDArray.h:130
void shrink_to_fit()
Definition SkTDArray.h:225
void pop_back()
Definition SkTDArray.h:223
void pop_back()
Definition SkTDArray.h:68
void erase(int index, int count)
void resize(int newSize)
Definition SkTDArray.cpp:84
int size() const
Definition SkTDArray.h:41
void clear()
Definition SkTDArray.h:40
const void * data() const
Definition SkTDArray.h:51
void removeShuffle(int index)
void * insert(int index)
void reserve(int newCapacity)
Definition SkTDArray.cpp:92
void shrink_to_fit()
friend bool operator!=(const SkTDStorage &a, const SkTDStorage &b)
Definition SkTDArray.h:74
void * data()
Definition SkTDArray.h:50
void reset()
Definition SkTDArray.cpp:70
bool empty() const
Definition SkTDArray.h:39
size_t size_bytes() const
Definition SkTDArray.h:43
void append()
int capacity() const
Definition SkTDArray.h:46
static void append(char **dst, size_t *count, const char *src, size_t n)
Definition editor.cpp:211
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
static bool b
struct MyStruct a[10]
Definition ref_ptr.h:256
#define T