ThePEG  1.8.0
MaxCmp.h
1 // -*- C++ -*-
2 //
3 // MaxCmp.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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 
79 private:
80 
84  bool init;
85 
89  T max;
90 
94  Indx indx;
95 
99  Cmp cmp;
100 
101 };
102 
106 template <typename T, typename Indx = int>
107 class MinCmp: public MaxCmp<T, Indx, less<T> > {
108 
109 public:
110 
114  MinCmp() {}
115 
119  MinCmp(const T & t, Indx in = Indx()) : MaxCmp<T, Indx, less<T> >(t, in) {}
120 
121 };
122 
123 }
124 
125 #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 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:89
Indx indx
The index for the largest value seen so far.
Definition: MaxCmp.h:94
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:99
MinCmp(const T &t, Indx in=Indx())
Constructors are not inherited.
Definition: MaxCmp.h:119
bool init
True if a first value has been given;.
Definition: MaxCmp.h:84
Special calss for Minimum comparisons.
Definition: MaxCmp.h:107
MinCmp()
Constructors are not inherited.
Definition: MaxCmp.h:114
MaxCmp()
The default constructor.
Definition: MaxCmp.h:38