blob: 56a2165fb733bf696b00ba48ffbbad4ad10d5f56 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#ifndef SkTDArray_DEFINED
10#define SkTDArray_DEFINED
11
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040012#include "SkMalloc.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "SkTo.h"
14#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Mike Reed5edcd312018-08-08 11:23:41 -040016#include <initializer_list>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040017#include <utility>
18
Mike Klein22c1f372018-04-02 20:37:42 +000019template <typename T> class SkTDArray {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020public:
Mike Klein22c1f372018-04-02 20:37:42 +000021 SkTDArray() : fArray(nullptr), fReserve(0), fCount(0) {}
22 SkTDArray(const T src[], int count) {
23 SkASSERT(src || count == 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
Mike Klein22c1f372018-04-02 20:37:42 +000025 fReserve = fCount = 0;
26 fArray = nullptr;
27 if (count) {
28 fArray = (T*)sk_malloc_throw(count * sizeof(T));
29 memcpy(fArray, src, sizeof(T) * count);
30 fReserve = fCount = count;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 }
Mike Klein22c1f372018-04-02 20:37:42 +000032 }
Mike Reed5edcd312018-08-08 11:23:41 -040033 SkTDArray(const std::initializer_list<T>& list) : SkTDArray(list.begin(), list.size()) {}
Mike Klein22c1f372018-04-02 20:37:42 +000034 SkTDArray(const SkTDArray<T>& src) : fArray(nullptr), fReserve(0), fCount(0) {
35 SkTDArray<T> tmp(src.fArray, src.fCount);
36 this->swap(tmp);
37 }
38 SkTDArray(SkTDArray<T>&& src) : fArray(nullptr), fReserve(0), fCount(0) {
39 this->swap(src);
40 }
41 ~SkTDArray() {
42 sk_free(fArray);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 }
44
Mike Klein22c1f372018-04-02 20:37:42 +000045 SkTDArray<T>& operator=(const SkTDArray<T>& src) {
46 if (this != &src) {
47 if (src.fCount > fReserve) {
48 SkTDArray<T> tmp(src.fArray, src.fCount);
49 this->swap(tmp);
50 } else {
51 sk_careful_memcpy(fArray, src.fArray, sizeof(T) * src.fCount);
52 fCount = src.fCount;
53 }
54 }
55 return *this;
Mike Klein80e1d562018-03-22 11:36:52 -040056 }
Mike Klein22c1f372018-04-02 20:37:42 +000057 SkTDArray<T>& operator=(SkTDArray<T>&& src) {
58 if (this != &src) {
59 this->swap(src);
60 src.reset();
61 }
62 return *this;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 }
64
Mike Klein22c1f372018-04-02 20:37:42 +000065 friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
66 return a.fCount == b.fCount &&
67 (a.fCount == 0 ||
68 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T)));
69 }
70 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) {
71 return !(a == b);
72 }
73
Ben Wagnerf08d1d02018-06-18 15:11:00 -040074 void swap(SkTDArray<T>& that) {
75 using std::swap;
76 swap(fArray, that.fArray);
77 swap(fReserve, that.fReserve);
78 swap(fCount, that.fCount);
Mike Klein22c1f372018-04-02 20:37:42 +000079 }
80
81 bool isEmpty() const { return fCount == 0; }
Mike Reed5edcd312018-08-08 11:23:41 -040082 bool empty() const { return this->isEmpty(); }
Mike Klein22c1f372018-04-02 20:37:42 +000083
84 /**
85 * Return the number of elements in the array
86 */
87 int count() const { return fCount; }
Mike Reed5edcd312018-08-08 11:23:41 -040088 size_t size() const { return fCount; }
Mike Klein22c1f372018-04-02 20:37:42 +000089
90 /**
91 * Return the total number of elements allocated.
92 * reserved() - count() gives you the number of elements you can add
93 * without causing an allocation.
94 */
95 int reserved() const { return fReserve; }
96
97 /**
98 * return the number of bytes in the array: count * sizeof(T)
99 */
100 size_t bytes() const { return fCount * sizeof(T); }
101
Mike Reed5edcd312018-08-08 11:23:41 -0400102 T* begin() { return fArray; }
Mike Klein22c1f372018-04-02 20:37:42 +0000103 const T* begin() const { return fArray; }
Mike Reed5edcd312018-08-08 11:23:41 -0400104 const T* cbegin() const { return fArray; }
105 T* end() { return fArray ? fArray + fCount : nullptr; }
Mike Klein22c1f372018-04-02 20:37:42 +0000106 const T* end() const { return fArray ? fArray + fCount : nullptr; }
Mike Reed5edcd312018-08-08 11:23:41 -0400107 const T* cend() const { return fArray ? fArray + fCount : nullptr; }
Mike Klein22c1f372018-04-02 20:37:42 +0000108
109 T& operator[](int index) {
110 SkASSERT(index < fCount);
111 return fArray[index];
112 }
113 const T& operator[](int index) const {
114 SkASSERT(index < fCount);
115 return fArray[index];
116 }
117
118 T& getAt(int index) {
119 return (*this)[index];
120 }
121 const T& getAt(int index) const {
122 return (*this)[index];
123 }
124
125 void reset() {
126 if (fArray) {
127 sk_free(fArray);
128 fArray = nullptr;
129 fReserve = fCount = 0;
130 } else {
131 SkASSERT(fReserve == 0 && fCount == 0);
132 }
133 }
134
135 void rewind() {
136 // same as setCount(0)
137 fCount = 0;
138 }
139
140 /**
141 * Sets the number of elements in the array.
142 * If the array does not have space for count elements, it will increase
143 * the storage allocated to some amount greater than that required.
144 * It will never shrink the storage.
145 */
146 void setCount(int count) {
147 SkASSERT(count >= 0);
148 if (count > fReserve) {
149 this->resizeStorageToAtLeast(count);
150 }
151 fCount = count;
152 }
153
154 void setReserve(int reserve) {
Mike Klein23e45442018-04-19 09:29:02 -0400155 SkASSERT(reserve >= 0);
Mike Klein22c1f372018-04-02 20:37:42 +0000156 if (reserve > fReserve) {
157 this->resizeStorageToAtLeast(reserve);
158 }
159 }
Mike Reed5edcd312018-08-08 11:23:41 -0400160 void reserve(size_t n) {
161 SkASSERT_RELEASE(SkTFitsIn<int>(n));
162 this->setReserve(SkToInt(n));
163 }
Mike Klein22c1f372018-04-02 20:37:42 +0000164
165 T* prepend() {
166 this->adjustCount(1);
167 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
168 return fArray;
169 }
170
171 T* append() {
172 return this->append(1, nullptr);
173 }
174 T* append(int count, const T* src = nullptr) {
175 int oldCount = fCount;
176 if (count) {
177 SkASSERT(src == nullptr || fArray == nullptr ||
178 src + count <= fArray || fArray + oldCount <= src);
179
180 this->adjustCount(count);
181 if (src) {
182 memcpy(fArray + oldCount, src, sizeof(T) * count);
183 }
184 }
185 return fArray + oldCount;
186 }
187
188 T* appendClear() {
189 T* result = this->append();
190 *result = 0;
191 return result;
192 }
193
194 T* insert(int index) {
195 return this->insert(index, 1, nullptr);
196 }
197 T* insert(int index, int count, const T* src = nullptr) {
198 SkASSERT(count);
199 SkASSERT(index <= fCount);
200 size_t oldCount = fCount;
201 this->adjustCount(count);
202 T* dst = fArray + index;
203 memmove(dst + count, dst, sizeof(T) * (oldCount - index));
204 if (src) {
205 memcpy(dst, src, sizeof(T) * count);
206 }
207 return dst;
208 }
209
210 void remove(int index, int count = 1) {
211 SkASSERT(index + count <= fCount);
212 fCount = fCount - count;
213 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index));
214 }
215
216 void removeShuffle(int index) {
217 SkASSERT(index < fCount);
218 int newCount = fCount - 1;
219 fCount = newCount;
220 if (index != newCount) {
221 memcpy(fArray + index, fArray + newCount, sizeof(T));
222 }
223 }
224
Mike Klein22c1f372018-04-02 20:37:42 +0000225 int find(const T& elem) const {
226 const T* iter = fArray;
227 const T* stop = fArray + fCount;
228
229 for (; iter < stop; iter++) {
230 if (*iter == elem) {
231 return SkToInt(iter - fArray);
232 }
233 }
234 return -1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 }
236
Mike Klein22c1f372018-04-02 20:37:42 +0000237 int rfind(const T& elem) const {
238 const T* iter = fArray + fCount;
239 const T* stop = fArray;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240
Mike Klein22c1f372018-04-02 20:37:42 +0000241 while (iter > stop) {
242 if (*--iter == elem) {
243 return SkToInt(iter - stop);
244 }
245 }
246 return -1;
247 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
Mike Klein22c1f372018-04-02 20:37:42 +0000249 /**
250 * Returns true iff the array contains this element.
251 */
252 bool contains(const T& elem) const {
253 return (this->find(elem) >= 0);
254 }
255
256 /**
257 * Copies up to max elements into dst. The number of items copied is
258 * capped by count - index. The actual number copied is returned.
259 */
260 int copyRange(T* dst, int index, int max) const {
261 SkASSERT(max >= 0);
262 SkASSERT(!max || dst);
263 if (index >= fCount) {
264 return 0;
265 }
266 int count = SkMin32(max, fCount - index);
267 memcpy(dst, fArray + index, sizeof(T) * count);
268 return count;
269 }
270
271 void copy(T* dst) const {
272 this->copyRange(dst, 0, fCount);
273 }
274
275 // routines to treat the array like a stack
Mike Reed5edcd312018-08-08 11:23:41 -0400276 void push_back(const T& v) { *this->append() = v; }
277 T* push() { return this->append(); }
Mike Klein22c1f372018-04-02 20:37:42 +0000278 const T& top() const { return (*this)[fCount - 1]; }
279 T& top() { return (*this)[fCount - 1]; }
280 void pop(T* elem) { SkASSERT(fCount > 0); if (elem) *elem = (*this)[fCount - 1]; --fCount; }
281 void pop() { SkASSERT(fCount > 0); --fCount; }
282
283 void deleteAll() {
284 T* iter = fArray;
285 T* stop = fArray + fCount;
286 while (iter < stop) {
287 delete *iter;
288 iter += 1;
289 }
290 this->reset();
291 }
292
293 void freeAll() {
294 T* iter = fArray;
295 T* stop = fArray + fCount;
296 while (iter < stop) {
297 sk_free(*iter);
298 iter += 1;
299 }
300 this->reset();
301 }
302
303 void unrefAll() {
304 T* iter = fArray;
305 T* stop = fArray + fCount;
306 while (iter < stop) {
307 (*iter)->unref();
308 iter += 1;
309 }
310 this->reset();
311 }
312
313 void safeUnrefAll() {
314 T* iter = fArray;
315 T* stop = fArray + fCount;
316 while (iter < stop) {
317 SkSafeUnref(*iter);
318 iter += 1;
319 }
320 this->reset();
321 }
322
323 void visitAll(void visitor(T&)) {
324 T* stop = this->end();
325 for (T* curr = this->begin(); curr < stop; curr++) {
326 if (*curr) {
327 visitor(*curr);
328 }
329 }
330 }
331
332#ifdef SK_DEBUG
333 void validate() const {
334 SkASSERT((fReserve == 0 && fArray == nullptr) ||
335 (fReserve > 0 && fArray != nullptr));
336 SkASSERT(fCount <= fReserve);
337 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338#endif
339
Mike Klein22c1f372018-04-02 20:37:42 +0000340 void shrinkToFit() {
341 fReserve = fCount;
342 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
343 }
344
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345private:
Mike Klein22c1f372018-04-02 20:37:42 +0000346 T* fArray;
347 int fReserve;
348 int fCount;
349
350 /**
351 * Adjusts the number of elements in the array.
352 * This is the same as calling setCount(count() + delta).
353 */
354 void adjustCount(int delta) {
Mike Klein12ee97c2018-04-19 10:34:08 -0400355 SkASSERT(delta > 0);
356
357 // We take care to avoid overflow here.
358 // The sum of fCount and delta is at most 4294967294, which fits fine in uint32_t.
359 uint32_t count = (uint32_t)fCount + (uint32_t)delta;
360 SkASSERT_RELEASE( SkTFitsIn<int>(count) );
361
362 this->setCount(SkTo<int>(count));
Mike Klein22c1f372018-04-02 20:37:42 +0000363 }
364
365 /**
366 * Increase the storage allocation such that it can hold (fCount + extra)
367 * elements.
368 * It never shrinks the allocation, and it may increase the allocation by
369 * more than is strictly required, based on a private growth heuristic.
370 *
371 * note: does NOT modify fCount
372 */
373 void resizeStorageToAtLeast(int count) {
374 SkASSERT(count > fReserve);
Mike Klein12ee97c2018-04-19 10:34:08 -0400375
376 // We take care to avoid overflow here.
377 // The maximum value we can get for reserve here is 2684354563, which fits in uint32_t.
378 uint32_t reserve = (uint32_t)count + 4;
379 reserve += reserve / 4;
380 SkASSERT_RELEASE( SkTFitsIn<int>(reserve) );
381
382 fReserve = SkTo<int>(reserve);
Mike Klein22c1f372018-04-02 20:37:42 +0000383 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
384 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385};
386
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400387template <typename T> static inline void swap(SkTDArray<T>& a, SkTDArray<T>& b) {
388 a.swap(b);
389}
390
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391#endif