| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkTDArray_DEFINED | 10 #ifndef SkTDArray_DEFINED |
| 11 #define SkTDArray_DEFINED | 11 #define SkTDArray_DEFINED |
| 12 | 12 |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 | 14 |
| 15 template <typename T> class SK_API SkTDArray { | 15 template <typename T> class SK_API SkTDArray { |
| 16 public: | 16 public: |
| 17 SkTDArray() { | 17 SkTDArray() { |
| 18 fReserve = fCount = 0; | 18 fReserve = fCount = 0; |
| 19 fArray = NULL; | 19 fArray = NULL; |
| 20 #ifdef SK_DEBUG | 20 #ifdef SK_DEBUG |
| 21 fData = NULL; | 21 fData = NULL; |
| 22 #endif | 22 #endif |
| 23 } | 23 } |
| 24 SkTDArray(const T src[], size_t count) { | 24 SkTDArray(const T src[], int count) { |
| 25 SkASSERT(src || count == 0); | 25 SkASSERT(src || count == 0); |
| 26 | 26 |
| 27 fReserve = fCount = 0; | 27 fReserve = fCount = 0; |
| 28 fArray = NULL; | 28 fArray = NULL; |
| 29 #ifdef SK_DEBUG | 29 #ifdef SK_DEBUG |
| 30 fData = NULL; | 30 fData = NULL; |
| 31 #endif | 31 #endif |
| 32 if (count) { | 32 if (count) { |
| 33 fArray = (T*)sk_malloc_throw(count * sizeof(T)); | 33 fArray = (T*)sk_malloc_throw(count * sizeof(T)); |
| 34 #ifdef SK_DEBUG | 34 #ifdef SK_DEBUG |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 fReserve = fCount = 0; | 91 fReserve = fCount = 0; |
| 92 SkDEBUGCODE(fData = NULL;) | 92 SkDEBUGCODE(fData = NULL;) |
| 93 return array; | 93 return array; |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool isEmpty() const { return fCount == 0; } | 96 bool isEmpty() const { return fCount == 0; } |
| 97 | 97 |
| 98 /** | 98 /** |
| 99 * Return the number of elements in the array | 99 * Return the number of elements in the array |
| 100 */ | 100 */ |
| 101 int count() const { return (int)fCount; } | 101 int count() const { return fCount; } |
| 102 | 102 |
| 103 /** | 103 /** |
| 104 * return the number of bytes in the array: count * sizeof(T) | 104 * return the number of bytes in the array: count * sizeof(T) |
| 105 */ | 105 */ |
| 106 size_t bytes() const { return fCount * sizeof(T); } | 106 size_t bytes() const { return fCount * sizeof(T); } |
| 107 | 107 |
| 108 T* begin() { return fArray; } | 108 T* begin() { return fArray; } |
| 109 const T* begin() const { return fArray; } | 109 const T* begin() const { return fArray; } |
| 110 T* end() { return fArray ? fArray + fCount : NULL; } | 110 T* end() { return fArray ? fArray + fCount : NULL; } |
| 111 const T* end() const { return fArray ? fArray + fCount : NULL; } | 111 const T* end() const { return fArray ? fArray + fCount : NULL; } |
| 112 | 112 |
| 113 T& operator[](int index) { | 113 T& operator[](int index) { |
| 114 SkASSERT((unsigned)index < fCount); | 114 SkASSERT(index < fCount); |
| 115 return fArray[index]; | 115 return fArray[index]; |
| 116 } | 116 } |
| 117 const T& operator[](int index) const { | 117 const T& operator[](int index) const { |
| 118 SkASSERT((unsigned)index < fCount); | 118 SkASSERT(index < fCount); |
| 119 return fArray[index]; | 119 return fArray[index]; |
| 120 } | 120 } |
| 121 | 121 |
| 122 T& getAt(int index) { | 122 T& getAt(int index) { |
| 123 return (*this)[index]; | 123 return (*this)[index]; |
| 124 } | 124 } |
| 125 const T& getAt(int index) const { | 125 const T& getAt(int index) const { |
| 126 return (*this)[index]; | 126 return (*this)[index]; |
| 127 } | 127 } |
| 128 | 128 |
| 129 void reset() { | 129 void reset() { |
| 130 if (fArray) { | 130 if (fArray) { |
| 131 sk_free(fArray); | 131 sk_free(fArray); |
| 132 fArray = NULL; | 132 fArray = NULL; |
| 133 #ifdef SK_DEBUG | 133 #ifdef SK_DEBUG |
| 134 fData = NULL; | 134 fData = NULL; |
| 135 #endif | 135 #endif |
| 136 fReserve = fCount = 0; | 136 fReserve = fCount = 0; |
| 137 } else { | 137 } else { |
| 138 SkASSERT(fReserve == 0 && fCount == 0); | 138 SkASSERT(fReserve == 0 && fCount == 0); |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 void rewind() { | 142 void rewind() { |
| 143 // same as setCount(0) | 143 // same as setCount(0) |
| 144 fCount = 0; | 144 fCount = 0; |
| 145 } | 145 } |
| 146 | 146 |
| 147 void setCount(size_t count) { | 147 void setCount(int count) { |
| 148 if (count > fReserve) { | 148 if (count > fReserve) { |
| 149 this->growBy(count - fCount); | 149 this->growBy(count - fCount); |
| 150 } else { | 150 } else { |
| 151 fCount = count; | 151 fCount = count; |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 void setReserve(size_t reserve) { | 155 void setReserve(int reserve) { |
| 156 if (reserve > fReserve) { | 156 if (reserve > fReserve) { |
| 157 SkASSERT(reserve > fCount); | 157 SkASSERT(reserve > fCount); |
| 158 size_t count = fCount; | 158 int count = fCount; |
| 159 this->growBy(reserve - fCount); | 159 this->growBy(reserve - fCount); |
| 160 fCount = count; | 160 fCount = count; |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 T* prepend() { | 164 T* prepend() { |
| 165 this->growBy(1); | 165 this->growBy(1); |
| 166 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); | 166 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); |
| 167 return fArray; | 167 return fArray; |
| 168 } | 168 } |
| 169 | 169 |
| 170 T* append() { | 170 T* append() { |
| 171 return this->append(1, NULL); | 171 return this->append(1, NULL); |
| 172 } | 172 } |
| 173 T* append(size_t count, const T* src = NULL) { | 173 T* append(int count, const T* src = NULL) { |
| 174 size_t oldCount = fCount; | 174 int oldCount = fCount; |
| 175 if (count) { | 175 if (count) { |
| 176 SkASSERT(src == NULL || fArray == NULL || | 176 SkASSERT(src == NULL || fArray == NULL || |
| 177 src + count <= fArray || fArray + oldCount <= src); | 177 src + count <= fArray || fArray + oldCount <= src); |
| 178 | 178 |
| 179 this->growBy(count); | 179 this->growBy(count); |
| 180 if (src) { | 180 if (src) { |
| 181 memcpy(fArray + oldCount, src, sizeof(T) * count); | 181 memcpy(fArray + oldCount, src, sizeof(T) * count); |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 return fArray + oldCount; | 184 return fArray + oldCount; |
| 185 } | 185 } |
| 186 | 186 |
| 187 T* appendClear() { | 187 T* appendClear() { |
| 188 T* result = this->append(); | 188 T* result = this->append(); |
| 189 *result = 0; | 189 *result = 0; |
| 190 return result; | 190 return result; |
| 191 } | 191 } |
| 192 | 192 |
| 193 T* insert(size_t index) { | 193 T* insert(int index) { |
| 194 return this->insert(index, 1, NULL); | 194 return this->insert(index, 1, NULL); |
| 195 } | 195 } |
| 196 T* insert(size_t index, size_t count, const T* src = NULL) { | 196 T* insert(int index, int count, const T* src = NULL) { |
| 197 SkASSERT(count); | 197 SkASSERT(count); |
| 198 SkASSERT(index <= fCount); | 198 SkASSERT(index <= fCount); |
| 199 size_t oldCount = fCount; | 199 size_t oldCount = fCount; |
| 200 this->growBy(count); | 200 this->growBy(count); |
| 201 T* dst = fArray + index; | 201 T* dst = fArray + index; |
| 202 memmove(dst + count, dst, sizeof(T) * (oldCount - index)); | 202 memmove(dst + count, dst, sizeof(T) * (oldCount - index)); |
| 203 if (src) { | 203 if (src) { |
| 204 memcpy(dst, src, sizeof(T) * count); | 204 memcpy(dst, src, sizeof(T) * count); |
| 205 } | 205 } |
| 206 return dst; | 206 return dst; |
| 207 } | 207 } |
| 208 | 208 |
| 209 void remove(size_t index, size_t count = 1) { | 209 void remove(int index, int count = 1) { |
| 210 SkASSERT(index + count <= fCount); | 210 SkASSERT(index + count <= fCount); |
| 211 fCount = fCount - count; | 211 fCount = fCount - count; |
| 212 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - in
dex)); | 212 memmove(fArray + index, fArray + index + count, sizeof(T) * (fCount - in
dex)); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void removeShuffle(size_t index) { | 215 void removeShuffle(int index) { |
| 216 SkASSERT(index < fCount); | 216 SkASSERT(index < fCount); |
| 217 size_t newCount = fCount - 1; | 217 int newCount = fCount - 1; |
| 218 fCount = newCount; | 218 fCount = newCount; |
| 219 if (index != newCount) { | 219 if (index != newCount) { |
| 220 memcpy(fArray + index, fArray + newCount, sizeof(T)); | 220 memcpy(fArray + index, fArray + newCount, sizeof(T)); |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 int find(const T& elem) const { | 224 int find(const T& elem) const { |
| 225 const T* iter = fArray; | 225 const T* iter = fArray; |
| 226 const T* stop = fArray + fCount; | 226 const T* stop = fArray + fCount; |
| 227 | 227 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 249 * Returns true iff the array contains this element. | 249 * Returns true iff the array contains this element. |
| 250 */ | 250 */ |
| 251 bool contains(const T& elem) const { | 251 bool contains(const T& elem) const { |
| 252 return (this->find(elem) >= 0); | 252 return (this->find(elem) >= 0); |
| 253 } | 253 } |
| 254 | 254 |
| 255 /** | 255 /** |
| 256 * Copies up to max elements into dst. The number of items copied is | 256 * Copies up to max elements into dst. The number of items copied is |
| 257 * capped by count - index. The actual number copied is returned. | 257 * capped by count - index. The actual number copied is returned. |
| 258 */ | 258 */ |
| 259 int copyRange(T* dst, size_t index, int max) const { | 259 int copyRange(T* dst, int index, int max) const { |
| 260 SkASSERT(max >= 0); | 260 SkASSERT(max >= 0); |
| 261 SkASSERT(!max || dst); | 261 SkASSERT(!max || dst); |
| 262 if (index >= fCount) { | 262 if (index >= fCount) { |
| 263 return 0; | 263 return 0; |
| 264 } | 264 } |
| 265 int count = SkMin32(max, fCount - index); | 265 int count = SkMin32(max, fCount - index); |
| 266 memcpy(dst, fArray + index, sizeof(T) * count); | 266 memcpy(dst, fArray + index, sizeof(T) * count); |
| 267 return count; | 267 return count; |
| 268 } | 268 } |
| 269 | 269 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 | 339 |
| 340 private: | 340 private: |
| 341 #ifdef SK_DEBUG | 341 #ifdef SK_DEBUG |
| 342 enum { | 342 enum { |
| 343 kDebugArraySize = 16 | 343 kDebugArraySize = 16 |
| 344 }; | 344 }; |
| 345 typedef T ArrayT[kDebugArraySize]; | 345 typedef T ArrayT[kDebugArraySize]; |
| 346 ArrayT* fData; | 346 ArrayT* fData; |
| 347 #endif | 347 #endif |
| 348 T* fArray; | 348 T* fArray; |
| 349 size_t fReserve, fCount; | 349 int fReserve; |
| 350 int fCount; |
| 350 | 351 |
| 351 void growBy(size_t extra) { | 352 void growBy(int extra) { |
| 352 SkASSERT(extra); | 353 SkASSERT(extra); |
| 353 | 354 |
| 354 if (fCount + extra > fReserve) { | 355 if (fCount + extra > fReserve) { |
| 355 size_t size = fCount + extra + 4; | 356 int size = fCount + extra + 4; |
| 356 size += size >> 2; | 357 size += size >> 2; |
| 357 | 358 |
| 358 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); | 359 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); |
| 359 #ifdef SK_DEBUG | 360 #ifdef SK_DEBUG |
| 360 fData = (ArrayT*)fArray; | 361 fData = (ArrayT*)fArray; |
| 361 #endif | 362 #endif |
| 362 fReserve = size; | 363 fReserve = size; |
| 363 } | 364 } |
| 364 fCount += extra; | 365 fCount += extra; |
| 365 } | 366 } |
| 366 }; | 367 }; |
| 367 | 368 |
| 368 #endif | 369 #endif |
| OLD | NEW |