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