thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
MaxCmp.h
1 // -*- C++ -*-
2 //
3 // MaxCmp.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef THEPEG_MaxCmp_H
10 #define THEPEG_MaxCmp_H
11 //
12 // This is the declaration of the MaxCmp class.
13 //
14 
15 #include <functional>
16 
17 namespace ThePEG {
18 
30 template <typename T = double, typename Indx = int, typename Cmp = std::greater<T> >
31 class MaxCmp {
32 
33 public:
34 
38  MaxCmp() : init(false), max(T()), indx(Indx()) {}
39 
43  MaxCmp(const T & t, Indx in = Indx()) : init(true), max(t), indx(in) {}
44 
45 public:
46 
51  bool operator()(const T & t, Indx i = Indx())
52  {
53  if ( !init || cmp(t, max) ) {
54  max = t;
55  init = true;
56  indx = i;
57  return true;
58  }
59  return false;
60  }
61 
65  operator const T & () const { return value(); }
66 
70  const T & value() const { return max; }
71 
75  Indx index() const {
76  return indx;
77  }
78 
82  bool operator!() const {
83  return !init;
84  }
85 
86 private:
87 
91  bool init;
92 
96  T max;
97 
101  Indx indx;
102 
106  Cmp cmp;
107 
108 };
109 
113 template <typename T, typename Indx = int>
114 class MinCmp: public MaxCmp<T, Indx, less<T> > {
115 
116 public:
117 
121  MinCmp() {}
122 
126  MinCmp(const T & t, Indx in = Indx()) : MaxCmp<T, Indx, less<T> >(t, in) {}
127 
128 };
129 
130 }
131 
132 #endif /* THEPEG_MaxCmp_H */
MaxCmp is a helper class to be used in a loop where one would like to keep track of the largest value...
Definition: MaxCmp.h:31
bool operator!() const
Return true if no index has been chosen.
Definition: MaxCmp.h:82
bool operator()(const T &t, Indx i=Indx())
If t is the largest value seen so far return true.
Definition: MaxCmp.h:51
Indx index() const
Return the index of the largest object seen so far.
Definition: MaxCmp.h:75
T max
The largest value seen so far.
Definition: MaxCmp.h:96
Indx indx
The index for the largest value seen so far.
Definition: MaxCmp.h:101
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
const T & value() const
Return the largest value so far.
Definition: MaxCmp.h:70
MaxCmp(const T &t, Indx in=Indx())
Constructor specifying an initial maximum value, t.
Definition: MaxCmp.h:43
Cmp cmp
The comparison object to be used.
Definition: MaxCmp.h:106
MinCmp(const T &t, Indx in=Indx())
Constructors are not inherited.
Definition: MaxCmp.h:126
bool init
True if a first value has been given;.
Definition: MaxCmp.h:91
Special calss for Minimum comparisons.
Definition: MaxCmp.h:114
MinCmp()
Constructors are not inherited.
Definition: MaxCmp.h:121
MaxCmp()
The default constructor.
Definition: MaxCmp.h:38