JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
BinaryNumberProvider.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 06/09/2018.
3 //
4 
5 #ifndef JODA_BINARYNUMBERPROVIDER_H
6 #define JODA_BINARYNUMBERPROVIDER_H
7 
8 #include <cmath>
9 
10 #include "IValueProvider.h"
11 
12 #define JODA_MATH_FUNC_NOOP "NOOP"
13 
14 namespace joda::query {
32 template <class Calc>
34  public:
36  std::vector<std::unique_ptr<IValueProvider>> &&parameters,
37  bool opMode = false)
38  : IValueProvider(std::move(parameters)), opMode(opMode) {
39  checkParamSize(2);
41  DCHECK(isAtom()) << "Only atom ReturnTypes allowed";
42  };
43 
45  return Calc::retType;
46  }
47 
48  std::string getName() const override {
49  if (opMode) {
50  DCHECK_STRNE(Calc::op, JODA_MATH_FUNC_NOOP) << "No operator assigned";
51  return Calc::op;
52  } else {
53  return Calc::name;
54  }
55  };
56 
57  std::string toString() const override {
58  if (!opMode)
59  return IValueProvider::toString();
60  else {
61  DCHECK_STRNE(Calc::op, JODA_MATH_FUNC_NOOP) << "No operator assigned";
62  return params[0]->toString() + " " + Calc::op + " " +
63  params[1]->toString();
64  }
65  }
66 
67  std::unique_ptr<joda::query::IValueProvider> duplicate() const override {
68  return std::make_unique<BinaryNumberProvider<Calc>>(duplicateParameters(),
69  opMode);
70  };
71 
72  bool isConst() const override {
73  bool c = true;
74  for (const auto &param : params) {
75  c &= param->isConst();
76  }
77  return c;
78  }
79 
81  RJMemoryPoolAlloc &alloc) const override {
82  const RJValue *lhs;
83  RJValue tmplhs;
84  const RJValue *rhs;
85  RJValue tmprhs;
86  // Get Pointer to value
87  if (params[0]->isAtom()) {
88  tmplhs = params[0]->getAtomValue(json, alloc);
89  lhs = &tmplhs;
90  } else {
91  lhs = params[0]->getValue(json, alloc);
92  }
93  if (lhs == nullptr || !lhs->IsNumber()) return RJValue();
94  // Get Pointer to List
95  if (params[1]->isAtom()) {
96  tmprhs = params[1]->getAtomValue(json, alloc);
97  rhs = &tmprhs;
98  } else {
99  rhs = params[1]->getValue(json, alloc);
100  }
101  if (rhs == nullptr || !rhs->IsNumber()) return RJValue();
102 
103  if (lhs->IsUint64() && rhs->IsUint64())
104  return Calc::calculate(lhs->GetUint64(), rhs->GetUint64(), alloc);
105  if (lhs->IsInt64() && rhs->IsInt64())
106  return Calc::calculate(lhs->GetInt64(), rhs->GetInt64(), alloc);
107  return Calc::calculate(lhs->GetDouble(), rhs->GetDouble(), alloc);
108  };
109 
110  const RJValue *getValue(const RapidJsonDocument &json,
111  RJMemoryPoolAlloc &alloc) const override {
112  DCHECK(!isAtom()) << "Did not check for atom first";
113  return nullptr;
114  };
115 
117 
118  protected:
119  bool opMode;
120 
122  for (unsigned int i = 0; i < params.size(); ++i) {
124  }
125  }
126 };
127 
128 /*
129  * Sum (+)
130  */
132  static constexpr auto name = "SUM";
133  static constexpr auto op = "+";
135 
136  inline static RJValue calculate(double lhs, double rhs,
137  RJMemoryPoolAlloc &alloc) {
138  return RJValue(lhs + rhs);
139  };
140 
141  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
142  RJMemoryPoolAlloc &alloc) {
143  return RJValue(lhs + rhs);
144  };
145 
146  inline static RJValue calculate(int64_t lhs, int64_t rhs,
147  RJMemoryPoolAlloc &alloc) {
148  return RJValue(lhs + rhs);
149  };
150 };
151 
153 
154 /*
155  * Sub (-)
156  */
158  static constexpr auto name = "SUB";
159  static constexpr auto op = "-";
161 
162  inline static RJValue calculate(double lhs, double rhs,
163  RJMemoryPoolAlloc &alloc) {
164  return RJValue(lhs - rhs);
165  };
166 
167  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
168  RJMemoryPoolAlloc &alloc) {
169  if (rhs > lhs) { // Check for underflow
170  return RJValue(int64_t(lhs - rhs));
171  }
172  return RJValue(lhs - rhs);
173  };
174 
175  inline static RJValue calculate(int64_t lhs, int64_t rhs,
176  RJMemoryPoolAlloc &alloc) {
177  return RJValue(lhs - rhs);
178  };
179 };
180 
182 
183 /*
184  * Div (/)
185  */
187  static constexpr auto name = "DIV";
188  static constexpr auto op = "/";
190 
191  inline static RJValue calculate(double lhs, double rhs,
192  RJMemoryPoolAlloc &alloc) {
193  if (rhs == 0) return RJValue();
194  return RJValue(lhs / rhs);
195  };
196 
197  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
198  RJMemoryPoolAlloc &alloc) {
199  if (rhs == 0) return RJValue();
200  return RJValue(lhs / rhs);
201  };
202 
203  inline static RJValue calculate(int64_t lhs, int64_t rhs,
204  RJMemoryPoolAlloc &alloc) {
205  if (rhs == 0) return RJValue();
206  return RJValue(lhs / rhs);
207  };
208 };
209 
211 
212 /*
213  * Product (/)
214  */
216  static constexpr auto name = "PROD";
217  static constexpr auto op = "*";
219 
220  inline static RJValue calculate(double lhs, double rhs,
221  RJMemoryPoolAlloc &alloc) {
222  return RJValue(lhs * rhs);
223  };
224 
225  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
226  RJMemoryPoolAlloc &alloc) {
227  return RJValue(lhs * rhs);
228  };
229 
230  inline static RJValue calculate(int64_t lhs, int64_t rhs,
231  RJMemoryPoolAlloc &alloc) {
232  return RJValue(lhs * rhs);
233  };
234 };
235 
237 
238 /*
239  * Modulo (%)
240  */
242  static constexpr auto name = "MOD";
243  static constexpr auto op = "%";
245 
246  inline static RJValue calculate(double lhs, double rhs,
247  RJMemoryPoolAlloc &alloc) {
248  if (rhs == 0) return RJValue();
249  return RJValue(std::fmod(lhs, rhs));
250  };
251 
252  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
253  RJMemoryPoolAlloc &alloc) {
254  if (rhs == 0) return RJValue();
255  return RJValue(lhs % rhs);
256  };
257 
258  inline static RJValue calculate(int64_t lhs, int64_t rhs,
259  RJMemoryPoolAlloc &alloc) {
260  if (rhs == 0) return RJValue();
261  return RJValue(lhs % rhs);
262  };
263 };
264 
266 
267 /*
268  * Power
269  */
271  static constexpr auto name = "POW";
272  static constexpr auto op = JODA_MATH_FUNC_NOOP;
274 
275  inline static RJValue calculate(double lhs, double rhs,
276  RJMemoryPoolAlloc &alloc) {
277  double const res = std::pow(lhs, rhs);
278  if (!std::isfinite(res)) return RJValue();
279  return RJValue(res);
280  };
281 
282  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
283  RJMemoryPoolAlloc &alloc) {
284  double const res = std::pow(lhs, rhs);
285  if (!std::isfinite(res)) return RJValue();
286  return RJValue(res);
287  };
288 
289  inline static RJValue calculate(int64_t lhs, int64_t rhs,
290  RJMemoryPoolAlloc &alloc) {
291  double const res = std::pow(lhs, rhs);
292  if (!std::isfinite(res)) return RJValue();
293  return RJValue(res);
294  };
295 };
296 
298 
299 /*
300  * Atan2
301  */
303  static constexpr auto name = "ATAN2";
304  static constexpr auto op = JODA_MATH_FUNC_NOOP;
306 
307  inline static RJValue calculate(double lhs, double rhs,
308  RJMemoryPoolAlloc &alloc) {
309  return RJValue(std::atan2(lhs, rhs));
310  };
311 
312  inline static RJValue calculate(u_int64_t lhs, u_int64_t rhs,
313  RJMemoryPoolAlloc &alloc) {
314  return RJValue(std::atan2(lhs, rhs));
315  };
316 
317  inline static RJValue calculate(int64_t lhs, int64_t rhs,
318  RJMemoryPoolAlloc &alloc) {
319  return RJValue(std::atan2(lhs, rhs));
320  };
321 };
322 
324 
326 
328 
330 
332 
334 
336 
338 
339 } // namespace joda::query
340 #endif // JODA_BINARYNUMBERPROVIDER_H
#define JODA_MATH_FUNC_NOOP
Definition: BinaryNumberProvider.h:12
#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
Definition: RapidJsonDocument.h:22
Definition: BinaryNumberProvider.h:33
bool isConst() const override
Definition: BinaryNumberProvider.h:72
const RJValue * getValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: BinaryNumberProvider.h:110
RJValue getAtomValue(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc) const override
Definition: BinaryNumberProvider.h:80
std::string toString() const override
Definition: BinaryNumberProvider.h:57
joda::query::IValueType getReturnType() const override
Definition: BinaryNumberProvider.h:44
BinaryNumberProvider(std::vector< std::unique_ptr< IValueProvider >> &&parameters, bool opMode=false)
Definition: BinaryNumberProvider.h:35
std::string getName() const override
Definition: BinaryNumberProvider.h:48
void checkAllParamTypes()
Definition: BinaryNumberProvider.h:121
bool opMode
Definition: BinaryNumberProvider.h:114
std::unique_ptr< joda::query::IValueProvider > duplicate() const override
Definition: BinaryNumberProvider.h:67
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: AttributeStatAggregator.h:12
BinaryNumberProvider< BinaryModCalculationFunction > ModuloProvider
Definition: BinaryNumberProvider.h:265
BinaryNumberProvider< BinaryAtan2CalculationFunction > Atan2Provider
Definition: BinaryNumberProvider.h:323
BinaryNumberProvider< BinarySumCalculationFunction > SumProvider
Definition: BinaryNumberProvider.h:152
BinaryNumberProvider< BinaryDivCalculationFunction > DivProvider
Definition: BinaryNumberProvider.h:210
BinaryNumberProvider< BinarySubCalculationFunction > SubtractProvider
Definition: BinaryNumberProvider.h:181
BinaryNumberProvider< BinaryProdCalculationFunction > ProductProvider
Definition: BinaryNumberProvider.h:236
BinaryNumberProvider< BinaryPowCalculationFunction > PowerProvider
Definition: BinaryNumberProvider.h:297
IValueType
Definition: IValueProvider.h:33
@ IV_Number
Definition: IValueProvider.h:35
Definition: BinaryNumberProvider.h:302
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:317
static constexpr auto op
Definition: BinaryNumberProvider.h:304
static constexpr auto name
Definition: BinaryNumberProvider.h:303
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:305
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:312
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:307
Definition: BinaryNumberProvider.h:186
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:189
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:191
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:203
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:197
static constexpr auto op
Definition: BinaryNumberProvider.h:188
static constexpr auto name
Definition: BinaryNumberProvider.h:187
Definition: BinaryNumberProvider.h:241
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:244
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:258
static constexpr auto name
Definition: BinaryNumberProvider.h:242
static constexpr auto op
Definition: BinaryNumberProvider.h:243
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:246
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:252
Definition: BinaryNumberProvider.h:270
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:282
static constexpr auto op
Definition: BinaryNumberProvider.h:272
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:289
static constexpr auto name
Definition: BinaryNumberProvider.h:271
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:273
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:275
Definition: BinaryNumberProvider.h:215
static constexpr auto name
Definition: BinaryNumberProvider.h:216
static constexpr auto op
Definition: BinaryNumberProvider.h:217
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:230
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:220
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:225
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:218
Definition: BinaryNumberProvider.h:157
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:175
static constexpr auto op
Definition: BinaryNumberProvider.h:159
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:160
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:162
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:167
static constexpr auto name
Definition: BinaryNumberProvider.h:158
Definition: BinaryNumberProvider.h:131
static constexpr auto op
Definition: BinaryNumberProvider.h:133
static constexpr joda::query::IValueType retType
Definition: BinaryNumberProvider.h:134
static constexpr auto name
Definition: BinaryNumberProvider.h:132
static RJValue calculate(int64_t lhs, int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:146
static RJValue calculate(u_int64_t lhs, u_int64_t rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:141
static RJValue calculate(double lhs, double rhs, RJMemoryPoolAlloc &alloc)
Definition: BinaryNumberProvider.h:136