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  FcnFunctor0() = delete;
126 
127  private:
128  Result (*fcn_)();
129  };
130 
135  template <typename Result, typename Arg1>
136  struct FcnFunctor1 : public Functor1<Result, Arg1> {
137  inline explicit FcnFunctor1(Result (*fcn)(Arg1)) : fcn_(fcn) {}
138 
139  inline Result operator()(const Arg1& a) const { return fcn_(a); }
140 
141  FcnFunctor1() = delete;
142 
143  private:
144  Result (*fcn_)(Arg1);
145  };
146 
151  template <typename Result, typename Arg1, typename Arg2>
152  struct FcnFunctor2 : public Functor2<Result, Arg1, Arg2> {
153  inline explicit FcnFunctor2(Result (*fcn)(Arg1, Arg2)) : fcn_(fcn) {}
154 
155  inline Result operator()(const Arg1& x, const Arg2& y) const { return fcn_(x, y); }
156 
157  FcnFunctor2() = delete;
158 
159  private:
160  Result (*fcn_)(Arg1, Arg2);
161  };
162 
167  template <typename Result, typename Arg1, typename Arg2, typename Arg3>
168  struct FcnFunctor3 : public Functor3<Result, Arg1, Arg2, Arg3> {
169  inline explicit FcnFunctor3(Result (*fcn)(Arg1, Arg2, Arg3)) : fcn_(fcn) {}
170 
171  inline Result operator()(const Arg1& x, const Arg2& y, const Arg3& z) const { return fcn_(x, y, z); }
172 
173  FcnFunctor3() = delete;
174 
175  private:
176  Result (*fcn_)(Arg1, Arg2, Arg3);
177  };
178 
183  template <class Container, class Result = typename Container::value_type>
184  struct Element1D : public Functor1<Result, Container> {
185  inline explicit Element1D(const unsigned long index) : idx(index) {}
186 
187  inline Result operator()(const Container& c) const { return c[idx]; }
188 
189  Element1D() = delete;
190 
191  private:
192  unsigned long idx;
193  };
194 
199  template <class Container, class Result = typename Container::value_type>
200  struct Element1DAt : public Functor1<Result, Container> {
201  inline explicit Element1DAt(const unsigned long index) : idx(index) {}
202 
203  inline Result operator()(const Container& c) const { return c.at(idx); }
204 
205  Element1DAt() = delete;
206 
207  private:
208  unsigned long idx;
209  };
210 
215  template <typename T1, typename T2>
216  struct assign_left {
217  inline T1& operator()(T1& left, const T2& right) const { return left = right; }
218  };
219 
224  template <typename T1, typename T2>
225  struct assign_right {
226  inline T2& operator()(const T1& left, T2& right) const { return right = left; }
227  };
228 
230  template <typename T1, typename T2>
231  struct pluseq_left {
232  inline T1& operator()(T1& left, const T2& right) const { return left += right; }
233  };
234 
236  template <typename T1, typename T2>
237  struct pluseq_right {
238  inline T2& operator()(const T1& left, T2& right) const { return right += left; }
239  };
240 
245  template <typename T1, typename T2>
246  struct addmul_left {
247  inline explicit addmul_left(const double weight) : w_(weight) {}
248 
249  inline T1& operator()(T1& left, const T2& right) const { return left += w_ * right; }
250 
251  addmul_left() = delete;
252 
253  private:
254  double w_;
255  };
256 
261  template <typename T1, typename T2>
262  struct addmul_right {
263  inline explicit addmul_right(const double weight) : w_(weight) {}
264 
265  inline T1& operator()(T1& left, const T2& right) const { return right += w_ * left; }
266 
267  addmul_right() = delete;
268 
269  private:
270  double w_;
271  };
272 
274  template <typename T1, typename T2>
275  struct minuseq_left {
276  inline T1& operator()(T1& left, const T2& right) const { return left -= right; }
277  };
278 
280  template <typename T1, typename T2>
281  struct minuseq_right {
282  inline T2& operator()(const T1& left, T2& right) const { return right -= left; }
283  };
284 
286  template <typename T1, typename T2>
287  struct multeq_left {
288  inline T1& operator()(T1& left, const T2& right) const { return left *= right; }
289  };
290 
292  template <typename T1, typename T2>
293  struct multeq_right {
294  inline T2& operator()(const T1& left, T2& right) const { return right *= left; }
295  };
296 
298  template <typename T1, typename T2>
299  struct diveq_left {
300  inline T1& operator()(T1& left, const T2& right) const { return left /= right; }
301  };
302 
304  template <typename T1, typename T2>
305  struct diveq_right {
306  inline T2& operator()(const T1& left, T2& right) const { return right /= left; }
307  };
308 
310  template <typename T1, typename T2>
312  inline diveq_left_0by0isC() : C(T1()), leftZero(T1()), rightZero(T2()) {}
313  inline explicit diveq_left_0by0isC(const T1& value) : C(value), leftZero(T1()), rightZero(T2()) {}
314 
315  inline T1& operator()(T1& left, const T2& right) const {
316  if (right == rightZero)
317  if (left == leftZero) {
318  left = C;
319  return left;
320  }
321  return left /= right;
322  }
323 
324  private:
325  T1 C;
328  };
329 
331  template <typename T1, typename T2>
333  inline diveq_right_0by0isC() : C(T2()), leftZero(T1()), rightZero(T2()) {}
334  inline explicit diveq_right_0by0isC(const T2& value) : C(value), leftZero(T1()), rightZero(T2()) {}
335 
336  inline T2& operator()(const T1& left, T2& right) const {
337  if (left == leftZero)
338  if (right == rightZero) {
339  right = C;
340  return right;
341  }
342  return right /= left;
343  }
344 
345  private:
346  T2 C;
349  };
350 
352  template <typename T1, typename T2, typename T3 = T1>
354  inline T1& operator()(T1& left, const T2& right) const { return left = static_cast<T3>(right); }
355  };
356 
358  template <typename T1, typename T2, typename T3 = T2>
360  inline T2& operator()(const T1& left, T2& right) const { return right = static_cast<T3>(left); }
361  };
362 
364  template <typename T1, typename T2, typename T3 = T1>
366  inline T1& operator()(T1& left, const T2& right) const { return left += static_cast<T3>(right); }
367  };
368 
370  template <typename T1, typename T2, typename T3 = T2>
372  inline T2& operator()(const T1& left, T2& right) const { return right += static_cast<T3>(left); }
373  };
374 
376  template <typename T1, typename T2, typename T3 = T1>
378  inline T1& operator()(T1& left, const T2& right) const { return left -= static_cast<T3>(right); }
379  };
380 
382  template <typename T1, typename T2, typename T3 = T2>
384  inline T2& operator()(const T1& left, T2& right) const { return right -= static_cast<T3>(left); }
385  };
386 } // namespace npstat
387 
388 #endif // NPSTAT_SIMPLEFUNCTORS_HH_
T1 & operator()(T1 &left, const T2 &right) const
Result operator()(const Container &c) const
virtual ~Functor3()
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
addmul_left(const double weight)
unsigned long idx
virtual Result operator()(const Arg1 &, const Arg2 &, const Arg3 &) const =0
diveq_right_0by0isC(const T2 &value)
virtual Result operator()(const Arg1 &, const Arg2 &) const =0
T2 & operator()(const T1 &left, T2 &right) const
virtual ~Functor0()
Definition: weight.py:1
Result(* fcn_)(Arg1, Arg2)
T2 & operator()(const T1 &left, T2 &right) const
Result(* fcn_)(Arg1, Arg2, Arg3)
T2 & operator()(const T1 &left, T2 &right) const
Element1D(const unsigned long index)
Result operator()(const Arg1 &x, const Arg2 &y, const Arg3 &z) const
T2 & operator()(const T1 &left, T2 &right) const
diveq_left_0by0isC(const T1 &value)
Result operator()(const Container &c) const
Result operator()(const Arg1 &, const Arg2 &, const Arg3 &) const
T2 & operator()(const T1 &left, T2 &right) const
virtual Result operator()(const Arg1 &) const =0
FcnFunctor2(Result(*fcn)(Arg1, Arg2))
const Result & operator()(const Result &a) const
Definition: value.py:1
FcnFunctor3(Result(*fcn)(Arg1, Arg2, Arg3))
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
Result operator()(const Arg1 &) const
T1 & operator()(T1 &left, const T2 &right) const
void fcn(int &, double *, double &, double *, int)
Result operator()(const Arg1 &a) const
T1 & operator()(T1 &left, const T2 &right) const
FcnFunctor0(Result(*fcn)())
Result operator()(const Arg1 &a) const
virtual ~Functor1()
T2 & operator()(const T1 &left, T2 &right) const
Result(* fcn_)(Arg1)
Result operator()() const
double a
Definition: hdecay.h:121
float x
Result operator()(const Result &a) const override
T1 & operator()(T1 &left, const T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const
virtual Result operator()() const =0
addmul_right(const double weight)
edm::AssociationVector< reco::JetRefBaseProd, Values > Container
Element1DAt(const unsigned long index)
Result operator()(const Arg1 &, const Arg2 &) const
T1 & operator()(T1 &left, const T2 &right) const
virtual ~Functor2()
T2 & operator()(const T1 &left, T2 &right) const
Result operator()(const Arg1 &x, const Arg2 &y) const
T2 & operator()(const T1 &left, T2 &right) const
T1 & operator()(T1 &left, const T2 &right) const