epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkTDArray_DEFINED |
| 11 | #define SkTDArray_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
ehsan.akhgari | db5f7bf | 2014-07-09 11:13:55 -0700 | [diff] [blame] | 15 | template <typename T> class SkTDArray { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | public: |
halcanary | 9be3720 | 2016-08-08 07:21:42 -0700 | [diff] [blame] | 17 | SkTDArray() : fArray(nullptr), fReserve(0), fCount(0) {} |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 18 | SkTDArray(const T src[], int count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | SkASSERT(src || count == 0); |
| 20 | |
| 21 | fReserve = fCount = 0; |
| 22 | fArray = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | if (count) { |
| 24 | fArray = (T*)sk_malloc_throw(count * sizeof(T)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | memcpy(fArray, src, sizeof(T) * count); |
| 26 | fReserve = fCount = count; |
| 27 | } |
| 28 | } |
halcanary | 9be3720 | 2016-08-08 07:21:42 -0700 | [diff] [blame] | 29 | SkTDArray(const SkTDArray<T>& src) : fArray(nullptr), fReserve(0), fCount(0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | SkTDArray<T> tmp(src.fArray, src.fCount); |
| 31 | this->swap(tmp); |
| 32 | } |
halcanary | 9be3720 | 2016-08-08 07:21:42 -0700 | [diff] [blame] | 33 | SkTDArray(SkTDArray<T>&& src) : fArray(nullptr), fReserve(0), fCount(0) { |
| 34 | this->swap(src); |
| 35 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | ~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 { |
mtklein | cc881da | 2015-12-08 11:55:17 -0800 | [diff] [blame] | 46 | sk_careful_memcpy(fArray, src.fArray, sizeof(T) * src.fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | fCount = src.fCount; |
| 48 | } |
| 49 | } |
| 50 | return *this; |
| 51 | } |
halcanary | 9be3720 | 2016-08-08 07:21:42 -0700 | [diff] [blame] | 52 | SkTDArray<T>& operator=(SkTDArray<T>&& src) { |
| 53 | if (this != &src) { |
| 54 | this->swap(src); |
| 55 | src.reset(); |
| 56 | } |
| 57 | return *this; |
| 58 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
reed@google.com | b530ef5 | 2011-07-20 19:55:42 +0000 | [diff] [blame] | 60 | friend bool operator==(const SkTDArray<T>& a, const SkTDArray<T>& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | return a.fCount == b.fCount && |
| 62 | (a.fCount == 0 || |
| 63 | !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T))); |
| 64 | } |
reed@google.com | 3467ee0 | 2013-05-29 18:05:52 +0000 | [diff] [blame] | 65 | friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) { |
| 66 | return !(a == b); |
| 67 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | |
| 69 | void swap(SkTDArray<T>& other) { |
| 70 | SkTSwap(fArray, other.fArray); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkTSwap(fReserve, other.fReserve); |
| 72 | SkTSwap(fCount, other.fCount); |
| 73 | } |
| 74 | |
reed@android.com | 0da41db | 2009-08-25 16:03:59 +0000 | [diff] [blame] | 75 | /** Return a ptr to the array of data, to be freed with sk_free. This also |
| 76 | resets the SkTDArray to be empty. |
| 77 | */ |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 78 | T* release() { |
reed@android.com | 0da41db | 2009-08-25 16:03:59 +0000 | [diff] [blame] | 79 | T* array = fArray; |
| 80 | fArray = NULL; |
| 81 | fReserve = fCount = 0; |
reed@android.com | 0da41db | 2009-08-25 16:03:59 +0000 | [diff] [blame] | 82 | return array; |
| 83 | } |
| 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | bool isEmpty() const { return fCount == 0; } |
reed@google.com | 1271d78 | 2011-11-28 19:54:12 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Return the number of elements in the array |
| 89 | */ |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 90 | int count() const { return fCount; } |
reed@google.com | 1271d78 | 2011-11-28 19:54:12 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
commit-bot@chromium.org | 046f1f6 | 2014-02-11 10:17:02 +0000 | [diff] [blame] | 93 | * 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.com | 1271d78 | 2011-11-28 19:54:12 +0000 | [diff] [blame] | 100 | * 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.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 104 | 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.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 110 | SkASSERT(index < fCount); |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 111 | return fArray[index]; |
| 112 | } |
| 113 | const T& operator[](int index) const { |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 114 | SkASSERT(index < fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | return fArray[index]; |
| 116 | } |
skia.committer@gmail.com | 7fc0e0a | 2013-01-15 02:01:40 +0000 | [diff] [blame] | 117 | |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 118 | T& getAt(int index) { |
| 119 | return (*this)[index]; |
| 120 | } |
| 121 | const T& getAt(int index) const { |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 122 | return (*this)[index]; |
| 123 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | |
| 125 | void reset() { |
| 126 | if (fArray) { |
| 127 | sk_free(fArray); |
| 128 | fArray = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | fReserve = fCount = 0; |
| 130 | } else { |
| 131 | SkASSERT(fReserve == 0 && fCount == 0); |
| 132 | } |
| 133 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | void rewind() { |
| 136 | // same as setCount(0) |
| 137 | fCount = 0; |
| 138 | } |
| 139 | |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 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. |
robertphillips | 91df6c2 | 2015-04-24 11:10:51 -0700 | [diff] [blame] | 144 | * It will never shrink the storage. |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 145 | */ |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 146 | void setCount(int count) { |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 147 | SkASSERT(count >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | if (count > fReserve) { |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 149 | this->resizeStorageToAtLeast(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | } |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 151 | fCount = count; |
| 152 | } |
| 153 | |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 154 | void setReserve(int reserve) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | if (reserve > fReserve) { |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 156 | this->resizeStorageToAtLeast(reserve); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | T* prepend() { |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 161 | this->adjustCount(1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | 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.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 169 | T* append(int count, const T* src = NULL) { |
| 170 | int oldCount = fCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | if (count) { |
| 172 | SkASSERT(src == NULL || fArray == NULL || |
| 173 | src + count <= fArray || fArray + oldCount <= src); |
| 174 | |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 175 | this->adjustCount(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | if (src) { |
| 177 | memcpy(fArray + oldCount, src, sizeof(T) * count); |
| 178 | } |
| 179 | } |
| 180 | return fArray + oldCount; |
| 181 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 182 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | T* appendClear() { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 184 | T* result = this->append(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | *result = 0; |
| 186 | return result; |
| 187 | } |
| 188 | |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 189 | T* insert(int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | return this->insert(index, 1, NULL); |
| 191 | } |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 192 | T* insert(int index, int count, const T* src = NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | SkASSERT(count); |
| 194 | SkASSERT(index <= fCount); |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 195 | size_t oldCount = fCount; |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 196 | this->adjustCount(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | 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.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 205 | void remove(int index, int count = 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | SkASSERT(index + count <= fCount); |
| 207 | fCount = fCount - count; |
| 208 | memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - index)); |
| 209 | } |
| 210 | |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 211 | void removeShuffle(int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | SkASSERT(index < fCount); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 213 | int newCount = fCount - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | 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) { |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 226 | return SkToInt(iter - fArray); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | } |
| 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.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 238 | return SkToInt(iter - stop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | return -1; |
| 242 | } |
| 243 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 244 | /** |
epoger@google.com | af07d06 | 2012-07-13 14:53:18 +0000 | [diff] [blame] | 245 | * 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.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 252 | * 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.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 255 | int copyRange(T* dst, int index, int max) const { |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 256 | 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.com | c6081ab | 2013-07-01 17:04:32 +0000 | [diff] [blame] | 267 | this->copyRange(dst, 0, fCount); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 268 | } |
| 269 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | // routines to treat the array like a stack |
mtklein | aa90d00 | 2014-09-11 06:36:11 -0700 | [diff] [blame] | 271 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | |
| 278 | void deleteAll() { |
| 279 | T* iter = fArray; |
| 280 | T* stop = fArray + fCount; |
| 281 | while (iter < stop) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 282 | delete *iter; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | 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.com | 8433b5d | 2009-07-03 02:52:27 +0000 | [diff] [blame] | 307 | |
| 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.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 318 | void visitAll(void visitor(T&)) { |
bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 319 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | #ifdef SK_DEBUG |
| 328 | void validate() const { |
| 329 | SkASSERT((fReserve == 0 && fArray == NULL) || |
| 330 | (fReserve > 0 && fArray != NULL)); |
| 331 | SkASSERT(fCount <= fReserve); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | } |
| 333 | #endif |
| 334 | |
mtklein | 092dab9 | 2014-10-09 18:22:41 -0700 | [diff] [blame] | 335 | void shrinkToFit() { |
| 336 | fReserve = fCount; |
| 337 | fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); |
| 338 | } |
| 339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | T* fArray; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 342 | int fReserve; |
| 343 | int fCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 345 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 353 | /** |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 354 | * 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.org | ca21a00 | 2014-02-13 18:35:54 +0000 | [diff] [blame] | 363 | fReserve = count + 4; |
| 364 | fReserve += fReserve / 4; |
| 365 | fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | } |
| 367 | }; |
| 368 | |
| 369 | #endif |