00001 //# TempLatticeImpl.h: A Lattice that can be used for temporary storage 00002 //# Copyright (C) 1997,1998,1999,2000,2003 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# 00027 //# $Id: TempLatticeImpl.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $ 00028 00029 #ifndef LATTICES_TEMPLATTICEIMPL_H 00030 #define LATTICES_TEMPLATTICEIMPL_H 00031 00032 00033 //# Includes 00034 #include <casacore/casa/aips.h> 00035 #include <casacore/lattices/Lattices/Lattice.h> 00036 #include <casacore/lattices/Lattices/TiledShape.h> 00037 #include <casacore/tables/Tables/Table.h> 00038 #include <casacore/casa/Utilities/CountedPtr.h> 00039 00040 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00041 00042 //# Forward Declarations 00043 class Table; 00044 00045 00046 // <summary> 00047 // The class implementing TempLattice 00048 // </summary> 00049 00050 // <use visibility=local> 00051 00052 // <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tTempLattice.cc" demos=""> 00053 // </reviewed> 00054 00055 // <prerequisite> 00056 // <li> <linkto class="TempLattice">Lattice</linkto> 00057 // </prerequisite> 00058 00059 // <synopsis> 00060 // The class is used as <src>CountedPtr<TempLatticeImpl></src> in class 00061 // TempLattice. In that way the making a copy of a TempLattice uses the 00062 // same object underneath. 00063 // This was needed to have a correct implementation of tempClose. Otherwise 00064 // when deleting a copy of a TempLattice, that destructor would delete the 00065 // underlying table and the original TempLattice could not reopen it. 00066 // </synopsis> 00067 00068 00069 template<class T> class TempLatticeImpl 00070 { 00071 public: 00072 // The default constructor creates a TempLatticeImpl containing a 00073 // default ArrayLattice object. 00074 TempLatticeImpl(); 00075 00076 // Create a TempLatticeImpl of the specified shape. You can specify how much 00077 // memory the Lattice can consume before it becomes disk based by giving a 00078 // non-negative value to the maxMemoryInMB argument. Otherwise it will assume 00079 // it can use up to 25% of the memory on your machine as defined in aipsrc 00080 // (this algorithm may change). Setting maxMemoryInMB to zero will force 00081 // the lattice to disk. 00082 // <group> 00083 TempLatticeImpl (const TiledShape& shape, Int maxMemoryInMB); 00084 TempLatticeImpl (const TiledShape& shape, Double maxMemoryInMB); 00085 // </group> 00086 00087 // The destructor removes the Lattice from memory and if necessary disk. 00088 ~TempLatticeImpl(); 00089 00090 // Is the TempLattice paged to disk? 00091 Bool isPaged() const 00092 { return (! itsTableName.empty()); } 00093 00094 // Can the lattice data be referenced as an array section? 00095 Bool canReferenceArray() const 00096 { return (itsTableName.empty()); } 00097 00098 // Is the TempLattice writable? It should be. 00099 Bool isWritable() const 00100 { return True; } 00101 00102 // Flush the data. 00103 void flush() 00104 { if (itsTablePtr != 0) itsTablePtr->flush(); } 00105 00106 // Close the Lattice temporarily (if it is paged to disk). 00107 // It'll be reopened automatically when needed or when 00108 // <src>reopen</src> is called explicitly. 00109 void tempClose(); 00110 00111 // If needed, reopen a temporarily closed TempLatticeImpl. 00112 void reopen(); 00113 00114 // Return the shape of the Lattice including all degenerate axes. 00115 // (ie. axes with a length of one) 00116 IPosition shape() const 00117 { doReopen(); return itsLatticePtr->shape(); } 00118 00119 // Set all of the elements in the Lattice to the given value. 00120 void set (const T& value) 00121 { doReopen(); itsLatticePtr->set (value); } 00122 00123 // Replace every element, x, of the Lattice with the result of f(x). You 00124 // must pass in the address of the function -- so the function must be 00125 // declared and defined in the scope of your program. All versions of 00126 // apply require a function that accepts a single argument of type T (the 00127 // Lattice template type) and return a result of the same type. The first 00128 // apply expects a function with an argument passed by value; the second 00129 // expects the argument to be passed by const reference; the third 00130 // requires an instance of the class <src>Functional<T,T></src>. The 00131 // first form ought to run faster for the built-in types, which may be an 00132 // issue for large Lattices stored in memory, where disk access is not an 00133 // issue. 00134 // <group> 00135 void apply (T (*function)(T)) 00136 { doReopen(); itsLatticePtr->apply (function); } 00137 void apply (T (*function)(const T&)) 00138 { doReopen(); itsLatticePtr->apply (function); } 00139 void apply (const Functional<T,T>& function) 00140 { doReopen(); itsLatticePtr->apply (function); } 00141 // </group> 00142 00143 // This function returns the recommended maximum number of pixels to 00144 // include in the cursor of an iterator. 00145 uInt advisedMaxPixels() const 00146 { doReopen(); return itsLatticePtr->advisedMaxPixels(); } 00147 00148 // Get the best cursor shape. 00149 IPosition doNiceCursorShape (uInt maxPixels) 00150 { doReopen(); return itsLatticePtr->niceCursorShape (maxPixels); } 00151 00152 // Maximum size - not necessarily all used. In pixels. 00153 uInt maximumCacheSize() const 00154 { return itsLatticePtr->maximumCacheSize(); } 00155 00156 // Set the maximum (allowed) cache size as indicated. 00157 void setMaximumCacheSize (uInt howManyPixels) 00158 { itsLatticePtr->setMaximumCacheSize (howManyPixels); } 00159 00160 // Set the cache size as to "fit" the indicated path. 00161 void setCacheSizeFromPath (const IPosition& sliceShape, 00162 const IPosition& windowStart, 00163 const IPosition& windowLength, 00164 const IPosition& axisPath) 00165 { itsLatticePtr->setCacheSizeFromPath (sliceShape, windowStart, windowLength, 00166 axisPath); } 00167 00168 // Set the actual cache size for this Array to be be big enough for the 00169 // indicated number of tiles. This cache is not shared with PagedArrays 00170 // in other rows and is always clipped to be less than the maximum value 00171 // set using the setMaximumCacheSize member function. 00172 // tiles. Tiles are cached using a first in first out algorithm. 00173 void setCacheSizeInTiles (uInt howManyTiles) 00174 { itsLatticePtr->setCacheSizeInTiles (howManyTiles); } 00175 00176 // Clears and frees up the caches, but the maximum allowed cache size is 00177 // unchanged from when setCacheSize was called 00178 void clearCache() 00179 { itsLatticePtr->clearCache(); } 00180 00181 // Report on cache success. 00182 void showCacheStatistics (ostream& os) const 00183 { itsLatticePtr->showCacheStatistics (os); } 00184 00185 // Get or put a single element in the lattice. 00186 // Note that Lattice::operator() can also be used to get a single element. 00187 // <group> 00188 T getAt (const IPosition& where) const 00189 { doReopen(); return itsLatticePtr->getAt (where); } 00190 void putAt (const T& value, const IPosition& where) 00191 { doReopen(); itsLatticePtr->putAt (value, where); } 00192 // </group> 00193 00194 // Check class internals - used for debugging. Should always return True 00195 Bool ok() const 00196 { doReopen(); return itsLatticePtr->ok(); } 00197 00198 // This function is used by the LatticeIterator class to generate an 00199 // iterator of the correct type for this Lattice. Not recommended 00200 // for general use. 00201 LatticeIterInterface<T>* makeIter (const LatticeNavigator& navigator, 00202 Bool useRef) const 00203 { doReopen(); return itsLatticePtr->makeIter (navigator, useRef); } 00204 00205 // Do the actual getting of an array of values. 00206 Bool doGetSlice (Array<T>& buffer, const Slicer& section) 00207 { doReopen(); return itsLatticePtr->doGetSlice (buffer, section); } 00208 00209 // Do the actual getting of an array of values. 00210 void doPutSlice (const Array<T>& sourceBuffer, 00211 const IPosition& where, 00212 const IPosition& stride) 00213 { doReopen(); itsLatticePtr->putSlice (sourceBuffer, where, stride); } 00214 00215 // Do the reopen of the table (if not open already). 00216 void doReopen() const 00217 { if (itsIsClosed) tempReopen(); } 00218 00219 private: 00220 // The copy constructor cannot be used. 00221 TempLatticeImpl (const TempLatticeImpl<T>& other) ; 00222 00223 // The assignment operator cannot be used. 00224 TempLatticeImpl<T>& operator= (const TempLatticeImpl<T>& other); 00225 00226 // Initialize the object. 00227 void init (const TiledShape& shape, Double maxMemoryInMB=-1); 00228 00229 // Do the actual reopen of the temporarily closed table (if not open already). 00230 void tempReopen() const; 00231 00232 // Make sure that the temporary table gets deleted. 00233 void deleteTable(); 00234 00235 00236 mutable Table* itsTablePtr; 00237 mutable CountedPtr<Lattice<T> > itsLatticePtr; 00238 String itsTableName; 00239 mutable Bool itsIsClosed; 00240 }; 00241 00242 00243 00244 } //# NAMESPACE CASACORE - END 00245 00246 #ifndef CASACORE_NO_AUTO_TEMPLATES 00247 #include <casacore/lattices/Lattices/TempLatticeImpl.tcc> 00248 #endif //# CASACORE_NO_AUTO_TEMPLATES 00249 #endif