casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MatrixIter.h
Go to the documentation of this file.
00001 //# MatrixIter.h: Iterate a matrix cursor through another array
00002 //# Copyright (C) 1993,1994,1995,1999
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 //# $Id: MatrixIter.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_MATRIXITER_H
00029 #define CASA_MATRIXITER_H
00030 
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/ArrayIter.h>
00034 #include <casa/Arrays/Matrix.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // 
00039 // <summary> Iterate a Matrix cursor through another Array. </summary>
00040 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00041 // </reviewed>
00042 //
00043 // MatrixIterator steps a Matrix (the "cursor") through an array.
00044 // The cursor "refers" to storage in the array, so that changing the
00045 // values in the cursor changes values in the original array.
00046 //
00047 // This class is derived from ArrayIterator; basically it only adds the
00048 // matrix() member function which allows you to access the cursor as a Matrix.
00049 //
00050 // <note role=tip>
00051 // The origin of the cursor, i.e. the subarray that moves through the
00052 // larger array, is always zero.
00053 // </note>
00054 //
00055 // In this example we want to make a "moment" map of a cube, i.e. collapse
00056 // the "Z" axis by averaging it.
00057 // <srcblock>
00058 // Cube<Float> cube;
00059 // MatrixIterator planeIter(cube);
00060 // Matrix<Float> average(planeIter.matrix().copy()); // init with first plane
00061 // planeIter.next(); // advance the iterator
00062 // while (! planeIter.pastEnd()) {
00063 //     average += planeIter.matrix(); // Sum the next plane
00064 //     planeIter.next();
00065 // }
00066 // average /= Float(cube.shape()(2));  // divide by the number of planes
00067 // </srcblock>
00068 
00069 template<class T> class MatrixIterator : public ArrayIterator<T>
00070 {
00071 public:
00072     // Iterate by matrices through array "a".
00073     // The first 2 axes form the cursor axes.
00074     explicit MatrixIterator(Array<T> &a);
00075 
00076     // Iterate by matrices through array "a".
00077     // The given axes form the cursor axes.
00078     MatrixIterator(Array<T> &a, uInt cursorAxis1, uInt cursorAxis2);
00079 
00080     // Return the matrix at the current position.
00081     Matrix<T> &matrix() {return *(Matrix<T> *)(this->ap_p);}
00082 
00083 private:
00084     // Not implemented.
00085     MatrixIterator(const MatrixIterator<T> &);
00086     // Not implemented.
00087     MatrixIterator<T> &operator=(const MatrixIterator<T> &);
00088 };
00089 
00090 // 
00091 // <summary> Iterate a Matrix cursor through a R/O Array. </summary>
00092 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00093 // </reviewed>
00094 //
00095 // ReadOnlyMatrixIterator behaves exactly like MatrixIterator (cf.) only
00096 // it should be used on const Arrays.
00097 //
00098 // <note role=tip> Note that the R/O MatrixIterator is not derived from R/O 
00099 //        ArrayIterator.
00100 // </note>
00101 //
00102 template<class T> class ReadOnlyMatrixIterator 
00103 {
00104 public:
00105     // <group>
00106     ReadOnlyMatrixIterator(const Array<T> &a) :
00107       mi(const_cast<Array<T>&>(a)) {}
00108 
00109     ReadOnlyMatrixIterator(const Array<T> &a,
00110                            uInt cursorAxis1, uInt cursorAxis2)
00111       : mi(const_cast<Array<T>&>(a), cursorAxis1, cursorAxis2) {}
00112 
00113     void next()   {mi.next();}
00114     void reset() {mi.origin();}
00115     void origin() {mi.origin();}
00116     
00117     const Array<T> &array() {return mi.array();}
00118     const Matrix<T> &matrix() {return mi.matrix();}
00119 
00120     Bool atStart() const {return mi.atStart();}
00121     Bool pastEnd() const {return mi.pastEnd();}
00122     const IPosition &pos() const {return mi.pos();}
00123     IPosition endPos() const {return mi.endPos();}
00124     uInt ndim() const {return mi.ndim();}
00125     // </group>
00126 private:
00127     // Not implemented.
00128     ReadOnlyMatrixIterator(const ReadOnlyMatrixIterator<T> &);
00129     // Not implemented.
00130     ReadOnlyMatrixIterator<T> &operator=(const ReadOnlyMatrixIterator<T> &);
00131 
00132     MatrixIterator<T> mi;
00133 };
00134 
00135 
00136 } //# NAMESPACE CASA - END
00137 
00138 #ifndef CASACORE_NO_AUTO_TEMPLATES
00139 #include <casa/Arrays/MatrixIter.tcc>
00140 #endif //# CASACORE_NO_AUTO_TEMPLATES
00141 #endif