Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:32

0001 // -*- C++ -*-
0002 //
0003 // MaxCmp.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef THEPEG_MaxCmp_H
0010 #define THEPEG_MaxCmp_H
0011 //
0012 // This is the declaration of the MaxCmp class.
0013 //
0014 
0015 #include <functional>
0016 
0017 namespace ThePEG {
0018 
0019 /**
0020  * MaxCmp is a helper class to be used in a loop where one would like
0021  * to keep track of the largest value so far of a certain
0022  * expression. The class simply checks if the given value to the
0023  * operator() is the largest so far (in which case true is returned,
0024  * and the value is saved together with the optional index
0025  * argument. The Cmp template argument is by default greater<T>, but
0026  * can be set to any comparison class to change the meaning of
0027  * maximum: MaxCmp<double, int, less<double> > will keep track of the
0028  * smallest value.
0029  */
0030 template <typename T = double, typename Indx = int, typename Cmp = std::greater<T> >
0031 class MaxCmp {
0032 
0033 public:
0034 
0035   /**
0036    * The default constructor.
0037    */
0038   MaxCmp() : init(false), max(T()), indx(Indx()) {}
0039 
0040   /**
0041    * Constructor specifying an initial maximum value, \a t.
0042    */
0043   MaxCmp(const T & t, Indx in = Indx()) : init(true), max(t), indx(in) {}
0044 
0045 public:
0046 
0047   /**
0048    * If \a t is the largest value seen so far return true. Otherwise
0049    * return false. \a i is an optional index for the value \a t.
0050    */
0051   bool operator()(const T & t, Indx i = Indx())
0052   {
0053     if ( !init || cmp(t, max) ) {
0054       max = t;
0055       init = true;
0056       indx = i;
0057       return true;
0058     }
0059     return false;
0060   }
0061 
0062   /**
0063    * Return the largest value so far.
0064    */
0065   operator const T & () const { return value(); }
0066 
0067   /**
0068    * Return the largest value so far.
0069    */
0070   const T & value() const { return max; }
0071 
0072   /**
0073    * Return the index of the largest object seen so far.
0074    */
0075   Indx index() const {
0076     return indx;
0077   }
0078 
0079   /**
0080    * Return true if no index has been chosen.
0081    */
0082   bool operator!() const {
0083     return !init;
0084   }
0085 
0086 private:
0087 
0088   /**
0089    * True if a first value has been given;
0090    */
0091   bool init;
0092 
0093   /**
0094    * The largest value seen so far.
0095    */
0096   T max;
0097 
0098   /**
0099    * The index for the largest value seen so far.
0100    */
0101   Indx indx;
0102 
0103   /**
0104    * The comparison object to be used.
0105    */
0106   Cmp cmp;
0107 
0108 };
0109 
0110 /**
0111  * Special calss for Minimum comparisons.
0112  */
0113 template <typename T, typename Indx = int>
0114 class MinCmp: public MaxCmp<T, Indx, less<T> > {
0115 
0116 public:
0117 
0118   /**
0119    * Constructors are not inherited.
0120    */
0121   MinCmp() {}
0122   
0123   /**
0124    * Constructors are not inherited.
0125    */
0126   MinCmp(const T & t, Indx in = Indx()) : MaxCmp<T, Indx, less<T> >(t, in) {}
0127   
0128 };
0129 
0130 }
0131 
0132 #endif /* THEPEG_MaxCmp_H */