casacore
Loading...
Searching...
No Matches
ExprNodeSet.h
Go to the documentation of this file.
1//# ExprNodeSet.h: Classes representing a set in table select expression
2//# Copyright (C) 1997,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 TABLES_EXPRNODESET_H
27#define TABLES_EXPRNODESET_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/TaQL/ExprNodeSetElem.h>
32#include <casacore/tables/Tables/TableError.h>
33#include <casacore/casa/Arrays/ArrayFwd.h>
34#include <vector>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38//# Forward Declarations
39class TableExprNode;
40class IPosition;
41class Slicer;
42
43
44// <summary>
45// Class to hold multiple table expression nodes.
46// </summary>
47
48// <use visibility=export>
49
50// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
51// </reviewed>
52
53// <prerequisite>
54//# Classes you should understand before using this one.
55// <li> TableExprNode
56// <li> TableExprNodeRep
57// <li> TableExprNodeBinary
58// </prerequisite>
59
60// <synopsis>
61// This class is used to assemble several table expression nodes.
62// It is used for 3 purposes:
63// <ol>
64// <li> To hold the arguments of a function.
65// All set elements must be single.
66// <li> To hold the variables of an index for an array slice.
67// All set elements must be of type int scalar and they must
68// represent a discrete interval (which includes single).
69// <li> To hold the elements of a set used with the IN operator.
70// All set elements must be scalars of any type.
71// </ol>
72// The type of all set elements has to be the same.
73// The set consists of
74// <linkto class=TableExprNodeSetElem>TableExprNodeSetElem</linkto>
75// elements. The <src>add</src> function has to be used to
76// add an element to the set.
77// <p>
78// It is possible to construct the object directly from an
79// <linkto class=IPosition>IPosition</linkto> object.
80// In that case all elements are single.
81// Furthermore it is possible to construct it directly from a
82// <linkto class=Slicer>Slicer</linkto> object.
83// In that case all elements represent a discrete interval.
84// </synopsis>
85
87{
88public:
89 // Construct an empty set.
91
92 // Construct from an <src>IPosition</src>.
93 // The number of elements in the set is the number of elements
94 // in the <src>IPosition</src>. All set elements are single values.
96
97 // Construct from a <src>Slicer</src>.
98 // The number of elements in the set is the dimensionality
99 // of the <src>Slicer</src>. All set elements are discrete intervals.
100 // Their start and/or end is undefined if it is was not defined
101 // (i.e. Slicer::MimicSource used) in the <src>Slicer</src> object.
103
104 // Construct a set with n*set.size() elements where n is the number
105 // of rows.
106 // Element i is constructed by evaluating the input element
107 // for row rownr[i].
109
111
113
114 // A copy of a TableExprNodeSet cannot be assigned.
116
117 // Add an element to the set.
118 // If adaptType=True, the data type is the highest of the elements added.
119 // Otherwise it is that of the first element.
120 // True is meant for a set of values, False for function arguments.
121 // <br>A constant mid-width interval is added as a normal interval.
122 // In this way constant intervals can never be mid-width which makes
123 // optimization easier.
124 void add (const TENSEBShPtr&, Bool adaptType=False);
125 void add (const TableExprNodeSetElem& elem, Bool adaptType=False)
126 { add (elem.getElem(), adaptType); }
127
128 // Show the node.
129 void show (ostream& os, uInt indent) const override;
130
131 // Flatten the node tree by adding the node and its children to the vector.
132 virtual void flattenTree (std::vector<TableExprNodeRep*>&) override;
133
134 // Check if the data type of the set elements are the same.
135 // If not, an exception is thrown.
136 //# Note that if itsCheckTypes is set, the data types are already
137 //# known to be equal.
139
140 // Contains the set only single elements?
141 // Single means that only single values are given (thus no end nor incr).
142 Bool isSingle() const;
143
144 // Contains the set only discrete elements?
145 // Discrete means that no continuous ranges are given, but discrete
146 // ranges (using :) are possible.
147 Bool isDiscrete() const;
148
149 // Is the set fully bounded (discrete and no undefined end values)?
150 Bool isBounded() const;
151
152 // Get the number of elements.
153 size_t size() const;
154 // For backward compatibility.
155 size_t nelements() const {return size();}
156
157 // Get the i-th element.
158 const TENSEBShPtr& operator[] (size_t index) const;
159
160 // Contains the set array values?
162
163 // Try to convert the set to an array.
164 // If not possible, a copy of the set is returned.
166
167 template<typename T>
168 MArray<T> toArray (const TableExprId& id) const;
169
170 // Get an array value for this bounded set in the given row.
171 // <group>
178 // </group>
179
180 // Does a value occur in the set?
181 // <group>
182 Bool contains (const TableExprId& id, Bool value) override;
183 Bool contains (const TableExprId& id, Int64 value) override;
184 Bool contains (const TableExprId& id, Double value) override;
185 Bool contains (const TableExprId& id, DComplex value) override;
186 Bool contains (const TableExprId& id, String value) override;
187 Bool contains (const TableExprId& id, MVTime value) override;
189 const MArray<Bool>& value) override;
191 const MArray<Int64>& value) override;
193 const MArray<Double>& value) override;
195 const MArray<DComplex>& value) override;
197 const MArray<String>& value) override;
199 const MArray<MVTime>& value) override;
200 // </group>
201
202 // Useful to make overloading clearer (mainly for test programs).
204 { return contains (id, Int64(value)); }
205 Bool contains (const TableExprId& id, const char* value)
206 { return contains (id, String(value)); }
207
208 // Let a set node convert itself to the given unit.
209 void adaptSetUnits (const Unit&) override;
210
211 // Try to set the set's shape for a bounded set with single elements.
212 void setShape();
213
214private:
215 // Convert the const set to an array.
217
218 // Get the array in a templated way.
219 // <group>
220 void getArray (MArray<Bool>& marr, const TENShPtr& node,
221 const TableExprId& id) const
222 { marr.reference (node->getArrayBool (id)); }
223 void getArray (MArray<Int64>& marr, const TENShPtr& node,
224 const TableExprId& id) const
225 { marr.reference (node->getArrayInt (id)); }
226 void getArray (MArray<Double>& marr, const TENShPtr& node,
227 const TableExprId& id) const
228 { marr.reference (node->getArrayDouble (id)); }
229 void getArray (MArray<DComplex>& marr, const TENShPtr& node,
230 const TableExprId& id) const
231 { marr.reference (node->getArrayDComplex (id)); }
232 void getArray (MArray<String>& marr, const TENShPtr& node,
233 const TableExprId& id) const
234 { marr.reference (node->getArrayString (id)); }
235 void getArray (MArray<MVTime>& marr, const TENShPtr& node,
236 const TableExprId& id) const
237 { marr.reference (node->getArrayDate (id)); }
238 // </group>
239
240 //# Data members
241 std::vector<TENSEBShPtr> itsElems;
244 Bool itsBounded; //# Set is discrete and all starts/ends are defined
245 Bool itsCheckTypes; //# True = checking data types is not needed
246};
247
248
250{
251 return itsSingle;
252}
254{
255 return itsDiscrete;
256}
258{
259 return itsBounded;
260}
261inline size_t TableExprNodeSet::size() const
262{
263 return itsElems.size();
264}
265inline const TENSEBShPtr& TableExprNodeSet::operator[] (size_t index) const
266{
267 return itsElems[index];
268}
269
270
271template<typename T>
273{
276 Int64 n = size();
277 if (hasArrays()) {
278 if (itsElems[0]->start()->valueType() != VTArray) {
279 throw TableInvExpr("scalar value cannot be given in a nested array");
280 }
281 // Handle a nested array; this is done recursively.
282 MArray<T> marr;
283 getArray (marr, itsElems[0]->start(), id);
284 if (marr.isNull()) {
285 return marr;
286 }
287 Array<T> result (marr.array());
288 Array<Bool> mask (marr.mask());
289 IPosition shp = result.shape();
290 uInt naxes = shp.size();
291 shp.append (IPosition(1,n));
292 IPosition maskShp(shp);
293 maskShp[maskShp.size()-1] = 1;
294 result.resize (shp, True);
295 if (! mask.empty()) {
296 mask.resize (shp, True);
297 }
298 // Iterate through the remaining arrays.
299 ArrayIterator<T> iter(result, shp.size()-1);
300 IPosition s(shp.size(), 0);
301 IPosition e(shp-1);
302 e[naxes] = 0;
303 for (Int64 i=1; i<n; i++) {
304 if (itsElems[i]->start()->valueType() != VTArray) {
305 throw TableInvExpr("scalar value cannot be given in a nested array");
306 }
307 iter.next();
308 s[naxes]++;
309 e[naxes]++;
310 MArray<T> marr;
311 getArray (marr, itsElems[i]->start(), id);
312 if (marr.isNull()) {
313 return marr;
314 }
315 if (! marr.shape().isEqual (iter.array().shape())) {
316 throw TableInvExpr("Shapes of nested arrays do not match");
317 }
318 iter.array() = marr.array();
319 if (marr.hasMask()) {
320 if (mask.empty()) {
321 // The first time a mask was found, so create the resulting mask.
322 mask.resize (shp);
323 mask = False;
324 }
325 mask(s,e) = marr.mask().reform(maskShp);
326 } else if (! mask.empty()) {
327 // This array has no mask, so set to False in resulting mask.
328 mask(s,e) = False;
329 }
330 }
331 return MArray<T>(result, mask);
332 } else {
333 // Combine scalars.
334 Int64 n = size();
335 Int64 cnt = 0;
336 Vector<T> result (n);
337 for (Int64 i=0; i<n; i++) {
338 itsElems[i]->fillVector (result, cnt, id);
339 }
340 result.resize (cnt, True);
341 return MArray<T>(result);
342 }
343}
344
345
346
347} //# NAMESPACE CASACORE - END
348
349#endif
#define DebugAssert(expr, exception)
Definition Assert.h:183
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:123
Array< T > & array()
Return the cursor.
Definition ArrayIter.h:113
virtual void next() override
Move the cursor to the next position.
void resize()
Make this array a different shape.
Array< T > reform(const IPosition &shape) const
It is occasionally useful to have an array which access the same storage appear to have a different s...
size_t size() const
Definition IPosition.h:570
bool isEqual(const IPosition &other) const
Element-by-element comparison for equality.
void append(const IPosition &other)
Append this IPosition with another one (causing a resize).
Bool isNull() const
Is the array null?
Definition MArrayBase.h:109
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:124
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:117
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:145
void reference(const MArray< T > &other)
Reference another array.
Definition MArray.h:112
const Array< T > & array() const
Get access to the array.
Definition MArray.h:151
String: the storage and methods of handling collections of characters.
Definition String.h:223
Abstract base class for a node in a table column expression tree.
ValueType valueType() const
Get the value type.
Class to hold the table expression nodes for an element in a set.
const TENSEBShPtr & getElem() const
Get the internal pointer to the underlying TableExprNodeSetElemBase.
MArray< Bool > contains(const TableExprId &id, const MArray< MVTime > &value) override
TableExprNodeSet(const Vector< rownr_t > &rownrs, const TableExprNodeSet &)
Construct a set with n*set.size() elements where n is the number of rows.
MArray< Bool > getArrayBool(const TableExprId &id) override
Get an array value for this bounded set in the given row.
MArray< String > getArrayString(const TableExprId &id) override
TENShPtr toConstArray() const
Convert the const set to an array.
void setShape()
Try to set the set's shape for a bounded set with single elements.
MArray< Int64 > getArrayInt(const TableExprId &id) override
void getArray(MArray< DComplex > &marr, const TENShPtr &node, const TableExprId &id) const
MArray< Bool > contains(const TableExprId &id, const MArray< Bool > &value) override
void checkEqualDataTypes() const
Check if the data type of the set elements are the same.
virtual void flattenTree(std::vector< TableExprNodeRep * > &) override
Flatten the node tree by adding the node and its children to the vector.
TableExprNodeSet(const TableExprNodeSet &)
Bool isSingle() const
Contains the set only single elements? Single means that only single values are given (thus no end no...
Bool contains(const TableExprId &id, String value) override
MArray< Bool > contains(const TableExprId &id, const MArray< String > &value) override
MArray< MVTime > getArrayDate(const TableExprId &id) override
MArray< Bool > contains(const TableExprId &id, const MArray< Int64 > &value) override
void getArray(MArray< Int64 > &marr, const TENShPtr &node, const TableExprId &id) const
Bool contains(const TableExprId &id, MVTime value) override
Bool contains(const TableExprId &id, Bool value) override
Does a value occur in the set?
MArray< Bool > contains(const TableExprId &id, const MArray< DComplex > &value) override
MArray< DComplex > getArrayDComplex(const TableExprId &id) override
void show(ostream &os, uInt indent) const override
Show the node.
Bool contains(const TableExprId &id, const char *value)
TableExprNodeSet(const Slicer &)
Construct from a Slicer.
void getArray(MArray< Double > &marr, const TENShPtr &node, const TableExprId &id) const
Bool contains(const TableExprId &id, Int64 value) override
void getArray(MArray< String > &marr, const TENShPtr &node, const TableExprId &id) const
void add(const TENSEBShPtr &, Bool adaptType=False)
Add an element to the set.
std::vector< TENSEBShPtr > itsElems
Bool contains(const TableExprId &id, int value)
Useful to make overloading clearer (mainly for test programs).
Bool contains(const TableExprId &id, Double value) override
Bool isDiscrete() const
Contains the set only discrete elements? Discrete means that no continuous ranges are given,...
Bool hasArrays() const
Contains the set array values?
MArray< Double > getArrayDouble(const TableExprId &id) override
void getArray(MArray< Bool > &marr, const TENShPtr &node, const TableExprId &id) const
Get the array in a templated way.
Bool isBounded() const
Is the set fully bounded (discrete and no undefined end values)?
const TENSEBShPtr & operator[](size_t index) const
Get the i-th element.
MArray< T > toArray(const TableExprId &id) const
size_t nelements() const
For backward compatibility.
size_t size() const
Get the number of elements.
void adaptSetUnits(const Unit &) override
Let a set node convert itself to the given unit.
void add(const TableExprNodeSetElem &elem, Bool adaptType=False)
void getArray(MArray< MVTime > &marr, const TENShPtr &node, const TableExprId &id) const
TableExprNodeSet & operator=(const TableExprNodeSet &)=delete
A copy of a TableExprNodeSet cannot be assigned.
Bool contains(const TableExprId &id, DComplex value) override
MArray< Bool > contains(const TableExprId &id, const MArray< Double > &value) override
TENShPtr setOrArray() const
Try to convert the set to an array.
TableExprNodeSet(const IPosition &)
Construct from an IPosition.
TableExprNodeSet()
Construct an empty set.
Table error; invalid select expression.
Definition TableError.h:458
void resize(size_t len, bool copyValues=false)
Definition Vector.h:164
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
std::shared_ptr< TableExprNodeRep > TENShPtr
Definition ExprNodeRep.h:55
std::shared_ptr< TableExprNodeSetElemBase > TENSEBShPtr
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53