13#ifndef VECTORLIKEARRAY_HPP_
14#define VECTORLIKEARRAY_HPP_
29 template<
typename StoredClass >
35 StoredClass* (*storedClassCopyFunction)( StoredClass
const& ) = NULL );
89 safeAt(
int const soughtIndex );
92 safeAt(
int const soughtIndex )
const;
110 std::vector< StoredClass* >&
111 getAsVector( std::vector< StoredClass* >& vectorToFill )
const;
114 std::list< StoredClass* >&
115 getAsList( std::list< StoredClass* >& listToFill )
const;
124 void (*firstArgumentBecomesCopyOfSecond)( StoredClass&,
125 StoredClass
const& ) = NULL );
174 template<
typename StoredClass >
177 pointerArray( new StoredClass*[ initialSize ] ),
178 allocatedSizeOfArray( initialSize ),
179 indexOfEndConstructed( -1 ),
180 currentSizeOfArray( initialSize ),
181 currentEndIndex( initialSize - 1 )
200 template<
typename StoredClass >
204 StoredClass* (*storedClassCopyFunction)( StoredClass
const& ) ) :
205 pointerArray( new StoredClass*[ copySource.currentSizeOfArray ] ),
206 allocatedSizeOfArray( copySource.currentSizeOfArray ),
207 indexOfEndConstructed( -1 ),
208 currentSizeOfArray( copySource.currentSizeOfArray ),
209 currentEndIndex( copySource.currentEndIndex )
211 if( NULL == storedClassCopyFunction )
231 template<
typename StoredClass >
235 while( 0 <= indexOfEndConstructed )
237 delete pointerArray[ indexOfEndConstructed ];
238 --indexOfEndConstructed;
240 delete[] pointerArray;
244 template<
typename StoredClass >
250 return *(unsafeGetPointer( soughtIndex ));
253 template<
typename StoredClass >
254 inline StoredClass
const&
259 return *(unsafeGetPointer( soughtIndex ));
262 template<
typename StoredClass >
268 if( 0 < currentSizeOfArray )
278 template<
typename StoredClass >
284 return currentSizeOfArray;
287 template<
typename StoredClass >
293 return currentEndIndex;
296 template<
typename StoredClass >
303 currentSizeOfArray = newSize;
304 currentEndIndex = ( newSize - 1 );
307 if( currentSizeOfArray > allocatedSizeOfArray )
309 if( !(0 < allocatedSizeOfArray ) )
311 allocatedSizeOfArray = currentSizeOfArray;
315 while( currentSizeOfArray > allocatedSizeOfArray )
317 allocatedSizeOfArray += allocatedSizeOfArray;
325 newPointerArray =
new StoredClass*[ allocatedSizeOfArray ];
329 for(
int copyIndex( indexOfEndConstructed );
333 newPointerArray[ copyIndex ] = pointerArray[ copyIndex ];
337 delete[] pointerArray;
338 pointerArray = newPointerArray;
342 while( currentEndIndex > indexOfEndConstructed )
344 ++indexOfEndConstructed;
345 pointerArray[ indexOfEndConstructed ] =
new StoredClass;
351 template<
typename StoredClass >
357 return setSize( numberToAdd + currentSizeOfArray );
360 template<
typename StoredClass >
367 return *(unsafeGetPointer( currentEndIndex ));
370 template<
typename StoredClass >
378 template<
typename StoredClass >
383 return safeGetPointer( soughtIndex );
386 template<
typename StoredClass >
387 inline StoredClass
const*
391 return safeGetPointer( soughtIndex );
394 template<
typename StoredClass >
399 return getReference( soughtIndex );
402 template<
typename StoredClass >
403 inline StoredClass
const&
407 return getReference( soughtIndex );
410 template<
typename StoredClass >
415 StoredClass* soughtPointer( safeGetPointer( soughtIndex ) );
416 if( NULL == soughtPointer )
418 throw std::out_of_range(
419 "VectorlikeArray::getReference(...) out of range" );
421 return *soughtPointer;
424 template<
typename StoredClass >
425 inline StoredClass
const&
429 StoredClass* soughtPointer( safeGetPointer( soughtIndex ) );
430 if( NULL == soughtPointer )
432 throw std::out_of_range(
433 "VectorlikeArray::getReference(...) out of range" );
435 return *soughtPointer;
438 template<
typename StoredClass >
444 return *(safeGetPointer( 0 ));
447 template<
typename StoredClass >
448 inline StoredClass
const&
453 return *(safeGetPointer( 0 ));
456 template<
typename StoredClass >
462 return *(safeGetPointer( currentEndIndex ));
465 template<
typename StoredClass >
466 inline StoredClass
const&
471 return *(safeGetPointer( currentEndIndex ));
474 template<
typename StoredClass >
475 inline std::vector< StoredClass* >&
477 std::vector< StoredClass* >& vectorToFill )
const
481 vectorToFill.assign( pointerArray,
482 ( pointerArray + currentSizeOfArray ) );
486 template<
typename StoredClass >
487 inline std::list< StoredClass* >&
489 std::list< StoredClass* >& listToFill )
const
493 listToFill.assign( pointerArray,
494 ( pointerArray + currentSizeOfArray ) );
498 template<
typename StoredClass >
504 currentSizeOfArray = newSize;
505 currentEndIndex = ( newSize - 1 );
506 while( indexOfEndConstructed >= newSize )
508 delete pointerArray[ indexOfEndConstructed ];
509 --indexOfEndConstructed;
511 if( newSize != allocatedSizeOfArray )
513 allocatedSizeOfArray = newSize;
515 newPointerArray =
new StoredClass*[ allocatedSizeOfArray ];
519 for(
int copyIndex( indexOfEndConstructed );
523 newPointerArray[ copyIndex ] = pointerArray[ copyIndex ];
527 delete[] pointerArray;
528 pointerArray = newPointerArray;
532 while( currentEndIndex > indexOfEndConstructed )
534 pointerArray[ ++indexOfEndConstructed ] =
new StoredClass;
542 template<
typename StoredClass >
547 return pointerArray[ soughtIndex ];
550 template<
typename StoredClass >
551 inline StoredClass
const*
553 int const soughtIndex )
const
556 return pointerArray[ soughtIndex ];
559 template<
typename StoredClass >
564 if( ( 0 <= soughtIndex )
566 ( currentEndIndex >= soughtIndex ) )
568 return unsafeGetPointer( soughtIndex );
576 template<
typename StoredClass >
577 inline StoredClass
const*
581 if( ( 0 <= soughtIndex )
583 ( currentEndIndex >= soughtIndex ) )
585 return unsafeGetPointer( soughtIndex );
593 template<
typename StoredClass >
597 void (*firstArgumentBecomesCopyOfSecond)( StoredClass&,
598 StoredClass
const& ) )
607 setSize( copySource.
getSize() );
608 if( NULL == firstArgumentBecomesCopyOfSecond )
614 *(pointerArray[ copyIndex ]) = copySource[ copyIndex ];
623 (*firstArgumentBecomesCopyOfSecond)( *(pointerArray[ copyIndex ]),
624 copySource[ copyIndex ] );
StoredClass * safeGetPointer(int const soughtIndex)
VectorlikeArray< StoredClass > & becomeCopyOf(VectorlikeArray< StoredClass > const ©Source, void(*firstArgumentBecomesCopyOfSecond)(StoredClass &, StoredClass const &)=NULL)
std::vector< StoredClass * > & getAsVector(std::vector< StoredClass * > &vectorToFill) const
VectorlikeArray< StoredClass > & increaseSize(int const numberToAdd=1)
VectorlikeArray< StoredClass > & setSize(int const newSize)
StoredClass ** pointerArray
VectorlikeArray< StoredClass > & memoryFreeingResize(int const newSize=0)
int indexOfEndConstructed
StoredClass * getPointer(int const soughtIndex)
StoredClass & operator[](int const soughtIndex)
StoredClass & safeAt(int const soughtIndex)
StoredClass & getReference(int const soughtIndex)
std::list< StoredClass * > & getAsList(std::list< StoredClass * > &listToFill) const
StoredClass * unsafeGetPointer(int const soughtIndex)
VectorlikeArray< StoredClass > & clearEntries()
VectorlikeArray(int const initialSize=0)