libStatGen Software  1
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 >:

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) const
 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) const

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.

{
    if((index < myNextEmpty) && (index >= 0))
    {
        // index is a valid position, so return that data.
        if(myCont[index] == NULL)
        {
            throw(std::runtime_error("ReusableVector::get BUG, found a null pointer."));
        }
        return(*myCont[index]);
    }

    // Not set in the vector, so throw an exception.
    throw(std::runtime_error("ReusableVector::get called with out of range index."));
    // return(myCont[0]);
}
template<class DATA_TYPE >
DATA_TYPE & ReusableVector< 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.

Definition at line 90 of file ReusableVector.h.

{
    if(myNextEmpty == myCont.size())
    {
        // We are at the end of the available entries, so add a new one.
        myCont.resize(myCont.size() + 1);

        // Create a new entry.
        myCont[myNextEmpty] = new DATA_TYPE;
    }
    else
    {
        // myNextEmpty is an element, and not the end.
        // So, clear out the data.
        myCont[myNextEmpty]->clear();
    }

    DATA_TYPE* returnVal = myCont[myNextEmpty];

    // Increment next empty to the next element.
    ++myNextEmpty;
    // return the element to be used.
    return(*returnVal);
}

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends