File: MaxCmp.h

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (125 lines) | stat: -rw-r--r-- 2,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// -*- C++ -*-
//
// MaxCmp.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef THEPEG_MaxCmp_H
#define THEPEG_MaxCmp_H
//
// This is the declaration of the MaxCmp class.
//

#include <functional>

namespace ThePEG {

/**
 * MaxCmp is a helper class to be used in a loop where one would like
 * to keep track of the largest value so far of a certain
 * expression. The class simply checks if the given value to the
 * operator() is the largest so far (in which case true is returned,
 * and the value is saved together with the optional index
 * argument. The Cmp template argument is by default greater<T>, but
 * can be set to any comparison class to change the meaning of
 * maximum: MaxCmp<double, int, less<double> > will keep track of the
 * smallest value.
 */
template <typename T = double, typename Indx = int, typename Cmp = std::greater<T> >
class MaxCmp {

public:

  /**
   * The default constructor.
   */
  MaxCmp() : init(false), max(T()), indx(Indx()) {}

  /**
   * Constructor specifying an initial maximum value, \a t.
   */
  MaxCmp(const T & t, Indx in = Indx()) : init(true), max(t), indx(in) {}

public:

  /**
   * If \a t is the largest value seen so far return true. Otherwise
   * return false. \a i is an optional index for the value \a t.
   */
  bool operator()(const T & t, Indx i = Indx())
  {
    if ( !init || cmp(t, max) ) {
      max = t;
      init = true;
      indx = i;
      return true;
    }
    return false;
  }

  /**
   * Return the largest value so far.
   */
  operator const T & () const { return value(); }

  /**
   * Return the largest value so far.
   */
  const T & value() const { return max; }

  /**
   * Return the index of the largest object seen so far.
   */
  Indx index() const {
    return indx;
  }

private:

  /**
   * True if a first value has been given;
   */
  bool init;

  /**
   * The largest value seen so far.
   */
  T max;

  /**
   * The index for the largest value seen so far.
   */
  Indx indx;

  /**
   * The comparison object to be used.
   */
  Cmp cmp;

};

/**
 * Special calss for Minimum comparisons.
 */
template <typename T, typename Indx = int>
class MinCmp: public MaxCmp<T, Indx, less<T> > {

public:

  /**
   * Constructors are not inherited.
   */
  MinCmp() {}
  
  /**
   * Constructors are not inherited.
   */
  MinCmp(const T & t, Indx in = Indx()) : MaxCmp<T, Indx, less<T> >(t, in) {}
  
};

}

#endif /* THEPEG_MaxCmp_H */