libStatGen Software 1
Loading...
Searching...
No Matches
ReusableVector.h
1/*
2 * Copyright (C) 2011 Regents of the University of Michigan,
3 * Hyun Min Kang, Matthew Flickenger, Matthew Snyder,
4 * and Goncalo Abecasis
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <vector>
21#include <stdexcept>
22
23#ifndef __REUSABLE_VECTOR_H__
24#define __REUSABLE_VECTOR_H__
25
26/// Create a vector of DATA_TYPE that reuses created objects to save on
27/// memory reallocations. DATA_TYPE must have a function called clear()
28/// that is used to reset it for reuse.
29template <class DATA_TYPE>
31{
32public:
33 ReusableVector(): myCont(), myNextEmpty(0) {}
34 virtual ~ReusableVector();
35
36 /// Clear the vector contents.
37 void reset();
38 /// Clear the vector contents.
39 void clear() {reset();}
40
41 /// Get a reference to a new entry to be populated so the user can
42 /// directly populate it rather than having to copy into it.
43 DATA_TYPE& getNextEmpty();
44
45 /// Get a reference to the data at the specified index.
46 /// Throws an exception if the index is out of range.
47 DATA_TYPE& get(unsigned int index) const;
48
49 /// Return the number of populated entries in the vector.
50 // The next empty position is the same as the size.
51 int size() const {return(myNextEmpty);}
52
53 void rmLast();
54
55protected:
56 std::vector<DATA_TYPE*> myCont;
57 unsigned int myNextEmpty;
58private:
59 ReusableVector& operator=(const ReusableVector& rv);
61};
62
63
64/////////////////////////////////////////////////////////////
65// ReusableVector
66template <class DATA_TYPE>
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}
78
79
80template <class DATA_TYPE>
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}
87
88
89template <class DATA_TYPE>
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}
114
115
116template <class DATA_TYPE>
117DATA_TYPE& ReusableVector<DATA_TYPE>::get(unsigned int index) const
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}
133
134template <class DATA_TYPE>
136{
137 if(myNextEmpty > 0)
138 {
139 --myNextEmpty;
140 }
141}
142
143#endif
Create a vector of DATA_TYPE that reuses created objects to save on memory reallocations.
DATA_TYPE & getNextEmpty()
Get a reference to a new entry to be populated so the user can directly populate it rather than havin...
void clear()
Clear the vector contents.
DATA_TYPE & get(unsigned int index) const
Get a reference to the data at the specified index.
void reset()
Clear the vector contents.
int size() const
Return the number of populated entries in the vector.