GeoTessCPP: include/ArrayReuse.h Source File

GeoTessCPP 2.0.0 Software to facilitate storage and retrieval of 3D information about the Earth.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Friends Macros
ArrayReuse.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35
36 #ifndef ARRAYREUSE_OBJECT_H
37 #define ARRAYREUSE_OBJECT_H
38
39 // **** _SYSTEM INCLUDES_ ******************************************************
40
41 #include <iostream>
42 #include <fstream>
43 #include <vector>
44
45 // use standard library objects
46 using namespace std;
47
48 // **** _LOCAL INCLUDES_ *******************************************************
49
50 #include "CPPUtils.h"
51
52 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
53
54 namespace geotess {
55
56 // **** _FORWARD REFERENCES_ ***************************************************
57
58 // **** _CLASS DEFINITION_ *****************************************************
59
68 template<typename T>
69 class GEOTESS_EXP_IMP ArrayReuse
70 {
71 private:
72
77 int arrayLength;
78
83 int arraysAddedPerAlloc;
84
89 int initArraysAddedPerAlloc;
90
94 int allocArrays;
95
100 vector<T*> store;
101
107 vector<T*> refStore;
108
115 void add(int count)
116 {
117 store.push_back(new T [count * arrayLength]);
118 for (int i = 0; i < count; ++i)
119 refStore.push_back(&store.back()[i * arrayLength]);
120
121 allocArrays += count;
122 }
123
127 void destroy()
128 {
129 refStore.clear();
130 for (int i = 0; i < (int) store.size(); ++i) delete [] store[i];
131 }
132
133 public:
134
138 ArrayReuse() : arrayLength(1), arraysAddedPerAlloc(1),
139 allocArrays(0) {};
140
154 ArrayReuse(int alngth, int acnt)
155 {
156 initialize(alngth, acnt, acnt, 8, acnt + acnt);
157 }
158
174 ArrayReuse(int alngth, int iacnt, int acnt)
175 {
176 initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
177 }
178
200 ArrayReuse(int alngth, int iacnt, int acnt,
201 int rsrvstr, int rsrvrefstr)
202 {
203 initialize(alngth, iacnt, acnt, rsrvstr, rsrvrefstr);
204 }
205
209 virtual ~ArrayReuse() { destroy(); }
210
215 T* getArray()
216 {
217 if (refStore.size() == 0) add(arraysAddedPerAlloc);
218
219 T* a = refStore.back();
220 refStore.pop_back();
221 return a;
222 }
223
228 void reuseArray(T* a) { refStore.push_back(a); }
229
237 void reset()
238 {
239 refStore.clear();
240 for (int i = 0; i < (int) store.size(); ++i)
241 for (int j = 0;
242 j < ((i == 0) ? initArraysAddedPerAlloc : arraysAddedPerAlloc); ++j)
243 {
244 refStore.push_back(&store[i][j * arrayLength]);
245 }
246 }
247
252 void resetIfRequired()
253 {
254 if ((int) refStore.size() != allocArrays) reset();
255 }
256
272 void initialize(int alngth, int acnt)
273 {
274 initialize(alngth, acnt, acnt, 8, acnt + acnt);
275 }
276
294 void initialize(int alngth, int iacnt, int acnt)
295 {
296 initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
297 }
298
320 void initialize(int alngth, int iacnt, int acnt,
321 int rsrvstr, int rsrvrefstr)
322 {
323 // first delete any previous allocation
324
325 destroy();
326
327 // reinitialize
328
329 arrayLength = alngth;
330 arraysAddedPerAlloc = acnt;
331 initArraysAddedPerAlloc = iacnt;
332 store.reserve(rsrvstr);
333 refStore.reserve(rsrvrefstr);
334 add(iacnt);
335 }
336
340 int getAllocatedArrays() const { return allocArrays; }
341
345 int getUsedArrayCount() const
346 {
347 return allocArrays - (int) refStore.size();
348 }
349
353 int getUnusedArrayCount() const
354 {
355 return (int) refStore.size();
356 }
357
361 int getArrayLength() const { return arrayLength; }
362
363 }; // end class ArrayReuse
364
365 } // end namespace geotess
366
367 #endif // ARRAYREUSE_OBJECT_H