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 private:
94 // Explicitly hide base function to prevent warning
96 };
97
98
99 // <summary>
100 // An optimized representation of a discrete selection set.
101 // </summary>
102
103 // <use visibility=local>
104
105 // <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
106 // </reviewed>
107
108 // <prerequisite>
109 //# Classes you should understand before using this one.
110 // <li> TableExprNodeSet
111 // </prerequisite>
112
113 // <synopsis>
114 // This templated class is an optimized representation of an constant
115 // integer or string array set used by the IN operator.
116 // If applicable, TableExprLogicNode instantiates an object of this class.
117 // <br>The representation is a std::unordered_map containing the array values
118 // and the index in the array.
119 // <br>Note that a std::unordered_map is used instead of std::map because its
120 // hashing mechanism makes it faster.
121 // </synopsis>
122
123 template <typename T>
125 {
126 public:
127 // Construct an empty set.
129
130 // Show the node.
131 void show (ostream& os, uInt indent) const override;
132
133 // Where does a value occur in the set? -1 is no match.
134 Int64 find (T value) const override;
135
136 private:
137 std::unordered_map<T,Int64> itsMap;
138 // Explicitly hide base function to prevent warning
140 };
141
142
143 // <summary>
144 // An optimized representation of a selection set with continuous intervals.
145 // </summary>
146
147 // <use visibility=local>
148
149 // <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
150 // </reviewed>
151
152 // <prerequisite>
153 //# Classes you should understand before using this one.
154 // <li> TableExprNodeSet
155 // </prerequisite>
156
157 // <synopsis>
158 // This class is the base class for the optimized representations of a
159 // constant selection set with continuous intervals.
160 // It holds the start and end values of the intervals.
161 // </synopsis>
162
163 template<typename T>
165 {
166 public:
167 // Construct from the original set and the start and end values of the
168 // intervals. The vectors must have the same length.
170 const std::vector<T>& starts,
171 const std::vector<T>& ends);
172 // Get the size (nr of intervals).
173 size_t size() const
174 { return itsStarts.size(); }
175 // Show the node.
176 void show (ostream& os, uInt indent) const override;
177 // Transform a set into an optimized one by ordering the intervals
178 // and optionally combining adjacent intervals.
179 // If not possible, an empty TENShPtr is returned.
181 Bool combine=True);
182 // Create the appropriate optimized OptContSet object.
183 // Note that leftC and rightC do not need to have the same length as start/end.
184 // If it is known that all intervals have the same leftC/rightC,
185 // a single value suffices.
187 const std::vector<T>& start,
188 const std::vector<T>& end,
189 const std::vector<Bool>& leftC,
190 const std::vector<Bool>& rightC);
191 protected:
192 std::vector<T> itsStarts;
193 std::vector<T> itsEnds;
194 };
195
196
197 // <summary>
198 // An optimized representation of a selection set with continuous intervals.
199 // </summary>
200
201 // <use visibility=local>
202
203 // <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
204 // </reviewed>
205
206 // <prerequisite>
207 //# Classes you should understand before using this one.
208 // <li> TableExprNodeSet
209 // </prerequisite>
210
211 // <synopsis>
212 // This class is an optimized representation of a constant selection set
213 // with continuous intervals using a mix of open and closed start and end.
214 // If applicable, TableExprLogicNode instantiates an object of this class.
215 // <br>The representation has std::vector objects containing the start
216 // and end values. A lookup using std::upper_bound on the end values is done
217 // to determine if a value is contained in one of the intervals.
218 // <br>This templated class is instantiated for Double and String.
219 // </synopsis>
220
221 template <typename T>
223 {
224 public:
226 const std::vector<T>& starts,
227 const std::vector<T>& ends,
228 const std::vector<Bool>& leftC,
229 const std::vector<Bool>& rightC);
230 // Show the node.
231 void show (ostream& os, uInt indent) const override;
232 // Tell which interval contains a value. -1 = no match.
233 Int64 find (T value) const override;
234 protected:
235 std::vector<Bool> itsLeftC;
236 std::vector<Bool> itsRightC;
237 private:
238 // Explicitly hide base function to prevent warning
240 };
241
242
243 // <summary>
244 // An optimized representation of a selection set with similar intervals.
245 // </summary>
246
247 // <use visibility=local>
248
249 // <reviewed reviewer="Mordante" date="2022/11/08" tests="tExprNodeSetOpt.cc">
250 // </reviewed>
251
252 // <prerequisite>
253 //# Classes you should understand before using this one.
254 // <li> TableExprNodeSetOptContSet
255 // </prerequisite>
256
257 // <synopsis>
258 // This class is a further optimized version of TableExprNodeSetOptContSet
259 // for continuous intervals all using the same open/closed interval types.
260 // It reduces the number of comparisons required.
261 // The left and right comparison functors tell if an interval side is
262 // open (uses std::less) or closed (uses std::less_equal).
263 // </synopsis>
264
265 template <typename T, typename LeftComp, typename RightComp>
267 {
268 public:
270 const std::vector<T>& starts,
271 const std::vector<T>& ends,
272 LeftComp leftCmp, RightComp rightCmp,
273 const String& cmpType);
274 // Show the node.
275 void show (ostream& os, uInt indent) const override;
276 // Tell which interval contains a value. -1 = no match.
277 Int64 find (T value) const override;
278 private:
279 LeftComp itsLeftCmp;
280 RightComp itsRightCmp;
282 // Explicitly hide base function to prevent warning
284 };
285
286
287} //# NAMESPACE CASACORE - END
288
289#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.
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.
std::unordered_map< T, Int64 > itsMap
Int64 find(T value) const override
Where does a value occur in the set?
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
const Bool True
Definition aipstype.h:41
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:368
double Double
Definition aipstype.h:53