GeoTessCPP: rstt/GeoTessCPP/include/ArrayReuse.h Source File

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

ArrayReuse.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government 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 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37
38 #ifndef ARRAYREUSE_OBJECT_H
39 #define ARRAYREUSE_OBJECT_H
40
41 // **** _SYSTEM INCLUDES_ ******************************************************
42
43 #include <iostream>
44 #include <fstream>
45 #include <vector>
46
47 // use standard library objects
48 using namespace std;
49
50 // **** _LOCAL INCLUDES_ *******************************************************
51
52 #include "CPPUtils.h"
53 #include "GeoTessException.h"
54
55 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
56
57 namespace geotess {
58
59 // **** _FORWARD REFERENCES_ ***************************************************
60
61 // **** _CLASS DEFINITION_ *****************************************************
62
71 template<typename T>
72 class GEOTESS_EXP_IMP ArrayReuse {
73 private:
74
79 int arrayLength;
80
85 int arraysAddedPerAlloc;
86
91 int initArraysAddedPerAlloc;
92
96 int allocArrays;
97
102 vector<T*> store;
103
109 vector<T*> refStore;
110
117 void add(int count) {
118 store.push_back(new T[count * arrayLength]);
119 for (int i = 0; i < count; ++i)
120 refStore.push_back(&store.back()[i * arrayLength]);
121
122 allocArrays += count;
123 }
124
128 void destroy() {
129 if ((int)refStore.size() != allocArrays)
130 {
131 ostringstream os;
132 os << endl << "ERROR in ArrayReuse::destroy()" << endl
133 << allocArrays << " arrays have been allocated but "
134 << allocArrays - refStore.size()
135 << " of them have not been returned." << endl;
136 throw GeoTessException(os, __FILE__, __LINE__, 23001);
137 }
138 refStore.clear();
139 for (int i = 0; i < (int) store.size(); ++i)
140 delete[] store[i];
141 }
142
143 public:
144
148 ArrayReuse() :
149 arrayLength(1), arraysAddedPerAlloc(1), allocArrays(0) {
150 }
151 ;
152
166 ArrayReuse(int alngth, int acnt) {
167 initialize(alngth, acnt, acnt, 8, acnt + acnt);
168 }
169
185 ArrayReuse(int alngth, int iacnt, int acnt) {
186 initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
187 }
188
210 ArrayReuse(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr) {
211 initialize(alngth, iacnt, acnt, rsrvstr, rsrvrefstr);
212 }
213
217 virtual ~ArrayReuse() {
218 destroy();
219 }
220
226 T* getArray() {
227 if (refStore.size() == 0)
228 add(arraysAddedPerAlloc);
229
230 T* a = refStore.back();
231 refStore.pop_back();
232 return a;
233 }
234
240 void reuseArray(T* a) {
241 refStore.push_back(a);
242 }
243
251 void reset() {
252 refStore.clear();
253 for (int i = 0; i < (int) store.size(); ++i)
254 for (int j = 0;
255 j
256 < ((i == 0) ?
257 initArraysAddedPerAlloc :
258 arraysAddedPerAlloc); ++j) {
259 refStore.push_back(&store[i][j * arrayLength]);
260 }
261 }
262
267 void resetIfRequired() {
268 if ((int) refStore.size() != allocArrays)
269 reset();
270 }
271
287 void initialize(int alngth, int acnt) {
288 initialize(alngth, acnt, acnt, 8, acnt + acnt);
289 }
290
308 void initialize(int alngth, int iacnt, int acnt) {
309 initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
310 }
311
333 void initialize(int alngth, int iacnt, int acnt, int rsrvstr,
334 int rsrvrefstr) {
335 // first delete any previous allocation
336
337 destroy();
338
339 // reinitialize
340
341 arrayLength = alngth;
342 arraysAddedPerAlloc = acnt;
343 initArraysAddedPerAlloc = iacnt;
344 store.reserve(rsrvstr);
345 refStore.reserve(rsrvrefstr);
346 add(iacnt);
347 }
348
353 int getAllocatedArrays() const {
354 return allocArrays;
355 }
356
361 int getUsedArrayCount() const {
362 return allocArrays - (int) refStore.size();
363 }
364
369 int getUnusedArrayCount() const {
370 return (int) refStore.size();
371 }
372
377 int getArrayLength() const {
378 return arrayLength;
379 }
380
381 };
382 // end class ArrayReuse
383
384 }// end namespace geotess
385
386 #endif // ARRAYREUSE_OBJECT_H
geotess
Definition: ArrayReuse.h:57
geotess::ArrayReuse::getAllocatedArrays
int getAllocatedArrays() const
Definition: ArrayReuse.h:353
GeoTessException.h
geotess::ArrayReuse::~ArrayReuse
virtual ~ArrayReuse()
Definition: ArrayReuse.h:217
geotess::ArrayReuse::getUsedArrayCount
int getUsedArrayCount() const
Definition: ArrayReuse.h:361
geotess::ArrayReuse::reuseArray
void reuseArray(T *a)
Definition: ArrayReuse.h:240
geotess::ArrayReuse::initialize
void initialize(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr)
Definition: ArrayReuse.h:333
geotess::ArrayReuse::getArray
T * getArray()
Definition: ArrayReuse.h:226
geotess::ArrayReuse::ArrayReuse
ArrayReuse(int alngth, int acnt)
Definition: ArrayReuse.h:166
geotess::ArrayReuse::initialize
void initialize(int alngth, int acnt)
Definition: ArrayReuse.h:287
geotess::ArrayReuse::initialize
void initialize(int alngth, int iacnt, int acnt)
Definition: ArrayReuse.h:308
geotess::ArrayReuse::ArrayReuse
ArrayReuse(int alngth, int iacnt, int acnt)
Definition: ArrayReuse.h:185
geotess::ArrayReuse::ArrayReuse
ArrayReuse()
Definition: ArrayReuse.h:148
geotess::ArrayReuse
An array reuse object for cases where arrays of some fixed type and size are required by the applicat...
Definition: ArrayReuse.h:72
geotess::ArrayReuse::resetIfRequired
void resetIfRequired()
Definition: ArrayReuse.h:267
geotess::ArrayReuse::ArrayReuse
ArrayReuse(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr)
Definition: ArrayReuse.h:210
geotess::ArrayReuse::getArrayLength
int getArrayLength() const
Definition: ArrayReuse.h:377
GEOTESS_EXP_IMP
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:73
geotess::ArrayReuse::getUnusedArrayCount
int getUnusedArrayCount() const
Definition: ArrayReuse.h:369
geotess::GeoTessException
An exception class for all GeoTess objects.
Definition: GeoTessException.h:68
geotess::ArrayReuse::reset
void reset()
Definition: ArrayReuse.h:251
CPPUtils.h