reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/animator/SkTDArray_Experimental.h |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef SkTDArray_Experimental_DEFINED |
| 19 | #define SkTDArray_Experimental_DEFINED |
| 20 | |
| 21 | #include "SkTypes.h" |
| 22 | |
| 23 | #ifdef SK_BUILD_FOR_UNIX |
| 24 | #define SK_BUILD_FOR_ADS_12 |
| 25 | #endif |
| 26 | |
| 27 | #ifndef SK_BUILD_FOR_ADS_12 |
| 28 | #define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 1 |
| 29 | #else |
| 30 | #define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 0 |
| 31 | #endif |
| 32 | |
| 33 | #if SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT == 0 |
| 34 | #include "SkTDArray.h" |
| 35 | #define SkIntArray(type) SkTDArray<type> |
| 36 | #define SkLongArray(type) SkTDArray<type> |
| 37 | #else |
| 38 | |
| 39 | class SkDS32Array { |
| 40 | protected: |
| 41 | SkDS32Array(); |
| 42 | SkDS32Array(const SkDS32Array& src); |
| 43 | SkDS32Array(const int32_t src[], U16CPU count); |
| 44 | SkDS32Array& operator=(const SkDS32Array& src); |
| 45 | friend int operator==(const SkDS32Array& a, const SkDS32Array& b); |
| 46 | int32_t* append() { return this->append(1, NULL); } |
| 47 | int32_t* append(U16CPU count, const int32_t* src = NULL); |
| 48 | |
| 49 | int32_t* appendClear() |
| 50 | { |
| 51 | int32_t* result = this->append(); |
| 52 | *result = 0; |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | int find(const int32_t& elem) const; |
| 57 | int32_t* insert(U16CPU index, U16CPU count, const int32_t* src); |
| 58 | int rfind(const int32_t& elem) const; |
| 59 | void swap(SkDS32Array& other); |
| 60 | public: |
| 61 | bool isEmpty() const { return fCount == 0; } |
| 62 | int count() const { return fCount; } |
| 63 | |
| 64 | void remove(U16CPU index, U16CPU count = 1) |
| 65 | { |
| 66 | SkASSERT(index + count <= fCount); |
| 67 | fCount = SkToU16(fCount - count); |
| 68 | memmove(fArray + index, fArray + index + count, sizeof(int32_t) * (fCount - index)); |
| 69 | } |
| 70 | |
| 71 | void reset() |
| 72 | { |
| 73 | if (fArray) |
| 74 | { |
| 75 | sk_free(fArray); |
| 76 | fArray = NULL; |
| 77 | #ifdef SK_DEBUG |
| 78 | fData = NULL; |
| 79 | #endif |
| 80 | fReserve = fCount = 0; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | SkASSERT(fReserve == 0 && fCount == 0); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void setCount(U16CPU count) |
| 89 | { |
| 90 | if (count > fReserve) |
| 91 | this->growBy(count - fCount); |
| 92 | else |
| 93 | fCount = SkToU16(count); |
| 94 | } |
| 95 | protected: |
| 96 | #ifdef SK_DEBUG |
| 97 | enum { |
| 98 | kDebugArraySize = 24 |
| 99 | }; |
| 100 | int32_t(* fData)[kDebugArraySize]; |
| 101 | #endif |
| 102 | int32_t* fArray; |
| 103 | uint16_t fReserve, fCount; |
| 104 | void growBy(U16CPU extra); |
| 105 | }; |
| 106 | |
| 107 | #ifdef SK_DEBUG |
| 108 | #define SYNC() fTData = (T (*)[kDebugArraySize]) fArray |
| 109 | #else |
| 110 | #define SYNC() |
| 111 | #endif |
| 112 | |
| 113 | template <typename T> class SkTDS32Array : public SkDS32Array { |
| 114 | public: |
| 115 | SkTDS32Array() { SkDEBUGCODE(fTData=NULL); SkASSERT(sizeof(T) == sizeof(int32_t)); } |
| 116 | SkTDS32Array(const SkTDS32Array<T>& src) : SkDS32Array(src) {} |
| 117 | ~SkTDS32Array() { sk_free(fArray); } |
| 118 | T& operator[](int index) const { SYNC(); SkASSERT((unsigned)index < fCount); return ((T*) fArray)[index]; } |
| 119 | SkTDS32Array<T>& operator=(const SkTDS32Array<T>& src) { |
| 120 | return (SkTDS32Array<T>&) SkDS32Array::operator=(src); } |
| 121 | friend int operator==(const SkTDS32Array<T>& a, const SkTDS32Array<T>& b) { |
| 122 | return operator==((const SkDS32Array&) a, (const SkDS32Array&) b); } |
| 123 | T* append() { return (T*) SkDS32Array::append(); } |
| 124 | T* appendClear() { return (T*) SkDS32Array::appendClear(); } |
| 125 | T* append(U16CPU count, const T* src = NULL) { return (T*) SkDS32Array::append(count, (const int32_t*) src); } |
| 126 | T* begin() const { SYNC(); return (T*) fArray; } |
| 127 | T* end() const { return (T*) (fArray ? fArray + fCount : NULL); } |
| 128 | int find(const T& elem) const { return SkDS32Array::find((const int32_t&) elem); } |
| 129 | T* insert(U16CPU index) { return this->insert(index, 1, NULL); } |
| 130 | T* insert(U16CPU index, U16CPU count, const T* src = NULL) { |
| 131 | return (T*) SkDS32Array::insert(index, count, (const int32_t*) src); } |
| 132 | int rfind(const T& elem) const { return SkDS32Array::rfind((const int32_t&) elem); } |
| 133 | T* push() { return this->append(); } |
| 134 | void push(T& elem) { *this->append() = elem; } |
| 135 | const T& top() const { return (*this)[fCount - 1]; } |
| 136 | T& top() { return (*this)[fCount - 1]; } |
| 137 | void pop(T* elem) { if (elem) *elem = (*this)[fCount - 1]; --fCount; } |
| 138 | void pop() { --fCount; } |
| 139 | private: |
| 140 | #ifdef SK_DEBUG |
| 141 | mutable T(* fTData)[kDebugArraySize]; |
| 142 | #endif |
| 143 | }; |
| 144 | |
| 145 | #define SkIntArray(type) SkTDS32Array<type> // holds 32 bit data types |
| 146 | #define SkLongArray(type) SkTDS32Array<type> // holds 32/64 bit data types depending on pointer size |
| 147 | |
| 148 | #endif // SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT |
| 149 | |
| 150 | #endif // SkTDArray_Experimental_DEFINED |