diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-10-20 18:12:33 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-10-20 18:12:33 +0000 |
commit | ff4de8ac02eb5f7bad4be800cc1fb6206f8fbd9c (patch) | |
tree | bb371ab821187ddafd15895d434b463861c3d9ff | |
parent | 4808617ef54d40efcf1a3ed30525898defb74e10 (diff) | |
download | libbu++-ff4de8ac02eb5f7bad4be800cc1fb6206f8fbd9c.tar.gz libbu++-ff4de8ac02eb5f7bad4be800cc1fb6206f8fbd9c.tar.bz2 libbu++-ff4de8ac02eb5f7bad4be800cc1fb6206f8fbd9c.tar.xz libbu++-ff4de8ac02eb5f7bad4be800cc1fb6206f8fbd9c.zip |
Hey, Bu::Array supports erasing elements in two distinct, yet flavourful ways.
Diffstat (limited to '')
-rw-r--r-- | src/array.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/array.h b/src/array.h index e8d8b30..30d281a 100644 --- a/src/array.h +++ b/src/array.h | |||
@@ -184,6 +184,26 @@ namespace Bu | |||
184 | iPos = -1; | 184 | iPos = -1; |
185 | return *this; | 185 | return *this; |
186 | } | 186 | } |
187 | |||
188 | iterator operator--( int ) | ||
189 | { | ||
190 | if( iPos < 0 ) | ||
191 | throw ArrayException( | ||
192 | "Cannot increment iterator past end of array."); | ||
193 | iPos--; | ||
194 | if( iPos < 0 ) | ||
195 | iPos = -1; | ||
196 | return *this; | ||
197 | } | ||
198 | |||
199 | iterator operator--() | ||
200 | { | ||
201 | if( iPos < src.getSize() ) | ||
202 | iPos--; | ||
203 | if( iPos <= 0 ) | ||
204 | iPos = -1; | ||
205 | return *this; | ||
206 | } | ||
187 | 207 | ||
188 | bool operator==( const iterator &oth ) | 208 | bool operator==( const iterator &oth ) |
189 | { | 209 | { |
@@ -247,6 +267,26 @@ namespace Bu | |||
247 | iPos = -1; | 267 | iPos = -1; |
248 | return *this; | 268 | return *this; |
249 | } | 269 | } |
270 | |||
271 | const_iterator operator--( int ) | ||
272 | { | ||
273 | if( iPos < 0 ) | ||
274 | throw ArrayException( | ||
275 | "Cannot increment iterator past end of array."); | ||
276 | iPos--; | ||
277 | if( iPos < 0 ) | ||
278 | iPos = -1; | ||
279 | return *this; | ||
280 | } | ||
281 | |||
282 | const_iterator operator--() | ||
283 | { | ||
284 | if( iPos < src.getSize() ) | ||
285 | iPos--; | ||
286 | if( iPos <= 0 ) | ||
287 | iPos = -1; | ||
288 | return *this; | ||
289 | } | ||
250 | 290 | ||
251 | bool operator==( const const_iterator &oth ) | 291 | bool operator==( const const_iterator &oth ) |
252 | { | 292 | { |
@@ -295,6 +335,45 @@ namespace Bu | |||
295 | return const_iterator( *this, -1 ); | 335 | return const_iterator( *this, -1 ); |
296 | } | 336 | } |
297 | 337 | ||
338 | /** | ||
339 | * If order is important, use this. It will delete the suggested item | ||
340 | * and move the rest of the data up a spot. This is a time O(n) | ||
341 | * operation. If the order isn't important, check swapErase | ||
342 | */ | ||
343 | void erase( iterator i ) | ||
344 | { | ||
345 | for( int j = i.iPos; j < iSize; j++ ) | ||
346 | { | ||
347 | va.destroy( &pData[j] ); | ||
348 | if( j == iSize-1 ) | ||
349 | { | ||
350 | iSize--; | ||
351 | return; | ||
352 | } | ||
353 | va.construct( &pData[j], pData[j+1] ); | ||
354 | } | ||
355 | } | ||
356 | |||
357 | /** | ||
358 | * In order to make swapErase faster, what it does is swap the given | ||
359 | * item in the array with the last item, then make the array shorter | ||
360 | * by one. It changes the order of the elements in the array, so it | ||
361 | * should be used carefully, but it is time O(1) instead of O(n) like | ||
362 | * erase. | ||
363 | */ | ||
364 | void swapErase( iterator i ) | ||
365 | { | ||
366 | if( i.iPos == iSize-1 ) | ||
367 | { | ||
368 | erase( i ); | ||
369 | return; | ||
370 | } | ||
371 | va.destroy( &pData[i.iPos] ); | ||
372 | va.construct( &pData[i.iPos], pData[iSize-1] ); | ||
373 | va.destroy( &pData[iSize-1] ); | ||
374 | iSize--; | ||
375 | } | ||
376 | |||
298 | private: | 377 | private: |
299 | valuealloc va; | 378 | valuealloc va; |
300 | value *pData; | 379 | value *pData; |