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 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 } else { | 144 } else { |
145 SkASSERT(fReserve == 0 && fCount == 0); | 145 SkASSERT(fReserve == 0 && fCount == 0); |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 void rewind() { | 149 void rewind() { |
150 // same as setCount(0) | 150 // same as setCount(0) |
151 fCount = 0; | 151 fCount = 0; |
152 } | 152 } |
153 | 153 |
154 /** | |
155 * Sets the number of elements in the array. | |
156 * If the array does not have space for count elements, it will increase | |
157 * the storage allocated to some amount greater than that required. | |
158 * It will never shrink the shrink the storage. | |
159 */ | |
160 void setCount(int count) { | 154 void setCount(int count) { |
161 // TODO(mtklein): eliminate this method, setCountExact -> setCount | |
162 SkASSERT(count >= 0); | |
163 if (count > fReserve) { | 155 if (count > fReserve) { |
164 resizeStorageToAtLeast(count); | 156 this->growBy(count - fCount); |
| 157 } else { |
| 158 fCount = count; |
165 } | 159 } |
166 fCount = count; | |
167 } | |
168 | |
169 /** | |
170 * Sets the number of elements in the array. | |
171 * If the array does not have space for count elements, it will increase | |
172 * the storage allocated to exactly the amount required, with no remaining | |
173 * reserved space. | |
174 * It will never shrink the shrink the storage. | |
175 */ | |
176 void setCountExact(int count) { | |
177 if (count > fReserve) { | |
178 this->resizeStorageToExact(count); | |
179 } | |
180 fCount = count; | |
181 } | 160 } |
182 | 161 |
183 void setReserve(int reserve) { | 162 void setReserve(int reserve) { |
184 if (reserve > fReserve) { | 163 if (reserve > fReserve) { |
185 resizeStorageToAtLeast(reserve); | 164 SkASSERT(reserve > fCount); |
| 165 int count = fCount; |
| 166 this->growBy(reserve - fCount); |
| 167 fCount = count; |
186 } | 168 } |
187 } | 169 } |
188 | 170 |
189 T* prepend() { | 171 T* prepend() { |
190 this->adjustCount(1); | 172 this->growBy(1); |
191 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); | 173 memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); |
192 return fArray; | 174 return fArray; |
193 } | 175 } |
194 | 176 |
195 T* append() { | 177 T* append() { |
196 return this->append(1, NULL); | 178 return this->append(1, NULL); |
197 } | 179 } |
198 T* append(int count, const T* src = NULL) { | 180 T* append(int count, const T* src = NULL) { |
199 int oldCount = fCount; | 181 int oldCount = fCount; |
200 if (count) { | 182 if (count) { |
201 SkASSERT(src == NULL || fArray == NULL || | 183 SkASSERT(src == NULL || fArray == NULL || |
202 src + count <= fArray || fArray + oldCount <= src); | 184 src + count <= fArray || fArray + oldCount <= src); |
203 | 185 |
204 this->adjustCount(count); | 186 this->growBy(count); |
205 if (src) { | 187 if (src) { |
206 memcpy(fArray + oldCount, src, sizeof(T) * count); | 188 memcpy(fArray + oldCount, src, sizeof(T) * count); |
207 } | 189 } |
208 } | 190 } |
209 return fArray + oldCount; | 191 return fArray + oldCount; |
210 } | 192 } |
211 | 193 |
212 T* appendClear() { | 194 T* appendClear() { |
213 T* result = this->append(); | 195 T* result = this->append(); |
214 *result = 0; | 196 *result = 0; |
215 return result; | 197 return result; |
216 } | 198 } |
217 | 199 |
218 T* insert(int index) { | 200 T* insert(int index) { |
219 return this->insert(index, 1, NULL); | 201 return this->insert(index, 1, NULL); |
220 } | 202 } |
221 T* insert(int index, int count, const T* src = NULL) { | 203 T* insert(int index, int count, const T* src = NULL) { |
222 SkASSERT(count); | 204 SkASSERT(count); |
223 SkASSERT(index <= fCount); | 205 SkASSERT(index <= fCount); |
224 size_t oldCount = fCount; | 206 size_t oldCount = fCount; |
225 this->adjustCount(count); | 207 this->growBy(count); |
226 T* dst = fArray + index; | 208 T* dst = fArray + index; |
227 memmove(dst + count, dst, sizeof(T) * (oldCount - index)); | 209 memmove(dst + count, dst, sizeof(T) * (oldCount - index)); |
228 if (src) { | 210 if (src) { |
229 memcpy(dst, src, sizeof(T) * count); | 211 memcpy(dst, src, sizeof(T) * count); |
230 } | 212 } |
231 return dst; | 213 return dst; |
232 } | 214 } |
233 | 215 |
234 void remove(int index, int count = 1) { | 216 void remove(int index, int count = 1) { |
235 SkASSERT(index + count <= fCount); | 217 SkASSERT(index + count <= fCount); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 enum { | 349 enum { |
368 kDebugArraySize = 16 | 350 kDebugArraySize = 16 |
369 }; | 351 }; |
370 typedef T ArrayT[kDebugArraySize]; | 352 typedef T ArrayT[kDebugArraySize]; |
371 ArrayT* fData; | 353 ArrayT* fData; |
372 #endif | 354 #endif |
373 T* fArray; | 355 T* fArray; |
374 int fReserve; | 356 int fReserve; |
375 int fCount; | 357 int fCount; |
376 | 358 |
377 /** | 359 void growBy(int extra) { |
378 * Adjusts the number of elements in the array. | 360 SkASSERT(extra); |
379 * This is the same as calling setCount(count() + delta). | |
380 */ | |
381 void adjustCount(int delta) { | |
382 setCount(fCount + delta); | |
383 } | |
384 | 361 |
385 /** | 362 if (fCount + extra > fReserve) { |
386 * This resizes the storage to *exactly* count elements, growing or | 363 int size = fCount + extra + 4; |
387 * shrinking the allocation as needed. It does not ASSERT anything about | 364 size += size >> 2; |
388 * the previous allocation size, or about fCount. | 365 |
389 * | 366 fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); |
390 * note: does NOT modify fCount | |
391 */ | |
392 void resizeStorageToExact(int count) { | |
393 SkASSERT(count >= 0); | |
394 fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T)); | |
395 #ifdef SK_DEBUG | 367 #ifdef SK_DEBUG |
396 fData = (ArrayT*)fArray; | 368 fData = (ArrayT*)fArray; |
397 #endif | 369 #endif |
398 fReserve = count; | 370 fReserve = size; |
399 } | 371 } |
400 | 372 fCount += extra; |
401 /** | |
402 * Increase the storage allocation such that it can hold (fCount + extra) | |
403 * elements. | |
404 * It never shrinks the allocation, and it may increase the allocation by | |
405 * more than is strictly required, based on a private growth heuristic. | |
406 * | |
407 * note: does NOT modify fCount | |
408 */ | |
409 void resizeStorageToAtLeast(int count) { | |
410 SkASSERT(count > fReserve); | |
411 int space = count + 4; | |
412 space += space>>2; | |
413 resizeStorageToExact(space); | |
414 } | 373 } |
415 }; | 374 }; |
416 | 375 |
417 #endif | 376 #endif |
OLD | NEW |