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 | |
Mike Klein | d9187c0 | 2018-09-07 17:32:47 +0000 | [diff] [blame] | 11 | #include "../private/SkSafe32.h" |
| 12 | #include "../private/SkTLogic.h" |
| 13 | #include "../private/SkTemplates.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 14 | #include "SkTypes.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 15 | |
| 16 | #include <new> |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 17 | #include <utility> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 18 | |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 19 | /** When MEM_MOVE is true T will be bit copied when moved. |
| 20 | When MEM_MOVE is false, T will be copy constructed / destructed. |
bungeman@google.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 21 | In all cases T will be default-initialized on allocation, |
bungeman@google.com | a12cc7f | 2011-10-07 20:54:15 +0000 | [diff] [blame] | 22 | and its destructor will be called from this object's destructor. |
| 23 | */ |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 24 | template <typename T, bool MEM_MOVE = false> class SkTArray { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 25 | public: |
| 26 | /** |
| 27 | * Creates an empty array with no initial storage |
| 28 | */ |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 29 | SkTArray() { this->init(); } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | * Creates an empty array that will preallocate space for reserveCount |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 33 | * elements. |
| 34 | */ |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 35 | explicit SkTArray(int reserveCount) { this->init(0, reserveCount); } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Copies one array to another. The new array will be heap allocated. |
| 39 | */ |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 40 | explicit SkTArray(const SkTArray& that) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 41 | this->init(that.fCount); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 42 | this->copy(that.fItemArray); |
| 43 | } |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 44 | |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 45 | explicit SkTArray(SkTArray&& that) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 46 | // TODO: If 'that' owns its memory why don't we just steal the pointer? |
| 47 | this->init(that.fCount); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 48 | that.move(fMemArray); |
| 49 | that.fCount = 0; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 53 | * 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] | 54 | * array will be heap allocated. Be careful not to use this constructor |
| 55 | * when you really want the (void*, int) version. |
| 56 | */ |
| 57 | SkTArray(const T* array, int count) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 58 | this->init(count); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 59 | this->copy(array); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 62 | SkTArray& operator=(const SkTArray& that) { |
Greg Daniel | 70131b9 | 2017-03-22 13:33:21 -0400 | [diff] [blame] | 63 | if (this == &that) { |
| 64 | return *this; |
| 65 | } |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 66 | for (int i = 0; i < fCount; ++i) { |
| 67 | fItemArray[i].~T(); |
| 68 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 69 | fCount = 0; |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 70 | this->checkRealloc(that.count()); |
| 71 | fCount = that.count(); |
| 72 | this->copy(that.fItemArray); |
| 73 | return *this; |
| 74 | } |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 75 | SkTArray& operator=(SkTArray&& that) { |
Greg Daniel | 70131b9 | 2017-03-22 13:33:21 -0400 | [diff] [blame] | 76 | if (this == &that) { |
| 77 | return *this; |
| 78 | } |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 79 | for (int i = 0; i < fCount; ++i) { |
| 80 | fItemArray[i].~T(); |
| 81 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 82 | fCount = 0; |
| 83 | this->checkRealloc(that.count()); |
| 84 | fCount = that.count(); |
| 85 | that.move(fMemArray); |
| 86 | that.fCount = 0; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 87 | return *this; |
| 88 | } |
| 89 | |
bsalomon | 383ff10 | 2015-07-31 11:53:11 -0700 | [diff] [blame] | 90 | ~SkTArray() { |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 91 | for (int i = 0; i < fCount; ++i) { |
| 92 | fItemArray[i].~T(); |
| 93 | } |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 94 | if (fOwnMemory) { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 95 | sk_free(fMemArray); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 100 | * Resets to count() == 0 and resets any reserve count. |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 101 | */ |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 102 | void reset() { |
| 103 | this->pop_back_n(fCount); |
| 104 | fReserved = false; |
| 105 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 106 | |
| 107 | /** |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 108 | * Resets to count() = n newly constructed T objects and resets any reserve count. |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 109 | */ |
| 110 | void reset(int n) { |
| 111 | SkASSERT(n >= 0); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 112 | for (int i = 0; i < fCount; ++i) { |
| 113 | fItemArray[i].~T(); |
| 114 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 115 | // 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] | 116 | fCount = 0; |
| 117 | this->checkRealloc(n); |
| 118 | fCount = n; |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 119 | for (int i = 0; i < fCount; ++i) { |
| 120 | new (fItemArray + i) T; |
| 121 | } |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 122 | fReserved = false; |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 126 | * Resets to a copy of a C array and resets any reserve count. |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 127 | */ |
| 128 | void reset(const T* array, int count) { |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 129 | for (int i = 0; i < fCount; ++i) { |
| 130 | fItemArray[i].~T(); |
| 131 | } |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 132 | fCount = 0; |
| 133 | this->checkRealloc(count); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 134 | fCount = count; |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 135 | this->copy(array); |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 136 | fReserved = false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Ensures there is enough reserved space for n additional elements. The is guaranteed at least |
| 141 | * until the array size grows above n and subsequently shrinks below n, any version of reset() |
| 142 | * is called, or reserve() is called again. |
| 143 | */ |
| 144 | void reserve(int n) { |
| 145 | SkASSERT(n >= 0); |
| 146 | if (n > 0) { |
| 147 | this->checkRealloc(n); |
| 148 | fReserved = fOwnMemory; |
| 149 | } else { |
| 150 | fReserved = false; |
| 151 | } |
bungeman@google.com | 95ebd17 | 2014-03-21 19:39:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void removeShuffle(int n) { |
| 155 | SkASSERT(n < fCount); |
| 156 | int newCount = fCount - 1; |
| 157 | fCount = newCount; |
| 158 | fItemArray[n].~T(); |
| 159 | if (n != newCount) { |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 160 | this->move(n, newCount); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 165 | * Number of elements in the array. |
| 166 | */ |
| 167 | int count() const { return fCount; } |
| 168 | |
| 169 | /** |
| 170 | * Is the array empty. |
| 171 | */ |
| 172 | bool empty() const { return !fCount; } |
| 173 | |
| 174 | /** |
bungeman@google.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 175 | * 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] | 176 | * the reference only remains valid until the next call that adds or removes |
| 177 | * elements. |
| 178 | */ |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 179 | T& push_back() { |
| 180 | void* newT = this->push_back_raw(1); |
| 181 | return *new (newT) T; |
| 182 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 183 | |
| 184 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 185 | * Version of above that uses a copy constructor to initialize the new item |
| 186 | */ |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 187 | T& push_back(const T& t) { |
| 188 | void* newT = this->push_back_raw(1); |
| 189 | return *new (newT) T(t); |
| 190 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 193 | * Version of above that uses a move constructor to initialize the new item |
| 194 | */ |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 195 | T& push_back(T&& t) { |
| 196 | void* newT = this->push_back_raw(1); |
| 197 | return *new (newT) T(std::move(t)); |
| 198 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 201 | * Construct a new T at the back of this array. |
| 202 | */ |
| 203 | template<class... Args> T& emplace_back(Args&&... args) { |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 204 | void* newT = this->push_back_raw(1); |
| 205 | return *new (newT) T(std::forward<Args>(args)...); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
bungeman@google.com | d58a856 | 2014-03-24 15:55:01 +0000 | [diff] [blame] | 209 | * Allocates n more default-initialized T values, and returns the address of |
| 210 | * the start of that new range. Note: this address is only valid until the |
| 211 | * 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] | 212 | */ |
| 213 | T* push_back_n(int n) { |
| 214 | SkASSERT(n >= 0); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 215 | void* newTs = this->push_back_raw(n); |
| 216 | for (int i = 0; i < n; ++i) { |
| 217 | new (static_cast<char*>(newTs) + i * sizeof(T)) T; |
| 218 | } |
| 219 | return static_cast<T*>(newTs); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 223 | * Version of above that uses a copy constructor to initialize all n items |
| 224 | * to the same T. |
| 225 | */ |
| 226 | T* push_back_n(int n, const T& t) { |
| 227 | SkASSERT(n >= 0); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 228 | void* newTs = this->push_back_raw(n); |
| 229 | for (int i = 0; i < n; ++i) { |
| 230 | new (static_cast<char*>(newTs) + i * sizeof(T)) T(t); |
| 231 | } |
| 232 | return static_cast<T*>(newTs); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Version of above that uses a copy constructor to initialize the n items |
| 237 | * to separate T values. |
| 238 | */ |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 239 | T* push_back_n(int n, const T t[]) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 240 | SkASSERT(n >= 0); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 241 | this->checkRealloc(n); |
| 242 | for (int i = 0; i < n; ++i) { |
| 243 | new (fItemArray + fCount + i) T(t[i]); |
| 244 | } |
| 245 | fCount += n; |
| 246 | return fItemArray + fCount - n; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 250 | * Version of above that uses the move constructor to set n items. |
| 251 | */ |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 252 | T* move_back_n(int n, T* t) { |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 253 | SkASSERT(n >= 0); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 254 | this->checkRealloc(n); |
| 255 | for (int i = 0; i < n; ++i) { |
| 256 | new (fItemArray + fCount + i) T(std::move(t[i])); |
| 257 | } |
| 258 | fCount += n; |
| 259 | return fItemArray + fCount - n; |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 263 | * Removes the last element. Not safe to call when count() == 0. |
| 264 | */ |
| 265 | void pop_back() { |
| 266 | SkASSERT(fCount > 0); |
| 267 | --fCount; |
| 268 | fItemArray[fCount].~T(); |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 269 | this->checkRealloc(0); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Removes the last n elements. Not safe to call when count() < n. |
| 274 | */ |
| 275 | void pop_back_n(int n) { |
| 276 | SkASSERT(n >= 0); |
| 277 | SkASSERT(fCount >= n); |
| 278 | fCount -= n; |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 279 | for (int i = 0; i < n; ++i) { |
| 280 | fItemArray[fCount + i].~T(); |
| 281 | } |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 282 | this->checkRealloc(0); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Pushes or pops from the back to resize. Pushes will be default |
| 287 | * initialized. |
| 288 | */ |
| 289 | void resize_back(int newCount) { |
| 290 | SkASSERT(newCount >= 0); |
| 291 | |
| 292 | if (newCount > fCount) { |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 293 | this->push_back_n(newCount - fCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 294 | } else if (newCount < fCount) { |
commit-bot@chromium.org | b4a8d97 | 2013-06-05 15:40:59 +0000 | [diff] [blame] | 295 | this->pop_back_n(fCount - newCount); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 299 | /** Swaps the contents of this array with that array. Does a pointer swap if possible, |
| 300 | otherwise copies the T values. */ |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 301 | void swap(SkTArray& that) { |
| 302 | using std::swap; |
| 303 | if (this == &that) { |
bsalomon | 3632f84 | 2015-02-10 19:46:58 -0800 | [diff] [blame] | 304 | return; |
| 305 | } |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 306 | if (fOwnMemory && that.fOwnMemory) { |
| 307 | swap(fItemArray, that.fItemArray); |
| 308 | swap(fCount, that.fCount); |
| 309 | swap(fAllocCount, that.fAllocCount); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 310 | } else { |
| 311 | // This could be more optimal... |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 312 | SkTArray copy(std::move(that)); |
| 313 | that = std::move(*this); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 314 | *this = std::move(copy); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 318 | T* begin() { |
| 319 | return fItemArray; |
| 320 | } |
| 321 | const T* begin() const { |
| 322 | return fItemArray; |
| 323 | } |
| 324 | T* end() { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 325 | return fItemArray ? fItemArray + fCount : nullptr; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 326 | } |
| 327 | const T* end() const { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 328 | return fItemArray ? fItemArray + fCount : nullptr; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 332 | * Get the i^th element. |
| 333 | */ |
| 334 | T& operator[] (int i) { |
| 335 | SkASSERT(i < fCount); |
| 336 | SkASSERT(i >= 0); |
| 337 | return fItemArray[i]; |
| 338 | } |
| 339 | |
| 340 | const T& operator[] (int i) const { |
| 341 | SkASSERT(i < fCount); |
| 342 | SkASSERT(i >= 0); |
| 343 | return fItemArray[i]; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * equivalent to operator[](0) |
| 348 | */ |
| 349 | T& front() { SkASSERT(fCount > 0); return fItemArray[0];} |
| 350 | |
| 351 | const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];} |
| 352 | |
| 353 | /** |
| 354 | * equivalent to operator[](count() - 1) |
| 355 | */ |
| 356 | T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];} |
| 357 | |
| 358 | const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];} |
| 359 | |
| 360 | /** |
| 361 | * equivalent to operator[](count()-1-i) |
| 362 | */ |
| 363 | T& fromBack(int i) { |
| 364 | SkASSERT(i >= 0); |
| 365 | SkASSERT(i < fCount); |
| 366 | return fItemArray[fCount - i - 1]; |
| 367 | } |
| 368 | |
| 369 | const T& fromBack(int i) const { |
| 370 | SkASSERT(i >= 0); |
| 371 | SkASSERT(i < fCount); |
| 372 | return fItemArray[fCount - i - 1]; |
| 373 | } |
| 374 | |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 375 | bool operator==(const SkTArray<T, MEM_MOVE>& right) const { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 376 | int leftCount = this->count(); |
| 377 | if (leftCount != right.count()) { |
| 378 | return false; |
| 379 | } |
| 380 | for (int index = 0; index < leftCount; ++index) { |
| 381 | if (fItemArray[index] != right.fItemArray[index]) { |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 388 | bool operator!=(const SkTArray<T, MEM_MOVE>& right) const { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 389 | return !(*this == right); |
| 390 | } |
| 391 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 392 | inline int allocCntForTest() const; |
| 393 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 394 | protected: |
| 395 | /** |
| 396 | * Creates an empty array that will use the passed storage block until it |
| 397 | * is insufficiently large to hold the entire array. |
| 398 | */ |
| 399 | template <int N> |
| 400 | SkTArray(SkAlignedSTStorage<N,T>* storage) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 401 | this->initWithPreallocatedStorage(0, storage->get(), N); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Copy another array, using preallocated storage if preAllocCount >= |
| 406 | * array.count(). Otherwise storage will only be used when array shrinks |
| 407 | * to fit. |
| 408 | */ |
| 409 | template <int N> |
| 410 | SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 411 | this->initWithPreallocatedStorage(array.fCount, storage->get(), N); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 412 | this->copy(array.fItemArray); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
Florin Malita | b1d800d | 2017-03-09 12:17:15 -0500 | [diff] [blame] | 416 | * Move another array, using preallocated storage if preAllocCount >= |
| 417 | * array.count(). Otherwise storage will only be used when array shrinks |
| 418 | * to fit. |
| 419 | */ |
| 420 | template <int N> |
| 421 | SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 422 | this->initWithPreallocatedStorage(array.fCount, storage->get(), N); |
Florin Malita | b1d800d | 2017-03-09 12:17:15 -0500 | [diff] [blame] | 423 | array.move(fMemArray); |
| 424 | array.fCount = 0; |
| 425 | } |
| 426 | |
| 427 | /** |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 428 | * Copy a C array, using preallocated storage if preAllocCount >= |
| 429 | * count. Otherwise storage will only be used when array shrinks |
| 430 | * to fit. |
| 431 | */ |
| 432 | template <int N> |
| 433 | SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 434 | this->initWithPreallocatedStorage(count, storage->get(), N); |
bungeman | 0d9e9be | 2016-04-20 10:22:20 -0700 | [diff] [blame] | 435 | this->copy(array); |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 438 | private: |
| 439 | void init(int count = 0, int reserveCount = 0) { |
junov@chromium.org | d80a509 | 2011-11-30 18:35:19 +0000 | [diff] [blame] | 440 | SkASSERT(count >= 0); |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 441 | SkASSERT(reserveCount >= 0); |
| 442 | fCount = count; |
| 443 | if (!count && !reserveCount) { |
| 444 | fAllocCount = 0; |
| 445 | fMemArray = nullptr; |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 446 | fOwnMemory = true; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 447 | fReserved = false; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 448 | } else { |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 449 | fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount)); |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 450 | fMemArray = sk_malloc_throw(fAllocCount, sizeof(T)); |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 451 | fOwnMemory = true; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 452 | fReserved = reserveCount > 0; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 453 | } |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 456 | void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) { |
| 457 | SkASSERT(count >= 0); |
| 458 | SkASSERT(preallocCount > 0); |
| 459 | SkASSERT(preallocStorage); |
| 460 | fCount = count; |
| 461 | fMemArray = nullptr; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 462 | fReserved = false; |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 463 | if (count > preallocCount) { |
| 464 | fAllocCount = SkTMax(count, kMinHeapAllocCount); |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 465 | fMemArray = sk_malloc_throw(fAllocCount, sizeof(T)); |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 466 | fOwnMemory = true; |
| 467 | } else { |
| 468 | fAllocCount = preallocCount; |
| 469 | fMemArray = preallocStorage; |
| 470 | fOwnMemory = false; |
| 471 | } |
| 472 | } |
| 473 | |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 474 | /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage. |
| 475 | * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage. |
| 476 | */ |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 477 | void copy(const T* src) { |
| 478 | // Some types may be trivially copyable, in which case we *could* use memcopy; but |
| 479 | // MEM_MOVE == true implies that the type is trivially movable, and not necessarily |
| 480 | // trivially copyable (think sk_sp<>). So short of adding another template arg, we |
| 481 | // must be conservative and use copy construction. |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 482 | for (int i = 0; i < fCount; ++i) { |
| 483 | new (fItemArray + i) T(src[i]); |
| 484 | } |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 485 | } |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 486 | |
| 487 | template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) { |
| 488 | memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T)); |
| 489 | } |
| 490 | template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) { |
| 491 | sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T)); |
| 492 | } |
| 493 | |
| 494 | template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) { |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 495 | new (&fItemArray[dst]) T(std::move(fItemArray[src])); |
| 496 | fItemArray[src].~T(); |
| 497 | } |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 498 | template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) { |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 499 | for (int i = 0; i < fCount; ++i) { |
| 500 | new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i])); |
| 501 | fItemArray[i].~T(); |
| 502 | } |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 503 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 504 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 505 | static constexpr int kMinHeapAllocCount = 8; |
Mike Reed | 6c14c8d | 2017-03-09 16:36:26 -0500 | [diff] [blame] | 506 | |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 507 | // Helper function that makes space for n objects, adjusts the count, but does not initialize |
| 508 | // the new objects. |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 509 | void* push_back_raw(int n) { |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 510 | this->checkRealloc(n); |
Mike Klein | c1db610 | 2018-10-10 21:01:37 +0000 | [diff] [blame^] | 511 | void* ptr = fItemArray + fCount; |
bsalomon@google.com | d510414 | 2013-06-13 15:13:46 +0000 | [diff] [blame] | 512 | fCount += n; |
| 513 | return ptr; |
| 514 | } |
| 515 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 516 | void checkRealloc(int delta) { |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 517 | SkASSERT(fCount >= 0); |
| 518 | SkASSERT(fAllocCount >= 0); |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 519 | SkASSERT(-delta <= fCount); |
| 520 | |
Mike Reed | cab2549 | 2018-05-10 10:55:43 -0400 | [diff] [blame] | 521 | // Move into 64bit math temporarily, to avoid local overflows |
| 522 | int64_t newCount = fCount + delta; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 523 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 524 | // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 525 | // when we're currently using preallocated memory, would allocate less than |
| 526 | // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded. |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 527 | bool mustGrow = newCount > fAllocCount; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 528 | bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved; |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 529 | if (!mustGrow && !shouldShrink) { |
| 530 | return; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 531 | } |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 532 | |
Mike Reed | cab2549 | 2018-05-10 10:55:43 -0400 | [diff] [blame] | 533 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 534 | // Whether we're growing or shrinking, we leave at least 50% extra space for future growth. |
Mike Reed | cab2549 | 2018-05-10 10:55:43 -0400 | [diff] [blame] | 535 | int64_t newAllocCount = newCount + ((newCount + 1) >> 1); |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 536 | // Align the new allocation count to kMinHeapAllocCount. |
| 537 | static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two."); |
| 538 | newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1); |
| 539 | // At small sizes the old and new alloc count can both be kMinHeapAllocCount. |
| 540 | if (newAllocCount == fAllocCount) { |
| 541 | return; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 542 | } |
Mike Reed | cab2549 | 2018-05-10 10:55:43 -0400 | [diff] [blame] | 543 | |
| 544 | fAllocCount = Sk64_pin_to_s32(newAllocCount); |
| 545 | SkASSERT(fAllocCount >= newCount); |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 546 | void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T)); |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 547 | this->move(newMemArray); |
| 548 | if (fOwnMemory) { |
| 549 | sk_free(fMemArray); |
| 550 | |
| 551 | } |
| 552 | fMemArray = newMemArray; |
| 553 | fOwnMemory = true; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 554 | fReserved = false; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 555 | } |
| 556 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 557 | union { |
| 558 | T* fItemArray; |
| 559 | void* fMemArray; |
| 560 | }; |
Brian Salomon | 610842a | 2017-06-16 06:47:30 -0400 | [diff] [blame] | 561 | int fCount; |
| 562 | int fAllocCount; |
| 563 | bool fOwnMemory : 1; |
| 564 | bool fReserved : 1; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 567 | template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) { |
| 568 | a.swap(b); |
| 569 | } |
| 570 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 571 | template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount; |
| 572 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 573 | /** |
| 574 | * Subclass of SkTArray that contains a preallocated memory block for the array. |
| 575 | */ |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 576 | template <int N, typename T, bool MEM_MOVE= false> |
| 577 | class SkSTArray : public SkTArray<T, MEM_MOVE> { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 578 | private: |
Florin Malita | c2d5bd0 | 2017-03-09 13:34:09 -0500 | [diff] [blame] | 579 | typedef SkTArray<T, MEM_MOVE> INHERITED; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 580 | |
| 581 | public: |
| 582 | SkSTArray() : INHERITED(&fStorage) { |
| 583 | } |
| 584 | |
| 585 | SkSTArray(const SkSTArray& array) |
| 586 | : INHERITED(array, &fStorage) { |
| 587 | } |
| 588 | |
Florin Malita | b1d800d | 2017-03-09 12:17:15 -0500 | [diff] [blame] | 589 | SkSTArray(SkSTArray&& array) |
| 590 | : INHERITED(std::move(array), &fStorage) { |
| 591 | } |
| 592 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 593 | explicit SkSTArray(const INHERITED& array) |
| 594 | : INHERITED(array, &fStorage) { |
| 595 | } |
| 596 | |
Florin Malita | b1d800d | 2017-03-09 12:17:15 -0500 | [diff] [blame] | 597 | explicit SkSTArray(INHERITED&& array) |
| 598 | : INHERITED(std::move(array), &fStorage) { |
| 599 | } |
| 600 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 601 | explicit SkSTArray(int reserveCount) |
| 602 | : INHERITED(reserveCount) { |
| 603 | } |
| 604 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 605 | SkSTArray(const T* array, int count) |
| 606 | : INHERITED(array, count, &fStorage) { |
| 607 | } |
| 608 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 609 | SkSTArray& operator=(const SkSTArray& array) { |
Florin Malita | d54639f | 2017-03-12 10:40:13 -0400 | [diff] [blame] | 610 | INHERITED::operator=(array); |
| 611 | return *this; |
| 612 | } |
| 613 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 614 | SkSTArray& operator=(SkSTArray&& array) { |
Florin Malita | d54639f | 2017-03-12 10:40:13 -0400 | [diff] [blame] | 615 | INHERITED::operator=(std::move(array)); |
| 616 | return *this; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 619 | SkSTArray& operator=(const INHERITED& array) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 620 | INHERITED::operator=(array); |
| 621 | return *this; |
| 622 | } |
| 623 | |
Brian Salomon | 69225d0 | 2017-03-15 20:52:35 -0400 | [diff] [blame] | 624 | SkSTArray& operator=(INHERITED&& array) { |
Florin Malita | d54639f | 2017-03-12 10:40:13 -0400 | [diff] [blame] | 625 | INHERITED::operator=(std::move(array)); |
| 626 | return *this; |
| 627 | } |
| 628 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 629 | private: |
| 630 | SkAlignedSTStorage<N,T> fStorage; |
| 631 | }; |
| 632 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 633 | #endif |