reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrTArray_DEFINED |
| 19 | #define GrTArray_DEFINED |
| 20 | |
| 21 | #include <new> |
| 22 | #include "GrTypes.h" |
| 23 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | // DATA_TYPE indicates that T has a trivial cons, destructor |
| 25 | // and can be shallow-copied |
| 26 | template <typename T, bool DATA_TYPE = false> class GrTArray { |
| 27 | public: |
| 28 | GrTArray() { |
| 29 | fCount = 0; |
| 30 | fReserveCount = MIN_ALLOC_COUNT; |
| 31 | fAllocCount = 0; |
| 32 | fMemArray = NULL; |
| 33 | fPreAllocMemArray = NULL; |
| 34 | } |
| 35 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 36 | explicit GrTArray(int reserveCount) { |
| 37 | GrAssert(reserveCount >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | fCount = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 39 | fReserveCount = reserveCount > MIN_ALLOC_COUNT ? reserveCount : |
| 40 | MIN_ALLOC_COUNT; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | fAllocCount = fReserveCount; |
| 42 | fMemArray = GrMalloc(sizeof(T) * fReserveCount); |
| 43 | fPreAllocMemArray = NULL; |
| 44 | } |
| 45 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 46 | GrTArray(void* preAllocStorage, int preAllocCount) { |
| 47 | GrAssert(preAllocCount >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | // we allow NULL,0 args and revert to the default cons. behavior |
| 49 | // this makes it possible for a owner-object to use same constructor |
| 50 | // to get either prealloc or nonprealloc behavior based using same line |
| 51 | GrAssert((NULL == preAllocStorage) == !preAllocCount); |
| 52 | |
| 53 | fCount = 0; |
| 54 | fReserveCount = preAllocCount > 0 ? preAllocCount : |
| 55 | MIN_ALLOC_COUNT; |
| 56 | fAllocCount = preAllocCount; |
| 57 | fMemArray = preAllocStorage; |
| 58 | fPreAllocMemArray = preAllocStorage; |
| 59 | } |
| 60 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 61 | explicit GrTArray(const GrTArray& array) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | fCount = array.count(); |
| 63 | fReserveCount = MIN_ALLOC_COUNT; |
| 64 | fAllocCount = GrMax(fReserveCount, fCount); |
| 65 | fMemArray = GrMalloc(sizeof(T) * fAllocCount); |
| 66 | fPreAllocMemArray = NULL; |
| 67 | if (DATA_TYPE) { |
| 68 | memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount); |
| 69 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 70 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | new (fItemArray + i) T(array[i]); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 76 | GrTArray(const T* array, int count) { |
| 77 | GrAssert(count >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | fCount = count; |
| 79 | fReserveCount = MIN_ALLOC_COUNT; |
| 80 | fAllocCount = GrMax(fReserveCount, fCount); |
| 81 | fMemArray = GrMalloc(sizeof(T) * fAllocCount); |
| 82 | fPreAllocMemArray = NULL; |
| 83 | if (DATA_TYPE) { |
| 84 | memcpy(fMemArray, array, sizeof(T) * fCount); |
| 85 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 86 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 87 | new (fItemArray + i) T(array[i]); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | GrTArray(const GrTArray& array, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 93 | void* preAllocStorage, int preAllocCount) { |
| 94 | |
| 95 | GrAssert(preAllocCount >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | |
| 97 | // for same reason as non-copying cons we allow NULL, 0 for prealloc |
| 98 | GrAssert((NULL == preAllocStorage) == !preAllocCount); |
| 99 | |
| 100 | fCount = array.count(); |
| 101 | fReserveCount = preAllocCount > 0 ? preAllocCount : |
| 102 | MIN_ALLOC_COUNT; |
| 103 | fPreAllocMemArray = preAllocStorage; |
| 104 | |
| 105 | if (fReserveCount >= fCount && preAllocCount) { |
| 106 | fAllocCount = fReserveCount; |
| 107 | fMemArray = preAllocStorage; |
| 108 | } else { |
| 109 | fAllocCount = GrMax(fCount, fReserveCount); |
| 110 | fMemArray = GrMalloc(fAllocCount * sizeof(T)); |
| 111 | } |
| 112 | |
| 113 | if (DATA_TYPE) { |
| 114 | memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount); |
| 115 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 116 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | new (fItemArray + i) T(array[i]); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 122 | GrTArray(const T* array, int count, |
| 123 | void* preAllocStorage, int preAllocCount) { |
| 124 | |
| 125 | GrAssert(count >= 0); |
| 126 | GrAssert(preAllocCount >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | |
| 128 | // for same reason as non-copying cons we allow NULL, 0 for prealloc |
| 129 | GrAssert((NULL == preAllocStorage) == !preAllocCount); |
| 130 | |
| 131 | fCount = count; |
| 132 | fReserveCount = (preAllocCount > 0) ? preAllocCount : |
| 133 | MIN_ALLOC_COUNT; |
| 134 | fPreAllocMemArray = preAllocStorage; |
| 135 | |
| 136 | if (fReserveCount >= fCount && preAllocCount) { |
| 137 | fAllocCount = fReserveCount; |
| 138 | fMemArray = preAllocStorage; |
| 139 | } else { |
| 140 | fAllocCount = GrMax(fCount, fReserveCount); |
| 141 | fMemArray = GrMalloc(fAllocCount * sizeof(T)); |
| 142 | } |
| 143 | |
| 144 | if (DATA_TYPE) { |
| 145 | memcpy(fMemArray, array, sizeof(T) * fCount); |
| 146 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 147 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | new (fItemArray + i) T(array[i]); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | GrTArray& operator =(const GrTArray& array) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 154 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | fItemArray[i].~T(); |
| 156 | } |
| 157 | fCount = 0; |
| 158 | checkRealloc((int)array.count()); |
| 159 | fCount = array.count(); |
| 160 | if (DATA_TYPE) { |
| 161 | memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount); |
| 162 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 163 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | new (fItemArray + i) T(array[i]); |
| 165 | } |
| 166 | } |
| 167 | return *this; |
| 168 | } |
| 169 | |
| 170 | ~GrTArray() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 171 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | fItemArray[i].~T(); |
| 173 | } |
| 174 | if (fMemArray != fPreAllocMemArray) { |
| 175 | GrFree(fMemArray); |
| 176 | } |
| 177 | } |
| 178 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 179 | int count() const { return fCount; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | |
| 181 | bool empty() const { return !fCount; } |
| 182 | |
| 183 | T& push_back() { |
| 184 | checkRealloc(1); |
| 185 | new ((char*)fMemArray+sizeof(T)*fCount) T; |
| 186 | ++fCount; |
| 187 | return fItemArray[fCount-1]; |
| 188 | } |
| 189 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 190 | void push_back_n(int n) { |
| 191 | GrAssert(n >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 192 | checkRealloc(n); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 193 | for (int i = 0; i < n; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | new (fItemArray + fCount + i) T; |
| 195 | } |
| 196 | fCount += n; |
| 197 | } |
| 198 | |
| 199 | void pop_back() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 200 | GrAssert(fCount > 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | --fCount; |
| 202 | fItemArray[fCount].~T(); |
| 203 | checkRealloc(0); |
| 204 | } |
| 205 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 206 | void pop_back_n(int n) { |
| 207 | GrAssert(n >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | GrAssert(fCount >= n); |
| 209 | fCount -= n; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 210 | for (int i = 0; i < n; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | fItemArray[i].~T(); |
| 212 | } |
| 213 | checkRealloc(0); |
| 214 | } |
| 215 | |
| 216 | // pushes or pops from the back to resize |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 217 | void resize_back(int newCount) { |
| 218 | GrAssert(newCount >= 0); |
| 219 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | if (newCount > fCount) { |
| 221 | push_back_n(newCount - fCount); |
| 222 | } else if (newCount < fCount) { |
| 223 | pop_back_n(fCount - newCount); |
| 224 | } |
| 225 | } |
| 226 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 227 | T& operator[] (int i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | GrAssert(i < fCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 229 | GrAssert(i >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 230 | return fItemArray[i]; |
| 231 | } |
| 232 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 233 | const T& operator[] (int i) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 234 | GrAssert(i < fCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 235 | GrAssert(i >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 236 | return fItemArray[i]; |
| 237 | } |
| 238 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 239 | T& front() { GrAssert(fCount > 0); return fItemArray[0];} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 241 | const T& front() const { GrAssert(fCount > 0); return fItemArray[0];} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | |
| 243 | T& back() { GrAssert(fCount); return fItemArray[fCount - 1];} |
| 244 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 245 | const T& back() const { GrAssert(fCount > 0); return fItemArray[fCount - 1];} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 247 | T& fromBack(int i) { |
| 248 | GrAssert(i >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | GrAssert(i < fCount); |
| 250 | return fItemArray[fCount - i - 1]; |
| 251 | } |
| 252 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 253 | const T& fromBack(int i) const { |
| 254 | GrAssert(i >= 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | GrAssert(i < fCount); |
| 256 | return fItemArray[fCount - i - 1]; |
| 257 | } |
| 258 | |
| 259 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 260 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 261 | static const int MIN_ALLOC_COUNT = 8; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 263 | inline void checkRealloc(int delta) { |
| 264 | GrAssert(fCount >= 0); |
| 265 | GrAssert(fAllocCount >= 0); |
| 266 | |
| 267 | GrAssert(-delta <= fCount); |
| 268 | |
| 269 | int newCount = fCount + delta; |
| 270 | int fNewAllocCount = fAllocCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | |
| 272 | if (newCount > fAllocCount) { |
| 273 | fNewAllocCount = GrMax(newCount + ((newCount + 1) >> 1), |
| 274 | fReserveCount); |
| 275 | } else if (newCount < fAllocCount / 3) { |
| 276 | fNewAllocCount = GrMax(fAllocCount / 2, fReserveCount); |
| 277 | } |
| 278 | |
| 279 | if (fNewAllocCount != fAllocCount) { |
| 280 | |
| 281 | fAllocCount = fNewAllocCount; |
| 282 | char* fNewMemArray; |
| 283 | |
| 284 | if (fAllocCount == fReserveCount && NULL != fPreAllocMemArray) { |
| 285 | fNewMemArray = (char*) fPreAllocMemArray; |
| 286 | } else { |
| 287 | fNewMemArray = (char*) GrMalloc(fAllocCount*sizeof(T)); |
| 288 | } |
| 289 | |
| 290 | if (DATA_TYPE) { |
| 291 | memcpy(fNewMemArray, fMemArray, fCount * sizeof(T)); |
| 292 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 293 | for (int i = 0; i < fCount; ++i) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | new (fNewMemArray + sizeof(T) * i) T(fItemArray[i]); |
| 295 | fItemArray[i].~T(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (fMemArray != fPreAllocMemArray) { |
| 300 | GrFree(fMemArray); |
| 301 | } |
| 302 | fMemArray = fNewMemArray; |
| 303 | } |
| 304 | } |
| 305 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 306 | int fReserveCount; |
| 307 | int fCount; |
| 308 | int fAllocCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | void* fPreAllocMemArray; |
| 310 | union { |
| 311 | T* fItemArray; |
| 312 | void* fMemArray; |
| 313 | }; |
| 314 | }; |
| 315 | |
| 316 | #endif |
| 317 | |