JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
UnaryNumberProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 06/09/2018.
3 //
4 
5 #ifndef JODA_UNARYNUMBERPROVIDER_H
6 #define JODA_UNARYNUMBERPROVIDER_H
7 
8 #include <cmath>
9 #include "IValueProvider.h"
10 
11 namespace joda::query {
12 #define degreesToRadians(angleDegrees) ((angleDegrees)*M_PI / 180.0)
13 #define radiansToDegrees(angleRadians) ((angleRadians)*180.0 / M_PI)
14 
30 template <class Calc>
32  public:
34  std::vector<std::unique_ptr<IValueProvider>> &&parameters)
35  : IValueProvider(std::move(parameters)) {
36  checkParamSize(1);
38  DCHECK(isAtom()) << "Only atom ReturnTypes allowed";
39  };
40 
42  return Calc::retType;
43  }
44 
45  std::string getName() const override { return Calc::name; };
46 
47  std::string toString() const override {
49  }
50 
51  std::unique_ptr<joda::query::IValueProvider> duplicate() const override {
52  return std::make_unique<UnaryNumberProvider<Calc>>(duplicateParameters());
53  };
54 
55  bool isConst() const override {
56  bool c = true;
57  for (const auto &param : params) {
58  c &= param->isConst();
59  }
60  return c;
61  }
62 
64  RJMemoryPoolAlloc &alloc) const override {
65  if (params[0]->isAtom()) {
66  auto val = params[0]->getAtomValue(json, alloc);
67  if (!val.IsNumber()) return RJValue();
68  if (val.IsUint64()) return Calc::calculate(val.GetUint64(), alloc);
69  if (val.IsInt64()) return Calc::calculate(val.GetInt64(), alloc);
70  return Calc::calculate(val.GetDouble(), alloc);
71  } else {
72  auto val = params[0]->getValue(json, alloc);
73  if (val == nullptr || !val->IsNumber()) return RJValue();
74  if (val->IsUint64()) return Calc::calculate(val->GetUint64(), alloc);
75  if (val->IsInt64()) return Calc::calculate(val->GetInt64(), alloc);
76  return Calc::calculate(val->GetDouble(), alloc);
77  }
78  };
79 
80  const RJValue *getValue(const RapidJsonDocument &json,
81  RJMemoryPoolAlloc &alloc) const override {
82  DCHECK(!isAtom()) << "Did not check for atom first";
83  return nullptr;
84  };
85 
87 
88  protected:
90  for (unsigned int i = 0; i < params.size(); ++i) {
92  }
93  }
94 };
95 
96 /*
97  * ABS
98  */
100  static constexpr auto name = "ABS";
102 
103  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
104  return RJValue(std::abs(val));
105  };
106 
107  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
108  return RJValue(val);
109  };
110 
111  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
112  return RJValue(std::abs(val));
113  };
114 };
115 
117 
118 /*
119  * ROUND
120  */
122  static constexpr auto name = "ROUND";
124 
125  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
126  return RJValue(std::round(val));
127  };
128 
129  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
130  return RJValue(val);
131  };
132 
133  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
134  return RJValue(val);
135  };
136 };
137 
139 
140 /*
141  * TRUNC
142  */
144  static constexpr auto name = "TRUNC";
146 
147  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
148  return RJValue(std::trunc(val));
149  };
150 
151  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
152  return RJValue(val);
153  };
154 
155  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
156  return RJValue(val);
157  };
158 };
159 
161 
162 /*
163  * SQRT
164  */
166  static constexpr auto name = "SQRT";
168 
169  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
170  if (val < 0) return RJValue();
171  return RJValue(std::sqrt(val));
172  };
173 
174  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
175  if (val < 0) return RJValue();
176  return RJValue(std::sqrt(val));
177  };
178 
179  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
180  if (val < 0) return RJValue();
181  return RJValue(std::sqrt(val));
182  };
183 };
184 
186 
187 /*
188  * CEIL
189  */
191  static constexpr auto name = "CEIL";
193 
194  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
195  return RJValue(std::ceil(val));
196  };
197 
198  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
199  return RJValue(val);
200  };
201 
202  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
203  return RJValue(val);
204  };
205 };
206 
208 
209 /*
210  * FLOOR
211  */
213  static constexpr auto name = "FLOOR";
215 
216  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
217  return RJValue(std::floor(val));
218  };
219 
220  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
221  return RJValue(val);
222  };
223 
224  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
225  return RJValue(val);
226  };
227 };
228 
230 
231 /*
232  * DEGREES
233  */
235  static constexpr auto name = "DEGREES";
237 
238  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
239  return RJValue(radiansToDegrees(val));
240  };
241 
242  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
243  return RJValue(radiansToDegrees(val));
244  };
245 
246  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
247  return RJValue(radiansToDegrees(val));
248  };
249 };
250 
252 
253 /*
254  * RADIANS
255  */
257  static constexpr auto name = "RADIANS";
259 
260  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
261  return RJValue(degreesToRadians(val));
262  };
263 
264  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
265  return RJValue(degreesToRadians(val));
266  };
267 
268  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
269  return RJValue(degreesToRadians(val));
270  };
271 };
272 
274 
275 /*
276  * ACOS
277  */
279  static constexpr auto name = "ACOS";
281 
282  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
283  if (val < -1 || val > 1) return RJValue();
284  return RJValue(std::acos(val));
285  };
286 
287  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
288  if (val > 1) return RJValue();
289  return RJValue(std::acos(val));
290  };
291 
292  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
293  if (val < -1 || val > 1) return RJValue();
294  return RJValue(std::acos(val));
295  };
296 };
297 
299 
300 /*
301  * ASIN
302  */
304  static constexpr auto name = "ASIN";
306 
307  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
308  if (val < -1 || val > 1) return RJValue();
309  return RJValue(std::asin(val));
310  };
311 
312  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
313  if (val > 1) return RJValue();
314  return RJValue(std::asin(val));
315  };
316 
317  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
318  if (val < -1 || val > 1) return RJValue();
319  return RJValue(std::asin(val));
320  };
321 };
322 
324 
325 /*
326  * ATAN
327  */
329  static constexpr auto name = "ATAN";
331 
332  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
333  return RJValue(std::atan(val));
334  };
335 
336  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
337  return RJValue(std::atan(val));
338  };
339 
340  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
341  return RJValue(std::atan(val));
342  };
343 };
344 
346 
347 /*
348  * COS
349  */
351  static constexpr auto name = "COS";
353 
354  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
355  return RJValue(std::cos(val));
356  };
357 
358  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
359  return RJValue(std::cos(val));
360  };
361 
362  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
363  return RJValue(std::cos(val));
364  };
365 };
366 
368 
369 /*
370  * SIN
371  */
373  static constexpr auto name = "SIN";
375 
376  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
377  return RJValue(std::sin(val));
378  };
379 
380  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
381  return RJValue(std::sin(val));
382  };
383 
384  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
385  return RJValue(std::sin(val));
386  };
387 };
388 
390 
391 /*
392  * TAN
393  */
395  static constexpr auto name = "TAN";
397 
398  inline static RJValue calculate(double val, RJMemoryPoolAlloc &alloc) {
399  return RJValue(std::tan(val));
400  };
401 
402  inline static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc) {
403  return RJValue(std::tan(val));
404  };
405 
406  inline static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc) {
407  return RJValue(std::tan(val));
408  };
409 };
410 
412 
414 
416 
418 
420 
422 
424 
426 
428 
430 
432 
434 
436 
438 
440 } // namespace joda::query
441 #endif // JODA_UNARYNUMBERPROVIDER_H
#define CREATE_FACTORY(FCLASS)
Definition: IValueProvider.h:20
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
#define radiansToDegrees(angleRadians)
Definition: UnaryNumberProvider.h:13
#define degreesToRadians(angleDegrees)
Definition: UnaryNumberProvider.h:12
Definition: RapidJsonDocument.h:22
Definition: IValueProvider.h:143
std::vector< std::unique_ptr< IValueProvider > > params
Definition: IValueProvider.h:373
void checkParamSize(unsigned int expected)
Definition: IValueProvider.cpp:112
virtual bool isAtom() const
Definition: IValueProvider.h:215
std::vector< std::unique_ptr< IValueProvider > > duplicateParameters() const
Definition: IValueProvider.cpp:104
void checkParamType(unsigned int i, IValueType expected)
Definition: IValueProvider.cpp:125
virtual std::string toString() const
Definition: IValueProvider.cpp:99
Definition: UnaryNumberProvider.h:31
UnaryNumberProvider(std::vector< std::unique_ptr< IValueProvider >> &&parameters)
Definition: UnaryNumberProvider.h:33
std::string getName() const override
Definition: UnaryNumberProvider.h:45
bool isConst() const override
Definition: UnaryNumberProvider.h:55
std::string toString() const override
Definition: UnaryNumberProvider.h:47
joda::query::IValueType getReturnType() const override
Definition: UnaryNumberProvider.h:41
std::unique_ptr< joda::query::IValueProvider > duplicate() const override
Definition: UnaryNumberProvider.h:51
void checkAllParamTypes()
Definition: UnaryNumberProvider.h:89
const RJValue * getValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: UnaryNumberProvider.h:80
RJValue getAtomValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: UnaryNumberProvider.h:63
Definition: AttributeStatAggregator.h:12
UnaryNumberProvider< UnaryAcosCalculationFunction > AcosProvider
Definition: UnaryNumberProvider.h:298
UnaryNumberProvider< UnaryCeilCalculationFunction > CeilProvider
Definition: UnaryNumberProvider.h:207
UnaryNumberProvider< UnarySqrtCalculationFunction > SqrtProvider
Definition: UnaryNumberProvider.h:185
UnaryNumberProvider< UnaryTanCalculationFunction > TanProvider
Definition: UnaryNumberProvider.h:411
UnaryNumberProvider< UnaryAsinCalculationFunction > AsinProvider
Definition: UnaryNumberProvider.h:323
UnaryNumberProvider< UnaryDegreesCalculationFunction > DegreesProvider
Definition: UnaryNumberProvider.h:251
UnaryNumberProvider< UnaryAbsCalculationFunction > AbsProvider
Definition: UnaryNumberProvider.h:116
UnaryNumberProvider< UnaryFloorCalculationFunction > FloorProvider
Definition: UnaryNumberProvider.h:229
UnaryNumberProvider< UnaryRadiansCalculationFunction > RadiansProvider
Definition: UnaryNumberProvider.h:273
UnaryNumberProvider< UnaryAtanCalculationFunction > AtanProvider
Definition: UnaryNumberProvider.h:345
UnaryNumberProvider< UnaryRoundCalculationFunction > RoundProvider
Definition: UnaryNumberProvider.h:138
UnaryNumberProvider< UnaryTruncCalculationFunction > TruncProvider
Definition: UnaryNumberProvider.h:160
UnaryNumberProvider< UnarySinCalculationFunction > SinProvider
Definition: UnaryNumberProvider.h:389
UnaryNumberProvider< UnaryCosCalculationFunction > CosProvider
Definition: UnaryNumberProvider.h:367
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
Definition: UnaryNumberProvider.h:99
static constexpr auto name
Definition: UnaryNumberProvider.h:100
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:101
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:103
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:111
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:107
Definition: UnaryNumberProvider.h:278
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:280
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:282
static constexpr auto name
Definition: UnaryNumberProvider.h:279
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:292
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:287
Definition: UnaryNumberProvider.h:303
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:307
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:305
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:317
static constexpr auto name
Definition: UnaryNumberProvider.h:304
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:312
Definition: UnaryNumberProvider.h:328
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:340
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:336
static constexpr auto name
Definition: UnaryNumberProvider.h:329
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:332
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:330
Definition: UnaryNumberProvider.h:190
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:192
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:194
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:198
static constexpr auto name
Definition: UnaryNumberProvider.h:191
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:202
Definition: UnaryNumberProvider.h:350
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:358
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:352
static constexpr auto name
Definition: UnaryNumberProvider.h:351
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:362
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:354
Definition: UnaryNumberProvider.h:234
static constexpr auto name
Definition: UnaryNumberProvider.h:235
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:238
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:246
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:236
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:242
Definition: UnaryNumberProvider.h:212
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:214
static constexpr auto name
Definition: UnaryNumberProvider.h:213
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:224
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:216
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:220
Definition: UnaryNumberProvider.h:256
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:268
static constexpr auto name
Definition: UnaryNumberProvider.h:257
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:258
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:264
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:260
Definition: UnaryNumberProvider.h:121
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:125
static constexpr auto name
Definition: UnaryNumberProvider.h:122
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:133
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:123
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:129
Definition: UnaryNumberProvider.h:372
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:384
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:380
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:376
static constexpr auto name
Definition: UnaryNumberProvider.h:373
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:374
Definition: UnaryNumberProvider.h:165
static constexpr auto name
Definition: UnaryNumberProvider.h:166
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:167
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:169
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:179
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:174
Definition: UnaryNumberProvider.h:394
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:396
static constexpr auto name
Definition: UnaryNumberProvider.h:395
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:402
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:406
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:398
Definition: UnaryNumberProvider.h:143
static constexpr joda::query::IValueType retType
Definition: UnaryNumberProvider.h:145
static RJValue calculate(int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:155
static RJValue calculate(double val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:147
static constexpr auto name
Definition: UnaryNumberProvider.h:144
static RJValue calculate(u_int64_t val, RJMemoryPoolAlloc &alloc)
Definition: UnaryNumberProvider.h:151