casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TempLatticeImpl.h
Go to the documentation of this file.
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 <lattices/Lattices/Lattice.h>
00035 #include <lattices/Lattices/TiledShape.h>
00036 #include <tables/Tables/Table.h>
00037 #include <casa/Utilities/CountedPtr.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 //# Forward Declarations
00042 class Table;
00043 
00044 
00045 // <summary>
00046 // The class implementing TempLattice
00047 // </summary>
00048 
00049 // <use visibility=local>
00050 
00051 // <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tTempLattice.cc" demos="">
00052 // </reviewed>
00053 
00054 // <prerequisite>
00055 //   <li> <linkto class="TempLattice">Lattice</linkto>
00056 // </prerequisite>
00057 
00058 // <synopsis>
00059 // The class is used as <src>CountedPtr<TempLatticeImpl></src> in class
00060 // TempLattice. In that way the making a copy of a TempLattice uses the
00061 // same object underneath.
00062 // This was needed to have a correct implementation of tempClose. Otherwise
00063 // when deleting a copy of a TempLattice, that destructor would delete the
00064 // underlying table and the original TempLattice could not reopen it.
00065 // </synopsis>
00066 
00067 
00068 template<class T> class TempLatticeImpl
00069 {
00070 public:
00071   // The default constructor creates a TempLatticeImpl containing a
00072   // default ArrayLattice object.
00073   TempLatticeImpl();
00074 
00075   // Create a TempLatticeImpl of the specified shape. You can specify how much
00076   // memory the Lattice can consume before it becomes disk based by giving a
00077   // non-negative value to the maxMemoryInMB argument. Otherwise it will assume
00078   // it can use up to 25% of the memory on your machine as defined in aipsrc
00079   // (this algorithm may change). Setting maxMemoryInMB to zero will force
00080   // the lattice to disk.
00081   // <group>
00082   TempLatticeImpl (const TiledShape& shape, Int maxMemoryInMB);
00083   TempLatticeImpl (const TiledShape& shape, Double maxMemoryInMB);
00084   // </group>
00085   
00086   // The destructor removes the Lattice from memory and if necessary disk.
00087   ~TempLatticeImpl();
00088 
00089   // Is the TempLattice paged to disk?
00090   Bool isPaged() const
00091     { return  (! itsTableName.empty()); }
00092 
00093   // Can the lattice data be referenced as an array section?
00094   Bool canReferenceArray() const
00095     { return  (itsTableName.empty()); }
00096 
00097   // Is the TempLattice writable? It should be.
00098   Bool isWritable() const
00099     { return True; }
00100 
00101   // Flush the data.
00102   void flush()
00103     { if (itsTablePtr != 0) itsTablePtr->flush(); }
00104 
00105   // Close the Lattice temporarily (if it is paged to disk).
00106   // It'll be reopened automatically when needed or when
00107   // <src>reopen</src> is called explicitly.
00108   void tempClose();
00109 
00110   // If needed, reopen a temporarily closed TempLatticeImpl.
00111   void reopen();
00112 
00113   // Return the shape of the Lattice including all degenerate axes.
00114   // (ie. axes with a length of one)
00115   IPosition shape() const
00116     { doReopen(); return itsLatticePtr->shape(); } 
00117 
00118   // Set all of the elements in the Lattice to the given value.
00119   void set (const T& value)
00120     { doReopen(); itsLatticePtr->set (value); }
00121 
00122   // Replace every element, x, of the Lattice with the result of f(x).  You
00123   // must pass in the address of the function -- so the function must be
00124   // declared and defined in the scope of your program.  All versions of
00125   // apply require a function that accepts a single argument of type T (the
00126   // Lattice template type) and return a result of the same type.  The first
00127   // apply expects a function with an argument passed by value; the second
00128   // expects the argument to be passed by const reference; the third
00129   // requires an instance of the class <src>Functional<T,T></src>.  The
00130   // first form ought to run faster for the built-in types, which may be an
00131   // issue for large Lattices stored in memory, where disk access is not an
00132   // issue.
00133   // <group>
00134   void apply (T (*function)(T))
00135     { doReopen(); itsLatticePtr->apply (function); }
00136   void apply (T (*function)(const T&))
00137     { doReopen(); itsLatticePtr->apply (function); }
00138   void apply (const Functional<T,T>& function)
00139     { doReopen(); itsLatticePtr->apply (function); }
00140   // </group>
00141 
00142   // This function returns the recommended maximum number of pixels to
00143   // include in the cursor of an iterator.
00144   uInt advisedMaxPixels() const
00145     { doReopen(); return itsLatticePtr->advisedMaxPixels(); }
00146 
00147   // Get the best cursor shape.
00148   IPosition doNiceCursorShape (uInt maxPixels) 
00149     { doReopen(); return itsLatticePtr->niceCursorShape (maxPixels); }
00150 
00151   // Maximum size - not necessarily all used. In pixels.
00152   uInt maximumCacheSize() const
00153     { return itsLatticePtr->maximumCacheSize(); }
00154 
00155   // Set the maximum (allowed) cache size as indicated.
00156   void setMaximumCacheSize (uInt howManyPixels)
00157     { itsLatticePtr->setMaximumCacheSize (howManyPixels); }
00158 
00159   // Set the cache size as to "fit" the indicated path.
00160   void setCacheSizeFromPath (const IPosition& sliceShape,
00161                                      const IPosition& windowStart,
00162                                      const IPosition& windowLength,
00163                                      const IPosition& axisPath)
00164     { itsLatticePtr->setCacheSizeFromPath (sliceShape, windowStart, windowLength,
00165                                            axisPath); }
00166     
00167   // Set the actual cache size for this Array to be be big enough for the
00168   // indicated number of tiles. This cache is not shared with PagedArrays
00169   // in other rows and is always clipped to be less than the maximum value
00170   // set using the setMaximumCacheSize member function.
00171   // tiles. Tiles are cached using a first in first out algorithm. 
00172   void setCacheSizeInTiles (uInt howManyTiles)
00173     { itsLatticePtr->setCacheSizeInTiles (howManyTiles); }
00174 
00175   // Clears and frees up the caches, but the maximum allowed cache size is 
00176   // unchanged from when setCacheSize was called
00177   void clearCache()
00178     { itsLatticePtr->clearCache(); }
00179 
00180   // Report on cache success.
00181   void showCacheStatistics (ostream& os) const
00182     { itsLatticePtr->showCacheStatistics (os); }
00183 
00184   // Get or put a single element in the lattice.
00185   // Note that Lattice::operator() can also be used to get a single element.
00186   // <group>
00187   T getAt (const IPosition& where) const
00188     { doReopen(); return itsLatticePtr->getAt (where); }
00189   void putAt (const T& value, const IPosition& where)
00190     { doReopen(); itsLatticePtr->putAt (value, where); }
00191   // </group>
00192   
00193   // Check class internals - used for debugging. Should always return True
00194   Bool ok() const
00195     { doReopen(); return itsLatticePtr->ok(); }
00196 
00197   // This function is used by the LatticeIterator class to generate an
00198   // iterator of the correct type for this Lattice. Not recommended
00199   // for general use. 
00200   LatticeIterInterface<T>* makeIter (const LatticeNavigator& navigator,
00201                                      Bool useRef) const
00202     { doReopen(); return itsLatticePtr->makeIter (navigator, useRef); }
00203 
00204   // Do the actual getting of an array of values.
00205   Bool doGetSlice (Array<T>& buffer, const Slicer& section)
00206     { doReopen(); return itsLatticePtr->doGetSlice (buffer, section); }
00207 
00208   // Do the actual getting of an array of values.
00209   void doPutSlice (const Array<T>& sourceBuffer,
00210                    const IPosition& where,
00211                    const IPosition& stride)
00212     { doReopen(); itsLatticePtr->putSlice (sourceBuffer, where, stride); }
00213   
00214   // Do the reopen of the table (if not open already).
00215   void doReopen() const
00216     { if (itsIsClosed) tempReopen(); }
00217 
00218 private:
00219   // The copy constructor cannot be used.
00220   TempLatticeImpl (const TempLatticeImpl<T>& other) ;
00221     
00222   // The assignment operator cannot be used.
00223   TempLatticeImpl<T>& operator= (const TempLatticeImpl<T>& other);
00224 
00225   // Initialize the object.
00226   void init (const TiledShape& shape, Double maxMemoryInMB=-1);
00227 
00228   // Do the actual reopen of the temporarily closed table (if not open already).
00229   void tempReopen() const;
00230 
00231   // Make sure that the temporary table gets deleted.
00232   void deleteTable();
00233 
00234 
00235   mutable Table*                  itsTablePtr;
00236   mutable CountedPtr<Lattice<T> > itsLatticePtr;
00237           String                  itsTableName;
00238   mutable Bool                    itsIsClosed;
00239 };
00240 
00241 
00242 
00243 } //# NAMESPACE CASA - END
00244 
00245 #ifndef CASACORE_NO_AUTO_TEMPLATES
00246 #include <lattices/Lattices/TempLatticeImpl.tcc>
00247 #endif //# CASACORE_NO_AUTO_TEMPLATES
00248 #endif