1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkTypedArray.h"
11
12SkTypedArray::SkTypedArray() : fType(SkType_Unknown) {
13}
14
15SkTypedArray::SkTypedArray(SkDisplayTypes type) : fType(type) {
16}
17
18bool SkTypedArray::getIndex(int index, SkOperand* operand) {
19    if (index >= count()) {
20        SkASSERT(0);
21        return false;
22    }
23    *operand = begin()[index];
24    return true;
25}
26
27
28#if SK_SMALLER_ARRAY_TEMPLATE_EXPERIMENT == 1
29SkDS32Array::SkDS32Array()
30{
31    fReserve = fCount = 0;
32    fArray = NULL;
33#ifdef SK_DEBUG
34    fData = NULL;
35#endif
36}
37
38SkDS32Array::SkDS32Array(const SkDS32Array& src)
39{
40    fReserve = fCount = 0;
41    fArray = NULL;
42#ifdef SK_DEBUG
43    fData = NULL;
44#endif
45    SkDS32Array tmp(src.fArray, src.fCount);
46    this->swap(tmp);
47}
48
49SkDS32Array::SkDS32Array(const int32_t src[], U16CPU count)
50{
51    SkASSERT(src || count == 0);
52
53    fReserve = fCount = 0;
54    fArray = NULL;
55#ifdef SK_DEBUG
56    fData = NULL;
57#endif
58    if (count)
59    {
60        fArray = (int32_t*)sk_malloc_throw(count * sizeof(int32_t));
61#ifdef SK_DEBUG
62        fData = (int32_t (*)[kDebugArraySize]) fArray;
63#endif
64        memcpy(fArray, src, sizeof(int32_t) * count);
65        fReserve = fCount = SkToU16(count);
66    }
67}
68
69SkDS32Array& SkDS32Array::operator=(const SkDS32Array& src)
70{
71    if (this != &src)
72    {
73        if (src.fCount > fReserve)
74        {
75            SkDS32Array tmp(src.fArray, src.fCount);
76            this->swap(tmp);
77        }
78        else
79        {
80            memcpy(fArray, src.fArray, sizeof(int32_t) * src.fCount);
81            fCount = src.fCount;
82        }
83    }
84    return *this;
85}
86
87int operator==(const SkDS32Array& a, const SkDS32Array& b)
88{
89    return a.fCount == b.fCount &&
90            (a.fCount == 0 || !memcmp(a.fArray, b.fArray, a.fCount * sizeof(int32_t)));
91}
92
93void SkDS32Array::swap(SkDS32Array& other)
94{
95    SkTSwap(fArray, other.fArray);
96#ifdef SK_DEBUG
97    SkTSwap(fData, other.fData);
98#endif
99    SkTSwap(fReserve, other.fReserve);
100    SkTSwap(fCount, other.fCount);
101}
102
103int32_t* SkDS32Array::append(U16CPU count, const int32_t* src)
104{
105    unsigned oldCount = fCount;
106    if (count)
107    {
108        SkASSERT(src == NULL || fArray == NULL ||
109                src + count <= fArray || fArray + count <= src);
110
111        this->growBy(count);
112        if (src)
113            memcpy(fArray + oldCount, src, sizeof(int32_t) * count);
114    }
115    return fArray + oldCount;
116}
117
118int SkDS32Array::find(const int32_t& elem) const
119{
120    const int32_t* iter = fArray;
121    const int32_t* stop = fArray + fCount;
122
123    for (; iter < stop; iter++)
124    {
125        if (*iter == elem)
126            return (int) (iter - fArray);
127    }
128    return -1;
129}
130
131void SkDS32Array::growBy(U16CPU extra)
132{
133    SkASSERT(extra);
134    SkASSERT(fCount + extra <= 0xFFFF);
135
136    if (fCount + extra > fReserve)
137    {
138        size_t size = fCount + extra + 4;
139        size += size >> 2;
140        int32_t* array = (int32_t*)sk_malloc_throw(size * sizeof(int32_t));
141        memcpy(array, fArray, fCount * sizeof(int32_t));
142
143        sk_free(fArray);
144        fArray = array;
145#ifdef SK_DEBUG
146        fData = (int32_t (*)[kDebugArraySize]) fArray;
147#endif
148        fReserve = SkToU16((U16CPU)size);
149    }
150    fCount = SkToU16(fCount + extra);
151}
152
153int32_t* SkDS32Array::insert(U16CPU index, U16CPU count, const int32_t* src)
154{
155    SkASSERT(count);
156    int oldCount = fCount;
157    this->growBy(count);
158    int32_t* dst = fArray + index;
159    memmove(dst + count, dst, sizeof(int32_t) * (oldCount - index));
160    if (src)
161        memcpy(dst, src, sizeof(int32_t) * count);
162    return dst;
163}
164
165
166    int SkDS32Array::rfind(const int32_t& elem) const
167    {
168        const int32_t* iter = fArray + fCount;
169        const int32_t* stop = fArray;
170
171        while (iter > stop)
172        {
173            if (*--iter == elem)
174                return (int) (iter - stop);
175        }
176        return -1;
177    }
178
179#endif
180