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