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_
npstat::scast_pluseq_left
Definition: SimpleFunctors.h:365
npstat::FcnFunctor1::operator()
Result operator()(const Arg1 &a) const
Definition: SimpleFunctors.h:139
npstat::multeq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:288
npstat::FcnFunctor0::fcn_
Result(* fcn_)()
Definition: SimpleFunctors.h:128
npstat::Functor1::operator()
virtual Result operator()(const Arg1 &) const =0
npstat::FcnFunctor1::fcn_
Result(* fcn_)(Arg1)
Definition: SimpleFunctors.h:144
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:171
npstat::multeq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:294
npstat::diveq_left
Definition: SimpleFunctors.h:299
npstat::assign_right
Definition: SimpleFunctors.h:225
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:305
npstat::addmul_left
Definition: SimpleFunctors.h:246
npstat::FcnFunctor2::FcnFunctor2
FcnFunctor2(Result(*fcn)(Arg1, Arg2))
Definition: SimpleFunctors.h:153
npstat::diveq_right_0by0isC
Definition: SimpleFunctors.h:332
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:232
npstat::diveq_left_0by0isC::diveq_left_0by0isC
diveq_left_0by0isC()
Definition: SimpleFunctors.h:312
npstat::DefaultConstructor1
Definition: SimpleFunctors.h:84
npstat::scast_assign_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:360
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:155
npstat::multeq_right
Definition: SimpleFunctors.h:293
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:313
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:300
npstat::diveq_right_0by0isC::diveq_right_0by0isC
diveq_right_0by0isC(const T2 &value)
Definition: SimpleFunctors.h:334
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:169
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:231
npstat::CastingCopyConstructor
Definition: SimpleFunctors.h:111
npstat::addmul_left::w_
double w_
Definition: SimpleFunctors.h:254
npstat
Definition: AbsArrayProjector.h:14
npstat::Functor1
Definition: SimpleFunctors.h:27
npstat::diveq_left_0by0isC
Definition: SimpleFunctors.h:311
npstat::minuseq_left
Definition: SimpleFunctors.h:275
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:270
npstat::Element1DAt::Element1DAt
Element1DAt(const unsigned long index)
Definition: SimpleFunctors.h:201
npstat::diveq_left_0by0isC::leftZero
T1 leftZero
Definition: SimpleFunctors.h:326
npstat::addmul_right::addmul_right
addmul_right(const double weight)
Definition: SimpleFunctors.h:263
npstat::DefaultConstructor2
Definition: SimpleFunctors.h:93
npstat::Element1D
Definition: SimpleFunctors.h:184
npstat::assign_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:217
npstat::scast_assign_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:354
npstat::pluseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:238
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:282
npstat::scast_minuseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:378
npstat::pluseq_right
Definition: SimpleFunctors.h:237
npstat::Element1D::Element1D
Element1D()=delete
npstat::diveq_right_0by0isC::leftZero
T1 leftZero
Definition: SimpleFunctors.h:347
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:336
npstat::Functor2
Definition: SimpleFunctors.h:37
npstat::Element1DAt
Definition: SimpleFunctors.h:200
npstat::diveq_left_0by0isC::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:315
npstat::assign_left
Definition: SimpleFunctors.h:216
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:325
npstat::scast_minuseq_left
Definition: SimpleFunctors.h:377
npstat::addmul_right::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:265
value
Definition: value.py:1
npstat::scast_pluseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:372
npstat::FcnFunctor3::fcn_
Result(* fcn_)(Arg1, Arg2, Arg3)
Definition: SimpleFunctors.h:176
npstat::diveq_right_0by0isC::diveq_right_0by0isC
diveq_right_0by0isC()
Definition: SimpleFunctors.h:333
npstat::diveq_left_0by0isC::rightZero
T2 rightZero
Definition: SimpleFunctors.h:327
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:287
npstat::Element1DAt::Element1DAt
Element1DAt()=delete
npstat::addmul_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:249
npstat::FcnFunctor1
Definition: SimpleFunctors.h:136
npstat::assign_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:226
npstat::diveq_right_0by0isC::C
T2 C
Definition: SimpleFunctors.h:346
npstat::diveq_right_0by0isC::rightZero
T2 rightZero
Definition: SimpleFunctors.h:348
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:306
npstat::scast_assign_left
Definition: SimpleFunctors.h:353
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:366
npstat::minuseq_left::operator()
T1 & operator()(T1 &left, const T2 &right) const
Definition: SimpleFunctors.h:276
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
npstat::Element1D::idx
unsigned long idx
Definition: SimpleFunctors.h:192
npstat::Element1DAt::idx
unsigned long idx
Definition: SimpleFunctors.h:208
npstat::FcnFunctor2::fcn_
Result(* fcn_)(Arg1, Arg2)
Definition: SimpleFunctors.h:160
npstat::addmul_left::addmul_left
addmul_left(const double weight)
Definition: SimpleFunctors.h:247
npstat::scast_minuseq_right
Definition: SimpleFunctors.h:383
npstat::FcnFunctor3
Definition: SimpleFunctors.h:168
npstat::Element1DAt::operator()
Result operator()(const Container &c) const
Definition: SimpleFunctors.h:203
npstat::Element1D::Element1D
Element1D(const unsigned long index)
Definition: SimpleFunctors.h:185
npstat::Element1D::operator()
Result operator()(const Container &c) const
Definition: SimpleFunctors.h:187
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
npstat::FcnFunctor1::FcnFunctor1
FcnFunctor1(Result(*fcn)(Arg1))
Definition: SimpleFunctors.h:137
npstat::Functor3::~Functor3
virtual ~Functor3()
Definition: SimpleFunctors.h:54
npstat::Functor3
Definition: SimpleFunctors.h:48
npstat::addmul_right
Definition: SimpleFunctors.h:262
npstat::Same
Definition: SimpleFunctors.h:60
npstat::minuseq_right
Definition: SimpleFunctors.h:281
npstat::Functor3::result_type
Result result_type
Definition: SimpleFunctors.h:49
npstat::FcnFunctor2
Definition: SimpleFunctors.h:152
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:371
npstat::scast_minuseq_right::operator()
T2 & operator()(const T1 &left, T2 &right) const
Definition: SimpleFunctors.h:384
npstat::scast_assign_right
Definition: SimpleFunctors.h:359
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