michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkTDArray_Experimental_DEFINED michael@0: #define SkTDArray_Experimental_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: #ifdef SK_BUILD_FOR_UNIX michael@0: #define SK_BUILD_FOR_ADS_12 michael@0: #endif michael@0: michael@0: #if !defined(SK_BUILD_FOR_ADS_12) && !defined(__x86_64__) michael@0: #define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 1 michael@0: #else michael@0: #define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 0 michael@0: #endif michael@0: michael@0: #if SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT == 0 michael@0: #include "SkTDArray.h" michael@0: #define SkIntArray(type) SkTDArray michael@0: #define SkLongArray(type) SkTDArray michael@0: #else michael@0: michael@0: class SkDS32Array { michael@0: protected: michael@0: SkDS32Array(); michael@0: SkDS32Array(const SkDS32Array& src); michael@0: SkDS32Array(const int32_t src[], U16CPU count); michael@0: SkDS32Array& operator=(const SkDS32Array& src); michael@0: friend int operator==(const SkDS32Array& a, const SkDS32Array& b); michael@0: int32_t* append() { return this->append(1, NULL); } michael@0: int32_t* append(U16CPU count, const int32_t* src = NULL); michael@0: michael@0: int32_t* appendClear() michael@0: { michael@0: int32_t* result = this->append(); michael@0: *result = 0; michael@0: return result; michael@0: } michael@0: michael@0: int find(const int32_t& elem) const; michael@0: int32_t* insert(U16CPU index, U16CPU count, const int32_t* src); michael@0: int rfind(const int32_t& elem) const; michael@0: void swap(SkDS32Array& other); michael@0: public: michael@0: bool isEmpty() const { return fCount == 0; } michael@0: int count() const { return fCount; } michael@0: michael@0: void remove(U16CPU index, U16CPU count = 1) michael@0: { michael@0: SkASSERT(index + count <= fCount); michael@0: fCount = SkToU16(fCount - count); michael@0: memmove(fArray + index, fArray + index + count, sizeof(int32_t) * (fCount - index)); michael@0: } michael@0: michael@0: void reset() michael@0: { michael@0: if (fArray) michael@0: { michael@0: sk_free(fArray); michael@0: fArray = NULL; michael@0: #ifdef SK_DEBUG michael@0: fData = NULL; michael@0: #endif michael@0: fReserve = fCount = 0; michael@0: } michael@0: else michael@0: { michael@0: SkASSERT(fReserve == 0 && fCount == 0); michael@0: } michael@0: } michael@0: michael@0: void setCount(U16CPU count) michael@0: { michael@0: if (count > fReserve) michael@0: this->growBy(count - fCount); michael@0: else michael@0: fCount = SkToU16(count); michael@0: } michael@0: protected: michael@0: #ifdef SK_DEBUG michael@0: enum { michael@0: kDebugArraySize = 24 michael@0: }; michael@0: int32_t(* fData)[kDebugArraySize]; michael@0: #endif michael@0: int32_t* fArray; michael@0: uint16_t fReserve, fCount; michael@0: void growBy(U16CPU extra); michael@0: }; michael@0: michael@0: #ifdef SK_DEBUG michael@0: #define SYNC() fTData = (T (*)[kDebugArraySize]) fArray michael@0: #else michael@0: #define SYNC() michael@0: #endif michael@0: michael@0: template class SkTDS32Array : public SkDS32Array { michael@0: public: michael@0: SkTDS32Array() { SkDEBUGCODE(fTData=NULL); SkASSERT(sizeof(T) == sizeof(int32_t)); } michael@0: SkTDS32Array(const SkTDS32Array& src) : SkDS32Array(src) {} michael@0: ~SkTDS32Array() { sk_free(fArray); } michael@0: T& operator[](int index) const { SYNC(); SkASSERT((unsigned)index < fCount); return ((T*) fArray)[index]; } michael@0: SkTDS32Array& operator=(const SkTDS32Array& src) { michael@0: return (SkTDS32Array&) SkDS32Array::operator=(src); } michael@0: friend int operator==(const SkTDS32Array& a, const SkTDS32Array& b) { michael@0: return operator==((const SkDS32Array&) a, (const SkDS32Array&) b); } michael@0: T* append() { return (T*) SkDS32Array::append(); } michael@0: T* appendClear() { return (T*) SkDS32Array::appendClear(); } michael@0: T* append(U16CPU count, const T* src = NULL) { return (T*) SkDS32Array::append(count, (const int32_t*) src); } michael@0: T* begin() const { SYNC(); return (T*) fArray; } michael@0: T* end() const { return (T*) (fArray ? fArray + fCount : NULL); } michael@0: int find(const T& elem) const { return SkDS32Array::find((const int32_t&) elem); } michael@0: T* insert(U16CPU index) { return this->insert(index, 1, NULL); } michael@0: T* insert(U16CPU index, U16CPU count, const T* src = NULL) { michael@0: return (T*) SkDS32Array::insert(index, count, (const int32_t*) src); } michael@0: int rfind(const T& elem) const { return SkDS32Array::rfind((const int32_t&) elem); } michael@0: T* push() { return this->append(); } michael@0: void push(T& elem) { *this->append() = elem; } michael@0: const T& top() const { return (*this)[fCount - 1]; } michael@0: T& top() { return (*this)[fCount - 1]; } michael@0: void pop(T* elem) { if (elem) *elem = (*this)[fCount - 1]; --fCount; } michael@0: void pop() { --fCount; } michael@0: private: michael@0: #ifdef SK_DEBUG michael@0: mutable T(* fTData)[kDebugArraySize]; michael@0: #endif michael@0: }; michael@0: michael@0: #define SkIntArray(type) SkTDS32Array // holds 32 bit data types michael@0: #define SkLongArray(type) SkTDS32Array michael@0: michael@0: #endif // SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT michael@0: michael@0: #endif // SkTDArray_Experimental_DEFINED