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_
npstat::scast_pluseq_left
Definition: SimpleFunctors.h:357
npstat::FcnFunctor1::operator()
Result operator()(const Arg1 &a) const
Definition: SimpleFunctors.h:138
npstat::multeq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:280
npstat::FcnFunctor0::fcn_
Result(* fcn_)()
Definition: SimpleFunctors.h:127
npstat::Functor1::operator()
virtual Result operator()(const Arg1 &) const =0
npstat::FcnFunctor1::fcn_
Result(* fcn_)(Arg1)
Definition: SimpleFunctors.h:142
npstat::Functor2::operator()
virtual Result operator()(const Arg1 &, const Arg2 &) const =0
npstat::Functor0::result_type
Result result_type
Definition: SimpleFunctors.h:19
npstat::Functor2::first_argument_type
Arg1 first_argument_type
Definition: SimpleFunctors.h:39
npstat::FcnFunctor3::operator()
Result operator()(const Arg1 &x, const Arg2 &y, const Arg3 &z) const
Definition: SimpleFunctors.h:168
npstat::multeq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:286
npstat::diveq_left
Definition: SimpleFunctors.h:291
npstat::assign_right
Definition: SimpleFunctors.h:219
npstat::addmul_left::addmul_left
addmul_left()=delete
npstat::addmul_right::addmul_right
addmul_right()=delete
npstat::DefaultConstructor0::operator()
Result operator()() const
Definition: SimpleFunctors.h:76
npstat::diveq_right
Definition: SimpleFunctors.h:297
npstat::addmul_left
Definition: SimpleFunctors.h:240
npstat::FcnFunctor2::FcnFunctor2
FcnFunctor2(Result(*fcn)(Arg1, Arg2))
Definition: SimpleFunctors.h:151
npstat::diveq_right_0by0isC
Definition: SimpleFunctors.h:324
npstat::Functor1::~Functor1
virtual ~Functor1()
Definition: SimpleFunctors.h:31
npstat::FcnFunctor1::FcnFunctor1
FcnFunctor1()=delete
npstat::pluseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:226
npstat::diveq_left_0by0isC::diveq_left_0by0isC
diveq_left_0by0isC()
Definition: SimpleFunctors.h:304
npstat::DefaultConstructor1
Definition: SimpleFunctors.h:84
npstat::scast_assign_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:352
npstat::SameRef
Definition: SimpleFunctors.h:66
npstat::Functor2::second_argument_type
Arg2 second_argument_type
Definition: SimpleFunctors.h:40
npstat::Functor2::result_type
Result result_type
Definition: SimpleFunctors.h:38
npstat::FcnFunctor2::operator()
Result operator()(const Arg1 &x, const Arg2 &y) const
Definition: SimpleFunctors.h:153
npstat::multeq_right
Definition: SimpleFunctors.h:285
npstat::Functor3::operator()
virtual Result operator()(const Arg1 &, const Arg2 &, const Arg3 &) const =0
fcn
void fcn(int &, double *, double &, double *, int)
Definition: LASBarrelAlgorithm.cc:388
npstat::diveq_left_0by0isC::diveq_left_0by0isC
diveq_left_0by0isC(const T1 &value)
Definition: SimpleFunctors.h:305
npstat::FcnFunctor0::FcnFunctor0
FcnFunctor0()=delete
npstat::Functor3::second_argument_type
Arg2 second_argument_type
Definition: SimpleFunctors.h:51
npstat::diveq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:292
npstat::diveq_right_0by0isC::diveq_right_0by0isC
diveq_right_0by0isC(const T2 &value)
Definition: SimpleFunctors.h:326
reco::JetExtendedAssociation::Container
edm::AssociationVector< reco::JetRefBaseProd, Values > Container
Definition: JetExtendedAssociation.h:29
npstat::FcnFunctor3::FcnFunctor3
FcnFunctor3(Result(*fcn)(Arg1, Arg2, Arg3))
Definition: SimpleFunctors.h:166
npstat::DefaultConstructor1::operator()
Result operator()(const Arg1 &) const
Definition: SimpleFunctors.h:85
npstat::SameRef::operator()
const Result & operator()(const Result &a) const
Definition: SimpleFunctors.h:67
npstat::pluseq_left
Definition: SimpleFunctors.h:225
npstat::CastingCopyConstructor
Definition: SimpleFunctors.h:111
npstat::addmul_left::w_
double w_
Definition: SimpleFunctors.h:247
npstat
Definition: AbsArrayProjector.h:14
npstat::Functor1
Definition: SimpleFunctors.h:27
npstat::diveq_left_0by0isC
Definition: SimpleFunctors.h:303
npstat::minuseq_left
Definition: SimpleFunctors.h:267
npstat::FcnFunctor0::FcnFunctor0
FcnFunctor0(Result(*fcn)())
Definition: SimpleFunctors.h:121
npstat::Functor1::result_type
Result result_type
Definition: SimpleFunctors.h:28
npstat::addmul_right::w_
double w_
Definition: SimpleFunctors.h:262
npstat::Element1DAt::Element1DAt
Element1DAt(const unsigned long index)
Definition: SimpleFunctors.h:196
npstat::diveq_left_0by0isC::leftZero
T1 leftZero
Definition: SimpleFunctors.h:318
npstat::addmul_right::addmul_right
addmul_right(const double weight)
Definition: SimpleFunctors.h:256
npstat::DefaultConstructor2
Definition: SimpleFunctors.h:93
npstat::Element1D
Definition: SimpleFunctors.h:180
npstat::assign_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:211
npstat::scast_assign_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:346
npstat::pluseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:232
npstat::Functor2::~Functor2
virtual ~Functor2()
Definition: SimpleFunctors.h:42
npstat::FcnFunctor2::FcnFunctor2
FcnFunctor2()=delete
npstat::minuseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:274
npstat::scast_minuseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:370
npstat::pluseq_right
Definition: SimpleFunctors.h:231
npstat::Element1D::Element1D
Element1D()=delete
npstat::diveq_right_0by0isC::leftZero
T1 leftZero
Definition: SimpleFunctors.h:339
npstat::FcnFunctor0
Definition: SimpleFunctors.h:120
a
double a
Definition: hdecay.h:119
npstat::FcnFunctor0::operator()
Result operator()() const
Definition: SimpleFunctors.h:123
npstat::diveq_right_0by0isC::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:328
npstat::Functor2
Definition: SimpleFunctors.h:37
npstat::Element1DAt
Definition: SimpleFunctors.h:195
npstat::diveq_left_0by0isC::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:307
npstat::assign_left
Definition: SimpleFunctors.h:210
npstat::FcnFunctor3::FcnFunctor3
FcnFunctor3()=delete
npstat::Functor3::third_argument_type
Arg3 third_argument_type
Definition: SimpleFunctors.h:52
npstat::Functor0::operator()
virtual Result operator()() const =0
npstat::DefaultConstructor3
Definition: SimpleFunctors.h:102
npstat::diveq_left_0by0isC::C
T1 C
Definition: SimpleFunctors.h:317
npstat::scast_minuseq_left
Definition: SimpleFunctors.h:369
npstat::addmul_right::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:258
value
Definition: value.py:1
npstat::scast_pluseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:364
npstat::FcnFunctor3::fcn_
Result(* fcn_)(Arg1, Arg2, Arg3)
Definition: SimpleFunctors.h:172
npstat::diveq_right_0by0isC::diveq_right_0by0isC
diveq_right_0by0isC()
Definition: SimpleFunctors.h:325
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
npstat::diveq_left_0by0isC::rightZero
T2 rightZero
Definition: SimpleFunctors.h:319
npstat::Functor1::first_argument_type
Arg1 first_argument_type
Definition: SimpleFunctors.h:29
npstat::Same::operator()
Result operator()(const Result &a) const override
Definition: SimpleFunctors.h:61
npstat::multeq_left
Definition: SimpleFunctors.h:279
npstat::Element1DAt::Element1DAt
Element1DAt()=delete
npstat::addmul_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:243
npstat::FcnFunctor1
Definition: SimpleFunctors.h:135
npstat::assign_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:220
npstat::diveq_right_0by0isC::C
T2 C
Definition: SimpleFunctors.h:338
npstat::diveq_right_0by0isC::rightZero
T2 rightZero
Definition: SimpleFunctors.h:340
npstat::DefaultConstructor2::operator()
Result operator()(const Arg1 &, const Arg2 &) const
Definition: SimpleFunctors.h:94
npstat::diveq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:298
npstat::scast_assign_left
Definition: SimpleFunctors.h:345
npstat::CastingCopyConstructor::operator()
Result operator()(const Arg1 &a) const
Definition: SimpleFunctors.h:112
npstat::scast_pluseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:358
npstat::minuseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:268
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
npstat::Element1D::idx
unsigned long idx
Definition: SimpleFunctors.h:187
npstat::Element1DAt::idx
unsigned long idx
Definition: SimpleFunctors.h:202
npstat::FcnFunctor2::fcn_
Result(* fcn_)(Arg1, Arg2)
Definition: SimpleFunctors.h:157
npstat::addmul_left::addmul_left
addmul_left(const double weight)
Definition: SimpleFunctors.h:241
npstat::scast_minuseq_right
Definition: SimpleFunctors.h:375
npstat::FcnFunctor3
Definition: SimpleFunctors.h:165
npstat::Element1DAt::operator()
Result operator()(const Container &c) const
Definition: SimpleFunctors.h:198
npstat::Element1D::Element1D
Element1D(const unsigned long index)
Definition: SimpleFunctors.h:181
npstat::Element1D::operator()
Result operator()(const Container &c) const
Definition: SimpleFunctors.h:183
npstat::FcnFunctor1::FcnFunctor1
FcnFunctor1(Result(*fcn)(Arg1))
Definition: SimpleFunctors.h:136
npstat::Functor3::~Functor3
virtual ~Functor3()
Definition: SimpleFunctors.h:54
npstat::Functor3
Definition: SimpleFunctors.h:48
npstat::addmul_right
Definition: SimpleFunctors.h:255
npstat::Same
Definition: SimpleFunctors.h:60
npstat::minuseq_right
Definition: SimpleFunctors.h:273
npstat::Functor3::result_type
Result result_type
Definition: SimpleFunctors.h:49
npstat::FcnFunctor2
Definition: SimpleFunctors.h:150
npstat::Functor3::first_argument_type
Arg1 first_argument_type
Definition: SimpleFunctors.h:50
weight
Definition: weight.py:1
npstat::DefaultConstructor0
Definition: SimpleFunctors.h:75
npstat::scast_pluseq_right
Definition: SimpleFunctors.h:363
npstat::scast_minuseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:376
npstat::scast_assign_right
Definition: SimpleFunctors.h:351
npstat::Functor0::~Functor0
virtual ~Functor0()
Definition: SimpleFunctors.h:21
npstat::DefaultConstructor3::operator()
Result operator()(const Arg1 &, const Arg2 &, const Arg3 &) const
Definition: SimpleFunctors.h:103
npstat::Functor0
Definition: SimpleFunctors.h:18