bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 8 | #ifndef SkTArray_DEFINED |
| 9 | #define SkTArray_DEFINED |
| 10 | |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 11 | #include "../private/SkTLogic.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 12 | #include "../private/SkTemplates.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 14 | |
| 15 | #include <new> |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 16 | #include <utility> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 17 | |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 18 | /** 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.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 20 | In all cases T will be default-initialized on allocation, |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 21 | and its destructor will be called from this object's destructor. |
| 22 | */ |
bungeman | 85dc359 | 2016-02-09 11:32:56 -0800 | [diff] [blame] | 23 | template <typename T, bool MEM_COPY = false> class SkTArray { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 24 | public: |
| 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 37 | * Creates an empty array that will preallocate space for reserveCount |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 38 | * elements. |
| 39 | */ |
| 40 | explicit SkTArray(int reserveCount) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 41 | this->init(0, NULL, reserveCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 42 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Copies one array to another. The new array will be heap allocated. |
| 46 | */ |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 47 | 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.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 58 | * Creates a SkTArray by copying contents of a standard C array. The new |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 59 | * 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) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 63 | this->init(count, NULL, 0); |
| 64 | this->copy(array); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * assign copy of array to this |
| 69 | */ |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 70 | SkTArray& operator =(const SkTArray& that) { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 71 | for (int i = 0; i < fCount; ++i) { |
| 72 | fItemArray[i].~T(); |
| 73 | } |
| 74 | fCount = 0; |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 75 | 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.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 89 | return *this; |
| 90 | } |
| 91 | |
bsalomon | 383ff10 | 2015-07-31 11:53:11 -0700 | [diff] [blame] | 92 | ~SkTArray() { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 93 | 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.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 107 | * 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 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 114 | // Set fCount to 0 before calling checkRealloc so that no elements are moved. |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 115 | fCount = 0; |
| 116 | this->checkRealloc(n); |
| 117 | fCount = n; |
| 118 | for (int i = 0; i < fCount; ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 119 | new (fItemArray + i) T; |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 124 | * 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.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 133 | * 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 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 139 | fCount = 0; |
| 140 | this->checkRealloc(count); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 141 | fCount = count; |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 142 | this->copy(array); |
bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 143 | } |
| 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) { |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 151 | this->move(n, newCount); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 156 | * 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.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 166 | * Adds 1 new default-initialized T value and returns it by reference. Note |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 167 | * the reference only remains valid until the next call that adds or removes |
| 168 | * elements. |
| 169 | */ |
| 170 | T& push_back() { |
krasin | e0c1d28 | 2016-04-21 08:34:00 -0700 | [diff] [blame] | 171 | void* newT = this->push_back_raw(1); |
| 172 | return *new (newT) T; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 176 | * Version of above that uses a copy constructor to initialize the new item |
| 177 | */ |
| 178 | T& push_back(const T& t) { |
krasin | e0c1d28 | 2016-04-21 08:34:00 -0700 | [diff] [blame] | 179 | void* newT = this->push_back_raw(1); |
| 180 | return *new (newT) T(t); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 184 | * Version of above that uses a move constructor to initialize the new item |
| 185 | */ |
| 186 | T& push_back(T&& t) { |
krasin | e0c1d28 | 2016-04-21 08:34:00 -0700 | [diff] [blame] | 187 | void* newT = this->push_back_raw(1); |
| 188 | return *new (newT) T(std::move(t)); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 192 | * Construct a new T at the back of this array. |
| 193 | */ |
| 194 | template<class... Args> T& emplace_back(Args&&... args) { |
krasin | e0c1d28 | 2016-04-21 08:34:00 -0700 | [diff] [blame] | 195 | void* newT = this->push_back_raw(1); |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 196 | return *new (newT) T(std::forward<Args>(args)...); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
bungeman@google.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 200 | * 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.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 203 | */ |
| 204 | T* push_back_n(int n) { |
| 205 | SkASSERT(n >= 0); |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 206 | void* newTs = this->push_back_raw(n); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 207 | for (int i = 0; i < n; ++i) { |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 208 | new (static_cast<char*>(newTs) + i * sizeof(T)) T; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 209 | } |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 210 | return static_cast<T*>(newTs); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 214 | * 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); |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 219 | void* newTs = this->push_back_raw(n); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 220 | for (int i = 0; i < n; ++i) { |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 221 | new (static_cast<char*>(newTs) + i * sizeof(T)) T(t); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 222 | } |
bungeman | 6a6f3c5 | 2016-04-21 10:52:03 -0700 | [diff] [blame] | 223 | return static_cast<T*>(newTs); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 224 | } |
| 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.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 232 | this->checkRealloc(n); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 233 | for (int i = 0; i < n; ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 234 | new (fItemArray + fCount + i) T(t[i]); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 235 | } |
| 236 | fCount += n; |
| 237 | return fItemArray + fCount - n; |
| 238 | } |
| 239 | |
| 240 | /** |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 241 | * Version of above that uses the move constructor to set n items. |
| 242 | */ |
| 243 | T* move_back_n(int n, T* t) { |
| 244 | SkASSERT(n >= 0); |
| 245 | this->checkRealloc(n); |
| 246 | for (int i = 0; i < n; ++i) { |
| 247 | new (fItemArray + fCount + i) T(std::move(t[i])); |
| 248 | } |
| 249 | fCount += n; |
| 250 | return fItemArray + fCount - n; |
| 251 | } |
| 252 | |
| 253 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 254 | * Removes the last element. Not safe to call when count() == 0. |
| 255 | */ |
| 256 | void pop_back() { |
| 257 | SkASSERT(fCount > 0); |
| 258 | --fCount; |
| 259 | fItemArray[fCount].~T(); |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 260 | this->checkRealloc(0); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Removes the last n elements. Not safe to call when count() < n. |
| 265 | */ |
| 266 | void pop_back_n(int n) { |
| 267 | SkASSERT(n >= 0); |
| 268 | SkASSERT(fCount >= n); |
| 269 | fCount -= n; |
| 270 | for (int i = 0; i < n; ++i) { |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 271 | fItemArray[fCount + i].~T(); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 272 | } |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 273 | this->checkRealloc(0); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Pushes or pops from the back to resize. Pushes will be default |
| 278 | * initialized. |
| 279 | */ |
| 280 | void resize_back(int newCount) { |
| 281 | SkASSERT(newCount >= 0); |
| 282 | |
| 283 | if (newCount > fCount) { |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 284 | this->push_back_n(newCount - fCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 285 | } else if (newCount < fCount) { |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 286 | this->pop_back_n(fCount - newCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 290 | /** Swaps the contents of this array with that array. Does a pointer swap if possible, |
| 291 | otherwise copies the T values. */ |
| 292 | void swap(SkTArray* that) { |
bsalomon | 3632f84 | 2015-02-10 19:46:58 -0800 | [diff] [blame] | 293 | if (this == that) { |
| 294 | return; |
| 295 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 296 | if (this->fPreAllocMemArray != this->fItemArray && |
| 297 | that->fPreAllocMemArray != that->fItemArray) { |
| 298 | // If neither is using a preallocated array then just swap. |
| 299 | SkTSwap(fItemArray, that->fItemArray); |
| 300 | SkTSwap(fCount, that->fCount); |
| 301 | SkTSwap(fAllocCount, that->fAllocCount); |
| 302 | } else { |
| 303 | // This could be more optimal... |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 304 | SkTArray copy(std::move(*that)); |
| 305 | *that = std::move(*this); |
| 306 | *this = std::move(copy); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 310 | T* begin() { |
| 311 | return fItemArray; |
| 312 | } |
| 313 | const T* begin() const { |
| 314 | return fItemArray; |
| 315 | } |
| 316 | T* end() { |
| 317 | return fItemArray ? fItemArray + fCount : NULL; |
| 318 | } |
| 319 | const T* end() const { |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 320 | return fItemArray ? fItemArray + fCount : NULL; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 324 | * Get the i^th element. |
| 325 | */ |
| 326 | T& operator[] (int i) { |
| 327 | SkASSERT(i < fCount); |
| 328 | SkASSERT(i >= 0); |
| 329 | return fItemArray[i]; |
| 330 | } |
| 331 | |
| 332 | const T& operator[] (int i) const { |
| 333 | SkASSERT(i < fCount); |
| 334 | SkASSERT(i >= 0); |
| 335 | return fItemArray[i]; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * equivalent to operator[](0) |
| 340 | */ |
| 341 | T& front() { SkASSERT(fCount > 0); return fItemArray[0];} |
| 342 | |
| 343 | const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];} |
| 344 | |
| 345 | /** |
| 346 | * equivalent to operator[](count() - 1) |
| 347 | */ |
| 348 | T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];} |
| 349 | |
| 350 | const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];} |
| 351 | |
| 352 | /** |
| 353 | * equivalent to operator[](count()-1-i) |
| 354 | */ |
| 355 | T& fromBack(int i) { |
| 356 | SkASSERT(i >= 0); |
| 357 | SkASSERT(i < fCount); |
| 358 | return fItemArray[fCount - i - 1]; |
| 359 | } |
| 360 | |
| 361 | const T& fromBack(int i) const { |
| 362 | SkASSERT(i >= 0); |
| 363 | SkASSERT(i < fCount); |
| 364 | return fItemArray[fCount - i - 1]; |
| 365 | } |
| 366 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 367 | bool operator==(const SkTArray<T, MEM_COPY>& right) const { |
| 368 | int leftCount = this->count(); |
| 369 | if (leftCount != right.count()) { |
| 370 | return false; |
| 371 | } |
| 372 | for (int index = 0; index < leftCount; ++index) { |
| 373 | if (fItemArray[index] != right.fItemArray[index]) { |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | bool operator!=(const SkTArray<T, MEM_COPY>& right) const { |
| 381 | return !(*this == right); |
| 382 | } |
| 383 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 384 | protected: |
| 385 | /** |
| 386 | * Creates an empty array that will use the passed storage block until it |
| 387 | * is insufficiently large to hold the entire array. |
| 388 | */ |
| 389 | template <int N> |
| 390 | SkTArray(SkAlignedSTStorage<N,T>* storage) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 391 | this->init(0, storage->get(), N); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Copy another array, using preallocated storage if preAllocCount >= |
| 396 | * array.count(). Otherwise storage will only be used when array shrinks |
| 397 | * to fit. |
| 398 | */ |
| 399 | template <int N> |
| 400 | SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 401 | this->init(array.fCount, storage->get(), N); |
| 402 | this->copy(array.fItemArray); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Copy a C array, using preallocated storage if preAllocCount >= |
| 407 | * count. Otherwise storage will only be used when array shrinks |
| 408 | * to fit. |
| 409 | */ |
| 410 | template <int N> |
| 411 | SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 412 | this->init(count, storage->get(), N); |
| 413 | this->copy(array); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 414 | } |
| 415 | |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 416 | void init(int count, void* preAllocStorage, int preAllocOrReserveCount) { |
junov@chromium.org | d80a509 | 2011-11-30 18:35:19 +0000 | [diff] [blame] | 417 | SkASSERT(count >= 0); |
| 418 | SkASSERT(preAllocOrReserveCount >= 0); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 419 | fCount = count; |
| 420 | fReserveCount = (preAllocOrReserveCount > 0) ? |
| 421 | preAllocOrReserveCount : |
| 422 | gMIN_ALLOC_COUNT; |
| 423 | fPreAllocMemArray = preAllocStorage; |
| 424 | if (fReserveCount >= fCount && |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 425 | preAllocStorage) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 426 | fAllocCount = fReserveCount; |
| 427 | fMemArray = preAllocStorage; |
| 428 | } else { |
junov@chromium.org | d80a509 | 2011-11-30 18:35:19 +0000 | [diff] [blame] | 429 | fAllocCount = SkMax32(fCount, fReserveCount); |
| 430 | fMemArray = sk_malloc_throw(fAllocCount * sizeof(T)); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 431 | } |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 432 | } |
| 433 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 434 | private: |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 435 | /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage. |
| 436 | * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage. |
| 437 | */ |
| 438 | template <bool E = MEM_COPY> SK_WHEN(E, void) copy(const T* src) { |
| 439 | sk_careful_memcpy(fMemArray, src, fCount * sizeof(T)); |
| 440 | } |
| 441 | template <bool E = MEM_COPY> SK_WHEN(E, void) move(int dst, int src) { |
| 442 | memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T)); |
| 443 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 444 | template <bool E = MEM_COPY> SK_WHEN(E, void) move(void* dst) { |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 445 | sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T)); |
| 446 | } |
| 447 | |
| 448 | template <bool E = MEM_COPY> SK_WHEN(!E, void) copy(const T* src) { |
| 449 | for (int i = 0; i < fCount; ++i) { |
| 450 | new (fItemArray + i) T(src[i]); |
| 451 | } |
| 452 | } |
| 453 | template <bool E = MEM_COPY> SK_WHEN(!E, void) move(int dst, int src) { |
| 454 | new (&fItemArray[dst]) T(std::move(fItemArray[src])); |
| 455 | fItemArray[src].~T(); |
| 456 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 457 | template <bool E = MEM_COPY> SK_WHEN(!E, void) move(void* dst) { |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 458 | for (int i = 0; i < fCount; ++i) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 459 | new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i])); |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 460 | fItemArray[i].~T(); |
| 461 | } |
| 462 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 463 | |
| 464 | static const int gMIN_ALLOC_COUNT = 8; |
| 465 | |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 466 | // Helper function that makes space for n objects, adjusts the count, but does not initialize |
| 467 | // the new objects. |
| 468 | void* push_back_raw(int n) { |
| 469 | this->checkRealloc(n); |
| 470 | void* ptr = fItemArray + fCount; |
| 471 | fCount += n; |
| 472 | return ptr; |
| 473 | } |
| 474 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 475 | inline void checkRealloc(int delta) { |
| 476 | SkASSERT(fCount >= 0); |
| 477 | SkASSERT(fAllocCount >= 0); |
| 478 | |
| 479 | SkASSERT(-delta <= fCount); |
| 480 | |
| 481 | int newCount = fCount + delta; |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 482 | int newAllocCount = fAllocCount; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 483 | |
bsalomon@google.com | 137209f | 2012-08-14 17:19:08 +0000 | [diff] [blame] | 484 | if (newCount > fAllocCount || newCount < (fAllocCount / 3)) { |
| 485 | // whether we're growing or shrinking, we leave at least 50% extra space for future |
| 486 | // growth (clamped to the reserve count). |
| 487 | newAllocCount = SkMax32(newCount + ((newCount + 1) >> 1), fReserveCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 488 | } |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 489 | if (newAllocCount != fAllocCount) { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 490 | |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 491 | fAllocCount = newAllocCount; |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 492 | void* newMemArray; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 493 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 494 | if (fAllocCount == fReserveCount && fPreAllocMemArray) { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 495 | newMemArray = fPreAllocMemArray; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 496 | } else { |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 497 | newMemArray = sk_malloc_throw(fAllocCount*sizeof(T)); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 498 | } |
| 499 | |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 500 | this->move(newMemArray); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 501 | |
| 502 | if (fMemArray != fPreAllocMemArray) { |
| 503 | sk_free(fMemArray); |
| 504 | } |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 505 | fMemArray = newMemArray; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 509 | int fReserveCount; |
| 510 | int fCount; |
| 511 | int fAllocCount; |
| 512 | void* fPreAllocMemArray; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 513 | union { |
| 514 | T* fItemArray; |
| 515 | void* fMemArray; |
| 516 | }; |
| 517 | }; |
| 518 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 519 | /** |
| 520 | * Subclass of SkTArray that contains a preallocated memory block for the array. |
| 521 | */ |
bsalomon@google.com | f47dd74 | 2013-02-26 15:40:01 +0000 | [diff] [blame] | 522 | template <int N, typename T, bool MEM_COPY = false> |
| 523 | class SkSTArray : public SkTArray<T, MEM_COPY> { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 524 | private: |
bsalomon@google.com | f47dd74 | 2013-02-26 15:40:01 +0000 | [diff] [blame] | 525 | typedef SkTArray<T, MEM_COPY> INHERITED; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 526 | |
| 527 | public: |
| 528 | SkSTArray() : INHERITED(&fStorage) { |
| 529 | } |
| 530 | |
| 531 | SkSTArray(const SkSTArray& array) |
| 532 | : INHERITED(array, &fStorage) { |
| 533 | } |
| 534 | |
| 535 | explicit SkSTArray(const INHERITED& array) |
| 536 | : INHERITED(array, &fStorage) { |
| 537 | } |
| 538 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 539 | explicit SkSTArray(int reserveCount) |
| 540 | : INHERITED(reserveCount) { |
| 541 | } |
| 542 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 543 | SkSTArray(const T* array, int count) |
| 544 | : INHERITED(array, count, &fStorage) { |
| 545 | } |
| 546 | |
| 547 | SkSTArray& operator= (const SkSTArray& array) { |
| 548 | return *this = *(const INHERITED*)&array; |
| 549 | } |
| 550 | |
| 551 | SkSTArray& operator= (const INHERITED& array) { |
| 552 | INHERITED::operator=(array); |
| 553 | return *this; |
| 554 | } |
| 555 | |
| 556 | private: |
| 557 | SkAlignedSTStorage<N,T> fStorage; |
| 558 | }; |
| 559 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 560 | #endif |