casacore
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: 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 TABLES_EXPRNODESET_H
29 #define TABLES_EXPRNODESET_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/tables/TaQL/ExprNodeRep.h>
34 #include <casacore/tables/TaQL/ExprNodeArray.h>
35 #include <casacore/casa/Containers/Block.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 class TableExprNode;
41 class IPosition;
42 class Slicer;
43 template<class T> class Vector;
44 
45 
46 // <summary>
47 // Class to hold the table expression nodes for an element in a set.
48 // </summary>
49 
50 // <use visibility=export>
51 
52 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
53 // </reviewed>
54 
55 // <prerequisite>
56 //# Classes you should understand before using this one.
57 // <li> TableExprNodeSet
58 // <li> TableExprNodeRep
59 // </prerequisite>
60 
61 // <synopsis>
62 // This class is used to assemble the table expression nodes
63 // representing an element in a set. A set element can be of 3 types:
64 // <ol>
65 // <li> A single discrete value, which can be of any type.
66 // It can be used for 3 purposes:
67 // <br>- A function argument.
68 // <br>- A single index in an array indexing operation.
69 // <br>- A single value in a set (used with the IN operator).
70 // This is in fact a bounded discrete interval (see below).
71 // <li> A discrete interval consisting of start, end and increment.
72 // Each of those has to be an int scalar. Increment defaults to 1.
73 // It can be used for 2 purposes:
74 // <br>- A slice in an array indexing operation. In that case start
75 // defaults to the beginning of the dimension and end defaults to the end.
76 // <br>- A discrete interval in a set. Start has to be given.
77 // When end is not given, the result is an unbounded discrete interval.
78 // For a discrete interval, the type of start and end can also be
79 // a datetime scalar.
80 // <li> A continuous interval, which can only be used in a set.
81 // It consists of a start and/or an end scalar value of type int, double,
82 // datetime, or string. The interval can be open or closed on one or
83 // both sides.
84 // </ol>
85 // Note the difference between a discrete and a continuous interval.
86 // E.g. the discrete interval 2,6 consists of the five values 2,3,4,5,6.
87 // The continuous interval 2,6 consists of all values between them.
88 // <br>Further note that a bounded discrete interval is automatically
89 // converted to a vector, which makes it possible to apply array
90 // functions to it.
91 // </synopsis>
92 
94 {
95 public:
96  // Create the object for a single expression node.
97  explicit TableExprNodeSetElem (const TableExprNode& node);
98 
99  // Create the object for a discrete interval.
100  // Each of the start, end, and incr pointers can be zero meaning
101  // that they are not given (see the synopsis for an explanation).
102  // Optionally the end is inclusive (C++ and Glish style) or exclusive
103  // (Python style).
105  const TableExprNode* end,
106  const TableExprNode* incr,
107  Bool isEndExcl = False);
108 
109  // Create the object for a continuous bounded interval. It can be
110  // open or closed on either side.
112  const TableExprNode& end, Bool isRightClosed);
113 
114  // Create the object for a continuous left-bounded interval.
115  TableExprNodeSetElem (Bool isLeftClosed, const TableExprNode& start);
116 
117  // Create the object for a continuous right-bounded interval.
118  TableExprNodeSetElem (const TableExprNode& end, Bool isRightClosed);
119 
120  // Copy constructor (copy semantics).
122 
124 
125  // Show the node.
126  void show (ostream& os, uInt indent) const;
127 
128  // Get the nodes representing an aggregate function.
129  virtual void getAggrNodes (vector<TableExprNodeRep*>& aggr);
130 
131  // Get the nodes representing a table column.
132  virtual void getColumnNodes (vector<TableExprNodeRep*>& cols);
133 
134  // Is it a discrete set element.
135  Bool isDiscrete() const;
136 
137  // Is a single value given?
138  Bool isSingle() const;
139 
140  // Is the interval left or right closed?
141  // <group>
142  Bool isLeftClosed() const;
143  Bool isRightClosed() const;
144  // </group>
145 
146  // Get the start, end or increment expression.
147  // Note that the pointer returned can be zero indicating that that
148  // value was not given.
149  // <group>
150  TableExprNodeRep* start() const;
151  TableExprNodeRep* end() const;
152  TableExprNodeRep* increment() const;
153  // </group>
154 
155  // Fill a vector with the value(s) from this element by appending them
156  // at the end of the vector; the end is given by argument <src>cnt</src>
157  // which gets incremented with the number of values appended.
158  // This is used by the system to convert a set to a vector.
159  // <group>
160  void fillVector (Vector<Bool>& vec, uInt& cnt,
161  const TableExprId& id) const;
162  void fillVector (Vector<Int64>& vec, uInt& cnt,
163  const TableExprId& id) const;
164  void fillVector (Vector<Double>& vec, uInt& cnt,
165  const TableExprId& id) const;
166  void fillVector (Vector<DComplex>& vec, uInt& cnt,
167  const TableExprId& id) const;
168  void fillVector (Vector<String>& vec, uInt& cnt,
169  const TableExprId& id) const;
170  void fillVector (Vector<MVTime>& vec, uInt& cnt,
171  const TableExprId& id) const;
172  // </group>
173 
174  // Set a flag in the match output array if the corresponding element
175  // in the value array is included in this set element.
176  // This is used by the system to implement the IN operator.
177  // <br>Note that it does NOT set match values to False; it is assumed they
178  // are initialized that way.
179  // <group>
180  void matchBool (Bool* match, const Bool* value, uInt nval,
181  const TableExprId& id) const;
182  void matchInt (Bool* match, const Int64* value, uInt nval,
183  const TableExprId& id) const;
184  void matchDouble (Bool* match, const Double* value, uInt nval,
185  const TableExprId& id) const;
186  void matchDComplex (Bool* match, const DComplex* value, uInt nval,
187  const TableExprId& id) const;
188  void matchString (Bool* match, const String* value, uInt nval,
189  const TableExprId& id) const;
190  void matchDate (Bool* match, const MVTime* value, uInt nval,
191  const TableExprId& id) const;
192  // </group>
193 
194  // Evaluate the element for the given row and construct a new
195  // (constant) element from it.
196  // This is used by the system to implement a set in a GIVING clause.
197  TableExprNodeSetElem* evaluate (const TableExprId& id) const;
198 
199  // Get the table of a node and check if the children use the same table.
200  void checkTable();
201 
202  // Let a set node convert itself to the given unit.
203  virtual void adaptSetUnits (const Unit&);
204 
205 private:
206  // A copy of a TableExprNodeSetElem cannot be made.
208 
209  // Construct an element from the given parts and take over their pointers.
210  // It is used by evaluate to construct an element in a rather cheap way.
212  TableExprNodeRep* start, TableExprNodeRep* end,
213  TableExprNodeRep* incr);
214 
215  // Setup the object for a continuous interval.
216  void setup (Bool isLeftClosed, const TableExprNode* start,
217  const TableExprNode* end, Bool isRightClosed);
218 
219 
228 };
229 
230 
231 
233 {
234  return itsDiscrete;
235 }
237 {
238  return itsSingle;
239 }
241 {
242  return itsLeftClosed;
243 }
245 {
246  return itsRightClosed;
247 }
249 {
250  return itsStart;
251 }
253 {
254  return itsEnd;
255 }
257 {
258  return itsIncr;
259 }
260 
261 
262 
263 // <summary>
264 // Class to hold multiple table expression nodes.
265 // </summary>
266 
267 // <use visibility=export>
268 
269 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
270 // </reviewed>
271 
272 // <prerequisite>
273 //# Classes you should understand before using this one.
274 // <li> TableExprNode
275 // <li> TableExprNodeRep
276 // <li> TableExprNodeBinary
277 // </prerequisite>
278 
279 // <synopsis>
280 // This class is used to assemble several table expression nodes.
281 // It is used for 3 purposes:
282 // <ol>
283 // <li> To hold the arguments of a function.
284 // All set elements must be single.
285 // <li> To hold the variables of an index for an array slice.
286 // All set elements must be of type int scalar and they must
287 // represent a discrete interval (which includes single).
288 // <li> To hold the elements of a set used with the IN operator.
289 // All set elements must be scalars of any type.
290 // </ol>
291 // The type of all set elements has to be the same.
292 // The set consists of
293 // <linkto class=TableExprNodeSetElem>TableExprNodeSetElem</linkto>
294 // elements. The <src>add</src> function has to be used to
295 // add an element to the set.
296 // <p>
297 // It is possible to construct the object directly from an
298 // <linkto class=IPosition>IPosition</linkto> object.
299 // In that case all elements are single.
300 // Furthermore it is possible to construct it directly from a
301 // <linkto class=Slicer>Slicer</linkto> object.
302 // In that case all elements represent a discrete interval.
303 // </synopsis>
304 
306 {
307 public:
308  // Construct an empty set.
310 
311  // Construct from an <src>IPosition</src>.
312  // The number of elements in the set is the number of elements
313  // in the <src>IPosition</src>. All set elements are single values.
314  TableExprNodeSet (const IPosition&);
315 
316  // Construct from a <src>Slicer</src>.
317  // The number of elements in the set is the dimensionality
318  // of the <src>Slicer</src>. All set elements are discrete intervals.
319  // Their start and/or end is undefined if it is was not defined
320  // (i.e. Slicer::MimicSource used) in the <src>Slicer</src> object.
321  TableExprNodeSet (const Slicer&);
322 
323  // Construct a set with n*set.nelements() elements where n is the number
324  // of rows.
325  // Element i is constructed by evaluating the input element
326  // for row rownr[i].
327  TableExprNodeSet (const Vector<uInt>& rownrs, const TableExprNodeSet&);
328 
330 
331  ~TableExprNodeSet();
332 
333  // Add an element to the set.
334  void add (const TableExprNodeSetElem&);
335 
336  // Show the node.
337  void show (ostream& os, uInt indent) const;
338 
339  // Get the nodes representing an aggregate function.
340  virtual void getAggrNodes (vector<TableExprNodeRep*>& aggr);
341 
342  // Get the nodes representing a table column.
343  virtual void getColumnNodes (vector<TableExprNodeRep*>& cols);
344 
345  // Check if the data type of the set elements are the same.
346  // If not, an exception is thrown.
347  //# Note that if itsCheckTypes is set, the data types are already
348  //# known to be equal.
349  void checkEqualDataTypes() const;
350 
351  // Contains the set only single elements?
352  // Single means that only single values are given (thus no end nor incr).
353  Bool isSingle() const;
354 
355  // Contains the set only discrete elements?
356  // Discrete means that no continuous ranges are given, but discrete
357  // ranges (using :) are possible.
358  Bool isDiscrete() const;
359 
360  // Is the set fully bounded (discrete and no undefined end values)?
361  Bool isBounded() const;
362 
363  // Get the number of elements.
364  uInt nelements() const;
365 
366  // Get the i-th element.
367  const TableExprNodeSetElem& operator[] (uInt index) const;
368 
369  // Contains the set array values?
370  Bool hasArrays() const;
371 
372  // Try to convert the set to an array.
373  // If not possible, a copy of the set is returned.
374  TableExprNodeRep* setOrArray() const;
375 
376  // Convert the const set to an array.
377  TableExprNodeRep* toArray() const;
378 
379  // Get an array value for this bounded set in the given row.
380  // <group>
381  virtual Array<Bool> getArrayBool (const TableExprId& id);
382  virtual Array<Int64> getArrayInt (const TableExprId& id);
383  virtual Array<Double> getArrayDouble (const TableExprId& id);
384  virtual Array<DComplex> getArrayDComplex (const TableExprId& id);
385  virtual Array<String> getArrayString (const TableExprId& id);
386  virtual Array<MVTime> getArrayDate (const TableExprId& id);
387  // </group>
388 
389  // Does a value occur in the set?
390  // <group>
391  virtual Bool hasBool (const TableExprId& id, Bool value);
392  virtual Bool hasInt (const TableExprId& id, Int64 value);
393  virtual Bool hasDouble (const TableExprId& id, Double value);
394  virtual Bool hasDComplex (const TableExprId& id, const DComplex& value);
395  virtual Bool hasString (const TableExprId& id, const String& value);
396  virtual Bool hasDate (const TableExprId& id, const MVTime& value);
397  virtual Array<Bool> hasArrayBool (const TableExprId& id,
398  const Array<Bool>& value);
399  virtual Array<Bool> hasArrayInt (const TableExprId& id,
400  const Array<Int64>& value);
401  virtual Array<Bool> hasArrayDouble (const TableExprId& id,
402  const Array<Double>& value);
403  virtual Array<Bool> hasArrayDComplex (const TableExprId& id,
404  const Array<DComplex>& value);
405  virtual Array<Bool> hasArrayString (const TableExprId& id,
406  const Array<String>& value);
407  virtual Array<Bool> hasArrayDate (const TableExprId& id,
408  const Array<MVTime>& value);
409  // </group>
410 
411  // Let a set node convert itself to the given unit.
412  virtual void adaptSetUnits (const Unit&);
413 
414 private:
415  // A copy of a TableExprNodeSet cannot be made.
417 
418  // Delete all set elements in itsElems.
419  void deleteElems();
420 
421  // Convert a bounded set to an Array.
422  // <group>
423  Array<Bool> toArrayBool (const TableExprId& id) const;
424  Array<Int64> toArrayInt (const TableExprId& id) const;
425  Array<Double> toArrayDouble (const TableExprId& id) const;
426  Array<DComplex> toArrayDComplex (const TableExprId& id) const;
427  Array<String> toArrayString (const TableExprId& id) const;
428  Array<MVTime> toArrayDate (const TableExprId& id) const;
429  // </group>
430 
431  // Sort and combine intervals.
432  // <group>
433  void combineIntIntervals();
434  void combineDoubleIntervals();
435  void combineDateIntervals();
436  // </group>
437 
438  // Define the functions to find a double, which depend on open/closed-ness.
439  // In this way a test on open/closed is done only once.
440  // <group>
441  typedef Bool (TableExprNodeSet::* FindFuncPtr) (Double value);
442  Bool findOpenOpen (Double value);
443  Bool findOpenClosed (Double value);
444  Bool findClosedOpen (Double value);
445  Bool findClosedClosed (Double value);
446  void setFindFunc (Bool isLeftClosed, Bool isRightClosed);
447  // </group>
448 
452  Bool itsBounded; //# Set is discrete and all starts/ends are defined
453  Bool itsCheckTypes; //# True = checking data types is not needed
454  Bool itsAllIntervals; //# True = all elements are const intervals (sorted)
455  Block<Double> itsStart; //# Start values of const intervals
456  Block<Double> itsEnd; //# End values of const intervals
457  FindFuncPtr itsFindFunc; //# Function to find a matching const interval
458 };
459 
460 
462 {
463  return itsSingle;
464 }
466 {
467  return itsDiscrete;
468 }
470 {
471  return itsBounded;
472 }
474 {
475  return itsElems.nelements();
476 }
477 inline const TableExprNodeSetElem&
479 {
480  return *(itsElems[index]);
481 }
482 
483 
484 
485 
486 } //# NAMESPACE CASACORE - END
487 
488 #endif
A Vector of integers, for indexing into Array<T> objects.
Definition: IPosition.h:119
TableExprNodeRep * itsIncr
Definition: ExprNodeSet.h:222
virtual void getAggrNodes(vector< TableExprNodeRep *> &aggr)
Get the nodes representing an aggregate function.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
virtual Array< String > getArrayString(const TableExprId &id)
void matchDate(Bool *match, const MVTime *value, uInt nval, const TableExprId &id) const
void matchBool(Bool *match, const Bool *value, uInt nval, const TableExprId &id) const
Set a flag in the match output array if the corresponding element in the value array is included in t...
uInt nelements() const
Get the number of elements.
Definition: ExprNodeSet.h:473
Block< Double > itsEnd
Definition: ExprNodeSet.h:456
virtual Array< MVTime > getArrayDate(const TableExprId &id)
Bool isBounded() const
Is the set fully bounded (discrete and no undefined end values)?
Definition: ExprNodeSet.h:469
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:305
Handle class for a table column expression tree.
Definition: ExprNode.h:578
Bool isLeftClosed() const
Is the interval left or right closed?
Definition: ExprNodeSet.h:240
TableExprNodeSetElem * evaluate(const TableExprId &id) const
Evaluate the element for the given row and construct a new (constant) element from it...
void show(ostream &os, uInt indent) const
Show the node.
TableExprNodeRep * start() const
Get the start, end or increment expression.
Definition: ExprNodeSet.h:248
Abstract base class for a node in a table column expression tree.
Definition: ExprNodeRep.h:150
void fillVector(Vector< Bool > &vec, uInt &cnt, const TableExprId &id) const
Fill a vector with the value(s) from this element by appending them at the end of the vector; the end...
void checkTable()
Get the table of a node and check if the children use the same table.
virtual Bool hasDComplex(const TableExprId &id, const DComplex &value)
virtual void adaptSetUnits(const Unit &)
Let a set node convert itself to the given unit.
virtual Bool hasInt(const TableExprId &id, Int64 value)
virtual Array< Double > getArrayDouble(const TableExprId &id)
defines physical units
Definition: Unit.h:189
virtual Array< Bool > hasArrayDouble(const TableExprId &id, const Array< Double > &value)
Class to hold the table expression nodes for an element in a set.
Definition: ExprNodeSet.h:93
double Double
Definition: aipstype.h:52
const TableExprNodeSetElem & operator[](uInt index) const
Get the i-th element.
Definition: ExprNodeSet.h:478
TableExprNodeSetElem(const TableExprNode &node)
Create the object for a single expression node.
LatticeExprNode nelements(const LatticeExprNode &expr)
1-argument function to get the number of elements in a lattice.
virtual Bool hasDate(const TableExprId &id, const MVTime &value)
virtual Bool hasBool(const TableExprId &id, Bool value)
Does a value occur in an array or set? The default implementation tests if it is in an array...
Bool isSingle() const
Is a single value given?
Definition: ExprNodeSet.h:236
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
Block< Double > itsStart
Definition: ExprNodeSet.h:455
virtual Array< Bool > hasArrayDate(const TableExprId &id, const Array< MVTime > &value)
Bool isDiscrete() const
Is it a discrete set element.
Definition: ExprNodeSet.h:232
Bool isDiscrete() const
Contains the set only discrete elements? Discrete means that no continuous ranges are given...
Definition: ExprNodeSet.h:465
void setup(Bool isLeftClosed, const TableExprNode *start, const TableExprNode *end, Bool isRightClosed)
Setup the object for a continuous interval.
const Bool False
Definition: aipstype.h:41
void matchString(Bool *match, const String *value, uInt nval, const TableExprId &id) const
A drop-in replacement for Block<T*>.
Definition: Block.h:861
Bool isSingle() const
Contains the set only single elements? Single means that only single values are given (thus no end no...
Definition: ExprNodeSet.h:461
virtual Array< Bool > hasArrayDComplex(const TableExprId &id, const Array< DComplex > &value)
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:275
TableExprNodeRep * increment() const
Definition: ExprNodeSet.h:256
virtual Bool hasDouble(const TableExprId &id, Double value)
PtrBlock< TableExprNodeSetElem * > itsElems
Definition: ExprNodeSet.h:449
void matchDComplex(Bool *match, const DComplex *value, uInt nval, const TableExprId &id) const
virtual Array< Int64 > getArrayInt(const TableExprId &id)
The identification of a TaQL selection subject.
Definition: TableExprId.h:98
TableExprNodeRep * end() const
Definition: ExprNodeSet.h:252
virtual Array< Bool > hasArrayBool(const TableExprId &id, const Array< Bool > &value)
virtual Array< DComplex > getArrayDComplex(const TableExprId &id)
String: the storage and methods of handling collections of characters.
Definition: String.h:223
virtual void getColumnNodes(vector< TableExprNodeRep *> &cols)
Get the nodes representing a table column.
virtual Array< Bool > hasArrayString(const TableExprId &id, const Array< String > &value)
TableExprNodeRep * itsEnd
Definition: ExprNodeSet.h:221
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:266
virtual Array< Bool > hasArrayInt(const TableExprId &id, const Array< Int64 > &value)
virtual Bool hasString(const TableExprId &id, const String &value)
TableExprNodeSetElem & operator=(const TableExprNodeSetElem &)
A copy of a TableExprNodeSetElem cannot be made.
TableExprNodeRep * itsStart
Definition: ExprNodeSet.h:220
void matchDouble(Bool *match, const Double *value, uInt nval, const TableExprId &id) const
this file contains all the compiler specific defines
Definition: mainpage.dox:28
void matchInt(Bool *match, const Int64 *value, uInt nval, const TableExprId &id) const
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
unsigned int uInt
Definition: aipstype.h:48
virtual Array< Bool > getArrayBool(const TableExprId &id)
Get an array value for this node in the given row.