CMS 3D CMS Logo

SimpleFunctors.h
Go to the documentation of this file.
1 #ifndef NPSTAT_SIMPLEFUNCTORS_HH_
2 #define NPSTAT_SIMPLEFUNCTORS_HH_
3 
15 namespace npstat {
17  template <typename Result>
18  struct Functor0
19  {
20  typedef Result result_type;
21 
22  inline virtual ~Functor0() {}
23  virtual Result operator()() const = 0;
24  };
25 
27  template <typename Result, typename Arg1>
28  struct Functor1
29  {
30  typedef Result result_type;
31  typedef Arg1 first_argument_type;
32 
33  inline virtual ~Functor1() {}
34  virtual Result operator()(const Arg1&) const = 0;
35  };
36 
38  template <typename Result, typename Arg1, typename Arg2>
39  struct Functor2
40  {
41  typedef Result result_type;
42  typedef Arg1 first_argument_type;
43  typedef Arg2 second_argument_type;
44 
45  inline virtual ~Functor2() {}
46  virtual Result operator()(const Arg1&, const Arg2&) const = 0;
47  };
48 
50  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
51  struct Functor3
52  {
53  typedef Result result_type;
54  typedef Arg1 first_argument_type;
55  typedef Arg2 second_argument_type;
56  typedef Arg3 third_argument_type;
57 
58  inline virtual ~Functor3() {}
59  virtual Result operator()(const Arg1&,const Arg2&,const Arg3&) const=0;
60  };
61 
63  template <typename Result>
64  struct Same : public Functor1<Result, Result>
65  {
66  inline Result operator()(const Result& a) const override {return a;}
67  };
68 
70  template <typename Result>
71  struct SameRef : public Functor1<const Result&, Result>
72  {
73  inline const Result& operator()(const Result& a) const {return a;}
74  };
75 
80  template <typename Result>
81  struct DefaultConstructor0 : public Functor0<Result>
82  {
83  inline Result operator()() const {return Result();}
84  };
85 
90  template <typename Result, typename Arg1>
91  struct DefaultConstructor1 : public Functor1<Result, Arg1>
92  {
93  inline Result operator()(const Arg1&) const {return Result();}
94  };
95 
100  template <typename Result, typename Arg1, typename Arg2>
101  struct DefaultConstructor2 : public Functor2<Result, Arg1, Arg2>
102  {
103  inline Result operator()(const Arg1&, const Arg2&) const
104  {return Result();}
105  };
106 
111  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
112  struct DefaultConstructor3 : public Functor3<Result, Arg1, Arg2, Arg3>
113  {
114  inline Result operator()(const Arg1&, const Arg2&, const Arg3&) const
115  {return Result();}
116  };
117 
122  template <typename Result, typename Arg1, typename CastType>
123  struct CastingCopyConstructor : public Functor1<Result, Arg1>
124  {
125  inline Result operator()(const Arg1& a) const
126  {return Result(static_cast<CastType>(a));}
127  };
128 
133  template <typename Result>
134  struct FcnFunctor0 : public Functor0<Result>
135  {
136  inline explicit FcnFunctor0(Result (*fcn)()) : fcn_(fcn) {}
137 
138  inline Result operator()() const {return fcn_();}
139 
140  private:
141  FcnFunctor0() = delete;
142  Result (*fcn_)();
143  };
144 
149  template <typename Result, typename Arg1>
150  struct FcnFunctor1 : public Functor1<Result, Arg1>
151  {
152  inline explicit FcnFunctor1(Result (*fcn)(Arg1)) : fcn_(fcn) {}
153 
154  inline Result operator()(const Arg1& a) const {return fcn_(a);}
155 
156  private:
157  FcnFunctor1() = delete;
158  Result (*fcn_)(Arg1);
159  };
160 
165  template <typename Result, typename Arg1, typename Arg2>
166  struct FcnFunctor2 : public Functor2<Result, Arg1, Arg2>
167  {
168  inline explicit FcnFunctor2(Result (*fcn)(Arg1, Arg2)) : fcn_(fcn) {}
169 
170  inline Result operator()(const Arg1& x, const Arg2& y) const
171  {return fcn_(x, y);}
172 
173  private:
174  FcnFunctor2() = delete;
175  Result (*fcn_)(Arg1, Arg2);
176  };
177 
182  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
183  struct FcnFunctor3 : public Functor3<Result, Arg1, Arg2, Arg3>
184  {
185  inline explicit FcnFunctor3(Result (*fcn)(Arg1,Arg2,Arg3)):fcn_(fcn) {}
186 
187  inline Result operator()(const Arg1&x,const Arg2&y,const Arg3&z) const
188  {return fcn_(x, y, z);}
189 
190  private:
191  FcnFunctor3() = delete;
192  Result (*fcn_)(Arg1, Arg2, Arg3);
193  };
194 
199  template <class Container, class Result = typename Container::value_type>
200  struct Element1D : public Functor1<Result, Container>
201  {
202  inline explicit Element1D(const unsigned long index) : idx(index) {}
203 
204  inline Result operator()(const Container& c) const {return c[idx];}
205 
206  private:
207  Element1D() = delete;
208  unsigned long idx;
209  };
210 
215  template <class Container, class Result = typename Container::value_type>
216  struct Element1DAt : public Functor1<Result, Container>
217  {
218  inline explicit Element1DAt(const unsigned long index) : idx(index) {}
219 
220  inline Result operator()(const Container& c) const {return c.at(idx);}
221 
222  private:
223  Element1DAt() = delete;
224  unsigned long idx;
225  };
226 
231  template <typename T1, typename T2>
232  struct assign_left
233  {
234  inline T1& operator()(T1& left, const T2& right) const
235  {return left = right;}
236  };
237 
242  template <typename T1, typename T2>
244  {
245  inline T2& operator()(const T1& left, T2& right) const
246  {return right = left;}
247  };
248 
250  template <typename T1, typename T2>
251  struct pluseq_left
252  {
253  inline T1& operator()(T1& left, const T2& right) const
254  {return left += right;}
255  };
256 
258  template <typename T1, typename T2>
260  {
261  inline T2& operator()(const T1& left, T2& right) const
262  {return right += left;}
263  };
264 
269  template <typename T1, typename T2>
270  struct addmul_left
271  {
272  inline explicit addmul_left(const double weight) : w_(weight) {}
273 
274  inline T1& operator()(T1& left, const T2& right) const
275  {return left += w_*right;}
276 
277  private:
278  addmul_left() = delete;
279  double w_;
280  };
281 
286  template <typename T1, typename T2>
288  {
289  inline explicit addmul_right(const double weight) : w_(weight) {}
290 
291  inline T1& operator()(T1& left, const T2& right) const
292  {return right += w_*left;}
293 
294  private:
295  addmul_right() = delete;
296  double w_;
297  };
298 
300  template <typename T1, typename T2>
302  {
303  inline T1& operator()(T1& left, const T2& right) const
304  {return left -= right;}
305  };
306 
308  template <typename T1, typename T2>
310  {
311  inline T2& operator()(const T1& left, T2& right) const
312  {return right -= left;}
313  };
314 
316  template <typename T1, typename T2>
317  struct multeq_left
318  {
319  inline T1& operator()(T1& left, const T2& right) const
320  {return left *= right;}
321  };
322 
324  template <typename T1, typename T2>
326  {
327  inline T2& operator()(const T1& left, T2& right) const
328  {return right *= left;}
329  };
330 
332  template <typename T1, typename T2>
333  struct diveq_left
334  {
335  inline T1& operator()(T1& left, const T2& right) const
336  {return left /= right;}
337  };
338 
340  template <typename T1, typename T2>
341  struct diveq_right
342  {
343  inline T2& operator()(const T1& left, T2& right) const
344  {return right /= left;}
345  };
346 
348  template <typename T1, typename T2>
350  {
351  inline diveq_left_0by0isC() :
352  C(T1()), leftZero(T1()), rightZero(T2()) {}
353  inline explicit diveq_left_0by0isC(const T1& value) :
354  C(value), leftZero(T1()), rightZero(T2()) {}
355 
356  inline T1& operator()(T1& left, const T2& right) const
357  {
358  if (right == rightZero)
359  if (left == leftZero)
360  {
361  left = C;
362  return left;
363  }
364  return left /= right;
365  }
366 
367  private:
368  T1 C;
371  };
372 
374  template <typename T1, typename T2>
376  {
378  C(T2()), leftZero(T1()), rightZero(T2()) {}
379  inline explicit diveq_right_0by0isC(const T2& value) :
380  C(value), leftZero(T1()), rightZero(T2()) {}
381 
382  inline T2& operator()(const T1& left, T2& right) const
383  {
384  if (left == leftZero)
385  if (right == rightZero)
386  {
387  right = C;
388  return right;
389  }
390  return right /= left;
391  }
392 
393  private:
394  T2 C;
397  };
398 
400  template <typename T1, typename T2, typename T3=T1>
402  {
403  inline T1& operator()(T1& left, const T2& right) const
404  {return left = static_cast<T3>(right);}
405  };
406 
408  template <typename T1, typename T2, typename T3=T2>
410  {
411  inline T2& operator()(const T1& left, T2& right) const
412  {return right = static_cast<T3>(left);}
413  };
414 
416  template <typename T1, typename T2, typename T3=T1>
418  {
419  inline T1& operator()(T1& left, const T2& right) const
420  {return left += static_cast<T3>(right);}
421  };
422 
424  template <typename T1, typename T2, typename T3=T2>
426  {
427  inline T2& operator()(const T1& left, T2& right) const
428  {return right += static_cast<T3>(left);}
429  };
430 
432  template <typename T1, typename T2, typename T3=T1>
434  {
435  inline T1& operator()(T1& left, const T2& right) const
436  {return left -= static_cast<T3>(right);}
437  };
438 
440  template <typename T1, typename T2, typename T3=T2>
442  {
443  inline T2& operator()(const T1& left, T2& right) const
444  {return right -= static_cast<T3>(left);}
445  };
446 }
447 
448 #endif // NPSTAT_SIMPLEFUNCTORS_HH_
449 
virtual ~Functor3()
virtual Result operator()() const =0
addmul_left(const double weight)
unsigned long idx
diveq_right_0by0isC(const T2 &value)
T2 & operator()(const T1 &left, T2 &right) const
Result operator()() const
virtual ~Functor0()
T1 & operator()(T1 &left, const T2 &right) const
T2 & operator()(const T1 &left, T2 &right) const
Definition: weight.py:1
Result operator()(const Arg1 &) const
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
T2 & operator()(const T1 &left, T2 &right) const
Element1D(const unsigned long index)
diveq_left_0by0isC(const T1 &value)
Result operator()(const Arg1 &x, const Arg2 &y, const Arg3 &z) const
Result operator()(const Container &c) const
T2 & operator()(const T1 &left, T2 &right) const
Result operator()(const Arg1 &, const Arg2 &, const Arg3 &) const
Result operator()(const Arg1 &x, const Arg2 &y) const
FcnFunctor2(Result(*fcn)(Arg1, Arg2))
Result operator()(const Arg1 &, const Arg2 &) const
Result operator()(const Arg1 &a) const
Result operator()(const Arg1 &a) const
Definition: value.py:1
FcnFunctor3(Result(*fcn)(Arg1, Arg2, Arg3))
T1 & operator()(T1 &left, const T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
FcnFunctor1(Result(*fcn)(Arg1))
T1 & operator()(T1 &left, const T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
void fcn(int &, double *, double &, double *, int)
T1 & operator()(T1 &left, const T2 &right) const
FcnFunctor0(Result(*fcn)())
virtual ~Functor1()
const Result & operator()(const Result &a) const
Result operator()(const Result &a) const override
double a
Definition: hdecay.h:121
T1 & operator()(T1 &left, const T2 &right) const
T2 & operator()(const T1 &left, T2 &right) const
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
addmul_right(const double weight)
edm::AssociationVector< reco::JetRefBaseProd, Values > Container
Element1DAt(const unsigned long index)
virtual ~Functor2()
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
Result operator()(const Container &c) const