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 |
11 #include <new> | 11 #include <new> |
12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 | 14 |
15 template <typename T, bool MEM_COPY = false> class SkTArray; | 15 template <typename T, bool MEM_COPY = false> class SkTArray; |
16 | 16 |
17 namespace SkTArrayExt { | 17 namespace SkTArrayExt { |
18 | 18 |
19 template<typename T> | 19 template<typename T> |
| 20 inline void copy(SkTArray<T, true>* self, int dst, int src) { |
| 21 memcpy(&self->fItemArray[dst], &self->fItemArray[src], sizeof(T)); |
| 22 } |
| 23 template<typename T> |
20 inline void copy(SkTArray<T, true>* self, const T* array) { | 24 inline void copy(SkTArray<T, true>* self, const T* array) { |
21 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); | 25 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); |
22 } | 26 } |
23 template<typename T> | 27 template<typename T> |
24 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { | 28 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { |
25 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); | 29 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); |
26 } | 30 } |
27 | 31 |
28 template<typename T> | 32 template<typename T> |
| 33 inline void copy(SkTArray<T, false>* self, int dst, int src) { |
| 34 SkNEW_PLACEMENT_ARGS(&self->fItemArray[dst], T, (self->fItemArray[src])); |
| 35 } |
| 36 template<typename T> |
29 inline void copy(SkTArray<T, false>* self, const T* array) { | 37 inline void copy(SkTArray<T, false>* self, const T* array) { |
30 for (int i = 0; i < self->fCount; ++i) { | 38 for (int i = 0; i < self->fCount; ++i) { |
31 SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i])); | 39 SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i])); |
32 } | 40 } |
33 } | 41 } |
34 template<typename T> | 42 template<typename T> |
35 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { | 43 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { |
36 for (int i = 0; i < self->fCount; ++i) { | 44 for (int i = 0; i < self->fCount; ++i) { |
37 SkNEW_PLACEMENT_ARGS(newMemArray + sizeof(T) * i, T, (self->fItemArray[i
])); | 45 SkNEW_PLACEMENT_ARGS(newMemArray + sizeof(T) * i, T, (self->fItemArray[i
])); |
38 self->fItemArray[i].~T(); | 46 self->fItemArray[i].~T(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 /** | 141 /** |
134 * Resets to a copy of a C array. | 142 * Resets to a copy of a C array. |
135 */ | 143 */ |
136 void reset(const T* array, int count) { | 144 void reset(const T* array, int count) { |
137 for (int i = 0; i < fCount; ++i) { | 145 for (int i = 0; i < fCount; ++i) { |
138 fItemArray[i].~T(); | 146 fItemArray[i].~T(); |
139 } | 147 } |
140 int delta = count - fCount; | 148 int delta = count - fCount; |
141 this->checkRealloc(delta); | 149 this->checkRealloc(delta); |
142 fCount = count; | 150 fCount = count; |
143 for (int i = 0; i < count; ++i) { | 151 SkTArrayExt::copy(this, array); |
144 SkTArrayExt::copy(this, array); | 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) { |
| 160 SkTArrayExt::copy(this, n, newCount); |
| 161 fItemArray[newCount].~T(); |
145 } | 162 } |
146 } | 163 } |
147 | 164 |
148 /** | 165 /** |
149 * Number of elements in the array. | 166 * Number of elements in the array. |
150 */ | 167 */ |
151 int count() const { return fCount; } | 168 int count() const { return fCount; } |
152 | 169 |
153 /** | 170 /** |
154 * Is the array empty. | 171 * Is the array empty. |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 437 |
421 if (fMemArray != fPreAllocMemArray) { | 438 if (fMemArray != fPreAllocMemArray) { |
422 sk_free(fMemArray); | 439 sk_free(fMemArray); |
423 } | 440 } |
424 fMemArray = newMemArray; | 441 fMemArray = newMemArray; |
425 } | 442 } |
426 } | 443 } |
427 | 444 |
428 friend void* operator new<T>(size_t, SkTArray*, int); | 445 friend void* operator new<T>(size_t, SkTArray*, int); |
429 | 446 |
| 447 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
int dst, int src); |
430 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
const X*); | 448 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
const X*); |
431 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true
>* that, char*); | 449 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true
>* that, char*); |
432 | 450 |
| 451 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
int dst, int src); |
433 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
const X*); | 452 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
const X*); |
434 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, fals
e>* that, char*); | 453 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, fals
e>* that, char*); |
435 | 454 |
436 int fReserveCount; | 455 int fReserveCount; |
437 int fCount; | 456 int fCount; |
438 int fAllocCount; | 457 int fAllocCount; |
439 void* fPreAllocMemArray; | 458 void* fPreAllocMemArray; |
440 union { | 459 union { |
441 T* fItemArray; | 460 T* fItemArray; |
442 void* fMemArray; | 461 void* fMemArray; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 SkSTArray& operator= (const INHERITED& array) { | 520 SkSTArray& operator= (const INHERITED& array) { |
502 INHERITED::operator=(array); | 521 INHERITED::operator=(array); |
503 return *this; | 522 return *this; |
504 } | 523 } |
505 | 524 |
506 private: | 525 private: |
507 SkAlignedSTStorage<N,T> fStorage; | 526 SkAlignedSTStorage<N,T> fStorage; |
508 }; | 527 }; |
509 | 528 |
510 #endif | 529 #endif |
OLD | NEW |