GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 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>
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 
216  T* getArray()
217  {
218  if (refStore.size() == 0) add(arraysAddedPerAlloc);
219 
220  T* a = refStore.back();
221  refStore.pop_back();
222  return a;
223  }
224 
230  void reuseArray(T* a) { refStore.push_back(a); }
231 
239  void reset()
240  {
241  refStore.clear();
242  for (int i = 0; i < (int) store.size(); ++i)
243  for (int j = 0;
244  j < ((i == 0) ? initArraysAddedPerAlloc : arraysAddedPerAlloc); ++j)
245  {
246  refStore.push_back(&store[i][j * arrayLength]);
247  }
248  }
249 
254  void resetIfRequired()
255  {
256  if ((int) refStore.size() != allocArrays) reset();
257  }
258 
274  void initialize(int alngth, int acnt)
275  {
276  initialize(alngth, acnt, acnt, 8, acnt + acnt);
277  }
278 
296  void initialize(int alngth, int iacnt, int acnt)
297  {
298  initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
299  }
300 
322  void initialize(int alngth, int iacnt, int acnt,
323  int rsrvstr, int rsrvrefstr)
324  {
325  // first delete any previous allocation
326 
327  destroy();
328 
329  // reinitialize
330 
331  arrayLength = alngth;
332  arraysAddedPerAlloc = acnt;
333  initArraysAddedPerAlloc = iacnt;
334  store.reserve(rsrvstr);
335  refStore.reserve(rsrvrefstr);
336  add(iacnt);
337  }
338 
343  int getAllocatedArrays() const { return allocArrays; }
344 
349  int getUsedArrayCount() const
350  {
351  return allocArrays - (int) refStore.size();
352  }
353 
358  int getUnusedArrayCount() const
359  {
360  return (int) refStore.size();
361  }
362 
367  int getArrayLength() const { return arrayLength; }
368 
369 }; // end class ArrayReuse
370 
371 } // end namespace geotess
372 
373 #endif // ARRAYREUSE_OBJECT_H