casa
$Rev:20696$
|
00001 //# VectorIter.h: Iterate a vector 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: VectorIter.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $ 00027 00028 #ifndef CASA_VECTORITER_H 00029 #define CASA_VECTORITER_H 00030 00031 00032 #include <casa/aips.h> 00033 #include <casa/Arrays/ArrayIter.h> 00034 #include <casa/Arrays/Vector.h> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 // 00039 // <summary> Iterate an Vector cursor through another Array. </summary> 00040 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""> 00041 // </reviewed> 00042 // 00043 // VectorIterator steps a Vector (the "cursor") through an array for the 00044 // given axis. 00045 // The cursor "refers" to storage in the array, so that changing the 00046 // values in the cursor changes values in the original array. 00047 // 00048 // This class is derived from ArrayIterator; basically it only adds 00049 // the vector() member function which allows you to access the cursor 00050 // as a Vector. 00051 // 00052 // <note role=tip> 00053 // The origin of the cursor, i.e. the subarray that moves through the 00054 // larger array, is always zero. 00055 // </note> 00056 // 00057 // In this example we sum all the elements of an array; of course we already 00058 // have the "sum" function in ArrayMath.h that we should use instead. 00059 // 00060 // <srcblock> 00061 // Array<Float> af; 00062 // // set af 00063 // VectorIterator vi(af); 00064 // Float sum = 0.0; 00065 // uInt n = vi.vector().nelements(); 00066 // while (! vi.pastEnd()) { 00067 // for (Int i=0; i < n; i++) { // N.B.; cursor always 0 based. 00068 // sum += vi.vector()(i); 00069 // } 00070 // vi.next(); 00071 // } 00072 // </srcblock> 00073 00074 template<class T> class VectorIterator : public ArrayIterator<T> 00075 { 00076 public: 00077 // Iterate by vector cursors through array "a". 00078 // The vector cursor is taken for the given axis. 00079 explicit VectorIterator(Array<T> &a, uInt axis=0); 00080 00081 // Return a Vector at the current position. 00082 Vector<T> &vector() {return *(Vector<T> *)this->ap_p;} 00083 00084 private: 00085 // Not implemented. 00086 VectorIterator(const VectorIterator<T> &); 00087 // Not implemented. 00088 VectorIterator<T> &operator=(const VectorIterator<T> &); 00089 }; 00090 00091 // 00092 // <summary> Iterate a Vector cursor through another Array. </summary> 00093 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""> 00094 // </reviewed> 00095 // 00096 // ReadOnlyVectorIterator behaves exactly like VectorIterator (cf.) only 00097 // it should be used on const Arrays. 00098 // 00099 // <note role=tip> Note that the R/O VectorIterator is not derived from R/O 00100 // ArrayIterator. 00101 // </note> 00102 00103 template<class T> class ReadOnlyVectorIterator 00104 { 00105 public: 00106 // <group> 00107 explicit ReadOnlyVectorIterator(const Array<T> &a, uInt axis=0) 00108 : vi(const_cast<Array<T>&>(a), axis) {} 00109 00110 void next() {vi.next();} 00111 void reset() {vi.origin();} 00112 void origin() {vi.origin();} 00113 00114 const Array<T> &array() {return vi.array();} 00115 const Vector<T> &vector() {return vi.vector();} 00116 00117 Bool atStart() const {return vi.atStart();} 00118 Bool pastEnd() const {return vi.pastEnd();} 00119 const IPosition &pos() const {return vi.pos();} 00120 IPosition endPos() const {return vi.endPos();} 00121 uInt ndim() const {return vi.ndim();} 00122 // </group> 00123 private: 00124 // Not implemented. 00125 ReadOnlyVectorIterator(const ReadOnlyVectorIterator<T> &); 00126 // Not implemented. 00127 ReadOnlyVectorIterator<T> &operator=(const ReadOnlyVectorIterator<T> &); 00128 00129 VectorIterator<T> vi; 00130 }; 00131 00132 00133 } //# NAMESPACE CASA - END 00134 00135 #ifndef CASACORE_NO_AUTO_TEMPLATES 00136 #include <casa/Arrays/VectorIter.tcc> 00137 #endif //# CASACORE_NO_AUTO_TEMPLATES 00138 #endif