libStatGen Software 1
Loading...
Searching...
No Matches
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 >:

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.

Constructor & Destructor Documentation

◆ ReusableVector()

template<class DATA_TYPE >
ReusableVector< DATA_TYPE >::ReusableVector ( )
inline

Definition at line 33 of file ReusableVector.h.

33: myCont(), myNextEmpty(0) {}

◆ ~ReusableVector()

template<class DATA_TYPE >
ReusableVector< DATA_TYPE >::~ReusableVector ( )
virtual

Definition at line 67 of file ReusableVector.h.

68{
69 for(unsigned int i = 0; i < myCont.size(); i++)
70 {
71 // Delete all the entries.
72 delete myCont[i];
73 myCont[i] = NULL;
74 }
75 myCont.clear();
76 myNextEmpty = 0;
77}

Member Function Documentation

◆ clear()

template<class DATA_TYPE >
void ReusableVector< DATA_TYPE >::clear ( )
inline

Clear the vector contents.

Definition at line 39 of file ReusableVector.h.

39{reset();}
void reset()
Clear the vector contents.

References ReusableVector< DATA_TYPE >::reset().

◆ get()

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.

118{
119 if((index < myNextEmpty) && (index >= 0))
120 {
121 // index is a valid position, so return that data.
122 if(myCont[index] == NULL)
123 {
124 throw(std::runtime_error("ReusableVector::get BUG, found a null pointer."));
125 }
126 return(*myCont[index]);
127 }
128
129 // Not set in the vector, so throw an exception.
130 throw(std::runtime_error("ReusableVector::get called with out of range index."));
131 // return(myCont[0]);
132}

◆ getNextEmpty()

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.

91{
92 if(myNextEmpty == myCont.size())
93 {
94 // We are at the end of the available entries, so add a new one.
95 myCont.resize(myCont.size() + 1);
96
97 // Create a new entry.
98 myCont[myNextEmpty] = new DATA_TYPE;
99 }
100 else
101 {
102 // myNextEmpty is an element, and not the end.
103 // So, clear out the data.
104 myCont[myNextEmpty]->clear();
105 }
106
107 DATA_TYPE* returnVal = myCont[myNextEmpty];
108
109 // Increment next empty to the next element.
110 ++myNextEmpty;
111 // return the element to be used.
112 return(*returnVal);
113}

◆ reset()

template<class DATA_TYPE >
void ReusableVector< DATA_TYPE >::reset ( )

Clear the vector contents.

Definition at line 81 of file ReusableVector.h.

82{
83 // Set the next empty element to be the first one on the list.
84 // That means there are none used.
85 myNextEmpty = 0;
86}

Referenced by ReusableVector< DATA_TYPE >::clear().

◆ rmLast()

template<class DATA_TYPE >
void ReusableVector< DATA_TYPE >::rmLast ( )

Definition at line 135 of file ReusableVector.h.

136{
137 if(myNextEmpty > 0)
138 {
139 --myNextEmpty;
140 }
141}

◆ size()

template<class DATA_TYPE >
int ReusableVector< DATA_TYPE >::size ( ) const
inline

Return the number of populated entries in the vector.

Definition at line 51 of file ReusableVector.h.

51{return(myNextEmpty);}

Member Data Documentation

◆ myCont

template<class DATA_TYPE >
std::vector<DATA_TYPE*> ReusableVector< DATA_TYPE >::myCont
protected

Definition at line 56 of file ReusableVector.h.

◆ myNextEmpty

template<class DATA_TYPE >
unsigned int ReusableVector< DATA_TYPE >::myNextEmpty
protected

Definition at line 57 of file ReusableVector.h.


The documentation for this class was generated from the following file: