blob: 1fe2c3857c1555c58efe20303709dbde649ee868 [file] [log] [blame]
bsalomon@google.com49313f62011-09-14 13:54:05 +00001/*
2 * Copyright 2011 Google Inc.
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
bsalomon@google.com49313f62011-09-14 13:54:05 +00008#ifndef SkTArray_DEFINED
9#define SkTArray_DEFINED
10
bungeman918090c2016-02-09 09:14:28 -080011#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "../private/SkTemplates.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000013#include "SkTypes.h"
bungemanf3c15b72015-08-19 11:56:48 -070014
15#include <new>
bungeman221524d2016-01-05 14:59:40 -080016#include <utility>
bsalomon@google.com49313f62011-09-14 13:54:05 +000017
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000018/** When MEM_COPY is true T will be bit copied when moved.
19 When MEM_COPY is false, T will be copy constructed / destructed.
bungeman@google.comd58a8562014-03-24 15:55:01 +000020 In all cases T will be default-initialized on allocation,
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000021 and its destructor will be called from this object's destructor.
22*/
bungeman85dc3592016-02-09 11:32:56 -080023template <typename T, bool MEM_COPY = false> class SkTArray {
bsalomon@google.com49313f62011-09-14 13:54:05 +000024public:
25 /**
26 * Creates an empty array with no initial storage
27 */
28 SkTArray() {
29 fCount = 0;
30 fReserveCount = gMIN_ALLOC_COUNT;
31 fAllocCount = 0;
32 fMemArray = NULL;
33 fPreAllocMemArray = NULL;
34 }
35
36 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000038 * elements.
39 */
40 explicit SkTArray(int reserveCount) {
bungeman0d9e9be2016-04-20 10:22:20 -070041 this->init(0, NULL, reserveCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +000042 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
bsalomon@google.com49313f62011-09-14 13:54:05 +000044 /**
45 * Copies one array to another. The new array will be heap allocated.
46 */
bungeman0d9e9be2016-04-20 10:22:20 -070047 explicit SkTArray(const SkTArray& that) {
48 this->init(that.fCount, NULL, 0);
49 this->copy(that.fItemArray);
50 }
51 explicit SkTArray(SkTArray&& that) {
52 this->init(that.fCount, NULL, 0);
53 that.move(fMemArray);
54 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000055 }
56
57 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000059 * array will be heap allocated. Be careful not to use this constructor
60 * when you really want the (void*, int) version.
61 */
62 SkTArray(const T* array, int count) {
bungeman0d9e9be2016-04-20 10:22:20 -070063 this->init(count, NULL, 0);
64 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000065 }
66
67 /**
68 * assign copy of array to this
69 */
bungeman0d9e9be2016-04-20 10:22:20 -070070 SkTArray& operator =(const SkTArray& that) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000071 for (int i = 0; i < fCount; ++i) {
72 fItemArray[i].~T();
73 }
74 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070075 this->checkRealloc(that.count());
76 fCount = that.count();
77 this->copy(that.fItemArray);
78 return *this;
79 }
80 SkTArray& operator =(SkTArray&& that) {
81 for (int i = 0; i < fCount; ++i) {
82 fItemArray[i].~T();
83 }
84 fCount = 0;
85 this->checkRealloc(that.count());
86 fCount = that.count();
87 that.move(fMemArray);
88 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000089 return *this;
90 }
91
bsalomon383ff102015-07-31 11:53:11 -070092 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000093 for (int i = 0; i < fCount; ++i) {
94 fItemArray[i].~T();
95 }
96 if (fMemArray != fPreAllocMemArray) {
97 sk_free(fMemArray);
98 }
99 }
100
101 /**
102 * Resets to count() == 0
103 */
104 void reset() { this->pop_back_n(fCount); }
105
106 /**
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000107 * Resets to count() = n newly constructed T objects.
108 */
109 void reset(int n) {
110 SkASSERT(n >= 0);
111 for (int i = 0; i < fCount; ++i) {
112 fItemArray[i].~T();
113 }
bungeman0d9e9be2016-04-20 10:22:20 -0700114 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000115 fCount = 0;
116 this->checkRealloc(n);
117 fCount = n;
118 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700119 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000120 }
121 }
122
123 /**
bungeman06ca8ec2016-06-09 08:01:03 -0700124 * Ensures there is enough reserved space for n elements.
125 */
126 void reserve(int n) {
127 if (fCount < n) {
128 this->checkRealloc(n - fCount);
129 }
130 }
131
132 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000133 * Resets to a copy of a C array.
134 */
135 void reset(const T* array, int count) {
136 for (int i = 0; i < fCount; ++i) {
137 fItemArray[i].~T();
138 }
bungeman0d9e9be2016-04-20 10:22:20 -0700139 fCount = 0;
140 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000141 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800142 this->copy(array);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000143 }
144
145 void removeShuffle(int n) {
146 SkASSERT(n < fCount);
147 int newCount = fCount - 1;
148 fCount = newCount;
149 fItemArray[n].~T();
150 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800151 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000152 }
153 }
154
155 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000156 * Number of elements in the array.
157 */
158 int count() const { return fCount; }
159
160 /**
161 * Is the array empty.
162 */
163 bool empty() const { return !fCount; }
164
165 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000166 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000167 * the reference only remains valid until the next call that adds or removes
168 * elements.
169 */
170 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700171 void* newT = this->push_back_raw(1);
172 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000173 }
174
175 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000176 * Version of above that uses a copy constructor to initialize the new item
177 */
178 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700179 void* newT = this->push_back_raw(1);
180 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000181 }
182
183 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800184 * Version of above that uses a move constructor to initialize the new item
185 */
186 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700187 void* newT = this->push_back_raw(1);
188 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800189 }
190
191 /**
halcanaryf12a1672015-09-23 12:45:49 -0700192 * Construct a new T at the back of this array.
193 */
194 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700195 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800196 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700197 }
198
199 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000200 * Allocates n more default-initialized T values, and returns the address of
201 * the start of that new range. Note: this address is only valid until the
202 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000203 */
204 T* push_back_n(int n) {
205 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700206 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000207 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700208 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000209 }
bungeman6a6f3c52016-04-21 10:52:03 -0700210 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000211 }
212
213 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000214 * Version of above that uses a copy constructor to initialize all n items
215 * to the same T.
216 */
217 T* push_back_n(int n, const T& t) {
218 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700219 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000220 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700221 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000222 }
bungeman6a6f3c52016-04-21 10:52:03 -0700223 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000224 }
225
226 /**
227 * Version of above that uses a copy constructor to initialize the n items
228 * to separate T values.
229 */
230 T* push_back_n(int n, const T t[]) {
231 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000232 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000233 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700234 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000235 }
236 fCount += n;
237 return fItemArray + fCount - n;
238 }
239
240 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000241 * Removes the last element. Not safe to call when count() == 0.
242 */
243 void pop_back() {
244 SkASSERT(fCount > 0);
245 --fCount;
246 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000247 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000248 }
249
250 /**
251 * Removes the last n elements. Not safe to call when count() < n.
252 */
253 void pop_back_n(int n) {
254 SkASSERT(n >= 0);
255 SkASSERT(fCount >= n);
256 fCount -= n;
257 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000258 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000259 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000260 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000261 }
262
263 /**
264 * Pushes or pops from the back to resize. Pushes will be default
265 * initialized.
266 */
267 void resize_back(int newCount) {
268 SkASSERT(newCount >= 0);
269
270 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000271 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000272 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000273 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000274 }
275 }
276
bsalomon23e619c2015-02-06 11:54:28 -0800277 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
278 otherwise copies the T values. */
279 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800280 if (this == that) {
281 return;
282 }
bsalomon23e619c2015-02-06 11:54:28 -0800283 if (this->fPreAllocMemArray != this->fItemArray &&
284 that->fPreAllocMemArray != that->fItemArray) {
285 // If neither is using a preallocated array then just swap.
286 SkTSwap(fItemArray, that->fItemArray);
287 SkTSwap(fCount, that->fCount);
288 SkTSwap(fAllocCount, that->fAllocCount);
289 } else {
290 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700291 SkTArray copy(std::move(*that));
292 *that = std::move(*this);
293 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800294 }
295 }
296
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000297 T* begin() {
298 return fItemArray;
299 }
300 const T* begin() const {
301 return fItemArray;
302 }
303 T* end() {
304 return fItemArray ? fItemArray + fCount : NULL;
305 }
306 const T* end() const {
tfarina567ff2f2015-04-27 07:01:44 -0700307 return fItemArray ? fItemArray + fCount : NULL;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000308 }
309
310 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000311 * Get the i^th element.
312 */
313 T& operator[] (int i) {
314 SkASSERT(i < fCount);
315 SkASSERT(i >= 0);
316 return fItemArray[i];
317 }
318
319 const T& operator[] (int i) const {
320 SkASSERT(i < fCount);
321 SkASSERT(i >= 0);
322 return fItemArray[i];
323 }
324
325 /**
326 * equivalent to operator[](0)
327 */
328 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
329
330 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
331
332 /**
333 * equivalent to operator[](count() - 1)
334 */
335 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
336
337 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
338
339 /**
340 * equivalent to operator[](count()-1-i)
341 */
342 T& fromBack(int i) {
343 SkASSERT(i >= 0);
344 SkASSERT(i < fCount);
345 return fItemArray[fCount - i - 1];
346 }
347
348 const T& fromBack(int i) const {
349 SkASSERT(i >= 0);
350 SkASSERT(i < fCount);
351 return fItemArray[fCount - i - 1];
352 }
353
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000354 bool operator==(const SkTArray<T, MEM_COPY>& right) const {
355 int leftCount = this->count();
356 if (leftCount != right.count()) {
357 return false;
358 }
359 for (int index = 0; index < leftCount; ++index) {
360 if (fItemArray[index] != right.fItemArray[index]) {
361 return false;
362 }
363 }
364 return true;
365 }
366
367 bool operator!=(const SkTArray<T, MEM_COPY>& right) const {
368 return !(*this == right);
369 }
370
bsalomon@google.com92669012011-09-27 19:10:05 +0000371protected:
372 /**
373 * Creates an empty array that will use the passed storage block until it
374 * is insufficiently large to hold the entire array.
375 */
376 template <int N>
377 SkTArray(SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700378 this->init(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000379 }
380
381 /**
382 * Copy another array, using preallocated storage if preAllocCount >=
383 * array.count(). Otherwise storage will only be used when array shrinks
384 * to fit.
385 */
386 template <int N>
387 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700388 this->init(array.fCount, storage->get(), N);
389 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000390 }
391
392 /**
393 * Copy a C array, using preallocated storage if preAllocCount >=
394 * count. Otherwise storage will only be used when array shrinks
395 * to fit.
396 */
397 template <int N>
398 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700399 this->init(count, storage->get(), N);
400 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000401 }
402
bungeman0d9e9be2016-04-20 10:22:20 -0700403 void init(int count, void* preAllocStorage, int preAllocOrReserveCount) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000404 SkASSERT(count >= 0);
405 SkASSERT(preAllocOrReserveCount >= 0);
bsalomon@google.com92669012011-09-27 19:10:05 +0000406 fCount = count;
407 fReserveCount = (preAllocOrReserveCount > 0) ?
408 preAllocOrReserveCount :
409 gMIN_ALLOC_COUNT;
410 fPreAllocMemArray = preAllocStorage;
411 if (fReserveCount >= fCount &&
bsalomon49f085d2014-09-05 13:34:00 -0700412 preAllocStorage) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000413 fAllocCount = fReserveCount;
414 fMemArray = preAllocStorage;
415 } else {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000416 fAllocCount = SkMax32(fCount, fReserveCount);
417 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
bsalomon@google.com92669012011-09-27 19:10:05 +0000418 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000419 }
420
bsalomon@google.com49313f62011-09-14 13:54:05 +0000421private:
bungeman918090c2016-02-09 09:14:28 -0800422 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
423 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
424 */
425 template <bool E = MEM_COPY> SK_WHEN(E, void) copy(const T* src) {
426 sk_careful_memcpy(fMemArray, src, fCount * sizeof(T));
427 }
428 template <bool E = MEM_COPY> SK_WHEN(E, void) move(int dst, int src) {
429 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
430 }
bungeman0d9e9be2016-04-20 10:22:20 -0700431 template <bool E = MEM_COPY> SK_WHEN(E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800432 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
433 }
434
435 template <bool E = MEM_COPY> SK_WHEN(!E, void) copy(const T* src) {
436 for (int i = 0; i < fCount; ++i) {
437 new (fItemArray + i) T(src[i]);
438 }
439 }
440 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(int dst, int src) {
441 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
442 fItemArray[src].~T();
443 }
bungeman0d9e9be2016-04-20 10:22:20 -0700444 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800445 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700446 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800447 fItemArray[i].~T();
448 }
449 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000450
451 static const int gMIN_ALLOC_COUNT = 8;
452
bsalomon@google.comd5104142013-06-13 15:13:46 +0000453 // Helper function that makes space for n objects, adjusts the count, but does not initialize
454 // the new objects.
455 void* push_back_raw(int n) {
456 this->checkRealloc(n);
457 void* ptr = fItemArray + fCount;
458 fCount += n;
459 return ptr;
460 }
461
bsalomon@google.com49313f62011-09-14 13:54:05 +0000462 inline void checkRealloc(int delta) {
463 SkASSERT(fCount >= 0);
464 SkASSERT(fAllocCount >= 0);
465
466 SkASSERT(-delta <= fCount);
467
468 int newCount = fCount + delta;
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000469 int newAllocCount = fAllocCount;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000470
bsalomon@google.com137209f2012-08-14 17:19:08 +0000471 if (newCount > fAllocCount || newCount < (fAllocCount / 3)) {
472 // whether we're growing or shrinking, we leave at least 50% extra space for future
473 // growth (clamped to the reserve count).
474 newAllocCount = SkMax32(newCount + ((newCount + 1) >> 1), fReserveCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000475 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000476 if (newAllocCount != fAllocCount) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000477
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000478 fAllocCount = newAllocCount;
bungeman0d9e9be2016-04-20 10:22:20 -0700479 void* newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000480
bsalomon49f085d2014-09-05 13:34:00 -0700481 if (fAllocCount == fReserveCount && fPreAllocMemArray) {
bungeman0d9e9be2016-04-20 10:22:20 -0700482 newMemArray = fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000483 } else {
bungeman0d9e9be2016-04-20 10:22:20 -0700484 newMemArray = sk_malloc_throw(fAllocCount*sizeof(T));
bsalomon@google.com49313f62011-09-14 13:54:05 +0000485 }
486
bungeman918090c2016-02-09 09:14:28 -0800487 this->move(newMemArray);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000488
489 if (fMemArray != fPreAllocMemArray) {
490 sk_free(fMemArray);
491 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000492 fMemArray = newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000493 }
494 }
495
bsalomon23e619c2015-02-06 11:54:28 -0800496 int fReserveCount;
497 int fCount;
498 int fAllocCount;
499 void* fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000500 union {
501 T* fItemArray;
502 void* fMemArray;
503 };
504};
505
bsalomon@google.com92669012011-09-27 19:10:05 +0000506/**
507 * Subclass of SkTArray that contains a preallocated memory block for the array.
508 */
bsalomon@google.comf47dd742013-02-26 15:40:01 +0000509template <int N, typename T, bool MEM_COPY = false>
510class SkSTArray : public SkTArray<T, MEM_COPY> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000511private:
bsalomon@google.comf47dd742013-02-26 15:40:01 +0000512 typedef SkTArray<T, MEM_COPY> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000513
514public:
515 SkSTArray() : INHERITED(&fStorage) {
516 }
517
518 SkSTArray(const SkSTArray& array)
519 : INHERITED(array, &fStorage) {
520 }
521
522 explicit SkSTArray(const INHERITED& array)
523 : INHERITED(array, &fStorage) {
524 }
525
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000526 explicit SkSTArray(int reserveCount)
527 : INHERITED(reserveCount) {
528 }
529
bsalomon@google.com92669012011-09-27 19:10:05 +0000530 SkSTArray(const T* array, int count)
531 : INHERITED(array, count, &fStorage) {
532 }
533
534 SkSTArray& operator= (const SkSTArray& array) {
535 return *this = *(const INHERITED*)&array;
536 }
537
538 SkSTArray& operator= (const INHERITED& array) {
539 INHERITED::operator=(array);
540 return *this;
541 }
542
543private:
544 SkAlignedSTStorage<N,T> fStorage;
545};
546
bsalomon@google.com49313f62011-09-14 13:54:05 +0000547#endif