180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkTDArray_Experimental_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkTDArray_Experimental_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTypes.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_BUILD_FOR_UNIX
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_BUILD_FOR_ADS_12
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger#if !defined(SK_BUILD_FOR_ADS_12) && !defined(__x86_64__)
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 1
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT 0
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT == 0
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h"
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkIntArray(type) SkTDArray<type>
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkLongArray(type) SkTDArray<type>
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkDS32Array {
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDS32Array();
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDS32Array(const SkDS32Array& src);
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDS32Array(const int32_t src[], U16CPU count);
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkDS32Array& operator=(const SkDS32Array& src);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    friend int operator==(const SkDS32Array& a, const SkDS32Array& b);
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t* append() { return this->append(1, NULL); }
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t* append(U16CPU count, const int32_t* src = NULL);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t* appendClear()
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        int32_t* result = this->append();
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        *result = 0;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return result;
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int find(const int32_t& elem) const;
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t* insert(U16CPU index, U16CPU count, const int32_t* src);
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int rfind(const int32_t& elem) const;
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void swap(SkDS32Array& other);
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool isEmpty() const { return fCount == 0; }
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int count() const { return fCount; }
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void remove(U16CPU index, U16CPU count = 1)
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkASSERT(index + count <= fCount);
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fCount = SkToU16(fCount - count);
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        memmove(fArray + index, fArray + index + count, sizeof(int32_t) * (fCount - index));
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void reset()
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (fArray)
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            sk_free(fArray);
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fArray = NULL;
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_DEBUG
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fData = NULL;
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fReserve = fCount = 0;
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        else
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkASSERT(fReserve == 0 && fCount == 0);
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setCount(U16CPU count)
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (count > fReserve)
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            this->growBy(count - fCount);
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        else
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fCount = SkToU16(count);
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_DEBUG
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum {
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kDebugArraySize = 24
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t(* fData)[kDebugArraySize];
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int32_t*    fArray;
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint16_t    fReserve, fCount;
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void growBy(U16CPU extra);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_DEBUG
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #define SYNC() fTData = (T (*)[kDebugArraySize]) fArray
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #define SYNC()
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutemplate <typename T> class SkTDS32Array : public SkDS32Array {
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDS32Array() { SkDEBUGCODE(fTData=NULL); SkASSERT(sizeof(T) == sizeof(int32_t)); }
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDS32Array(const SkTDS32Array<T>& src) : SkDS32Array(src) {}
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ~SkTDS32Array() { sk_free(fArray); }
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T&  operator[](int index) const { SYNC(); SkASSERT((unsigned)index < fCount); return ((T*) fArray)[index]; }
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDS32Array<T>& operator=(const SkTDS32Array<T>& src) {
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return (SkTDS32Array<T>&) SkDS32Array::operator=(src); }
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    friend int operator==(const SkTDS32Array<T>& a, const SkTDS32Array<T>& b) {
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return operator==((const SkDS32Array&) a, (const SkDS32Array&) b); }
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T* append() { return (T*) SkDS32Array::append(); }
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T* appendClear() { return (T*) SkDS32Array::appendClear(); }
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T* append(U16CPU count, const T* src = NULL) { return (T*) SkDS32Array::append(count, (const int32_t*) src); }
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T*  begin() const { SYNC(); return (T*) fArray; }
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T*  end() const { return (T*) (fArray ? fArray + fCount : NULL); }
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int find(const T& elem) const { return SkDS32Array::find((const int32_t&) elem); }
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T* insert(U16CPU index) { return this->insert(index, 1, NULL); }
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T* insert(U16CPU index, U16CPU count, const T* src = NULL) {
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return (T*) SkDS32Array::insert(index, count, (const int32_t*) src); }
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int rfind(const T& elem) const { return SkDS32Array::rfind((const int32_t&) elem); }
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T*          push() { return this->append(); }
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void        push(T& elem) { *this->append() = elem; }
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const T&    top() const { return (*this)[fCount - 1]; }
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    T&          top() { return (*this)[fCount - 1]; }
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void        pop(T* elem) { if (elem) *elem = (*this)[fCount - 1]; --fCount; }
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void        pop() { --fCount; }
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_DEBUG
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    mutable T(* fTData)[kDebugArraySize];
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkIntArray(type) SkTDS32Array<type> // holds 32 bit data types
138096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger#define SkLongArray(type) SkTDS32Array<type>
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif // SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif // SkTDArray_Experimental_DEFINED
143