blob: d6ef3a3834141d4d1ee14021617926169fdfbe4b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkTDArray_DEFINED
11#define SkTDArray_DEFINED
12
13#include "SkTypes.h"
14
ehsan.akhgaridb5f7bf2014-07-09 11:13:55 -070015template <typename T> class SkTDArray {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016public:
halcanary9be37202016-08-08 07:21:42 -070017 SkTDArray() : fArray(nullptr), fReserve(0), fCount(0) {}
robertphillips@google.come9cd27d2013-10-16 17:48:11 +000018 SkTDArray(const T src[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 SkASSERT(src || count == 0);
20
21 fReserve = fCount = 0;
22 fArray = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 if (count) {
24 fArray = (T*)sk_malloc_throw(count * sizeof(T));
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 memcpy(fArray, src, sizeof(T) * count);
26 fReserve = fCount = count;
27 }
28 }
halcanary9be37202016-08-08 07:21:42 -070029 SkTDArray(const SkTDArray<T>& src) : fArray(nullptr), fReserve(0), fCount(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 SkTDArray<T> tmp(src.fArray, src.fCount);
31 this->swap(tmp);
32 }
halcanary9be37202016-08-08 07:21:42 -070033 SkTDArray(SkTDArray<T>&& src) : fArray(nullptr), fReserve(0), fCount(0) {
34 this->swap(src);
35 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 ~SkTDArray() {
37 sk_free(fArray);
38 }
39
40 SkTDArray<T>& operator=(const SkTDArray<T>& src) {
41 if (this != &src) {
42 if (src.fCount > fReserve) {
43 SkTDArray<T> tmp(src.fArray, src.fCount);
44 this->swap(tmp);
45 } else {
mtkleincc881da2015-12-08 11:55:17 -080046 sk_careful_memcpy(fArray, src.fArray, sizeof(T) * src.fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 fCount = src.fCount;
48 }
49 }
50 return *this;
51 }
halcanary9be37202016-08-08 07:21:42 -070052 SkTDArray<T>& operator=(SkTDArray<T>&& src) {
53 if (this != &src) {
54 this->swap(src);
55 src.reset();
56 }
57 return *this;
58 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
reed@google.comb530ef52011-07-20 19:55:42 +000060 friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 return a.fCount == b.fCount &&
62 (a.fCount == 0 ||
63 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T)));
64 }
reed@google.com3467ee02013-05-29 18:05:52 +000065 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) {
66 return !(a == b);
67 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
69 void swap(SkTDArray<T>& other) {
70 SkTSwap(fArray, other.fArray);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkTSwap(fReserve, other.fReserve);
72 SkTSwap(fCount, other.fCount);
73 }
74
reed@android.com0da41db2009-08-25 16:03:59 +000075 /** Return a ptr to the array of data, to be freed with sk_free. This also
76 resets the SkTDArray to be empty.
77 */
mtklein18300a32016-03-16 13:53:35 -070078 T* release() {
reed@android.com0da41db2009-08-25 16:03:59 +000079 T* array = fArray;
80 fArray = NULL;
81 fReserve = fCount = 0;
reed@android.com0da41db2009-08-25 16:03:59 +000082 return array;
83 }
84
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 bool isEmpty() const { return fCount == 0; }
reed@google.com1271d782011-11-28 19:54:12 +000086
87 /**
88 * Return the number of elements in the array
89 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +000090 int count() const { return fCount; }
reed@google.com1271d782011-11-28 19:54:12 +000091
92 /**
commit-bot@chromium.org046f1f62014-02-11 10:17:02 +000093 * Return the total number of elements allocated.
94 * reserved() - count() gives you the number of elements you can add
95 * without causing an allocation.
96 */
97 int reserved() const { return fReserve; }
98
99 /**
reed@google.com1271d782011-11-28 19:54:12 +0000100 * return the number of bytes in the array: count * sizeof(T)
101 */
102 size_t bytes() const { return fCount * sizeof(T); }
103
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000104 T* begin() { return fArray; }
105 const T* begin() const { return fArray; }
106 T* end() { return fArray ? fArray + fCount : NULL; }
107 const T* end() const { return fArray ? fArray + fCount : NULL; }
108
109 T& operator[](int index) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000110 SkASSERT(index < fCount);
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000111 return fArray[index];
112 }
113 const T& operator[](int index) const {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000114 SkASSERT(index < fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 return fArray[index];
116 }
skia.committer@gmail.com7fc0e0a2013-01-15 02:01:40 +0000117
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000118 T& getAt(int index) {
119 return (*this)[index];
120 }
121 const T& getAt(int index) const {
humper@google.com7af56be2013-01-14 18:49:19 +0000122 return (*this)[index];
123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
125 void reset() {
126 if (fArray) {
127 sk_free(fArray);
128 fArray = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 fReserve = fCount = 0;
130 } else {
131 SkASSERT(fReserve == 0 && fCount == 0);
132 }
133 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 void rewind() {
136 // same as setCount(0)
137 fCount = 0;
138 }
139
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000140 /**
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.
robertphillips91df6c22015-04-24 11:10:51 -0700144 * It will never shrink the storage.
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000145 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000146 void setCount(int count) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000147 SkASSERT(count >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 if (count > fReserve) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000149 this->resizeStorageToAtLeast(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 }
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000151 fCount = count;
152 }
153
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000154 void setReserve(int reserve) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 if (reserve > fReserve) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000156 this->resizeStorageToAtLeast(reserve);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 }
158 }
159
160 T* prepend() {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000161 this->adjustCount(1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
163 return fArray;
164 }
165
166 T* append() {
167 return this->append(1, NULL);
168 }
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000169 T* append(int count, const T* src = NULL) {
170 int oldCount = fCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 if (count) {
172 SkASSERT(src == NULL || fArray == NULL ||
173 src + count <= fArray || fArray + oldCount <= src);
174
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000175 this->adjustCount(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 if (src) {
177 memcpy(fArray + oldCount, src, sizeof(T) * count);
178 }
179 }
180 return fArray + oldCount;
181 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 T* appendClear() {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000184 T* result = this->append();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 *result = 0;
186 return result;
187 }
188
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000189 T* insert(int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 return this->insert(index, 1, NULL);
191 }
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000192 T* insert(int index, int count, const T* src = NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 SkASSERT(count);
194 SkASSERT(index <= fCount);
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000195 size_t oldCount = fCount;
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000196 this->adjustCount(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 T* dst = fArray + index;
198 memmove(dst + count, dst, sizeof(T) * (oldCount - index));
199 if (src) {
200 memcpy(dst, src, sizeof(T) * count);
201 }
202 return dst;
203 }
204
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000205 void remove(int index, int count = 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 SkASSERT(index + count <= fCount);
207 fCount = fCount - count;
208 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index));
209 }
210
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000211 void removeShuffle(int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 SkASSERT(index < fCount);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000213 int newCount = fCount - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 fCount = newCount;
215 if (index != newCount) {
216 memcpy(fArray + index, fArray + newCount, sizeof(T));
217 }
218 }
219
220 int find(const T& elem) const {
221 const T* iter = fArray;
222 const T* stop = fArray + fCount;
223
224 for (; iter < stop; iter++) {
225 if (*iter == elem) {
fmalitaf89f60f2015-02-13 08:55:24 -0800226 return SkToInt(iter - fArray);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 }
228 }
229 return -1;
230 }
231
232 int rfind(const T& elem) const {
233 const T* iter = fArray + fCount;
234 const T* stop = fArray;
235
236 while (iter > stop) {
237 if (*--iter == elem) {
reed@google.com6fcd28b2014-02-04 16:03:51 +0000238 return SkToInt(iter - stop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 }
240 }
241 return -1;
242 }
243
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000244 /**
epoger@google.comaf07d062012-07-13 14:53:18 +0000245 * Returns true iff the array contains this element.
246 */
247 bool contains(const T& elem) const {
248 return (this->find(elem) >= 0);
249 }
250
251 /**
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000252 * Copies up to max elements into dst. The number of items copied is
253 * capped by count - index. The actual number copied is returned.
254 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000255 int copyRange(T* dst, int index, int max) const {
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000256 SkASSERT(max >= 0);
257 SkASSERT(!max || dst);
258 if (index >= fCount) {
259 return 0;
260 }
261 int count = SkMin32(max, fCount - index);
262 memcpy(dst, fArray + index, sizeof(T) * count);
263 return count;
264 }
265
266 void copy(T* dst) const {
zachr@google.comc6081ab2013-07-01 17:04:32 +0000267 this->copyRange(dst, 0, fCount);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000268 }
269
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 // routines to treat the array like a stack
mtkleinaa90d002014-09-11 06:36:11 -0700271 T* push() { return this->append(); }
272 void push(const T& elem) { *this->append() = elem; }
273 const T& top() const { return (*this)[fCount - 1]; }
274 T& top() { return (*this)[fCount - 1]; }
275 void pop(T* elem) { SkASSERT(fCount > 0); if (elem) *elem = (*this)[fCount - 1]; --fCount; }
276 void pop() { SkASSERT(fCount > 0); --fCount; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277
278 void deleteAll() {
279 T* iter = fArray;
280 T* stop = fArray + fCount;
281 while (iter < stop) {
halcanary385fe4d2015-08-26 13:07:48 -0700282 delete *iter;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 iter += 1;
284 }
285 this->reset();
286 }
287
288 void freeAll() {
289 T* iter = fArray;
290 T* stop = fArray + fCount;
291 while (iter < stop) {
292 sk_free(*iter);
293 iter += 1;
294 }
295 this->reset();
296 }
297
298 void unrefAll() {
299 T* iter = fArray;
300 T* stop = fArray + fCount;
301 while (iter < stop) {
302 (*iter)->unref();
303 iter += 1;
304 }
305 this->reset();
306 }
reed@android.com8433b5d2009-07-03 02:52:27 +0000307
308 void safeUnrefAll() {
309 T* iter = fArray;
310 T* stop = fArray + fCount;
311 while (iter < stop) {
312 SkSafeUnref(*iter);
313 iter += 1;
314 }
315 this->reset();
316 }
317
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000318 void visitAll(void visitor(T&)) {
bsalomon@google.com21cbec42013-01-07 17:23:00 +0000319 T* stop = this->end();
320 for (T* curr = this->begin(); curr < stop; curr++) {
321 if (*curr) {
322 visitor(*curr);
323 }
324 }
325 }
326
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327#ifdef SK_DEBUG
328 void validate() const {
329 SkASSERT((fReserve == 0 && fArray == NULL) ||
330 (fReserve > 0 && fArray != NULL));
331 SkASSERT(fCount <= fReserve);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 }
333#endif
334
mtklein092dab92014-10-09 18:22:41 -0700335 void shrinkToFit() {
336 fReserve = fCount;
337 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
338 }
339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 T* fArray;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000342 int fReserve;
343 int fCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000345 /**
346 * Adjusts the number of elements in the array.
347 * This is the same as calling setCount(count() + delta).
348 */
349 void adjustCount(int delta) {
350 this->setCount(fCount + delta);
351 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000353 /**
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000354 * Increase the storage allocation such that it can hold (fCount + extra)
355 * elements.
356 * It never shrinks the allocation, and it may increase the allocation by
357 * more than is strictly required, based on a private growth heuristic.
358 *
359 * note: does NOT modify fCount
360 */
361 void resizeStorageToAtLeast(int count) {
362 SkASSERT(count > fReserve);
commit-bot@chromium.orgca21a002014-02-13 18:35:54 +0000363 fReserve = count + 4;
364 fReserve += fReserve / 4;
365 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 }
367};
368
369#endif