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// <summary>
44// Class to hold multiple table expression nodes.
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50// </reviewed>
51
52// <prerequisite>
53// # Classes you should understand before using this one.
54// <li> TableExprNode
55// <li> TableExprNodeRep
56// <li> TableExprNodeBinary
57// </prerequisite>
58
59// <synopsis>
60// This class is used to assemble several table expression nodes.
61// It is used for 3 purposes:
62// <ol>
63// <li> To hold the arguments of a function.
64// All set elements must be single.
65// <li> To hold the variables of an index for an array slice.
66// All set elements must be of type int scalar and they must
67// represent a discrete interval (which includes single).
68// <li> To hold the elements of a set used with the IN operator.
69// All set elements must be scalars of any type.
70// </ol>
71// The type of all set elements has to be the same.
72// The set consists of
73// <linkto class=TableExprNodeSetElem>TableExprNodeSetElem</linkto>
74// elements. The <src>add</src> function has to be used to
75// add an element to the set.
76// <p>
77// It is possible to construct the object directly from an
78// <linkto class=IPosition>IPosition</linkto> object.
79// In that case all elements are single.
80// Furthermore it is possible to construct it directly from a
81// <linkto class=Slicer>Slicer</linkto> object.
82// In that case all elements represent a discrete interval.
83// </synopsis>
84
86 public:
87 // Construct an empty set.
89
90 // Construct from an <src>IPosition</src>.
91 // The number of elements in the set is the number of elements
92 // in the <src>IPosition</src>. All set elements are single values.
94
95 // Construct from a <src>Slicer</src>.
96 // The number of elements in the set is the dimensionality
97 // of the <src>Slicer</src>. All set elements are discrete intervals.
98 // Their start and/or end is undefined if it is was not defined
99 // (i.e. Slicer::MimicSource used) in the <src>Slicer</src> object.
101
102 // Construct a set with n*set.size() elements where n is the number
103 // of rows.
104 // Element i is constructed by evaluating the input element
105 // for row rownr[i].
107
109
111
112 // A copy of a TableExprNodeSet cannot be assigned.
114
115 // Add an element to the set.
116 // If adaptType=True, the data type is the highest of the elements added.
117 // Otherwise it is that of the first element.
118 // True is meant for a set of values, False for function arguments.
119 // <br>A constant mid-width interval is added as a normal interval.
120 // In this way constant intervals can never be mid-width which makes
121 // optimization easier.
122 void add(const TENSEBShPtr&, Bool adaptType = False);
123 void add(const TableExprNodeSetElem& elem, Bool adaptType = False) {
124 add(elem.getElem(), adaptType);
125 }
126
127 // Show the node.
128 void show(ostream& os, uInt indent) const override;
129
130 // Flatten the node tree by adding the node and its children to the vector.
131 virtual void flattenTree(std::vector<TableExprNodeRep*>&) override;
132
133 // Check if the data type of the set elements are the same.
134 // If not, an exception is thrown.
135 // # Note that if itsCheckTypes is set, the data types are already
136 // # known to be equal.
138
139 // Contains the set only single elements?
140 // Single means that only single values are given (thus no end nor incr).
141 Bool isSingle() const;
142
143 // Contains the set only discrete elements?
144 // Discrete means that no continuous ranges are given, but discrete
145 // ranges (using :) are possible.
146 Bool isDiscrete() const;
147
148 // Is the set fully bounded (discrete and no undefined end values)?
149 Bool isBounded() const;
150
151 // Get the number of elements.
152 size_t size() const;
153 // For backward compatibility.
154 size_t nelements() const { return size(); }
155
156 // Get the i-th element.
157 const TENSEBShPtr& operator[](size_t index) const;
158
159 // Contains the set array values?
161
162 // Try to convert the set to an array.
163 // If not possible, a copy of the set is returned.
165
166 template <typename T>
167 MArray<T> toArray(const TableExprId& id) const;
168
169 // Get an array value for this bounded set in the given row.
170 // <group>
177 // </group>
178
179 // Does a value occur in the set?
180 // <group>
181 Bool contains(const TableExprId& id, Bool value) override;
182 Bool contains(const TableExprId& id, Int64 value) override;
183 Bool contains(const TableExprId& id, Double value) override;
184 Bool contains(const TableExprId& id, DComplex value) override;
185 Bool contains(const TableExprId& id, String value) override;
186 Bool contains(const TableExprId& id, MVTime value) override;
193 // </group>
194
195 // Useful to make overloading clearer (mainly for test programs).
196 Bool contains(const TableExprId& id, int value) { return contains(id, Int64(value)); }
197 Bool contains(const TableExprId& id, const char* value) { return contains(id, String(value)); }
198
199 // Let a set node convert itself to the given unit.
200 void adaptSetUnits(const Unit&) override;
201
202 // Try to set the set's shape for a bounded set with single elements.
203 void setShape();
204
205 private:
206 // Convert the const set to an array.
208
209 // Get the array in a templated way.
210 // <group>
211 void getArray(MArray<Bool>& marr, const TENShPtr& node, const TableExprId& id) const {
212 marr.reference(node->getArrayBool(id));
213 }
214 void getArray(MArray<Int64>& marr, const TENShPtr& node, const TableExprId& id) const {
215 marr.reference(node->getArrayInt(id));
216 }
217 void getArray(MArray<Double>& marr, const TENShPtr& node, const TableExprId& id) const {
218 marr.reference(node->getArrayDouble(id));
219 }
220 void getArray(MArray<DComplex>& marr, const TENShPtr& node, const TableExprId& id) const {
221 marr.reference(node->getArrayDComplex(id));
222 }
223 void getArray(MArray<String>& marr, const TENShPtr& node, const TableExprId& id) const {
224 marr.reference(node->getArrayString(id));
225 }
226 void getArray(MArray<MVTime>& marr, const TENShPtr& node, const TableExprId& id) const {
227 marr.reference(node->getArrayDate(id));
228 }
229 // </group>
230
231 // # Data members
232 std::vector<TENSEBShPtr> itsElems;
235 Bool itsBounded; // # Set is discrete and all starts/ends are defined
236 Bool itsCheckTypes; // # True = checking data types is not needed
237};
238
239inline Bool TableExprNodeSet::isSingle() const { return itsSingle; }
242inline size_t TableExprNodeSet::size() const { return itsElems.size(); }
243inline const TENSEBShPtr& TableExprNodeSet::operator[](size_t index) const {
244 return itsElems[index];
245}
246
247template <typename T>
251 Int64 n = size();
252 if (hasArrays()) {
253 if (itsElems[0]->start()->valueType() != VTArray) {
254 throw TableInvExpr("scalar value cannot be given in a nested array");
255 }
256 // Handle a nested array; this is done recursively.
257 MArray<T> marr;
258 getArray(marr, itsElems[0]->start(), id);
259 if (marr.isNull()) {
260 return marr;
261 }
262 Array<T> result(marr.array());
263 Array<Bool> mask(marr.mask());
264 IPosition shp = result.shape();
265 uInt naxes = shp.size();
266 shp.append(IPosition(1, n));
267 IPosition maskShp(shp);
268 maskShp[maskShp.size() - 1] = 1;
269 result.resize(shp, True);
270 if (!mask.empty()) {
271 mask.resize(shp, True);
272 }
273 // Iterate through the remaining arrays.
274 ArrayIterator<T> iter(result, shp.size() - 1);
275 IPosition s(shp.size(), 0);
276 IPosition e(shp - 1);
277 e[naxes] = 0;
278 for (Int64 i = 1; i < n; i++) {
279 if (itsElems[i]->start()->valueType() != VTArray) {
280 throw TableInvExpr("scalar value cannot be given in a nested array");
281 }
282 iter.next();
283 s[naxes]++;
284 e[naxes]++;
285 MArray<T> marr;
286 getArray(marr, itsElems[i]->start(), id);
287 if (marr.isNull()) {
288 return marr;
289 }
290 if (!marr.shape().isEqual(iter.array().shape())) {
291 throw TableInvExpr("Shapes of nested arrays do not match");
292 }
293 iter.array() = marr.array();
294 if (marr.hasMask()) {
295 if (mask.empty()) {
296 // The first time a mask was found, so create the resulting mask.
297 mask.resize(shp);
298 mask = False;
299 }
300 mask(s, e) = marr.mask().reform(maskShp);
301 } else if (!mask.empty()) {
302 // This array has no mask, so set to False in resulting mask.
303 mask(s, e) = False;
304 }
305 }
306 return MArray<T>(result, mask);
307 } else {
308 // Combine scalars.
309 Int64 n = size();
310 Int64 cnt = 0;
311 Vector<T> result(n);
312 for (Int64 i = 0; i < n; i++) {
313 itsElems[i]->fillVector(result, cnt, id);
314 }
315 result.resize(cnt, True);
316 return MArray<T>(result);
317 }
318}
319
320} // namespace casacore
321
322#endif
#define DebugAssert(expr, exception)
Definition Assert.h:161
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:116
Array< T > & array()
Return the cursor.
Definition ArrayIter.h:111
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:552
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:103
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:118
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:112
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:134
void reference(const MArray< T > &other)
Reference another array.
Definition MArray.h:101
const Array< T > & array() const
Get access to the array.
Definition MArray.h:136
String: the storage and methods of handling collections of characters.
Definition String.h:355
TableExprNodeRep(NodeDataType, ValueType, OperType, ArgType, ExprType, Int ndim, const IPosition &shape)
Construct a node.
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?
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?
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:437
void resize(size_t len, bool copyValues=false)
Definition Vector.h:164
For temporary backward namespace compatibility, use casa as alias for casacore.
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:56
std::shared_ptr< TableExprNodeSetElemBase > TENSEBShPtr
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
double Double
Definition aipstype.h:53