ReusableVector< DATA_TYPE > Class Template Reference

Create a vector of DATA_TYPE that reuses created objects to save on memory reallocations. More...

#include <ReusableVector.h>

Collaboration diagram for ReusableVector< DATA_TYPE >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void reset ()
 Clear the vector contents.
void clear ()
 Clear the vector contents.
DATA_TYPE & getNextEmpty ()
 Get a reference to a new entry to be populated so the user can directly populate it rather than having to copy into it.
DATA_TYPE & get (unsigned int index)
 Get a reference to the data at the specified index.
int size () const
 Return the number of populated entries in the vector.
void rmLast ()

Protected Attributes

std::vector< DATA_TYPE * > myCont
unsigned int myNextEmpty

Detailed Description

template<class DATA_TYPE>
class ReusableVector< DATA_TYPE >

Create a vector of DATA_TYPE that reuses created objects to save on memory reallocations.

DATA_TYPE must have a function called clear() that is used to reset it for reuse.

Definition at line 30 of file ReusableVector.h.


Member Function Documentation

template<class DATA_TYPE >
DATA_TYPE & ReusableVector< DATA_TYPE >::get ( unsigned int  index  )  [inline]

Get a reference to the data at the specified index.

Throws an exception if the index is out of range.

Definition at line 117 of file ReusableVector.h.

00118 {
00119     if((index < myNextEmpty) && (index >= 0))
00120     {
00121         // index is a valid position, so return that data.
00122         if(myCont[index] == NULL)
00123         {
00124             throw(std::runtime_error("ReusableVector::get BUG, found a null pointer."));
00125         }
00126         return(*myCont[index]);
00127     }
00128 
00129     // Not set in the vector, so throw an exception.
00130     throw(std::runtime_error("ReusableVector::get called with out of range index."));
00131     // return(myCont[0]);
00132 }

template<class DATA_TYPE >
DATA_TYPE & ReusableVector< DATA_TYPE >::getNextEmpty (  )  [inline]

Get a reference to a new entry to be populated so the user can directly populate it rather than having to copy into it.

Definition at line 90 of file ReusableVector.h.

00091 {
00092     if(myNextEmpty == myCont.size())
00093     {
00094         // We are at the end of the available entries, so add a new one.
00095         myCont.resize(myCont.size() + 1);
00096 
00097         // Create a new entry.
00098         myCont[myNextEmpty] = new DATA_TYPE;
00099     }
00100     else
00101     {
00102         // myNextEmpty is an element, and not the end.
00103         // So, clear out the data.
00104         myCont[myNextEmpty]->clear();
00105     }
00106 
00107     DATA_TYPE* returnVal = myCont[myNextEmpty];
00108 
00109     // Increment next empty to the next element.
00110     ++myNextEmpty;
00111     // return the element to be used.
00112     return(*returnVal);
00113 }


The documentation for this class was generated from the following file:
Generated on Mon Feb 11 13:45:25 2013 for libStatGen Software by  doxygen 1.6.3