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 SkTDArray { | 15 template <typename T> class 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 | |
21 fData = NULL; | |
22 #endif | |
23 } | 20 } |
24 SkTDArray(const T src[], int count) { | 21 SkTDArray(const T src[], int count) { |
25 SkASSERT(src || count == 0); | 22 SkASSERT(src || count == 0); |
26 | 23 |
27 fReserve = fCount = 0; | 24 fReserve = fCount = 0; |
28 fArray = NULL; | 25 fArray = NULL; |
29 #ifdef SK_DEBUG | |
30 fData = NULL; | |
31 #endif | |
32 if (count) { | 26 if (count) { |
33 fArray = (T*)sk_malloc_throw(count * sizeof(T)); | 27 fArray = (T*)sk_malloc_throw(count * sizeof(T)); |
34 #ifdef SK_DEBUG | |
35 fData = (ArrayT*)fArray; | |
36 #endif | |
37 memcpy(fArray, src, sizeof(T) * count); | 28 memcpy(fArray, src, sizeof(T) * count); |
38 fReserve = fCount = count; | 29 fReserve = fCount = count; |
39 } | 30 } |
40 } | 31 } |
41 SkTDArray(const SkTDArray<T>& src) { | 32 SkTDArray(const SkTDArray<T>& src) { |
42 fReserve = fCount = 0; | 33 fReserve = fCount = 0; |
43 fArray = NULL; | 34 fArray = NULL; |
44 #ifdef SK_DEBUG | |
45 fData = NULL; | |
46 #endif | |
47 SkTDArray<T> tmp(src.fArray, src.fCount); | 35 SkTDArray<T> tmp(src.fArray, src.fCount); |
48 this->swap(tmp); | 36 this->swap(tmp); |
49 } | 37 } |
50 ~SkTDArray() { | 38 ~SkTDArray() { |
51 sk_free(fArray); | 39 sk_free(fArray); |
52 } | 40 } |
53 | 41 |
54 SkTDArray<T>& operator=(const SkTDArray<T>& src) { | 42 SkTDArray<T>& operator=(const SkTDArray<T>& src) { |
55 if (this != &src) { | 43 if (this != &src) { |
56 if (src.fCount > fReserve) { | 44 if (src.fCount > fReserve) { |
(...skipping 11 matching lines...) Expand all Loading... |
68 return a.fCount == b.fCount && | 56 return a.fCount == b.fCount && |
69 (a.fCount == 0 || | 57 (a.fCount == 0 || |
70 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T))); | 58 !memcmp(a.fArray, b.fArray, a.fCount * sizeof(T))); |
71 } | 59 } |
72 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) { | 60 friend bool operator!=(const SkTDArray<T>& a, const SkTDArray<T>& b) { |
73 return !(a == b); | 61 return !(a == b); |
74 } | 62 } |
75 | 63 |
76 void swap(SkTDArray<T>& other) { | 64 void swap(SkTDArray<T>& other) { |
77 SkTSwap(fArray, other.fArray); | 65 SkTSwap(fArray, other.fArray); |
78 #ifdef SK_DEBUG | |
79 SkTSwap(fData, other.fData); | |
80 #endif | |
81 SkTSwap(fReserve, other.fReserve); | 66 SkTSwap(fReserve, other.fReserve); |
82 SkTSwap(fCount, other.fCount); | 67 SkTSwap(fCount, other.fCount); |
83 } | 68 } |
84 | 69 |
85 /** Return a ptr to the array of data, to be freed with sk_free. This also | 70 /** Return a ptr to the array of data, to be freed with sk_free. This also |
86 resets the SkTDArray to be empty. | 71 resets the SkTDArray to be empty. |
87 */ | 72 */ |
88 T* detach() { | 73 T* detach() { |
89 T* array = fArray; | 74 T* array = fArray; |
90 fArray = NULL; | 75 fArray = NULL; |
91 fReserve = fCount = 0; | 76 fReserve = fCount = 0; |
92 SkDEBUGCODE(fData = NULL;) | |
93 return array; | 77 return array; |
94 } | 78 } |
95 | 79 |
96 bool isEmpty() const { return fCount == 0; } | 80 bool isEmpty() const { return fCount == 0; } |
97 | 81 |
98 /** | 82 /** |
99 * Return the number of elements in the array | 83 * Return the number of elements in the array |
100 */ | 84 */ |
101 int count() const { return fCount; } | 85 int count() const { return fCount; } |
102 | 86 |
(...skipping 27 matching lines...) Expand all Loading... |
130 return (*this)[index]; | 114 return (*this)[index]; |
131 } | 115 } |
132 const T& getAt(int index) const { | 116 const T& getAt(int index) const { |
133 return (*this)[index]; | 117 return (*this)[index]; |
134 } | 118 } |
135 | 119 |
136 void reset() { | 120 void reset() { |
137 if (fArray) { | 121 if (fArray) { |
138 sk_free(fArray); | 122 sk_free(fArray); |
139 fArray = NULL; | 123 fArray = NULL; |
140 #ifdef SK_DEBUG | |
141 fData = NULL; | |
142 #endif | |
143 fReserve = fCount = 0; | 124 fReserve = fCount = 0; |
144 } else { | 125 } else { |
145 SkASSERT(fReserve == 0 && fCount == 0); | 126 SkASSERT(fReserve == 0 && fCount == 0); |
146 } | 127 } |
147 } | 128 } |
148 | 129 |
149 void rewind() { | 130 void rewind() { |
150 // same as setCount(0) | 131 // same as setCount(0) |
151 fCount = 0; | 132 fCount = 0; |
152 } | 133 } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 visitor(*curr); | 317 visitor(*curr); |
337 } | 318 } |
338 } | 319 } |
339 } | 320 } |
340 | 321 |
341 #ifdef SK_DEBUG | 322 #ifdef SK_DEBUG |
342 void validate() const { | 323 void validate() const { |
343 SkASSERT((fReserve == 0 && fArray == NULL) || | 324 SkASSERT((fReserve == 0 && fArray == NULL) || |
344 (fReserve > 0 && fArray != NULL)); | 325 (fReserve > 0 && fArray != NULL)); |
345 SkASSERT(fCount <= fReserve); | 326 SkASSERT(fCount <= fReserve); |
346 SkASSERT(fData == (ArrayT*)fArray); | |
347 } | 327 } |
348 #endif | 328 #endif |
349 | 329 |
350 void shrinkToFit() { | 330 void shrinkToFit() { |
351 fReserve = fCount; | 331 fReserve = fCount; |
352 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); | 332 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); |
353 } | 333 } |
354 | 334 |
355 private: | 335 private: |
356 #ifdef SK_DEBUG | |
357 enum { | |
358 kDebugArraySize = 16 | |
359 }; | |
360 typedef T ArrayT[kDebugArraySize]; | |
361 ArrayT* fData; | |
362 #endif | |
363 T* fArray; | 336 T* fArray; |
364 int fReserve; | 337 int fReserve; |
365 int fCount; | 338 int fCount; |
366 | 339 |
367 /** | 340 /** |
368 * Adjusts the number of elements in the array. | 341 * Adjusts the number of elements in the array. |
369 * This is the same as calling setCount(count() + delta). | 342 * This is the same as calling setCount(count() + delta). |
370 */ | 343 */ |
371 void adjustCount(int delta) { | 344 void adjustCount(int delta) { |
372 this->setCount(fCount + delta); | 345 this->setCount(fCount + delta); |
373 } | 346 } |
374 | 347 |
375 /** | 348 /** |
376 * Increase the storage allocation such that it can hold (fCount + extra) | 349 * Increase the storage allocation such that it can hold (fCount + extra) |
377 * elements. | 350 * elements. |
378 * It never shrinks the allocation, and it may increase the allocation by | 351 * It never shrinks the allocation, and it may increase the allocation by |
379 * more than is strictly required, based on a private growth heuristic. | 352 * more than is strictly required, based on a private growth heuristic. |
380 * | 353 * |
381 * note: does NOT modify fCount | 354 * note: does NOT modify fCount |
382 */ | 355 */ |
383 void resizeStorageToAtLeast(int count) { | 356 void resizeStorageToAtLeast(int count) { |
384 SkASSERT(count > fReserve); | 357 SkASSERT(count > fReserve); |
385 fReserve = count + 4; | 358 fReserve = count + 4; |
386 fReserve += fReserve / 4; | 359 fReserve += fReserve / 4; |
387 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); | 360 fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); |
388 #ifdef SK_DEBUG | |
389 fData = (ArrayT*)fArray; | |
390 #endif | |
391 } | 361 } |
392 }; | 362 }; |
393 | 363 |
394 #endif | 364 #endif |
OLD | NEW |