casacore
Loading...
Searching...
No Matches
ExprNodeSetOpt.h
Go to the documentation of this file.
1// # ExprNodeSetOpt.h: Classes representing an optimized set in table select expression
2// # Copyright (C) 2022
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_EXPRNODESETOPT_H
27#define TABLES_EXPRNODESETOPT_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/TaQL/ExprNodeRep.h>
32#include <unordered_map>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// Forward Declarations
38
39// <summary>
40// Abstract base class for optimized set representations
41// </summary>
42
43// <use visibility=local>
44
45// <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
46// </reviewed>
47
48// <prerequisite>
49// # Classes you should understand before using this one.
50// <li> TableExprNodeSet
51// </prerequisite>
52
53// <synopsis>
54// This class is the abstract base class for the optimized representation of
55// constant value or interval sets used by the IN operator or join operator.
56//
57// The <src>find</src> function can operate on integer, double and string values.
58// Note that datetimes are handled as doubles. It returns the index of the
59// value or interval matching the value searched for.
60// </synopsis>
61
63 public:
65 // Does the set contain the given value?
66 // They call the <src>find</src> function.
67 // <group>
68 Bool contains(const TableExprId& id, Int64 value) override;
69 Bool contains(const TableExprId& id, Double value) override;
70 Bool contains(const TableExprId& id, String value) override;
71 // </group>
72 // Tell for each array value if the set contains that value.
73 // It calls the scalar <src>contains</src> function for each value.
74 // <group>
78 // </group>
79 // Tell which key matches a value. -1 = no match.
80 // The default implementations throw a 'not implemented' exception.
81 // # The String version is passed by value to use the same mechanism
82 // # as used for the other types to make templates possible.
83 // <group>
84 virtual Int64 find(Int64 value) const;
85 virtual Int64 find(Double value) const;
86 virtual Int64 find(String value) const;
87 // </group>
88 private:
89 // Explicitly hide base function to prevent warning
91};
92
93// <summary>
94// An optimized representation of a discrete selection set.
95// </summary>
96
97// <use visibility=local>
98
99// <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
100// </reviewed>
101
102// <prerequisite>
103// # Classes you should understand before using this one.
104// <li> TableExprNodeSet
105// </prerequisite>
106
107// <synopsis>
108// This templated class is an optimized representation of an constant
109// integer or string array set used by the IN operator.
110// If applicable, TableExprLogicNode instantiates an object of this class.
111// <br>The representation is a std::unordered_map containing the array values
112// and the index in the array.
113// <br>Note that a std::unordered_map is used instead of std::map because its
114// hashing mechanism makes it faster.
115// </synopsis>
116
117template <typename T>
119 public:
120 // Construct an empty set.
122
123 // Show the node.
124 void show(ostream& os, uInt indent) const override;
125
126 // Where does a value occur in the set? -1 is no match.
127 Int64 find(T value) const override;
128
129 private:
130 std::unordered_map<T, Int64> itsMap;
131 // Explicitly hide base function to prevent warning
133};
134
135// <summary>
136// An optimized representation of a selection set with continuous intervals.
137// </summary>
138
139// <use visibility=local>
140
141// <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
142// </reviewed>
143
144// <prerequisite>
145// # Classes you should understand before using this one.
146// <li> TableExprNodeSet
147// </prerequisite>
148
149// <synopsis>
150// This class is the base class for the optimized representations of a
151// constant selection set with continuous intervals.
152// It holds the start and end values of the intervals.
153// </synopsis>
154
155template <typename T>
157 public:
158 // Construct from the original set and the start and end values of the
159 // intervals. The vectors must have the same length.
161 const std::vector<T>& starts, const std::vector<T>& ends);
162 // Get the size (nr of intervals).
163 size_t size() const { return itsStarts.size(); }
164 // Show the node.
165 void show(ostream& os, uInt indent) const override;
166 // Transform a set into an optimized one by ordering the intervals
167 // and optionally combining adjacent intervals.
168 // If not possible, an empty TENShPtr is returned.
169 static TENShPtr transform(const TableExprNodeSet& set, Bool combine = True);
170 // Create the appropriate optimized OptContSet object.
171 // Note that leftC and rightC do not need to have the same length as start/end.
172 // If it is known that all intervals have the same leftC/rightC,
173 // a single value suffices.
174 static TENShPtr createOptSet(const TableExprNodeSet& set, const std::vector<T>& start,
175 const std::vector<T>& end, const std::vector<Bool>& leftC,
176 const std::vector<Bool>& rightC);
177
178 protected:
179 std::vector<T> itsStarts;
180 std::vector<T> itsEnds;
181};
182
183// <summary>
184// An optimized representation of a selection set with continuous intervals.
185// </summary>
186
187// <use visibility=local>
188
189// <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
190// </reviewed>
191
192// <prerequisite>
193// # Classes you should understand before using this one.
194// <li> TableExprNodeSet
195// </prerequisite>
196
197// <synopsis>
198// This class is an optimized representation of a constant selection set
199// with continuous intervals using a mix of open and closed start and end.
200// If applicable, TableExprLogicNode instantiates an object of this class.
201// <br>The representation has std::vector objects containing the start
202// and end values. A lookup using std::upper_bound on the end values is done
203// to determine if a value is contained in one of the intervals.
204// <br>This templated class is instantiated for Double and String.
205// </synopsis>
206
207template <typename T>
209 public:
210 TableExprNodeSetOptContSetMixOC(const TableExprNodeSet& orig, const std::vector<T>& starts,
211 const std::vector<T>& ends, const std::vector<Bool>& leftC,
212 const std::vector<Bool>& rightC);
213 // Show the node.
214 void show(ostream& os, uInt indent) const override;
215 // Tell which interval contains a value. -1 = no match.
216 Int64 find(T value) const override;
217
218 protected:
219 std::vector<Bool> itsLeftC;
220 std::vector<Bool> itsRightC;
221
222 private:
223 // Explicitly hide base function to prevent warning
225};
226
227// <summary>
228// An optimized representation of a selection set with similar intervals.
229// </summary>
230
231// <use visibility=local>
232
233// <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
234// </reviewed>
235
236// <prerequisite>
237// # Classes you should understand before using this one.
238// <li> TableExprNodeSetOptContSet
239// </prerequisite>
240
241// <synopsis>
242// This class is a further optimized version of TableExprNodeSetOptContSet
243// for continuous intervals all using the same open/closed interval types.
244// It reduces the number of comparisons required.
245// The left and right comparison functors tell if an interval side is
246// open (uses std::less) or closed (uses std::less_equal).
247// </synopsis>
248
249template <typename T, typename LeftComp, typename RightComp>
251 public:
252 TableExprNodeSetOptContSet(const TableExprNodeSet& orig, const std::vector<T>& starts,
253 const std::vector<T>& ends, LeftComp leftCmp, RightComp rightCmp,
254 const String& cmpType);
255 // Show the node.
256 void show(ostream& os, uInt indent) const override;
257 // Tell which interval contains a value. -1 = no match.
258 Int64 find(T value) const override;
259
260 private:
261 LeftComp itsLeftCmp;
262 RightComp itsRightCmp;
264 // Explicitly hide base function to prevent warning
266};
267
268} // namespace casacore
269
270#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
Abstract base class for a node in a table column expression tree.
virtual Bool contains(const TableExprId &id, Bool value)
Does a set or array contain the value?
TableExprNodeRep(NodeDataType, ValueType, OperType, ArgType, ExprType, Int ndim, const IPosition &shape)
Construct a node.
TableExprNodeSetOptBase(const TableExprNodeRep &orig)
Bool contains(const TableExprId &id, String value) override
Bool contains(const TableExprId &id, Double value) override
MArray< Bool > contains(const TableExprId &id, const MArray< Double > &value) override
MArray< Bool > contains(const TableExprId &id, const MArray< Int64 > &value) override
Tell for each array value if the set contains that value.
MArray< Bool > contains(const TableExprId &id, const MArray< String > &value) override
Bool contains(const TableExprId &id, Int64 value) override
Does the set contain the given value?
virtual Int64 find(String value) const
virtual Int64 find(Double value) const
virtual Int64 find(Int64 value) const
Tell which key matches a value.
static TENShPtr createOptSet(const TableExprNodeSet &set, const std::vector< T > &start, const std::vector< T > &end, const std::vector< Bool > &leftC, const std::vector< Bool > &rightC)
Create the appropriate optimized OptContSet object.
static TENShPtr transform(const TableExprNodeSet &set, Bool combine=True)
Transform a set into an optimized one by ordering the intervals and optionally combining adjacent int...
TableExprNodeSetOptContSetBase(const TableExprNodeSet &orig, const std::vector< T > &starts, const std::vector< T > &ends)
Construct from the original set and the start and end values of the intervals.
size_t size() const
Get the size (nr of intervals).
void show(ostream &os, uInt indent) const override
Show the node.
TableExprNodeSetOptContSetMixOC(const TableExprNodeSet &orig, const std::vector< T > &starts, const std::vector< T > &ends, const std::vector< Bool > &leftC, const std::vector< Bool > &rightC)
Int64 find(T value) const override
Tell which interval contains a value.
void show(ostream &os, uInt indent) const override
Show the node.
void show(ostream &os, uInt indent) const override
Show the node.
TableExprNodeSetOptContSet(const TableExprNodeSet &orig, const std::vector< T > &starts, const std::vector< T > &ends, LeftComp leftCmp, RightComp rightCmp, const String &cmpType)
Int64 find(T value) const override
Tell which interval contains a value.
Int64 find(T value) const override
Where does a value occur in the set?
std::unordered_map< T, Int64 > itsMap
void show(ostream &os, uInt indent) const override
Show the node.
TableExprNodeSetOptUSet(const TableExprNodeRep &orig, const Array< T > &)
Construct an empty set.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void set(const T &val)
Definition Block.h:541
unsigned int uInt
Definition aipstype.h:49
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
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
iterator end()
Definition Block.h:601