casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector.h
Go to the documentation of this file.
1 //# Vector.h: A 1-D Specialization of the Array Class
2 //# Copyright (C) 1993,1994,1995,1996,1998,1999,2000,2001,2002,2003
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_VECTOR_H
29 #define CASA_VECTOR_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
34 
35 //# Forward declarations
36 //template <class T, class U> class vector;
38 
39 namespace casacore { //#Begin namespace casacore
40 
41 // <summary> A 1-D Specialization of the Array class </summary>
42 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
43 // </reviewed>
44 //
45 // Vector objects are one-dimensional specializations (e.g., more convenient
46 // and efficient indexing) of the general Array class. You might also want
47 // to look at the Array documentation to see inherited functionality.
48 //
49 // Generally the member functions of Array are also available in
50 // Vector versions
51 // which take an integer where the array needs an IPosition. Since the Vector
52 // is one-dimensional, the IPositions are overkill, although you may
53 // use those versions if you want to.
54 // <srcblock>
55 // Vector<Int> vi(100); // Vector 100 elements long.
56 // vi.resize(50); // Now only 50 long.
57 // </srcblock>
58 //
59 // Slices may be taken with the Slice class. To take a slice, one "indexes"
60 // with Slice(start, length, inc) where end and inc are optional.
61 // <srcblock>
62 // Vector<Float> vf(100);
63 // //...
64 // vf(Slice(0,50,2)) = vf(Slice(1,50,2)); // Copy values from odd onto even
65 // Vector<Float> firstHalf, secondHalf;
66 // firstHalf.reference(vf(Slice(0,50)));
67 // secondHalf.reference(vf(Slice(50,50)));
68 // // Now we have aliases for two slices into the Vector
69 // </srcblock>
70 //
71 // Element-by-element arithmetic and logical operations are available (in
72 // aips/ArrayMath.h and aips/ArrayLogical.h) as well as dot and cross
73 // products (in aips/MatrixMath.h).
74 //
75 // A Vector can be constructed from an STL <src>vector</src>. The reverse
76 // operation (<src>Array::tovector()</src>) can construct an STL
77 // <src>vector</src> from any Array.
78 // <note role=tip> To create any other STL container from an Array (or
79 // the reverse), always create from/to a <src>vector</src>, and use the
80 // range constructor to create from/to others (like set, list, deque). </note>
81 //
82 // As with the Arrays, if the preprocessor symbol AIPS_DEBUG is
83 // defined at compile time invariants will be checked on entry to most
84 // member functions. Additionally, if AIPS_ARRAY_INDEX_CHECK is defined
85 // index operations will be bounds-checked. Neither of these should
86 // be defined for production code.
87 
88 template<class T> class Vector : public Array<T>
89 {
90 public:
91  // A zero-length Vector.
92  Vector();
93 
94  // A Vector with a defined length and origin of zero.
95  // <group>
96  explicit Vector(size_t Length);
97  Vector(size_t Length, ArrayInitPolicy initPolicy);
98  explicit Vector(const IPosition& Length);
99  Vector(const IPosition& Length, ArrayInitPolicy initPolicy);
100  // </group>
101 
102  // A Vector with a defined length and origin of zero.
103  // Fill it with the initial value.
104  // <group>
105  Vector(size_t Length, const T &initialValue);
106  Vector(const IPosition& Length, const T &initialValue);
107  // </group>
108 
109  // Create a Vector from the given Block "other." Make it length "nr"
110  // and copy over that many elements.
111  Vector(const Block<T> &other, Int64 nr);
112  // Create a Vector of length other.nelements() and copy over its values.
113  explicit Vector(const Block<T> &other);
114 
115  // Create a reference to other.
116  Vector(const Vector<T> &other);
117 
118  // Create a reference to the other array.
119  // It is always possible if the array has zero or one axes.
120  // If it has > 1 axes, it is only possible if the array has at most
121  // one axis with length > 1. In that case the degenerated axes are removed.
122  Vector(const Array<T> &other);
123 
124  // Create an Vector of a given shape from a pointer.
125  Vector(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY);
126  // Create an Vector of a given shape from a pointer.
127  Vector(const IPosition &shape, T *storage, StorageInitPolicy policy, AbstractAllocator<T> const &allocator);
128  // Create an Vector of a given shape from a pointer. Because the pointer
129  // is const, a copy is always made.
130  Vector(const IPosition &shape, const T *storage);
131 
132  // Create a Vector from an STL vector (see <src>tovector()</src> in
133  // <linkto class=Array>Array</linkto> for the reverse operation).
134  // <note role=tip> Both this constructor and the tovector() are
135  // defined in <src>Vector2.cc</src>. </note>
136  // It does implicit promotion/demotion of the type U if different from T.
137  template <class U, class V>
138  Vector(const vector<U, V> &other);
139 
140  // Create a Vector from a container iterator and its length.
141  // <note> The length is used instead of last, because the distance
142  // function needed to calculate the length can be expensive.
143  // <br>A third dummy argument is unfortunately needed to avoid ambiguity
144  // with another Vector constructor (taking two uInts).
145  // </note>
146  template<typename Iterator>
147  Vector(Iterator first, size_t size, int dummy);
148 
149  // Define a destructor, otherwise the compiler makes a static one.
150  virtual ~Vector();
151 
152  // Assign the other array (which must be of dimension one) to this vector.
153  // If the shapes mismatch, this array is resized.
154  virtual void assign (const Array<T>& other);
155 
156  // Create a reference to "other", which must be of dimension one.
157  virtual void reference(const Array<T> &other);
158 
159  // Resize this Vector to the given length.
160  // The default copyValues flag is False.
161  //# Note that the 3rd resize function is necessary, because that
162  //# function is virtual in the base class (otherwise it would
163  //# be hidden).
164  // Resize without argument is equal to resize(0, False).
165  // <group>
166  using Array<T>::resize;
167  void resize(size_t len, Bool copyValues=False)
168  { Vector<T>::resize(len, copyValues, Array<T>::defaultArrayInitPolicy()); }
169  void resize(size_t len, Bool copyValues, ArrayInitPolicy policy)
170  { if (len != this->nelements()) resize (IPosition(1,len), copyValues, policy); }
171  virtual void resize();
172  virtual void resize(const IPosition &len, Bool copyValues, ArrayInitPolicy policy);
173  // </group>
174 
175  // Assign to this Vector. If this Vector is zero-length, then resize
176  // to be the same size as other. Otherwise this and other have to be
177  // conformant (same size).
178  // <br>Note that the assign function can be used to assign a
179  // non-conforming vector.
180  // <group>
181  Vector<T> &operator=(const Vector<T> &other);
182  // Other must be a 1-dimensional array.
183  virtual Array<T> &operator=(const Array<T> &other);
184  // </group>
185 
186  // Set every element of this Vector to Val.
187  Array<T> &operator=(const T &val)
188  { return Array<T>::operator=(val); }
189 
190  // Copy to this those values in marray whose corresponding elements
191  // in marray's mask are True.
193  { Array<T> (*this) = marray; return *this; }
194 
195  // Convert a Vector to a Block, resizing the block and copying values.
196  // This is done this way to avoid having the simpler Block class
197  // containing dependencies on the Vector class.
198  void toBlock(Block<T> &other) const;
199 
200  // Single-pixel addressing. If AIPS_ARRAY_INDEX_CHECK is defined,
201  // bounds checking is performed (not for [])..
202  // <group>
203  T &operator[](size_t index)
204  { return (this->contiguous_p ? this->begin_p[index] : this->begin_p[index*this->inc_p(0)]); }
205  const T &operator[](size_t index) const
206  { return (this->contiguous_p ? this->begin_p[index] : this->begin_p[index*this->inc_p(0)]); }
207  T &operator()(const IPosition &i)
208  { return Array<T>::operator()(i); }
209  const T &operator()(const IPosition &i) const
210  { return Array<T>::operator()(i); }
211  T &operator()(size_t index)
212  {
213 #if defined(AIPS_ARRAY_INDEX_CHECK)
214  this->validateIndex(index); //# Throws an exception on failure
215 #endif
216  return *(this->begin_p + index*this->inc_p(0));
217  }
218 
219  const T &operator()(size_t index) const
220  {
221 #if defined(AIPS_ARRAY_INDEX_CHECK)
222  this->validateIndex(index); //# Throws an exception on failure
223 #endif
224  return *(this->begin_p + index*this->inc_p(0));
225  }
226  // </group>
227 
228  // Take a slice of this vector. Slices are always indexed starting
229  // at zero. This uses reference semantics, i.e. changing a value
230  // in the slice changes the original.
231  // <srcblock>
232  // Vector<Double> vd(100);
233  // //...
234  // vd(Slice(0,10)) = -1.0; // First 10 elements of vd set to -1
235  // </srcblock>
236  // <group>
237  Vector<T> operator()(const Slice &slice);
238  const Vector<T> operator()(const Slice &slice) const;
239  // </group>
240 
241  // Slice using IPositions. Required to be defined, otherwise the base
242  // class versions are hidden.
243  // <group>
244  Array<T> operator()(const IPosition &blc, const IPosition &trc,
245  const IPosition &incr)
246  { return Array<T>::operator()(blc,trc,incr); }
247  const Array<T> operator()(const IPosition &blc, const IPosition &trc,
248  const IPosition &incr) const
249  { return Array<T>::operator()(blc,trc,incr); }
250  Array<T> operator()(const IPosition &blc, const IPosition &trc)
251  { return Array<T>::operator()(blc,trc); }
252  const Array<T> operator()(const IPosition &blc, const IPosition &trc) const
253  { return Array<T>::operator()(blc,trc); }
254  Array<T> operator()(const Slicer& slicer)
255  { return Array<T>::operator()(slicer); }
256  const Array<T> operator()(const Slicer& slicer) const
257  { return Array<T>::operator()(slicer); }
258  // </group>
259 
260  // The array is masked by the input LogicalArray.
261  // This mask must conform to the array.
262  // <group>
263 
264  // Return a MaskedArray.
265  MaskedArray<T> operator() (const LogicalArray &mask) const
266  { return Array<T>::operator() (mask); }
267 
268  // Return a MaskedArray.
269  MaskedArray<T> operator() (const LogicalArray &mask)
270  { return Array<T>::operator() (mask); }
271 
272  // </group>
273 
274 
275  // The array is masked by the input MaskedLogicalArray.
276  // The mask is effectively the AND of the internal LogicalArray
277  // and the internal mask of the MaskedLogicalArray.
278  // The MaskedLogicalArray must conform to the array.
279  // <group>
280 
281  // Return a MaskedArray.
282  MaskedArray<T> operator() (const MaskedLogicalArray &mask) const
283  { return Array<T>::operator() (mask); }
284 
285  // Return a MaskedArray.
286  MaskedArray<T> operator() (const MaskedLogicalArray &mask)
287  { return Array<T>::operator() (mask); }
288 
289  // </group>
290 
291 
292  // The length of the Vector.
293  const IPosition &shape() const
294  { return this->length_p; }
295  void shape(Int &Shape) const
296  { Shape = this->length_p(0); }
297 
298  // Verify that dimensionality is 1 and then call Array<T>::ok()
299  virtual Bool ok() const;
300 
301 protected:
302  virtual void preTakeStorage(const IPosition &shape);
303  // Remove the degenerate axes from other and store result in this vector.
304  // An exception is thrown if removing degenerate axes does not result
305  // in a vector.
306  virtual void doNonDegenerate(const Array<T> &other,
307  const IPosition &ignoreAxes);
308 
309 private:
310  // Helper functions for constructors.
311  void initVector(const Block<T> &, Int64 nr); // copy semantics
312 };
313 
314 
315 //# Declare extern templates for often used types.
316  extern template class Vector<Bool>;
317  extern template class Vector<Char>;
318  extern template class Vector<Short>;
319  extern template class Vector<uShort>;
320  extern template class Vector<Int>;
321  extern template class Vector<uInt>;
322  extern template class Vector<Int64>;
323  extern template class Vector<Float>;
324  extern template class Vector<Double>;
325  extern template class Vector<Complex>;
326  extern template class Vector<DComplex>;
327  extern template class Vector<String>;
328 
329 } //#End namespace casacore
330 
331 
332 #ifndef CASACORE_NO_AUTO_TEMPLATES
333 #include <casacore/casa/Arrays/Vector.tcc>
334 #include <casacore/casa/Arrays/Vector2.tcc>
335 #endif //# CASACORE_NO_AUTO_TEMPLATES
336 #endif
Bool contiguous_p
Are the data contiguous?
Definition: ArrayBase.h:268
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
T & operator[](size_t index)
Single-pixel addressing.
Definition: Vector.h:203
A 1-D Specialization of the Array class.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
int Int
Definition: aipstype.h:50
std::vector< double > Vector
Definition: ds9context.h:24
Vector()
A zero-length Vector.
const T & operator[](size_t index) const
Definition: Vector.h:205
size_t nelements() const
How many elements does this array have? Product of all axis lengths.
Definition: ArrayBase.h:99
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
struct Node * first
Definition: malloc.h:330
TableExprNode marray(const TableExprNode &array, const TableExprNode &mask)
Form a masked array.
Definition: ExprNode.h:1892
A global enum used by some Array/Block constructors.
Definition: Allocator.h:54
IPosition length_p
Used to hold the shape, increment into the underlying storage and originalLength of the array...
Definition: ArrayBase.h:271
Array< T > operator()(const IPosition &blc, const IPosition &trc)
Definition: Vector.h:250
virtual void doNonDegenerate(const Array< T > &other, const IPosition &ignoreAxes)
Remove the degenerate axes from other and store result in this vector.
const Array< T > operator()(const IPosition &blc, const IPosition &trc) const
Definition: Vector.h:252
Class for masking an Array for operations on that Array.
void validateIndex(const IPosition &) const
void toBlock(Block< T > &other) const
Convert a Vector to a Block, resizing the block and copying values.
Array< T > & operator=(const T &val)
Set every element of this Vector to Val.
Definition: Vector.h:187
Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr)
Slice using IPositions.
Definition: Vector.h:244
Vector< T > & operator=(const Vector< T > &other)
Assign to this Vector.
virtual void assign(const Array< T > &other)
Assign the other array (which must be of dimension one) to this vector.
define a (start,length,increment) along an axis
Definition: Slice.h:93
virtual Array< T > & operator=(const Array< T > &other)
Copy the values in other to this.
const IPosition & shape() const
The length of the Vector.
Definition: Vector.h:293
void initVector(const Block< T > &, Int64 nr)
Helper functions for constructors.
T & operator()(const IPosition &)
Access a single element of the array.
T & operator()(size_t index)
Definition: Vector.h:211
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
virtual ~Vector()
Define a destructor, otherwise the compiler makes a static one.
virtual void resize()
Make this array a different shape.
T * begin_p
This pointer is adjusted to point to the first element of the array.
Definition: Array.h:913
const Bool False
Definition: aipstype.h:44
virtual void preTakeStorage(const IPosition &shape)
pre/post processing hook of takeStorage() for subclasses.
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
Array< T > operator()(const Slicer &slicer)
Definition: Vector.h:254
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
virtual Bool ok() const
Verify that dimensionality is 1 and then call Array&lt;T&gt;::ok()
simple 1-D array
const T & operator()(const IPosition &i) const
Definition: Vector.h:209
void resize(size_t len, Bool copyValues, ArrayInitPolicy policy)
Definition: Vector.h:169
COPY is used when an internal copy of the storage is to be made.
Definition: ArrayBase.h:56
StorageInitPolicy
A global enum used by some Array constructors.
Definition: ArrayBase.h:53
size_t size() const
Definition: ArrayBase.h:101
void resize(size_t len, Bool copyValues=False)
Definition: Vector.h:167
virtual void reference(const Array< T > &other)
Create a reference to &quot;other&quot;, which must be of dimension one.
const T & operator()(size_t index) const
Definition: Vector.h:219
Class for those physical parameters having dimensions of Length [L].
Definition: ATMLength.h:44
T & operator()(const IPosition &i)
Definition: Vector.h:207
const Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr) const
Definition: Vector.h:247
const Array< T > operator()(const Slicer &slicer) const
Definition: Vector.h:256
void shape(Int &Shape) const
Definition: Vector.h:295
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42