| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkTDArray_DEFINED | 10 #ifndef SkTDArray_DEFINED |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 fCount = 0; | 151 fCount = 0; |
| 152 } | 152 } |
| 153 | 153 |
| 154 /** | 154 /** |
| 155 * Sets the number of elements in the array. | 155 * Sets the number of elements in the array. |
| 156 * If the array does not have space for count elements, it will increase | 156 * If the array does not have space for count elements, it will increase |
| 157 * the storage allocated to some amount greater than that required. | 157 * the storage allocated to some amount greater than that required. |
| 158 * It will never shrink the shrink the storage. | 158 * It will never shrink the shrink the storage. |
| 159 */ | 159 */ |
| 160 void setCount(int count) { | 160 void setCount(int count) { |
| 161 // TODO(mtklein): eliminate this method, setCountExact -> setCount | |
| 162 SkASSERT(count >= 0); | 161 SkASSERT(count >= 0); |
| 163 if (count > fReserve) { | 162 if (count > fReserve) { |
| 164 this->resizeStorageToAtLeast(count); | 163 this->resizeStorageToAtLeast(count); |
| 165 } | 164 } |
| 166 fCount = count; | 165 fCount = count; |
| 167 } | 166 } |
| 168 | 167 |
| 169 /** | |
| 170 * Sets the number of elements in the array. | |
| 171 * If the array does not have space for count elements, it will increase | |
| 172 * the storage allocated to exactly the amount required, with no remaining | |
| 173 * reserved space. | |
| 174 * It will never shrink the shrink the storage. | |
| 175 */ | |
| 176 void setCountExact(int count) { | |
| 177 if (count > fReserve) { | |
| 178 this->resizeStorageToExact(count); | |
| 179 } | |
| 180 fCount = count; | |
| 181 } | |
| 182 | |
| 183 void setReserve(int reserve) { | 168 void setReserve(int reserve) { |
| 184 if (reserve > fReserve) { | 169 if (reserve > fReserve) { |
| 185 this->resizeStorageToAtLeast(reserve); | 170 this->resizeStorageToAtLeast(reserve); |
| 186 } | 171 } |
| 187 } | 172 } |
| 188 | 173 |
| 189 T* prepend() { | 174 T* prepend() { |
| 190 this->adjustCount(1); | 175 this->adjustCount(1); |
| 191 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); | 176 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); |
| 192 return fArray; | 177 return fArray; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 | 361 |
| 377 /** | 362 /** |
| 378 * Adjusts the number of elements in the array. | 363 * Adjusts the number of elements in the array. |
| 379 * This is the same as calling setCount(count() + delta). | 364 * This is the same as calling setCount(count() + delta). |
| 380 */ | 365 */ |
| 381 void adjustCount(int delta) { | 366 void adjustCount(int delta) { |
| 382 this->setCount(fCount + delta); | 367 this->setCount(fCount + delta); |
| 383 } | 368 } |
| 384 | 369 |
| 385 /** | 370 /** |
| 386 * This resizes the storage to *exactly* count elements, growing or | |
| 387 * shrinking the allocation as needed. It does not ASSERT anything about | |
| 388 * the previous allocation size, or about fCount. | |
| 389 * | |
| 390 * note: does NOT modify fCount | |
| 391 */ | |
| 392 void resizeStorageToExact(int count) { | |
| 393 SkASSERT(count >= 0); | |
| 394 fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T)); | |
| 395 #ifdef SK_DEBUG | |
| 396 fData = (ArrayT*)fArray; | |
| 397 #endif | |
| 398 fReserve = count; | |
| 399 } | |
| 400 | |
| 401 /** | |
| 402 * Increase the storage allocation such that it can hold (fCount + extra) | 371 * Increase the storage allocation such that it can hold (fCount + extra) |
| 403 * elements. | 372 * elements. |
| 404 * It never shrinks the allocation, and it may increase the allocation by | 373 * It never shrinks the allocation, and it may increase the allocation by |
| 405 * more than is strictly required, based on a private growth heuristic. | 374 * more than is strictly required, based on a private growth heuristic. |
| 406 * | 375 * |
| 407 * note: does NOT modify fCount | 376 * note: does NOT modify fCount |
| 408 */ | 377 */ |
| 409 void resizeStorageToAtLeast(int count) { | 378 void resizeStorageToAtLeast(int count) { |
| 410 SkASSERT(count > fReserve); | 379 SkASSERT(count > fReserve); |
| 411 int space = count + 4; | 380 fReserve = count + 4; |
| 412 space += space>>2; | 381 fReserve += fReserve / 4; |
| 413 this->resizeStorageToExact(space); | 382 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); |
| 383 #ifdef SK_DEBUG |
| 384 fData = (ArrayT*)fArray; |
| 385 #endif |
| 414 } | 386 } |
| 415 }; | 387 }; |
| 416 | 388 |
| 417 #endif | 389 #endif |
| OLD | NEW |