GeoTessCPP  2.0.0
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
include/ArrayReuse.h
Go to the documentation of this file.
00001 //- ****************************************************************************
00002 //- 
00003 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
00004 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00005 //- retains certain rights in this software.
00006 //- 
00007 //- BSD Open Source License.
00008 //- All rights reserved.
00009 //- 
00010 //- Redistribution and use in source and binary forms, with or without
00011 //- modification, are permitted provided that the following conditions are met:
00012 //- 
00013 //-    * Redistributions of source code must retain the above copyright notice,
00014 //-      this list of conditions and the following disclaimer.
00015 //-    * Redistributions in binary form must reproduce the above copyright
00016 //-      notice, this list of conditions and the following disclaimer in the
00017 //-      documentation and/or other materials provided with the distribution.
00018 //-    * Neither the name of Sandia National Laboratories nor the names of its
00019 //-      contributors may be used to endorse or promote products derived from
00020 //-      this software without specific prior written permission.
00021 //- 
00022 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00026 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00028 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00029 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00030 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 //- POSSIBILITY OF SUCH DAMAGE.
00033 //-
00034 //- ****************************************************************************
00035 
00036 #ifndef ARRAYREUSE_OBJECT_H
00037 #define ARRAYREUSE_OBJECT_H
00038 
00039 // **** _SYSTEM INCLUDES_ ******************************************************
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 #include <vector>
00044 
00045 // use standard library objects
00046 using namespace std;
00047 
00048 // **** _LOCAL INCLUDES_ *******************************************************
00049 
00050 #include "CPPUtils.h"
00051 
00052 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
00053 
00054 namespace geotess {
00055 
00056 // **** _FORWARD REFERENCES_ ***************************************************
00057 
00058 // **** _CLASS DEFINITION_ *****************************************************
00059 
00068 template<typename T>
00069 class GEOTESS_EXP_IMP ArrayReuse
00070 {
00071         private:
00072 
00077                 int                                                     arrayLength;
00078 
00083                 int                                                     arraysAddedPerAlloc;
00084 
00089                 int                                                     initArraysAddedPerAlloc;
00090 
00094                 int                                                     allocArrays;
00095 
00100                 vector<T*>                      store;
00101 
00107                 vector<T*>                      refStore;
00108 
00115                 void                                            add(int count)
00116                 {
00117                         store.push_back(new T [count * arrayLength]);
00118                         for (int i = 0; i < count; ++i)
00119                                 refStore.push_back(&store.back()[i * arrayLength]);
00120 
00121                         allocArrays += count;
00122                 }
00123 
00127                 void                                            destroy()
00128                 {
00129                         refStore.clear();
00130                         for (int i = 0; i < (int) store.size(); ++i) delete [] store[i];
00131                 }
00132 
00133         public:
00134 
00138                                                                                 ArrayReuse() : arrayLength(1), arraysAddedPerAlloc(1),
00139                                                                                                                                          allocArrays(0) {};
00140 
00154                                                                                 ArrayReuse(int alngth, int acnt)
00155                                                                                 {
00156                                                                                         initialize(alngth, acnt, acnt, 8, acnt + acnt);
00157                                                                                 }
00158 
00174                                                                                 ArrayReuse(int alngth, int iacnt, int acnt)
00175                                                                                 {
00176                                                                                         initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
00177                                                                                 }
00178 
00200                                                                                 ArrayReuse(int alngth, int iacnt, int acnt,
00201                                                                                                                                                 int rsrvstr, int rsrvrefstr)
00202                                                                                 {
00203                                                                                         initialize(alngth, iacnt, acnt, rsrvstr, rsrvrefstr);
00204                                                                                 }
00205 
00209                 virtual                                 ~ArrayReuse() { destroy(); }
00210 
00216                 T*                                                      getArray()
00217                                                                                 {
00218                                                                                         if (refStore.size() == 0)       add(arraysAddedPerAlloc);
00219 
00220                                                                                         T* a = refStore.back();
00221                                                                                         refStore.pop_back();
00222                                                                                         return a;
00223                                                                                 }
00224 
00230                 void                                            reuseArray(T* a) { refStore.push_back(a); }
00231 
00239                 void                                            reset()
00240                 {
00241                         refStore.clear();
00242                         for (int i = 0; i < (int) store.size(); ++i)
00243                                 for (int j = 0;
00244                                                  j < ((i == 0) ? initArraysAddedPerAlloc : arraysAddedPerAlloc); ++j)
00245                                 {
00246                                         refStore.push_back(&store[i][j * arrayLength]);
00247                                 }
00248                 }
00249 
00254                 void                                            resetIfRequired()
00255                                                                                 {
00256                                                                                         if ((int) refStore.size() != allocArrays) reset();
00257                                                                                 }
00258 
00274                 void                                            initialize(int alngth, int acnt)
00275                                                                                 {
00276                                                                                         initialize(alngth, acnt, acnt, 8, acnt + acnt);
00277                                                                                 }
00278 
00296                 void                                            initialize(int alngth, int iacnt, int acnt)
00297                                                                                 {
00298                                                                                         initialize(alngth, iacnt, acnt, 8, iacnt + acnt);
00299                                                                                 }
00300 
00322                 void                                            initialize(int alngth, int iacnt, int acnt,
00323                                                                                                                                 int rsrvstr, int rsrvrefstr)
00324                                                                                 {
00325                                                                                         // first delete any previous allocation
00326 
00327                                                                                         destroy();
00328 
00329                                                                                         // reinitialize
00330 
00331                                                                                         arrayLength = alngth;
00332                                                                                         arraysAddedPerAlloc = acnt;
00333                                                                                         initArraysAddedPerAlloc = iacnt;
00334                                                                                         store.reserve(rsrvstr);
00335                                                                                         refStore.reserve(rsrvrefstr);
00336                                                                                         add(iacnt);
00337                                                                                 }
00338 
00343                 int                                                     getAllocatedArrays() const { return allocArrays; }
00344 
00349                 int                                                     getUsedArrayCount() const
00350                                                                                 {
00351                                                                                         return allocArrays - (int) refStore.size();
00352                                                                                 }
00353 
00358                 int                                                     getUnusedArrayCount() const
00359                                                                                 {
00360                                                                                         return (int) refStore.size();
00361                                                                                 }
00362 
00367                 int                                                     getArrayLength() const { return arrayLength; }
00368 
00369 }; // end class ArrayReuse
00370 
00371 } // end namespace geotess
00372 
00373 #endif  // ARRAYREUSE_OBJECT_H