| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkTArray_DEFINED | 8 #ifndef SkTArray_DEFINED |
| 9 #define SkTArray_DEFINED | 9 #define SkTArray_DEFINED |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); | 21 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); |
| 22 } | 22 } |
| 23 template<typename T> | 23 template<typename T> |
| 24 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { | 24 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { |
| 25 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); | 25 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); |
| 26 } | 26 } |
| 27 | 27 |
| 28 template<typename T> | 28 template<typename T> |
| 29 inline void copy(SkTArray<T, false>* self, const T* array) { | 29 inline void copy(SkTArray<T, false>* self, const T* array) { |
| 30 for (int i = 0; i < self->fCount; ++i) { | 30 for (int i = 0; i < self->fCount; ++i) { |
| 31 new (self->fItemArray + i) T(array[i]); | 31 SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i])); |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 template<typename T> | 34 template<typename T> |
| 35 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { | 35 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { |
| 36 for (int i = 0; i < self->fCount; ++i) { | 36 for (int i = 0; i < self->fCount; ++i) { |
| 37 new (newMemArray + sizeof(T) * i) T(self->fItemArray[i]); | 37 SkNEW_PLACEMENT_ARGS(newMemArray + sizeof(T) * i, T, (self->fItemArray[i
])); |
| 38 self->fItemArray[i].~T(); | 38 self->fItemArray[i].~T(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 } | 42 } |
| 43 | 43 |
| 44 /** When MEM_COPY is true T will be bit copied when moved. | 44 /** When MEM_COPY is true T will be bit copied when moved. |
| 45 When MEM_COPY is false, T will be copy constructed / destructed. | 45 When MEM_COPY is false, T will be copy constructed / destructed. |
| 46 In all cases T's constructor will be called on allocation, | 46 In all cases T's constructor will be called on allocation, |
| 47 and its destructor will be called from this object's destructor. | 47 and its destructor will be called from this object's destructor. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 sk_free(fMemArray); | 105 sk_free(fMemArray); |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Resets to count() == 0 | 110 * Resets to count() == 0 |
| 111 */ | 111 */ |
| 112 void reset() { this->pop_back_n(fCount); } | 112 void reset() { this->pop_back_n(fCount); } |
| 113 | 113 |
| 114 /** | 114 /** |
| 115 * Resets to count() = n newly constructed T objects. |
| 116 */ |
| 117 void reset(int n) { |
| 118 SkASSERT(n >= 0); |
| 119 for (int i = 0; i < fCount; ++i) { |
| 120 fItemArray[i].~T(); |
| 121 } |
| 122 // set fCount to 0 before calling checkRealloc so that no copy cons. are
called. |
| 123 fCount = 0; |
| 124 this->checkRealloc(n); |
| 125 fCount = n; |
| 126 for (int i = 0; i < fCount; ++i) { |
| 127 SkNEW_PLACEMENT(fItemArray + i, T); |
| 128 } |
| 129 } |
| 130 |
| 131 /** |
| 115 * Resets to a copy of a C array. | 132 * Resets to a copy of a C array. |
| 116 */ | 133 */ |
| 117 void reset(const T* array, int count) { | 134 void reset(const T* array, int count) { |
| 118 for (int i = 0; i < fCount; ++i) { | 135 for (int i = 0; i < fCount; ++i) { |
| 119 fItemArray[i].~T(); | 136 fItemArray[i].~T(); |
| 120 } | 137 } |
| 121 int delta = count - fCount; | 138 int delta = count - fCount; |
| 122 this->checkRealloc(delta); | 139 this->checkRealloc(delta); |
| 123 fCount = count; | 140 fCount = count; |
| 124 for (int i = 0; i < count; ++i) { | 141 for (int i = 0; i < count; ++i) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 135 * Is the array empty. | 152 * Is the array empty. |
| 136 */ | 153 */ |
| 137 bool empty() const { return !fCount; } | 154 bool empty() const { return !fCount; } |
| 138 | 155 |
| 139 /** | 156 /** |
| 140 * Adds 1 new default-constructed T value and returns in by reference. Note | 157 * Adds 1 new default-constructed T value and returns in by reference. Note |
| 141 * the reference only remains valid until the next call that adds or removes | 158 * the reference only remains valid until the next call that adds or removes |
| 142 * elements. | 159 * elements. |
| 143 */ | 160 */ |
| 144 T& push_back() { | 161 T& push_back() { |
| 145 checkRealloc(1); | 162 this->checkRealloc(1); |
| 146 new ((char*)fMemArray+sizeof(T)*fCount) T; | 163 SkNEW_PLACEMENT((char*)fMemArray + sizeof(T) * fCount, T); |
| 147 ++fCount; | 164 ++fCount; |
| 148 return fItemArray[fCount-1]; | 165 return fItemArray[fCount-1]; |
| 149 } | 166 } |
| 150 | 167 |
| 151 /** | 168 /** |
| 152 * Version of above that uses a copy constructor to initialize the new item | 169 * Version of above that uses a copy constructor to initialize the new item |
| 153 */ | 170 */ |
| 154 T& push_back(const T& t) { | 171 T& push_back(const T& t) { |
| 155 checkRealloc(1); | 172 this->checkRealloc(1); |
| 156 new ((char*)fMemArray+sizeof(T)*fCount) T(t); | 173 SkNEW_PLACEMENT_ARGS((char*)fMemArray + sizeof(T) * fCount, T, (t)); |
| 157 ++fCount; | 174 ++fCount; |
| 158 return fItemArray[fCount-1]; | 175 return fItemArray[fCount-1]; |
| 159 } | 176 } |
| 160 | 177 |
| 161 /** | 178 /** |
| 162 * Allocates n more default T values, and returns the address of the start | 179 * Allocates n more default T values, and returns the address of the start |
| 163 * of that new range. Note: this address is only valid until the next API | 180 * of that new range. Note: this address is only valid until the next API |
| 164 * call made on the array that might add or remove elements. | 181 * call made on the array that might add or remove elements. |
| 165 */ | 182 */ |
| 166 T* push_back_n(int n) { | 183 T* push_back_n(int n) { |
| 167 SkASSERT(n >= 0); | 184 SkASSERT(n >= 0); |
| 168 checkRealloc(n); | 185 this->checkRealloc(n); |
| 169 for (int i = 0; i < n; ++i) { | 186 for (int i = 0; i < n; ++i) { |
| 170 new (fItemArray + fCount + i) T; | 187 SkNEW_PLACEMENT(fItemArray + fCount + i, T); |
| 171 } | 188 } |
| 172 fCount += n; | 189 fCount += n; |
| 173 return fItemArray + fCount - n; | 190 return fItemArray + fCount - n; |
| 174 } | 191 } |
| 175 | 192 |
| 176 /** | 193 /** |
| 177 * Version of above that uses a copy constructor to initialize all n items | 194 * Version of above that uses a copy constructor to initialize all n items |
| 178 * to the same T. | 195 * to the same T. |
| 179 */ | 196 */ |
| 180 T* push_back_n(int n, const T& t) { | 197 T* push_back_n(int n, const T& t) { |
| 181 SkASSERT(n >= 0); | 198 SkASSERT(n >= 0); |
| 182 checkRealloc(n); | 199 this->checkRealloc(n); |
| 183 for (int i = 0; i < n; ++i) { | 200 for (int i = 0; i < n; ++i) { |
| 184 new (fItemArray + fCount + i) T(t); | 201 SkNEW_PLACEMENT_ARGS(fItemArray + fCount + i, T, (t)); |
| 185 } | 202 } |
| 186 fCount += n; | 203 fCount += n; |
| 187 return fItemArray + fCount - n; | 204 return fItemArray + fCount - n; |
| 188 } | 205 } |
| 189 | 206 |
| 190 /** | 207 /** |
| 191 * Version of above that uses a copy constructor to initialize the n items | 208 * Version of above that uses a copy constructor to initialize the n items |
| 192 * to separate T values. | 209 * to separate T values. |
| 193 */ | 210 */ |
| 194 T* push_back_n(int n, const T t[]) { | 211 T* push_back_n(int n, const T t[]) { |
| 195 SkASSERT(n >= 0); | 212 SkASSERT(n >= 0); |
| 196 checkRealloc(n); | 213 this->checkRealloc(n); |
| 197 for (int i = 0; i < n; ++i) { | 214 for (int i = 0; i < n; ++i) { |
| 198 new (fItemArray + fCount + i) T(t[i]); | 215 SkNEW_PLACEMENT_ARGS(fItemArray + fCount + i, T, (t[i])); |
| 199 } | 216 } |
| 200 fCount += n; | 217 fCount += n; |
| 201 return fItemArray + fCount - n; | 218 return fItemArray + fCount - n; |
| 202 } | 219 } |
| 203 | 220 |
| 204 /** | 221 /** |
| 205 * Removes the last element. Not safe to call when count() == 0. | 222 * Removes the last element. Not safe to call when count() == 0. |
| 206 */ | 223 */ |
| 207 void pop_back() { | 224 void pop_back() { |
| 208 SkASSERT(fCount > 0); | 225 SkASSERT(fCount > 0); |
| 209 --fCount; | 226 --fCount; |
| 210 fItemArray[fCount].~T(); | 227 fItemArray[fCount].~T(); |
| 211 checkRealloc(0); | 228 this->checkRealloc(0); |
| 212 } | 229 } |
| 213 | 230 |
| 214 /** | 231 /** |
| 215 * Removes the last n elements. Not safe to call when count() < n. | 232 * Removes the last n elements. Not safe to call when count() < n. |
| 216 */ | 233 */ |
| 217 void pop_back_n(int n) { | 234 void pop_back_n(int n) { |
| 218 SkASSERT(n >= 0); | 235 SkASSERT(n >= 0); |
| 219 SkASSERT(fCount >= n); | 236 SkASSERT(fCount >= n); |
| 220 fCount -= n; | 237 fCount -= n; |
| 221 for (int i = 0; i < n; ++i) { | 238 for (int i = 0; i < n; ++i) { |
| 222 fItemArray[i].~T(); | 239 fItemArray[i].~T(); |
| 223 } | 240 } |
| 224 checkRealloc(0); | 241 this->checkRealloc(0); |
| 225 } | 242 } |
| 226 | 243 |
| 227 /** | 244 /** |
| 228 * Pushes or pops from the back to resize. Pushes will be default | 245 * Pushes or pops from the back to resize. Pushes will be default |
| 229 * initialized. | 246 * initialized. |
| 230 */ | 247 */ |
| 231 void resize_back(int newCount) { | 248 void resize_back(int newCount) { |
| 232 SkASSERT(newCount >= 0); | 249 SkASSERT(newCount >= 0); |
| 233 | 250 |
| 234 if (newCount > fCount) { | 251 if (newCount > fCount) { |
| 235 push_back_n(newCount - fCount); | 252 this->push_back_n(newCount - fCount); |
| 236 } else if (newCount < fCount) { | 253 } else if (newCount < fCount) { |
| 237 pop_back_n(fCount - newCount); | 254 this->pop_back_n(fCount - newCount); |
| 238 } | 255 } |
| 239 } | 256 } |
| 240 | 257 |
| 241 T* begin() { | 258 T* begin() { |
| 242 return fItemArray; | 259 return fItemArray; |
| 243 } | 260 } |
| 244 const T* begin() const { | 261 const T* begin() const { |
| 245 return fItemArray; | 262 return fItemArray; |
| 246 } | 263 } |
| 247 T* end() { | 264 T* end() { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 SkSTArray& operator= (const INHERITED& array) { | 465 SkSTArray& operator= (const INHERITED& array) { |
| 449 INHERITED::operator=(array); | 466 INHERITED::operator=(array); |
| 450 return *this; | 467 return *this; |
| 451 } | 468 } |
| 452 | 469 |
| 453 private: | 470 private: |
| 454 SkAlignedSTStorage<N,T> fStorage; | 471 SkAlignedSTStorage<N,T> fStorage; |
| 455 }; | 472 }; |
| 456 | 473 |
| 457 #endif | 474 #endif |
| OLD | NEW |