#ifndef SIZE_LIMITED_VECTOR_INCLUDED #define SIZE_LIMITED_VECTOR_INCLUDED #include "minorGems/util/SimpleVector.h" // vector that trims earlier elements if size passes double of maxSize template class SizeLimitedVector : public SimpleVector { public: // inDeleteWhenCulling true to perform a "delete" operation // on culled elements before discarding them (should be set // when vector contains pointers) SizeLimitedVector( int inLimit, char inDeleteWhenCulling ); // override void push_back( Type x ); protected: int mLimit; char mDeleteWhenCulling; // must be implemented specially for each instantiation types // used to destroy elements when culling excess in vector // // can do nothing if no element destruction necessary // Only called if inDeleteWhenCulling is set in constructor void deleteElementOfType( Type inElement ); }; template inline SizeLimitedVector::SizeLimitedVector( int inLimit, char inDeleteWhenCulling ) : mLimit( inLimit ), mDeleteWhenCulling( inDeleteWhenCulling ) { } template inline void SizeLimitedVector::push_back( Type x ) { SimpleVector::push_back( x ); int oldNum = SimpleVector::size(); if( oldNum >= mLimit * 2 ) { printf( "Passed limit of %d with %d elements...\n", mLimit, oldNum ); // cull back down to mLimit elements by removing excess int numToRemove = oldNum - mLimit; if( mDeleteWhenCulling ) { for( int i=0; i::elements[i]; deleteElementOfType( SimpleVector::elements[i] ); } } for( int i=numToRemove; i::elements[i - numToRemove] = SimpleVector::elements[i]; } SimpleVector::numFilledElements -= numToRemove; printf( "...After culling, %d left\n", SimpleVector::size() ); } } #endif