casacore
Loading...
Searching...
No Matches
ArrayUtil.h
Go to the documentation of this file.
1// # ArrayUtil.h: Utility functions for arrays
2// # Copyright (C) 1995,1999,2000,2001
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_ARRAYUTIL_2_H
27#define CASA_ARRAYUTIL_2_H
28
29// # Includes
30#include "Vector.h"
31
32#include <regex>
33#include <string>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// Split a std::string into its elements.
39// </summary>
40
41// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil">
42
43// <prerequisite>
44// <li> <linkto class=Vector>Vector</linkto>
45// <li> std::string
46// </prerequisite>
47
48// <etymology>
49// stringToVector converts a std::string to a Vector of Strings.
50// </etymology>
51
52// <synopsis>
53// The function stringToVector splits a string into its elements
54// using the given delimiter and returns them in a <src>Vector<std::string></src>.
55// The default delimiter is a comma (,).
56// It is very useful when using a function taking a vector of strings
57// as shown in the example.
58// <p>
59// A more advanced way of splitting a string is by using a
60// regular expression as delimiter.
61// It makes it, for example, possible to treat whitespace around a comma
62// as part of the delimiter (as shown in an example below).
63// <p>
64// A string with length 0 results in a zero-length vector.
65// </synopsis>
66
67// <motivation>
68// As shown in the example, the function stringToVector makes
69// passing a Vector of Strings far easier.
70// </motivation>
71
72// <example>
73// <srcblock>
74// someFunction (stringToVector ("abc,def ,,gh"));
75// </srcblock>
76// This results in a vector with 4 elements containing the values
77// "abc", "def ", "", and "gh". The vector is passed to someFunction.
78// This is far easier than having to do it as:
79// <srcblock>
80// Vector<std::string> vector(4);
81// vector(0) = "abc";
82// vector(1) = "def ";
83// vector(2) = "";
84// vector(3) = "gh";
85// someFunction (vector);
86// </srcblock>
87//
88// The following example shows how to use a delimiter consisting of a comma
89// surrounded by possible whitespace.
90// <srcblock>
91// Vector<std::string> result = stringToVector (source, Regex(" *, *"));
92// </srcblock>
93// <example>
94
95// <group name=stringToVector>
96Vector<std::string> strToVector(const std::string& string, char delim = ',');
97Vector<std::string> strToVector(const std::string& string, const std::regex& delim);
98// </group>
99
100// <summary>
101// Concatenate two Arrays.
102// </summary>
103
104// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil">
105
106// <prerequisite>
107// <li> <linkto class=Array>Array</linkto>
108// </prerequisite>
109
110// <etymology>
111// concatenateArray concatenates two Arrays into a new Array.
112// </etymology>
113
114// <synopsis>
115// The function concatenates two Arrays into a new Array.
116// The shape of both arrays must match except for the last dimension.
117// The shape of the resulting array is equal to that of the input
118// arrays with its last dimension as the sum of both last dimensions.
119// <p>
120// An exception ArrayConformanceError is thrown when the shapes
121// do not match.
122// </synopsis>
123
124// <motivation>
125// The table system needed this function.
126// </motivation>
127
128// <example>
129// <srcblock>
130// Vector<int> vector1(5);
131// Vector<int> vector2(10);
132// indgen (vector1); // fill with values 0..4
133// indgen (vector2); // fill with values 0..9
134// Vector<int> result = concatenateVector (vector1, vector2);
135// </srcblock>
136// The example above results in a vector with length 15 and values
137// 0,1,2,3,4,0,1,2,3,4,5,6,7,8,9.
138// <p>
139// It can also be used with matrices or arrays with higher dimensionality
140// as long as all dimensions but the last one have equal length.
141// <srcblock>
142// Matrix<int> matrix1 (3,4);
143// Matrix<int> matrix2 (3,5);
144// Matrix<int> matrix3 (4,4);
145// // Concatenation of matrix1 and matrix 2 will succeed and result
146// // in a 3x9 matrix.
147// Matrix<int> matrixConc = concatenateArray (matrix1, matrix2);
148// if (matrixConc.shape() != IPosition(2,3,9)) {
149// cout << "Error in shape of concatenated matrices" << endl;
150// }
151// // Concatenation of matrix1 and matrix3 will fail, because the
152// // first dimensions have a different length (3 vs. 4).
153// try {
154// concatenateArray (matrix1, matrix2);
155// } catch (ArrayConformanceError x) {
156// cout << x.what() << endl;
157// }
158// </srcblock>
159// <example>
160
161// <group name=concatenateArray>
162template <class T>
163Array<T> concatenateArray(const Array<T>& left, const Array<T>& right);
164// </group>
165
166// <summary> Helper function for partialX functions </summary>
167// <use visibility=export>
168// <synopsis>
169// This is a specialized helper function for functions like partialSums.
170// It determines the shape of the resulting array and calculates the
171// result increments when iterating linearly through the source array.
172// It returns the first result axis which indicates the number of the first
173// contiguous collapse axes. The number of contiguous data points is
174// returned in nelemCont.
175// </synopsis>
176// <group name=partialFuncHelper>
177size_t partialFuncHelper(int& nelemCont, IPosition& resultShape, IPosition& incr,
178 const IPosition& sourceShape, const IPosition& collapseAxes);
179// </group>
180
181// <summary>
182// Reverse the order of one or more axes of an array.
183// </summary>
184
185// <use visibility=export>
186
187// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
188
189// <synopsis>
190// This function makes it possible to reverse one or more axes of an array by
191// swapping around the elements of each axis.
192// The resulting array is a copy of the input array with its data
193// moved around according to the new order.
194// If the order does not change, a copy is returned if the
195// <src>alwaysCopy</src> is true. Otherwise a reference of the
196// input array is returned.
197// </synopsis>
198
199// <example>
200// Reversing axis 0 of a Vector means that the Vector is reversed.
201// Reversing axis 1 of a Matrix means that its rows are reversed.
202// Reversing axis 0 of an N-dim array means that the elements of each Vector
203// in that array are reversed.
204// </example>
205
206// <group name=reverseArray>
207template <class T>
208Array<T> reverseArray(const Array<T>& array, const IPosition& reversedAxes, bool alwaysCopy = true);
209template <class T>
210Array<T> reverseArray(const Array<T>& array, size_t axis, bool alwaysCopy = true);
211// </group>
212
213// <summary>
214// Reorder the axes of an array.
215// </summary>
216
217// <use visibility=export>
218
219// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
220
221// <synopsis>
222// This function makes it possible to reorder the axes of an array.
223// The resulting array is a copy of the input array with its data
224// moved around according to the new array order.
225// If the order does not change, a copy is returned if the
226// <src>alwaysCopy</src> is true. Otherwise a reference of the
227// input array is returned.
228// <p>
229// The <src>newAxisOrder</src> defines the new axes order.
230// Its length can be less than the dimensionality of the input array.
231// It is appended with the non-specified axes in their natural order.
232// <src>newAxisOrder(i)</src> gives the axis in the original array
233// which will now get axis <src>i</src>.
234// </synopsis>
235
236// <example>
237// <srcblock>
238// Array<int> result = reorderArray (someArray, IPosition(2,1,3));
239// </srcblock>
240// Say that someArray is a 4D array with shape [3,4,5,6].
241// The non-specified axes get appended to the axis order
242// specification [1,3] resulting in [1,3,0,2].
243// <br> This means that axis 1 gets axis 0, axis 3 gets axis 1, axis 0 gets
244// axis 2, and axis 2 gets axis 3.
245// Thus the resulting shape is [4,6,3,5] and the data are moved accordingly.
246// </example>
247
248// <motivation>
249// This function was needed for an efficient implementation of the
250// functions partialMedians and partialFractiles.
251// </motivation>
252
253// <group name=reorderArray>
254template <class T>
255Array<T> reorderArray(const Array<T>& array, const IPosition& newAxisOrder, bool alwaysCopy = true);
256// </group>
257
258// <summary>
259// Helper function for function reorderArray.
260// </summary>
261
262// <use visibility=local>
263
264// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayUtil2.cc">
265
266// <synopsis>
267// This is a specialized helper function for function reorderArray.
268// It determines the shape of the resulting array and calculates the
269// result increments when iterating linearly through the source array.
270// It returns the number of the first non-reordered axes.
271// </synopsis>
272
273// <motivation>
274// Split off common non-templated code.
275// </motivation>
276
277// <group name=reorderArrayHelper>
278size_t reorderArrayHelper(IPosition& newShape, IPosition& incr, const IPosition& shape,
279 const IPosition& newAxisOrder);
280// </group>
281
282} // namespace casacore
283
284#include "ArrayUtil.tcc"
285
286#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
T * array
The actual storage.
Definition Block.h:689
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.
Array< T > concatenateArray(const Array< T > &left, const Array< T > &right)
Helper function for partialX functions.
Definition ArrayUtil.h:177
size_t partialFuncHelper(int &nelemCont, IPosition &resultShape, IPosition &incr, const IPosition &sourceShape, const IPosition &collapseAxes)
Helper function for function reorderArray.
Definition ArrayUtil.h:278
size_t reorderArrayHelper(IPosition &newShape, IPosition &incr, const IPosition &shape, const IPosition &newAxisOrder)
Array< T > reorderArray(const Array< T > &array, const IPosition &newAxisOrder, bool alwaysCopy=true)
Reverse the order of one or more axes of an array.
Definition ArrayUtil.h:207
Array< T > reverseArray(const Array< T > &array, size_t axis, bool alwaysCopy=true)
Array< T > reverseArray(const Array< T > &array, const IPosition &reversedAxes, bool alwaysCopy=true)
Vector< std::string > strToVector(const std::string &string, char delim=',')
Vector< std::string > strToVector(const std::string &string, const std::regex &delim)