OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTArray_DEFINED | 8 #ifndef SkTArray_DEFINED |
9 #define SkTArray_DEFINED | 9 #define SkTArray_DEFINED |
10 | 10 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 void resize_back(int newCount) { | 263 void resize_back(int newCount) { |
264 SkASSERT(newCount >= 0); | 264 SkASSERT(newCount >= 0); |
265 | 265 |
266 if (newCount > fCount) { | 266 if (newCount > fCount) { |
267 this->push_back_n(newCount - fCount); | 267 this->push_back_n(newCount - fCount); |
268 } else if (newCount < fCount) { | 268 } else if (newCount < fCount) { |
269 this->pop_back_n(fCount - newCount); | 269 this->pop_back_n(fCount - newCount); |
270 } | 270 } |
271 } | 271 } |
272 | 272 |
| 273 /** Swaps the contents of this array with that array. Does a pointer swap if
possible, |
| 274 otherwise copies the T values. */ |
| 275 void swap(SkTArray* that) { |
| 276 if (this->fPreAllocMemArray != this->fItemArray && |
| 277 that->fPreAllocMemArray != that->fItemArray) { |
| 278 // If neither is using a preallocated array then just swap. |
| 279 SkTSwap(fItemArray, that->fItemArray); |
| 280 SkTSwap(fCount, that->fCount); |
| 281 SkTSwap(fAllocCount, that->fAllocCount); |
| 282 } else { |
| 283 // This could be more optimal... |
| 284 SkTArray copy(*that); |
| 285 *that = *this; |
| 286 *this = copy; |
| 287 } |
| 288 } |
| 289 |
273 T* begin() { | 290 T* begin() { |
274 return fItemArray; | 291 return fItemArray; |
275 } | 292 } |
276 const T* begin() const { | 293 const T* begin() const { |
277 return fItemArray; | 294 return fItemArray; |
278 } | 295 } |
279 T* end() { | 296 T* end() { |
280 return fItemArray ? fItemArray + fCount : NULL; | 297 return fItemArray ? fItemArray + fCount : NULL; |
281 } | 298 } |
282 const T* end() const { | 299 const T* end() const { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 * Copy a C array, using preallocated storage if preAllocCount >= | 385 * Copy a C array, using preallocated storage if preAllocCount >= |
369 * count. Otherwise storage will only be used when array shrinks | 386 * count. Otherwise storage will only be used when array shrinks |
370 * to fit. | 387 * to fit. |
371 */ | 388 */ |
372 template <int N> | 389 template <int N> |
373 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) { | 390 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) { |
374 this->init(array, count, storage->get(), N); | 391 this->init(array, count, storage->get(), N); |
375 } | 392 } |
376 | 393 |
377 void init(const T* array, int count, | 394 void init(const T* array, int count, |
378 void* preAllocStorage, int preAllocOrReserveCount) { | 395 void* preAllocStorage, int preAllocOrReserveCount) { |
379 SkASSERT(count >= 0); | 396 SkASSERT(count >= 0); |
380 SkASSERT(preAllocOrReserveCount >= 0); | 397 SkASSERT(preAllocOrReserveCount >= 0); |
381 fCount = count; | 398 fCount = count; |
382 fReserveCount = (preAllocOrReserveCount > 0) ? | 399 fReserveCount = (preAllocOrReserveCount > 0) ? |
383 preAllocOrReserveCount : | 400 preAllocOrReserveCount : |
384 gMIN_ALLOC_COUNT; | 401 gMIN_ALLOC_COUNT; |
385 fPreAllocMemArray = preAllocStorage; | 402 fPreAllocMemArray = preAllocStorage; |
386 if (fReserveCount >= fCount && | 403 if (fReserveCount >= fCount && |
387 preAllocStorage) { | 404 preAllocStorage) { |
388 fAllocCount = fReserveCount; | 405 fAllocCount = fReserveCount; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 friend void* operator new<T>(size_t, SkTArray*, int); | 462 friend void* operator new<T>(size_t, SkTArray*, int); |
446 | 463 |
447 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
int dst, int src); | 464 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
int dst, int src); |
448 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
const X*); | 465 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
const X*); |
449 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true
>* that, char*); | 466 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true
>* that, char*); |
450 | 467 |
451 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
int dst, int src); | 468 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
int dst, int src); |
452 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
const X*); | 469 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
const X*); |
453 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, fals
e>* that, char*); | 470 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, fals
e>* that, char*); |
454 | 471 |
455 int fReserveCount; | 472 int fReserveCount; |
456 int fCount; | 473 int fCount; |
457 int fAllocCount; | 474 int fAllocCount; |
458 void* fPreAllocMemArray; | 475 void* fPreAllocMemArray; |
459 union { | 476 union { |
460 T* fItemArray; | 477 T* fItemArray; |
461 void* fMemArray; | 478 void* fMemArray; |
462 }; | 479 }; |
463 }; | 480 }; |
464 | 481 |
465 // Use the below macro (SkNEW_APPEND_TO_TARRAY) rather than calling this directl
y | 482 // Use the below macro (SkNEW_APPEND_TO_TARRAY) rather than calling this directl
y |
466 template <typename T, bool MEM_COPY> | 483 template <typename T, bool MEM_COPY> |
467 void* operator new(size_t, SkTArray<T, MEM_COPY>* array, int SkDEBUGCODE(atIndex
)) { | 484 void* operator new(size_t, SkTArray<T, MEM_COPY>* array, int SkDEBUGCODE(atIndex
)) { |
468 // Currently, we only support adding to the end of the array. When the array
class itself | 485 // Currently, we only support adding to the end of the array. When the array
class itself |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 SkSTArray& operator= (const INHERITED& array) { | 537 SkSTArray& operator= (const INHERITED& array) { |
521 INHERITED::operator=(array); | 538 INHERITED::operator=(array); |
522 return *this; | 539 return *this; |
523 } | 540 } |
524 | 541 |
525 private: | 542 private: |
526 SkAlignedSTStorage<N,T> fStorage; | 543 SkAlignedSTStorage<N,T> fStorage; |
527 }; | 544 }; |
528 | 545 |
529 #endif | 546 #endif |
OLD | NEW |