diff options
author | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-21 19:39:02 +0000 |
---|---|---|
committer | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-21 19:39:02 +0000 |
commit | 95ebd17cf4f66952862289882ee5ac00da31cb8a (patch) | |
tree | c9f257f7a39deeb036fc3295fc11e99c2cf079ef /include | |
parent | b463d5668a498b672b80047f09901981afe513ed (diff) |
Add removeShuffle to SkTArray and add SkTArray tests.
R=reed@google.com
Review URL: https://codereview.chromium.org/208153008
git-svn-id: http://skia.googlecode.com/svn/trunk@13895 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkTArray.h | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/include/core/SkTArray.h b/include/core/SkTArray.h index f7bdc5a158..31bcabae18 100644 --- a/include/core/SkTArray.h +++ b/include/core/SkTArray.h @@ -17,6 +17,10 @@ template <typename T, bool MEM_COPY = false> class SkTArray; namespace SkTArrayExt { template<typename T> +inline void copy(SkTArray<T, true>* self, int dst, int src) { + memcpy(&self->fItemArray[dst], &self->fItemArray[src], sizeof(T)); +} +template<typename T> inline void copy(SkTArray<T, true>* self, const T* array) { memcpy(self->fMemArray, array, self->fCount * sizeof(T)); } @@ -26,6 +30,10 @@ inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { } template<typename T> +inline void copy(SkTArray<T, false>* self, int dst, int src) { + SkNEW_PLACEMENT_ARGS(&self->fItemArray[dst], T, (self->fItemArray[src])); +} +template<typename T> inline void copy(SkTArray<T, false>* self, const T* array) { for (int i = 0; i < self->fCount; ++i) { SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i])); @@ -140,8 +148,17 @@ public: int delta = count - fCount; this->checkRealloc(delta); fCount = count; - for (int i = 0; i < count; ++i) { - SkTArrayExt::copy(this, array); + SkTArrayExt::copy(this, array); + } + + void removeShuffle(int n) { + SkASSERT(n < fCount); + int newCount = fCount - 1; + fCount = newCount; + fItemArray[n].~T(); + if (n != newCount) { + SkTArrayExt::copy(this, n, newCount); + fItemArray[newCount].~T(); } } @@ -427,9 +444,11 @@ private: friend void* operator new<T>(size_t, SkTArray*, int); + template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that, int dst, int src); template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that, const X*); template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true>* that, char*); + template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that, int dst, int src); template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that, const X*); template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, false>* that, char*); |