GeoTessCPP  2.2
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 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 #include "GeoTessException.h"
52 
53 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
54 
55 namespace geotess {
56 
57 // **** _FORWARD REFERENCES_ ***************************************************
58 
59 // **** _CLASS DEFINITION_ *****************************************************
60 
69 template<typename T>
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  store.push_back(new T[count * arrayLength]);
117  for (int i = 0; i < count; ++i)
118  refStore.push_back(&store.back()[i * arrayLength]);
119 
120  allocArrays += count;
121  }
122 
126  void destroy() {
127  if ((int)refStore.size() != allocArrays)
128  {
129  ostringstream os;
130  os << endl << "ERROR in ArrayReuse::destroy()" << endl
131  << allocArrays << " arrays have been allocated but "
132  << allocArrays - refStore.size()
133  << " of them have not been returned." << endl;
134  throw GeoTessException(os, __FILE__, __LINE__, 23001);
135  }
136  refStore.clear();
137  for (int i = 0; i < (int) store.size(); ++i)
138  delete[] store[i];
139  }
140 
141 public:
142 
147  arrayLength(1), arraysAddedPerAlloc(1), allocArrays(0) {
148  }
149  ;
150 
164  ArrayReuse(int alngth, int acnt) {
165  initialize(alngth, acnt, acnt, 8, acnt + acnt);
166  }
167 
183  ArrayReuse(int alngth, int iacnt, int acnt) {
184  initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
185  }
186 
208  ArrayReuse(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr) {
209  initialize(alngth, iacnt, acnt, rsrvstr, rsrvrefstr);
210  }
211 
215  virtual ~ArrayReuse() {
216  destroy();
217  }
218 
224  T* getArray() {
225  if (refStore.size() == 0)
226  add(arraysAddedPerAlloc);
227 
228  T* a = refStore.back();
229  refStore.pop_back();
230  return a;
231  }
232 
238  void reuseArray(T* a) {
239  refStore.push_back(a);
240  }
241 
249  void reset() {
250  refStore.clear();
251  for (int i = 0; i < (int) store.size(); ++i)
252  for (int j = 0;
253  j
254  < ((i == 0) ?
255  initArraysAddedPerAlloc :
256  arraysAddedPerAlloc); ++j) {
257  refStore.push_back(&store[i][j * arrayLength]);
258  }
259  }
260 
266  if ((int) refStore.size() != allocArrays)
267  reset();
268  }
269 
285  void initialize(int alngth, int acnt) {
286  initialize(alngth, acnt, acnt, 8, acnt + acnt);
287  }
288 
306  void initialize(int alngth, int iacnt, int acnt) {
307  initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
308  }
309 
331  void initialize(int alngth, int iacnt, int acnt, int rsrvstr,
332  int rsrvrefstr) {
333  // first delete any previous allocation
334 
335  destroy();
336 
337  // reinitialize
338 
339  arrayLength = alngth;
340  arraysAddedPerAlloc = acnt;
341  initArraysAddedPerAlloc = iacnt;
342  store.reserve(rsrvstr);
343  refStore.reserve(rsrvrefstr);
344  add(iacnt);
345  }
346 
351  int getAllocatedArrays() const {
352  return allocArrays;
353  }
354 
359  int getUsedArrayCount() const {
360  return allocArrays - (int) refStore.size();
361  }
362 
367  int getUnusedArrayCount() const {
368  return (int) refStore.size();
369  }
370 
375  int getArrayLength() const {
376  return arrayLength;
377  }
378 
379 };
380 // end class ArrayReuse
381 
382 }// end namespace geotess
383 
384 #endif // ARRAYREUSE_OBJECT_H
void initialize(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr)
Initializes sizes where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:331
int getUnusedArrayCount() const
Returns the current number of unused allocated arrays.
Definition: ArrayReuse.h:367
void reuseArray(T *a)
Returns an array that was being used by a client back into the reuse pool.
Definition: ArrayReuse.h:238
Definition: ArrayReuse.h:55
int getAllocatedArrays() const
Returns the total number of allocated arrays (each of arrayLength).
Definition: ArrayReuse.h:351
void initialize(int alngth, int acnt)
Initializes sizes where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:285
An array reuse object for cases where arrays of some fixed type and size are required by the applicat...
Definition: ArrayReuse.h:70
T * getArray()
Returns a new array of size arrayLength for use by requesting clients.
Definition: ArrayReuse.h:224
ArrayReuse(int alngth, int acnt)
Standard constructor where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:164
ArrayReuse(int alngth, int iacnt, int acnt)
Standard constructor where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:183
ArrayReuse()
Default constructor.
Definition: ArrayReuse.h:146
An exception class for all GeoTess objects.
Definition: GeoTessException.h:65
ArrayReuse(int alngth, int iacnt, int acnt, int rsrvstr, int rsrvrefstr)
Standard constructor where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:208
void initialize(int alngth, int iacnt, int acnt)
Initializes sizes where alngth is the size of the arrays to be provided to requesting clients...
Definition: ArrayReuse.h:306
int getUsedArrayCount() const
Returns the number of arrays currently in use by clients.
Definition: ArrayReuse.h:359
void resetIfRequired()
Calls reset if the size of refStore is not equal to the total number of allocated arrays (allocArrays...
Definition: ArrayReuse.h:265
int getArrayLength() const
Returns the array length setting.
Definition: ArrayReuse.h:375
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
virtual ~ArrayReuse()
Destructor.
Definition: ArrayReuse.h:215
void reset()
Resets refStore to all allocated references in store.
Definition: ArrayReuse.h:249