Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.atnf.csiro.au/computing/software/casacore/casacore-1.2.0/doc/html/Array_8h_source.html
Дата изменения: Unknown Дата индексирования: Mon Feb 14 19:34:16 2011 Кодировка: |
00001 //# Array.h: A templated N-D Array class with zero origin 00002 //# Copyright (C) 1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,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 //# $Id: Array.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $ 00027 00028 #ifndef CASA_ARRAY_H 00029 #define CASA_ARRAY_H 00030 00031 //# Includes 00032 #include <casa/Arrays/ArrayBase.h> 00033 #include <casa/Containers/Block.h> 00034 #include <casa/Utilities/CountedPtr.h> 00035 #include <casa/Arrays/MaskLogiArrFwd.h> 00036 #include <casa/Arrays/IPosition.h> 00037 #include <casa/ostream.h> 00038 #include <iterator> 00039 #if defined(WHATEVER_VECTOR_FORWARD_DEC) 00040 WHATEVER_VECTOR_FORWARD_DEC; 00041 #else 00042 #include <casa/stdvector.h> 00043 #endif 00044 00045 namespace casa { //#Begin casa namespace 00046 //# Forward Declarations 00047 class AipsIO; 00048 class Slice; 00049 class Slicer; 00050 template<class T> class Matrix; 00051 template<class T> class ArrayIterator; 00052 template<class T> class MaskedArray; 00053 template<class Domain, class Range> class Functional; 00054 //template <class T, class U> class vector; 00055 00056 00057 // <summary> A templated N-D Array class with zero origin </summary> 00058 00059 // Array<T> is a templated, N-dimensional, Array class. The origin is zero, 00060 // but by default indices are zero-based. This Array class is the 00061 // base class for specialized Vector<T>, Matrix<T>, and Cube<T> classes. 00062 // 00063 // Indexing into the array, and positions in general, are given with IPosition 00064 // (essentially a vector of integers) objects. That is, an N-dimensional 00065 // array requires a length-N IPosition to define a position within the array. 00066 // Unlike C, indexing is done with (), not []. Also, the storage order 00067 // is the same as in FORTRAN, i.e. memory varies most rapidly with the first 00068 // index. 00069 // <srcblock> 00070 // // axisLengths = [1,2,3,4,5] 00071 // IPosition axisLengths(5, 1, 2, 3, 4, 5); 00072 // Array<Int> ai(axisLengths); // ai is a 5 dimensional array of 00073 // // integers; indices are 0-based 00074 // // => ai.nelements() == 120 00075 // Array<Int> ai2(axisLengths); // The first element is at index 0 00076 // IPosition zero(5); zero = 0; // [0,0,0,0,0] 00077 // //... 00078 // </srcblock> 00079 // Indexing into an N-dimensional array is relatively expensive. Normally 00080 // you will index into a Vector, Matrix, or Cube. These may be obtained from 00081 // an N-dimensional array by creating a reference, or by using an 00082 // ArrayIterator. The "shape" of the array is an IPosition which gives the 00083 // length of each axis. 00084 // 00085 // An Array may be standalone, or it may refer to another array, or to 00086 // part of another array (by refer we mean that if you change a pixel in 00087 // the current array, a pixel in the referred to array also changes, i.e. 00088 // they share underlying storage). 00089 // <note role=warning> 00090 // One way one array can reference another is through the copy 00091 // constructor. While this might be what you want, you should 00092 // probably use the reference() member function to make it explicit. 00093 // The copy constructor is used when arguments are passed by value; 00094 // normally functions should not pass Arrays by value, rather they 00095 // should pass a reference or a const reference. On the positive 00096 // side, returning an array from a function is efficient since no 00097 // copying need be done. 00098 // </note> 00099 // 00100 // Aside from the explicit reference() member function, a user will 00101 // most commonly encounter an array which references another array 00102 // when he takes an array slice (or section). A slice is a sub-region of 00103 // an array (which might also have a stride: every nth row, every mth column, 00104 // ...). 00105 // <srcblock> 00106 // IPosition lengths(3,10,20,30); 00107 // Array<Int> ai(lengths); // A 10x20x30 cube 00108 // Cube<Int> ci; 00109 // //... 00110 // ci.reference(ai1); // ci and ai now reference the same 00111 // // storage 00112 // ci(0,0,0) = 123; // Can use Cube indexing 00113 // ci.xyPlane(2) = 0; // and other member functions 00114 // IPosition zero(3,0,0,0); 00115 // assert(ai(zero) == 123); // True because ai, ci are references 00116 // //... 00117 // Array<Int> subArray; 00118 // IPosition blc(3,0,0,0), trc(3,5,5,5); 00119 // subArray.reference(ai(blc, trc)); 00120 // subArray = 10; // All of subArray, which is the 00121 // // subcube from 0,0,0 to 5,5,5 in 00122 // // ai, has the value 10. 00123 // </srcblock> 00124 // While the last example has an array slice referenced explicitly by another 00125 // array variable, normally the user will often only use the slice as 00126 // a temporary in an expresion, for example: 00127 // <srcblock> 00128 // Array<Complex> array; 00129 // IPosition blc, trc, offset; 00130 // //... 00131 // // Copy from one region of the array into another 00132 // array(blc, trc) = array(blc+offset, trc+offset); 00133 // </srcblock> 00134 // 00135 // The Array classes are intended to operate on relatively large 00136 // amounts of data. While they haven't been extensively tuned yet, 00137 // they are relatively efficient in terms of speed. Presently they 00138 // are not space efficient -- the overhead is about 15 words. While 00139 // this will be improved (probably to about 1/2 that), these array 00140 // classes are not appropriate for very large numbers of very small 00141 // arrays. The Block<T> class may be what you want in this circumstance. 00142 // 00143 // Element by element mathematical and logical operations are available 00144 // for arrays (defined in aips/ArrayMath.h and aips/ArrayLogical.h). 00145 // Because arithmetic and logical functions are split out, it is possible 00146 // to create an Array<T> (and hence Vector<T> etc) for any type T that has 00147 // a default constructor, assignment operator, and copy constructor. In 00148 // particular, Array<String> works. 00149 // 00150 // If compiled with the preprocessor symbol AIPS_DEBUG symbol, array 00151 // consistency ("invariants") will be checked in most member 00152 // functions, and indexing will be range-checked. This should not be 00153 // defined for production runs. 00154 // 00155 // A tutorial for the ArrayClasses is available in the "AIPS++ Programming 00156 // Manual." 00157 // 00158 // <note role=tip> 00159 // Most of the data members and functions which are "protected" should 00160 // likely become "private". 00161 // </note> 00162 // 00163 // <todo asof="1999/12/30"> 00164 // <li> Integrate into the Lattice hierarchy 00165 // <li> Factor out the common functions (shape etc) into a type-independent 00166 // base class. 00167 // </todo> 00168 00169 template<class T> class Array : public ArrayBase 00170 { 00171 public: 00172 00173 // Result has dimensionality of zero, and nelements is zero. 00174 Array(); 00175 00176 // Create an array of the given shape, i.e. after construction 00177 // array.ndim() == shape.nelements() and array.shape() == shape. 00178 // The origin of the Array is zero. 00179 explicit Array(const IPosition &shape); 00180 00181 // Create an array of the given shape and initialize it with the 00182 // initial value. 00183 Array(const IPosition &shape, const T &initialValue); 00184 00185 // After construction, this and other reference the same storage. 00186 Array(const Array<T> &other); 00187 00188 // Create an Array of a given shape from a pointer. 00189 Array(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY); 00190 // Create an Array of a given shape from a pointer. Because the pointer 00191 // is const, a copy is always made. 00192 Array(const IPosition &shape, const T *storage); 00193 00194 // Frees up storage only if this array was the last reference to it. 00195 virtual ~Array(); 00196 00197 // Assign the other array to this array. 00198 // If the shapes mismatch, this array is resized. 00199 virtual void assign (const Array<T>& other); 00200 00201 // Set every element of the array to "value." Also could use the 00202 // assignment operator which assigns an array from a scalar. 00203 void set(const T &value); 00204 00205 // Apply the function to every element of the array. This modifies 00206 // the array in place. 00207 // <group> 00208 // This version takes a function which takes a T and returns a T. 00209 void apply(T (*function)(T)); 00210 // This version takes a function which takes a const T reference and 00211 // returns a T. 00212 void apply(T (*function)(const T &)); 00213 // This version applies a functional. 00214 void apply(const Functional<T,T> &function); 00215 // </group> 00216 00217 // After invocation, this array and other reference the same storage. That 00218 // is, modifying an element through one will show up in the other. The 00219 // arrays appear to be identical; they have the same shape. 00220 // <br>Please note that this function makes it possible to reference a 00221 // const Array, thus effectively it makes a const Array non-const. 00222 // Although this may seem undesirable at first sight, it is necessary to 00223 // be able to make references to temporary Array objects, in particular to 00224 // Array slices. Otherwise one first needs to use the copy constructor. 00225 //# The const has been introduced on 2005-Mar-31 because of the hassle 00226 //# involved in calling the copy ctor before reference. 00227 virtual void reference(const Array<T> &other); 00228 00229 // Copy the values in other to this. If the array on the left hand 00230 // side has no elements, then it is resized to be the same size as 00231 // as the array on the right hand side. Otherwise, the arrays must 00232 // conform (same shapes). 00233 // <srcblock> 00234 // IPosition shape(2,10,10); // some shape 00235 // Array<Double> ad(shape); 00236 // //... 00237 // Array<Double> ad2; // N.B. ad2.nelements() == 0 00238 // ad2 = ad; // ad2 resizes, then elements 00239 // // are copied. 00240 // shape = 20; 00241 // Array<Double> ad3(shape); 00242 // ad3 = ad; // Error: arrays do not conform 00243 // </srcblock> 00244 // Note that the assign function can be used to assign a 00245 // non-conforming array. 00246 virtual Array<T>