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  typedef Result result_type;
20 
21  inline virtual ~Functor0() {}
22  virtual Result operator()() const = 0;
23  };
24 
26  template <typename Result, typename Arg1>
27  struct Functor1 {
28  typedef Result result_type;
29  typedef Arg1 first_argument_type;
30 
31  inline virtual ~Functor1() {}
32  virtual Result operator()(const Arg1&) const = 0;
33  };
34 
36  template <typename Result, typename Arg1, typename Arg2>
37  struct Functor2 {
38  typedef Result result_type;
39  typedef Arg1 first_argument_type;
40  typedef Arg2 second_argument_type;
41 
42  inline virtual ~Functor2() {}
43  virtual Result operator()(const Arg1&, const Arg2&) const = 0;
44  };
45 
47  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
48  struct Functor3 {
49  typedef Result result_type;
50  typedef Arg1 first_argument_type;
51  typedef Arg2 second_argument_type;
52  typedef Arg3 third_argument_type;
53 
54  inline virtual ~Functor3() {}
55  virtual Result operator()(const Arg1&, const Arg2&, const Arg3&) const = 0;
56  };
57 
59  template <typename Result>
60  struct Same : public Functor1<Result, Result> {
61  inline Result operator()(const Result& a) const override { return a; }
62  };
63 
65  template <typename Result>
66  struct SameRef : public Functor1<const Result&, Result> {
67  inline const Result& operator()(const Result& a) const { return a; }
68  };
69 
74  template <typename Result>
75  struct DefaultConstructor0 : public Functor0<Result> {
76  inline Result operator()() const { return Result(); }
77  };
78 
83  template <typename Result, typename Arg1>
84  struct DefaultConstructor1 : public Functor1<Result, Arg1> {
85  inline Result operator()(const Arg1&) const { return Result(); }
86  };
87 
92  template <typename Result, typename Arg1, typename Arg2>
93  struct DefaultConstructor2 : public Functor2<Result, Arg1, Arg2> {
94  inline Result operator()(const Arg1&, const Arg2&) const { return Result(); }
95  };
96 
101  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
102  struct DefaultConstructor3 : public Functor3<Result, Arg1, Arg2, Arg3> {
103  inline Result operator()(const Arg1&, const Arg2&, const Arg3&) const { return Result(); }
104  };
105 
110  template <typename Result, typename Arg1, typename CastType>
111  struct CastingCopyConstructor : public Functor1<Result, Arg1> {
112  inline Result operator()(const Arg1& a) const { return Result(static_cast<CastType>(a)); }
113  };
114 
119  template <typename Result>
120  struct FcnFunctor0 : public Functor0<Result> {
121  inline explicit FcnFunctor0(Result (*fcn)()) : fcn_(fcn) {}
122 
123  inline Result operator()() const { return fcn_(); }
124 
125  private:
126  FcnFunctor0() = delete;
127  Result (*fcn_)();
128  };
129 
134  template <typename Result, typename Arg1>
135  struct FcnFunctor1 : public Functor1<Result, Arg1> {
136  inline explicit FcnFunctor1(Result (*fcn)(Arg1)) : fcn_(fcn) {}
137 
138  inline Result operator()(const Arg1& a) const { return fcn_(a); }
139 
140  private:
141  FcnFunctor1() = delete;
142  Result (*fcn_)(Arg1);
143  };
144 
149  template <typename Result, typename Arg1, typename Arg2>
150  struct FcnFunctor2 : public Functor2<Result, Arg1, Arg2> {
151  inline explicit FcnFunctor2(Result (*fcn)(Arg1, Arg2)) : fcn_(fcn) {}
152 
153  inline Result operator()(const Arg1& x, const Arg2& y) const { return fcn_(x, y); }
154 
155  private:
156  FcnFunctor2() = delete;
157  Result (*fcn_)(Arg1, Arg2);
158  };
159 
164  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
165  struct FcnFunctor3 : public Functor3<Result, Arg1, Arg2, Arg3> {
166  inline explicit FcnFunctor3(Result (*fcn)(Arg1, Arg2, Arg3)) : fcn_(fcn) {}
167 
168  inline Result operator()(const Arg1& x, const Arg2& y, const Arg3& z) const { return fcn_(x, y, z); }
169 
170  private:
171  FcnFunctor3() = delete;
172  Result (*fcn_)(Arg1, Arg2, Arg3);
173  };
174 
179  template <class Container, class Result = typename Container::value_type>
180  struct Element1D : public Functor1<Result, Container> {
181  inline explicit Element1D(const unsigned long index) : idx(index) {}
182 
183  inline Result operator()(const Container& c) const { return c[idx]; }
184 
185  private:
186  Element1D() = delete;
187  unsigned long idx;
188  };
189 
194  template <class Container, class Result = typename Container::value_type>
195  struct Element1DAt : public Functor1<Result, Container> {
196  inline explicit Element1DAt(const unsigned long index) : idx(index) {}
197 
198  inline Result operator()(const Container& c) const { return c.at(idx); }
199 
200  private:
201  Element1DAt() = delete;
202  unsigned long idx;
203  };
204 
209  template <typename T1, typename T2>
210  struct assign_left {
211  inline T1& operator()(T1& left, const T2& right) const { return left = right; }
212  };
213 
218  template <typename T1, typename T2>
219  struct assign_right {
220  inline T2& operator()(const T1& left, T2& right) const { return right = left; }
221  };
222 
224  template <typename T1, typename T2>
225  struct pluseq_left {
226  inline T1& operator()(T1& left, const T2& right) const { return left += right; }
227  };
228 
230  template <typename T1, typename T2>
231  struct pluseq_right {
232  inline T2& operator()(const T1& left, T2& right) const { return right += left; }
233  };
234 
239  template <typename T1, typename T2>
240  struct addmul_left {
241  inline explicit addmul_left(const double weight) : w_(weight) {}
242 
243  inline T1& operator()(T1& left, const T2& right) const { return left += w_ * right; }
244 
245  private:
246  addmul_left() = delete;
247  double w_;
248  };
249 
254  template <typename T1, typename T2>
255  struct addmul_right {
256  inline explicit addmul_right(const double weight) : w_(weight) {}
257 
258  inline T1& operator()(T1& left, const T2& right) const { return right += w_ * left; }
259 
260  private:
261  addmul_right() = delete;
262  double w_;
263  };
264 
266  template <typename T1, typename T2>
267  struct minuseq_left {
268  inline T1& operator()(T1& left, const T2& right) const { return left -= right; }
269  };
270 
272  template <typename T1, typename T2>
273  struct minuseq_right {
274  inline T2& operator()(const T1& left, T2& right) const { return right -= left; }
275  };
276 
278  template <typename T1, typename T2>
279  struct multeq_left {
280  inline T1& operator()(T1& left, const T2& right) const { return left *= right; }
281  };
282 
284  template <typename T1, typename T2>
285  struct multeq_right {
286  inline T2& operator()(const T1& left, T2& right) const { return right *= left; }
287  };
288 
290  template <typename T1, typename T2>
291  struct diveq_left {
292  inline T1& operator()(T1& left, const T2& right) const { return left /= right; }
293  };
294 
296  template <typename T1, typename T2>
297  struct diveq_right {
298  inline T2& operator()(const T1& left, T2& right) const { return right /= left; }
299  };
300 
302  template <typename T1, typename T2>
304  inline diveq_left_0by0isC() : C(T1()), leftZero(T1()), rightZero(T2()) {}
305  inline explicit diveq_left_0by0isC(const T1& value) : C(value), leftZero(T1()), rightZero(T2()) {}
306 
307  inline T1& operator()(T1& left, const T2& right) const {
308  if (right == rightZero)
309  if (left == leftZero) {
310  left = C;
311  return left;
312  }
313  return left /= right;
314  }
315 
316  private:
317  T1 C;
320  };
321 
323  template <typename T1, typename T2>
325  inline diveq_right_0by0isC() : C(T2()), leftZero(T1()), rightZero(T2()) {}
326  inline explicit diveq_right_0by0isC(const T2& value) : C(value), leftZero(T1()), rightZero(T2()) {}
327 
328  inline T2& operator()(const T1& left, T2& right) const {
329  if (left == leftZero)
330  if (right == rightZero) {
331  right = C;
332  return right;
333  }
334  return right /= left;
335  }
336 
337  private:
338  T2 C;
341  };
342 
344  template <typename T1, typename T2, typename T3 = T1>
346  inline T1& operator()(T1& left, const T2& right) const { return left = static_cast<T3>(right); }
347  };
348 
350  template <typename T1, typename T2, typename T3 = T2>
352  inline T2& operator()(const T1& left, T2& right) const { return right = static_cast<T3>(left); }
353  };
354 
356  template <typename T1, typename T2, typename T3 = T1>
358  inline T1& operator()(T1& left, const T2& right) const { return left += static_cast<T3>(right); }
359  };
360 
362  template <typename T1, typename T2, typename T3 = T2>
364  inline T2& operator()(const T1& left, T2& right) const { return right += static_cast<T3>(left); }
365  };
366 
368  template <typename T1, typename T2, typename T3 = T1>
370  inline T1& operator()(T1& left, const T2& right) const { return left -= static_cast<T3>(right); }
371  };
372 
374  template <typename T1, typename T2, typename T3 = T2>
376  inline T2& operator()(const T1& left, T2& right) const { return right -= static_cast<T3>(left); }
377  };
378 } // namespace npstat
379 
380 #endif // NPSTAT_SIMPLEFUNCTORS_HH_
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:119
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