casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef CASA_VECTOR_2_H
27#define CASA_VECTOR_2_H
28
29//# Includes
30#include "Array.h"
31
32namespace casacore { //#Begin namespace casacore
33
34// <summary> A 1-D Specialization of the Array class </summary>
35// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
36// </reviewed>
37//
38// Vector objects are one-dimensional specializations (e.g., more convenient
39// and efficient indexing) of the general Array class. You might also want
40// to look at the Array documentation to see inherited functionality.
41//
42// Generally the member functions of Array are also available in
43// Vector versions
44// which take an integer where the array needs an IPosition. Since the Vector
45// is one-dimensional, the IPositions are overkill, although you may
46// use those versions if you want to.
47// <srcblock>
48// Vector<int> vi(100); // Vector 100 elements long.
49// vi.resize(50); // Now only 50 long.
50// </srcblock>
51//
52// Slices may be taken with the Slice class. To take a slice, one "indexes"
53// with Slice(start, length, inc) where end and inc are optional.
54// <srcblock>
55// Vector<float> vf(100);
56// //...
57// vf(Slice(0,50,2)) = vf(Slice(1,50,2)); // Copy values from odd onto even
58// Vector<float> firstHalf, secondHalf;
59// firstHalf.reference(vf(Slice(0,50)));
60// secondHalf.reference(vf(Slice(50,50)));
61// // Now we have aliases for two slices into the Vector
62// </srcblock>
63//
64// Element-by-element arithmetic and logical operations are available (in
65// aips/ArrayMath.h and aips/ArrayLogical.h) as well as dot and cross
66// products (in aips/MatrixMath.h).
67//
68// A Vector can be constructed from an STL <src>vector</src>. The reverse
69// operation (<src>Array::tovector()</src>) can construct an STL
70// <src>vector</src> from any Array.
71// <note role=tip> To create any other STL container from an Array (or
72// the reverse), always create from/to a <src>vector</src>, and use the
73// range constructor to create from/to others (like set, list, deque). </note>
74//
75// As with the Arrays, if the preprocessor symbol AIPS_DEBUG is
76// defined at compile time invariants will be checked on entry to most
77// member functions. Additionally, if AIPS_ARRAY_INDEX_CHECK is defined
78// index operations will be bounds-checked. Neither of these should
79// be defined for production code.
80
81template<typename T> class Vector : public Array<T>
82{
83public:
84 // A zero-length Vector.
86
87 // A Vector with a defined length and origin of zero.
88 // <group>
89 explicit Vector(size_t Length);
90 explicit Vector(const IPosition& Length);
91 // </group>
92
93 // A Vector with a defined length and origin of zero.
94 // Fill it with the initial value.
95 // <group>
96 Vector(size_t Length, const T &initialValue);
97 Vector(const IPosition& Length, const T &initialValue);
98 // </group>
99
100 // An uninitialized Vector with a defined length.
101 // <group>
102 Vector(size_t Length, typename Array<T>::uninitializedType);
104 // </group>
105
106 // Create a Vector from the given std::vector "other." Make it length "nr"
107 // and copy over that many elements.
108 // This used to take a 'Block'
109 Vector(const std::vector<T> &other, long long nr);
110
111 // Create a Vector of length other.size() and copy over its values.
112 // This used to take a 'Block'
113 explicit Vector(const std::vector<T> &other);
114
115 // Create a Vector from an initializer list.
116 Vector(std::initializer_list<T> list);
117
118 // Create a reference to other.
119 Vector(const Vector<T> &other);
120
121 // Move
122 Vector(Vector<T>&& source) noexcept;
123
124 // Create a reference to the other array.
125 // It is always possible if the array has zero or one axes.
126 // If it has > 1 axes, it is only possible if the array has at most
127 // one axis with length > 1. In that case the degenerated axes are removed.
128 Vector(const Array<T> &other);
129
130 // Create an Vector of a given shape from a pointer.
131 Vector(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY);
132 // Create an Vector of a given shape from a pointer. Because the pointer
133 // is const, a copy is always made.
134 Vector(const IPosition &shape, const T *storage);
135
136 template<typename InputIterator>
137 Vector(InputIterator startIter, InputIterator endIter);
138
139 // Create a Vector from an STL vector (see <src>tovector()</src> in
140 // <linkto class=Array>Array</linkto> for the reverse operation).
141 // <note role=tip> Both this constructor and the tovector() are
142 // defined in <src>Vector2.cc</src>. </note>
143 // It does implicit promotion/demotion of the type U if different from T.
144 template <typename U, typename V>
145 explicit Vector(const std::vector<U, V> &other);
146
147 // Create a Vector from a container iterator and its length.
148 // <note> The length is used instead of last, because the distance
149 // function needed to calculate the length can be expensive.
150 // <br>A third dummy argument is unfortunately needed to avoid ambiguity
151 // with another Vector constructor (taking two uInts).
152 // </note>
153 template<typename Iterator>
154 Vector(Iterator first, size_t size, int dummy);
155
156 // Resize this Vector to the given length.
157 // The default copyValues flag is false.
158 //# Note that the 3rd resize function is necessary, because that
159 //# function is virtual in the base class (otherwise it would
160 //# be hidden).
161 // Resize without argument is equal to resize(0, false).
162 // <group>
163 using Array<T>::resize;
164 void resize(size_t len, bool copyValues=false)
165 { if (len != this->nelements()) resize(IPosition(1,len), copyValues); }
166 virtual void resize(const IPosition &len, bool copyValues=false) final override;
167 // </group>
168
169 // Assign to this Vector. If this Vector is zero-length, then resize
170 // to be the same size as other. Otherwise this and other have to be
171 // conformant (same size).
172 // <br>Note that the assign function can be used to assign a
173 // non-conforming vector.
174 // <group>
175 // TODO unlike Array, a Vector assign to an empty Vector does
176 // not create a reference but does a value copy of the source.
177 // This should be made consistent.
178 using Array<T>::assign_conforming;
179 Vector<T>& assign_conforming(const Vector<T>& source)
180 {
181 return assign_conforming_implementation(source, std::is_copy_assignable<T>());
182 }
184 // source must be a 1-dimensional array.
187 // </group>
188
189 using Array<T>::operator=;
191 { return assign_conforming(source); }
193 { return assign_conforming(std::move(source)); }
195 { assign_conforming(source); return *this; }
197 { assign_conforming(std::move(source)); return *this; }
198
199 // Convert a Vector to a Block, resizing the block and copying values.
200 // This is done this way to avoid having the simpler Block class
201 // containing dependencies on the Vector class.
202 //void toBlock(Block<T> &other) const;
203
204 // Single-pixel addressing. If AIPS_ARRAY_INDEX_CHECK is defined,
205 // bounds checking is performed (not for [])..
206 // <group>
207 T &operator[](size_t index)
208 { return (this->contiguous_p ? this->begin_p[index] : this->begin_p[index*this->inc_p(0)]); }
209 const T &operator[](size_t index) const
210 { return (this->contiguous_p ? this->begin_p[index] : this->begin_p[index*this->inc_p(0)]); }
212 { return Array<T>::operator()(i); }
213 const T &operator()(const IPosition &i) const
214 { return Array<T>::operator()(i); }
215 T &operator()(size_t index)
216 {
217#if defined(AIPS_ARRAY_INDEX_CHECK)
218 this->validateIndex(index); //# Throws an exception on failure
219#endif
220 return *(this->begin_p + index*this->inc_p(0));
221 }
222
223 const T &operator()(size_t index) const
224 {
225#if defined(AIPS_ARRAY_INDEX_CHECK)
226 this->validateIndex(index); //# Throws an exception on failure
227#endif
228 return *(this->begin_p + index*this->inc_p(0));
229 }
230 // </group>
231
232 // Take a slice of this vector. Slices are always indexed starting
233 // at zero. This uses reference semantics, i.e. changing a value
234 // in the slice changes the original.
235 // <srcblock>
236 // Vector<double> vd(100);
237 // //...
238 // vd(Slice(0,10)) = -1.0; // First 10 elements of vd set to -1
239 // </srcblock>
240 // <group>
242 const Vector<T> operator()(const Slice &slice) const;
243 // </group>
244
245 // Slice using IPositions. Required to be defined, otherwise the base
246 // class versions are hidden.
247 // <group>
248 Array<T> operator()(const IPosition &blc, const IPosition &trc,
249 const IPosition &incr)
250 { return Array<T>::operator()(blc,trc,incr); }
251 const Array<T> operator()(const IPosition &blc, const IPosition &trc,
252 const IPosition &incr) const
253 { return Array<T>::operator()(blc,trc,incr); }
254 Array<T> operator()(const IPosition &blc, const IPosition &trc)
255 { return Array<T>::operator()(blc,trc); }
256 const Array<T> operator()(const IPosition &blc, const IPosition &trc) const
257 { return Array<T>::operator()(blc,trc); }
259 { return Array<T>::operator()(slicer); }
260 const Array<T> operator()(const Slicer& slicer) const
261 { return Array<T>::operator()(slicer); }
262 // </group>
263
264 // The array is masked by the input LogicalArray.
265 // This mask must conform to the array.
266 // <group>
267
268 // Return a MaskedArray.
271
272 // Return a MaskedArray.
275
276 // </group>
277
278
279 // The array is masked by the input MaskedLogicalArray.
280 // The mask is effectively the AND of the internal LogicalArray
281 // and the internal mask of the MaskedLogicalArray.
282 // The MaskedLogicalArray must conform to the array.
283 // <group>
284
285 // Return a MaskedArray.
288
289 // Return a MaskedArray.
292
293 // </group>
294
295
296 // The length of the Vector.
297 const IPosition &shape() const
298 { return this->length_p; }
299 void shape(int &Shape) const
300 { Shape = this->length_p(0); }
301
302 // Verify that dimensionality is 1 and then call Array<T>::ok()
303 virtual bool ok() const final override;
304
305protected:
306 // Remove the degenerate axes from other and store result in this vector.
307 // An exception is thrown if removing degenerate axes does not result
308 // in a vector.
309 virtual void doNonDegenerate(const Array<T> &other,
310 const IPosition &ignoreAxes) final override;
311
312 virtual size_t fixedDimensionality() const final override { return 1; }
313
314private:
315 Vector<T>& assign_conforming_implementation(const Vector<T> &v, std::false_type isCopyable);
316 Vector<T>& assign_conforming_implementation(const Vector<T> &v, std::true_type isCopyable);
317
318 // Helper functions for constructors.
319 void initVector(const std::vector<T>&, long long nr); // copy semantics
320
321 // The following two constructors are used to make distinguish between
322 // Vector<literal>(literal size, literal value)
323 // and
324 // Vector<literal>(iterator, iterator)
325 template<typename InputIterator>
326 Vector(InputIterator startIter, InputIterator endIter, std::false_type /* T is not a literal */);
327
328 template<typename InputIterator>
329 Vector(InputIterator startIter, InputIterator endIter, std::true_type /* T is literal */);
330};
331
332} //#End namespace casacore
333
334#include "Vector.tcc"
335#include "Vector2.tcc"
336
337#endif
void validateIndex(const IPosition &) const
size_t nelements() const
How many elements does this array have? Product of all axis lengths.
Definition ArrayBase.h:101
size_t size() const
Definition ArrayBase.h:103
bool contiguous_p
Are the data contiguous?
Definition ArrayBase.h:271
IPosition length_p
Used to hold the shape, increment into the underlying storage and originalLength of the array.
Definition ArrayBase.h:274
T * begin_p
This pointer is adjusted to point to the first element of the array.
Definition Array.h:968
T & operator()(const IPosition &)
Access a single element of the array.
void resize()
Make this array a different shape.
Vector< T > & assign_conforming(const Array< T > &source)
source must be a 1-dimensional array.
Vector< T > & assign_conforming(Array< T > &&source)
Vector< T > & assign_conforming(const Vector< T > &source)
Definition Vector.h:179
const Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr) const
Definition Vector.h:251
Vector< T > & assign_conforming_implementation(const Vector< T > &v, std::false_type isCopyable)
Vector(Vector< T > &&source) noexcept
Move.
Vector(const std::vector< T > &other, long long nr)
Create a Vector from the given std::vector "other." Make it length "nr" and copy over that many eleme...
Vector(std::initializer_list< T > list)
Create a Vector from an initializer list.
const Array< T > operator()(const IPosition &blc, const IPosition &trc) const
Definition Vector.h:256
virtual void resize(const IPosition &len, bool copyValues=false) final override
Resize the array and optionally copy the values.
Vector(const Array< T > &other)
Create a reference to the other array.
Vector< T > operator()(const Slice &slice)
Take a slice of this vector.
void resize(size_t len, bool copyValues=false)
Definition Vector.h:164
T & operator[](size_t index)
Convert a Vector to a Block, resizing the block and copying values.
Definition Vector.h:207
Vector(const IPosition &shape, const T *storage)
Create an Vector of a given shape from a pointer.
const T & operator()(const IPosition &i) const
Definition Vector.h:213
const Array< T > operator()(const Slicer &slicer) const
Definition Vector.h:260
void initVector(const std::vector< T > &, long long nr)
Helper functions for constructors.
Vector< T > & operator=(const Vector< T > &source)
Definition Vector.h:190
Vector(size_t Length)
A Vector with a defined length and origin of zero.
virtual size_t fixedDimensionality() const final override
Subclasses can return their dimensionality.
Definition Vector.h:312
Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr)
Slice using IPositions.
Definition Vector.h:248
Vector(const IPosition &Length)
Vector< T > & assign_conforming_implementation(const Vector< T > &v, std::true_type isCopyable)
Array< T > operator()(const Slicer &slicer)
Definition Vector.h:258
const T & operator()(size_t index) const
Definition Vector.h:223
Vector< T > & operator=(Vector< T > &&source)
Definition Vector.h:192
Vector(InputIterator startIter, InputIterator endIter, std::false_type)
The following two constructors are used to make distinguish between Vector<literal>(literal size,...
Vector(Iterator first, size_t size, int dummy)
Create a Vector from a container iterator and its length.
Vector< T > & operator=(const Array< T > &source)
Definition Vector.h:194
Vector(InputIterator startIter, InputIterator endIter, std::true_type)
Vector< T > & assign_conforming(Vector< T > &&source)
Vector(const std::vector< T > &other)
Create a Vector of length other.size() and copy over its values.
const T & operator[](size_t index) const
Definition Vector.h:209
void shape(int &Shape) const
Definition Vector.h:299
Vector(const IPosition &shape, T *storage, StorageInitPolicy policy=COPY)
Create an Vector of a given shape from a pointer.
const Vector< T > operator()(const Slice &slice) const
Vector(const IPosition &Length, const T &initialValue)
virtual void doNonDegenerate(const Array< T > &other, const IPosition &ignoreAxes) final override
Remove the degenerate axes from other and store result in this vector.
T & operator()(size_t index)
Definition Vector.h:215
Vector(InputIterator startIter, InputIterator endIter)
virtual bool ok() const final override
Verify that dimensionality is 1 and then call Array<T>::ok()
Vector()
A zero-length Vector.
Vector(const IPosition &Length, typename Array< T >::uninitializedType)
Vector(const std::vector< U, V > &other)
Create a Vector from an STL vector (see tovector() in Array for the reverse operation).
Vector(size_t Length, const T &initialValue)
A Vector with a defined length and origin of zero.
Vector< T > & operator=(Array< T > &&source)
Definition Vector.h:196
T & operator()(const IPosition &i)
Definition Vector.h:211
Vector(const Vector< T > &other)
Create a reference to other.
Vector(size_t Length, typename Array< T >::uninitializedType)
An uninitialized Vector with a defined length.
const IPosition & shape() const
The length of the Vector.
Definition Vector.h:297
Array< T > operator()(const IPosition &blc, const IPosition &trc)
Definition Vector.h:254
StorageInitPolicy
Definition ArrayBase.h:49
@ COPY
COPY is used when an internal copy of the storage is to be made.
Definition ArrayBase.h:52
struct Node * first
Definition malloc.h:328
this file contains all the compiler specific defines
Definition mainpage.dox:28
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
This is a tag for the constructor that may be used to construct an uninitialized Array.
Definition Array.h:179