3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkTDArray_DEFINED
11 #define SkTDArray_DEFINED
15 template <typename T> class SK_API SkTDArray {
18 fReserve = fCount = 0;
24 SkTDArray(const T src[], int count) {
25 SkASSERT(src || count == 0);
27 fReserve = fCount = 0;
33 fArray = (T*)sk_malloc_throw(count * sizeof(T));
35 fData = (ArrayT*)fArray;
37 memcpy(fArray, src, sizeof(T) * count);
38 fReserve = fCount = count;
41 SkTDArray(const SkTDArray<T>& src) {
42 fReserve = fCount = 0;
47 SkTDArray<T> tmp(src.fArray, src.fCount);
54 SkTDArray<T>& operator=(const SkTDArray<T>& src) {
56 if (src.fCount > fReserve) {
57 SkTDArray<T> tmp(src.fArray, src.fCount);
60 memcpy(fArray, src.fArray, sizeof(T) * src.fCount);
67 friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) {
68 return a.fCount == b.fCount &&
70 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T)));
72 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) {
76 void swap(SkTDArray<T>& other) {
77 SkTSwap(fArray, other.fArray);
79 SkTSwap(fData, other.fData);
81 SkTSwap(fReserve, other.fReserve);
82 SkTSwap(fCount, other.fCount);
85 /** Return a ptr to the array of data, to be freed with sk_free. This also
86 resets the SkTDArray to be empty.
91 fReserve = fCount = 0;
92 SkDEBUGCODE(fData = NULL;)
96 bool isEmpty() const { return fCount == 0; }
99 * Return the number of elements in the array
101 int count() const { return fCount; }
104 * Return the total number of elements allocated.
105 * reserved() - count() gives you the number of elements you can add
106 * without causing an allocation.
108 int reserved() const { return fReserve; }
111 * return the number of bytes in the array: count * sizeof(T)
113 size_t bytes() const { return fCount * sizeof(T); }
115 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; }
120 T& operator[](int index) {
121 SkASSERT(index < fCount);
122 return fArray[index];
124 const T& operator[](int index) const {
125 SkASSERT(index < fCount);
126 return fArray[index];
129 T& getAt(int index) {
130 return (*this)[index];
132 const T& getAt(int index) const {
133 return (*this)[index];
143 fReserve = fCount = 0;
145 SkASSERT(fReserve == 0 && fCount == 0);
150 // same as setCount(0)
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.
160 void setCount(int count) {
161 SkASSERT(count >= 0);
162 if (count > fReserve) {
163 this->resizeStorageToAtLeast(count);
168 void setReserve(int reserve) {
169 if (reserve > fReserve) {
170 this->resizeStorageToAtLeast(reserve);
175 this->adjustCount(1);
176 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
181 return this->append(1, NULL);
183 T* append(int count, const T* src = NULL) {
184 int oldCount = fCount;
186 SkASSERT(src == NULL || fArray == NULL ||
187 src + count <= fArray || fArray + oldCount <= src);
189 this->adjustCount(count);
191 memcpy(fArray + oldCount, src, sizeof(T) * count);
194 return fArray + oldCount;
198 T* result = this->append();
203 T* insert(int index) {
204 return this->insert(index, 1, NULL);
206 T* insert(int index, int count, const T* src = NULL) {
208 SkASSERT(index <= fCount);
209 size_t oldCount = fCount;
210 this->adjustCount(count);
211 T* dst = fArray + index;
212 memmove(dst + count, dst, sizeof(T) * (oldCount - index));
214 memcpy(dst, src, sizeof(T) * count);
219 void remove(int index, int count = 1) {
220 SkASSERT(index + count <= fCount);
221 fCount = fCount - count;
222 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index));
225 void removeShuffle(int index) {
226 SkASSERT(index < fCount);
227 int newCount = fCount - 1;
229 if (index != newCount) {
230 memcpy(fArray + index, fArray + newCount, sizeof(T));
234 int find(const T& elem) const {
235 const T* iter = fArray;
236 const T* stop = fArray + fCount;
238 for (; iter < stop; iter++) {
240 return (int) (iter - fArray);
246 int rfind(const T& elem) const {
247 const T* iter = fArray + fCount;
248 const T* stop = fArray;
250 while (iter > stop) {
251 if (*--iter == elem) {
252 return SkToInt(iter - stop);
259 * Returns true iff the array contains this element.
261 bool contains(const T& elem) const {
262 return (this->find(elem) >= 0);
266 * Copies up to max elements into dst. The number of items copied is
267 * capped by count - index. The actual number copied is returned.
269 int copyRange(T* dst, int index, int max) const {
271 SkASSERT(!max || dst);
272 if (index >= fCount) {
275 int count = SkMin32(max, fCount - index);
276 memcpy(dst, fArray + index, sizeof(T) * count);
280 void copy(T* dst) const {
281 this->copyRange(dst, 0, fCount);
284 // 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; }
294 T* stop = fArray + fCount;
295 while (iter < stop) {
304 T* stop = fArray + fCount;
305 while (iter < stop) {
314 T* stop = fArray + fCount;
315 while (iter < stop) {
322 void safeUnrefAll() {
324 T* stop = fArray + fCount;
325 while (iter < stop) {
332 void visitAll(void visitor(T&)) {
333 T* stop = this->end();
334 for (T* curr = this->begin(); curr < stop; curr++) {
342 void validate() const {
343 SkASSERT((fReserve == 0 && fArray == NULL) ||
344 (fReserve > 0 && fArray != NULL));
345 SkASSERT(fCount <= fReserve);
346 SkASSERT(fData == (ArrayT*)fArray);
355 typedef T ArrayT[kDebugArraySize];
363 * Adjusts the number of elements in the array.
364 * This is the same as calling setCount(count() + delta).
366 void adjustCount(int delta) {
367 this->setCount(fCount + delta);
371 * Increase the storage allocation such that it can hold (fCount + extra)
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.
376 * note: does NOT modify fCount
378 void resizeStorageToAtLeast(int count) {
379 SkASSERT(count > fReserve);
380 fReserve = count + 4;
381 fReserve += fReserve / 4;
382 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T));
384 fData = (ArrayT*)fArray;