blob: 4c90460b947f25809e0c21084fff7b924c3c78e1 [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:
17 SkTDArray() {
18 fReserve = fCount = 0;
19 fArray = NULL;
20#ifdef SK_DEBUG
21 fData = NULL;
22#endif
23 }
robertphillips@google.come9cd27d2013-10-16 17:48:11 +000024 SkTDArray(const T src[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 SkASSERT(src || count == 0);
26
27 fReserve = fCount = 0;
28 fArray = NULL;
29#ifdef SK_DEBUG
30 fData = NULL;
31#endif
32 if (count) {
33 fArray = (T*)sk_malloc_throw(count * sizeof(T));
34#ifdef SK_DEBUG
35 fData = (ArrayT*)fArray;
36#endif
37 memcpy(fArray, src, sizeof(T) * count);
38 fReserve = fCount = count;
39 }
40 }
41 SkTDArray(const SkTDArray<T>& src) {
42 fReserve = fCount = 0;
43 fArray = NULL;
44#ifdef SK_DEBUG
45 fData = NULL;
46#endif
47 SkTDArray<T> tmp(src.fArray, src.fCount);
48 this->swap(tmp);
49 }
50 ~SkTDArray() {
51 sk_free(fArray);
52 }
53
54 SkTDArray<T>& operator=(const SkTDArray<T>& src) {
55 if (this != &src) {
56 if (src.fCount > fReserve) {
57 SkTDArray<T> tmp(src.fArray, src.fCount);
58 this->swap(tmp);
59 } else {
60 memcpy(fArray, src.fArray, sizeof(T) * src.fCount);
61 fCount = src.fCount;
62 }
63 }
64 return *this;
65 }
66
reed@google.comb530ef52011-07-20 19:55:42 +000067 friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 return a.fCount == b.fCount &&
69 (a.fCount == 0 ||
70 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T)));
71 }
reed@google.com3467ee02013-05-29 18:05:52 +000072 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) {
73 return !(a == b);
74 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
76 void swap(SkTDArray<T>& other) {
77 SkTSwap(fArray, other.fArray);
78#ifdef SK_DEBUG
79 SkTSwap(fData, other.fData);
80#endif
81 SkTSwap(fReserve, other.fReserve);
82 SkTSwap(fCount, other.fCount);
83 }
84
reed@android.com0da41db2009-08-25 16:03:59 +000085 /** Return a ptr to the array of data, to be freed with sk_free. This also
86 resets the SkTDArray to be empty.
87 */
88 T* detach() {
89 T* array = fArray;
90 fArray = NULL;
91 fReserve = fCount = 0;
92 SkDEBUGCODE(fData = NULL;)
93 return array;
94 }
95
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 bool isEmpty() const { return fCount == 0; }
reed@google.com1271d782011-11-28 19:54:12 +000097
98 /**
99 * Return the number of elements in the array
100 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000101 int count() const { return fCount; }
reed@google.com1271d782011-11-28 19:54:12 +0000102
103 /**
commit-bot@chromium.org046f1f62014-02-11 10:17:02 +0000104 * Return the total number of elements allocated.
105 * reserved() - count() gives you the number of elements you can add
106 * without causing an allocation.
107 */
108 int reserved() const { return fReserve; }
109
110 /**
reed@google.com1271d782011-11-28 19:54:12 +0000111 * return the number of bytes in the array: count * sizeof(T)
112 */
113 size_t bytes() const { return fCount * sizeof(T); }
114
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000115 T* begin() { return fArray; }
116 const T* begin() const { return fArray; }
117 T* end() { return fArray ? fArray + fCount : NULL; }
118 const T* end() const { return fArray ? fArray + fCount : NULL; }
119
120 T& operator[](int index) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000121 SkASSERT(index < fCount);
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000122 return fArray[index];
123 }
124 const T& operator[](int index) const {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000125 SkASSERT(index < fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 return fArray[index];
127 }
skia.committer@gmail.com7fc0e0a2013-01-15 02:01:40 +0000128
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000129 T& getAt(int index) {
130 return (*this)[index];
131 }
132 const T& getAt(int index) const {
humper@google.com7af56be2013-01-14 18:49:19 +0000133 return (*this)[index];
134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
136 void reset() {
137 if (fArray) {
138 sk_free(fArray);
139 fArray = NULL;
140#ifdef SK_DEBUG
141 fData = NULL;
142#endif
143 fReserve = fCount = 0;
144 } else {
145 SkASSERT(fReserve == 0 && fCount == 0);
146 }
147 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 void rewind() {
150 // same as setCount(0)
151 fCount = 0;
152 }
153
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000154 /**
155 * Sets the number of elements in the array.
156 * If the array does not have space for count elements, it will increase
157 * the storage allocated to some amount greater than that required.
158 * It will never shrink the shrink the storage.
159 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000160 void setCount(int count) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000161 SkASSERT(count >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 if (count > fReserve) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000163 this->resizeStorageToAtLeast(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 }
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000165 fCount = count;
166 }
167
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000168 void setReserve(int reserve) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 if (reserve > fReserve) {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000170 this->resizeStorageToAtLeast(reserve);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 }
172 }
173
174 T* prepend() {
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000175 this->adjustCount(1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
177 return fArray;
178 }
179
180 T* append() {
181 return this->append(1, NULL);
182 }
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000183 T* append(int count, const T* src = NULL) {
184 int oldCount = fCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 if (count) {
186 SkASSERT(src == NULL || fArray == NULL ||
187 src + count <= fArray || fArray + oldCount <= src);
188
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000189 this->adjustCount(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 if (src) {
191 memcpy(fArray + oldCount, src, sizeof(T) * count);
192 }
193 }
194 return fArray + oldCount;
195 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 T* appendClear() {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198 T* result = this->append();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 *result = 0;
200 return result;
201 }
202
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000203 T* insert(int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 return this->insert(index, 1, NULL);
205 }
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000206 T* insert(int index, int count, const T* src = NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 SkASSERT(count);
208 SkASSERT(index <= fCount);
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000209 size_t oldCount = fCount;
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000210 this->adjustCount(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 T* dst = fArray + index;
212 memmove(dst + count, dst, sizeof(T) * (oldCount - index));
213 if (src) {
214 memcpy(dst, src, sizeof(T) * count);
215 }
216 return dst;
217 }
218
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000219 void remove(int index, int count = 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 SkASSERT(index + count <= fCount);
221 fCount = fCount - count;
222 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index));
223 }
224
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000225 void removeShuffle(int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 SkASSERT(index < fCount);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000227 int newCount = fCount - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 fCount = newCount;
229 if (index != newCount) {
230 memcpy(fArray + index, fArray + newCount, sizeof(T));
231 }
232 }
233
234 int find(const T& elem) const {
235 const T* iter = fArray;
236 const T* stop = fArray + fCount;
237
238 for (; iter < stop; iter++) {
239 if (*iter == elem) {
240 return (int) (iter - fArray);
241 }
242 }
243 return -1;
244 }
245
246 int rfind(const T& elem) const {
247 const T* iter = fArray + fCount;
248 const T* stop = fArray;
249
250 while (iter > stop) {
251 if (*--iter == elem) {
reed@google.com6fcd28b2014-02-04 16:03:51 +0000252 return SkToInt(iter - stop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 }
254 }
255 return -1;
256 }
257
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000258 /**
epoger@google.comaf07d062012-07-13 14:53:18 +0000259 * Returns true iff the array contains this element.
260 */
261 bool contains(const T& elem) const {
262 return (this->find(elem) >= 0);
263 }
264
265 /**
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000266 * Copies up to max elements into dst. The number of items copied is
267 * capped by count - index. The actual number copied is returned.
268 */
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000269 int copyRange(T* dst, int index, int max) const {
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000270 SkASSERT(max >= 0);
271 SkASSERT(!max || dst);
272 if (index >= fCount) {
273 return 0;
274 }
275 int count = SkMin32(max, fCount - index);
276 memcpy(dst, fArray + index, sizeof(T) * count);
277 return count;
278 }
279
280 void copy(T* dst) const {
zachr@google.comc6081ab2013-07-01 17:04:32 +0000281 this->copyRange(dst, 0, fCount);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000282 }
283
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 // routines to treat the array like a stack
285 T* push() { return this->append(); }
286 void push(const T& elem) { *this->append() = elem; }
287 const T& top() const { return (*this)[fCount - 1]; }
288 T& top() { return (*this)[fCount - 1]; }
289 void pop(T* elem) { if (elem) *elem = (*this)[fCount - 1]; --fCount; }
290 void pop() { --fCount; }
291
292 void deleteAll() {
293 T* iter = fArray;
294 T* stop = fArray + fCount;
295 while (iter < stop) {
scroggoc51db022012-08-16 20:30:18 +0000296 SkDELETE (*iter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 iter += 1;
298 }
299 this->reset();
300 }
301
302 void freeAll() {
303 T* iter = fArray;
304 T* stop = fArray + fCount;
305 while (iter < stop) {
306 sk_free(*iter);
307 iter += 1;
308 }
309 this->reset();
310 }
311
312 void unrefAll() {
313 T* iter = fArray;
314 T* stop = fArray + fCount;
315 while (iter < stop) {
316 (*iter)->unref();
317 iter += 1;
318 }
319 this->reset();
320 }
reed@android.com8433b5d2009-07-03 02:52:27 +0000321
322 void safeUnrefAll() {
323 T* iter = fArray;
324 T* stop = fArray + fCount;
325 while (iter < stop) {
326 SkSafeUnref(*iter);
327 iter += 1;
328 }
329 this->reset();
330 }
331
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +0000332 void visitAll(void visitor(T&)) {
bsalomon@google.com21cbec42013-01-07 17:23:00 +0000333 T* stop = this->end();
334 for (T* curr = this->begin(); curr < stop; curr++) {
335 if (*curr) {
336 visitor(*curr);
337 }
338 }
339 }
340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341#ifdef SK_DEBUG
342 void validate() const {
343 SkASSERT((fReserve == 0 && fArray == NULL) ||
344 (fReserve > 0 && fArray != NULL));
345 SkASSERT(fCount <= fReserve);
346 SkASSERT(fData == (ArrayT*)fArray);
347 }
348#endif
349
350private:
351#ifdef SK_DEBUG
352 enum {
353 kDebugArraySize = 16
354 };
355 typedef T ArrayT[kDebugArraySize];
356 ArrayT* fData;
357#endif
358 T* fArray;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000359 int fReserve;
360 int fCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000362 /**
363 * Adjusts the number of elements in the array.
364 * This is the same as calling setCount(count() + delta).
365 */
366 void adjustCount(int delta) {
367 this->setCount(fCount + delta);
368 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000370 /**
commit-bot@chromium.orga87b21c2014-02-11 18:22:04 +0000371 * Increase the storage allocation such that it can hold (fCount + extra)
372 * elements.
373 * It never shrinks the allocation, and it may increase the allocation by
374 * more than is strictly required, based on a private growth heuristic.
375 *
376 * note: does NOT modify fCount
377 */
378 void resizeStorageToAtLeast(int count) {
379 SkASSERT(count > fReserve);
commit-bot@chromium.orgca21a002014-02-13 18:35:54 +0000380 fReserve = count + 4;
381 fReserve += fReserve / 4;
382 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
383#ifdef SK_DEBUG
384 fData = (ArrayT*)fArray;
385#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 }
387};
388
389#endif