SUNphi  1.0
ValWithExtreme.hpp
Go to the documentation of this file.
1 #ifndef _VALWITHEXTREME_HPP
2 #define _VALWITHEXTREME_HPP
3 
4 /// \file ValWithExtreme.hpp
5 ///
6 /// \brief Defines a class which keeps track of extreme of a quantity
7 ///
8 /// The class holds value in the internal variable \c val
9 /// The extreme value is host in the \c extr variable
10 /// When the class is implicitly access
11 
12 #include <limits>
13 
14 #include <metaprogramming/SwallowSemicolon.hpp>
15 
16 namespace SUNphi
17 {
18  /// Possible extreme types
19  enum Extreme{MINIMUM,MAXIMUM};
20 
21  /// Class which keeps track of extreme values of a given type
22  template <typename T,
23  Extreme E>
25  {
26  /// Stored value
27  T val;
28 
29  /// Extreme value
30  T extr;
31 
32  /// Update the extreme
34  {
35  /// Result of whether it's extreme or not
36  bool is;
37 
38  switch(E)
39  {
40  case MINIMUM:
41  is=
42  (val<extr);
43  break;
44  case MAXIMUM:
45  is=
46  (val>extr);
47  break;
48  }
49 
50  if(is)
51  extr=
52  val;
53 
54  return
55  *this;
56  }
57 
58  public:
59 
60  /// Retrurn extreme value
61  const T& extreme()
62  const
63  {
64  return
65  extr;
66  }
67 
68  /// Reset to standard value
69  void reset()
70  {
71  switch(E)
72  {
73  case MINIMUM:
74  extr=
75  std::numeric_limits<T>::max();
76  break;
77  case MAXIMUM:
78  extr=
79  std::numeric_limits<T>::min();
80  break;
81  }
82  }
83 
84  /// Constructor
85  template <typename V=T>
86  ValWithExtreme(const V& init) :
87  val(init),
88  extr(init)
89  {
90  }
91 
92  /// Default constructor
94  {
95  reset();
96  }
97 
98  /// Implicit cast to const value reference
99  operator const T&()
100  const
101  {
102  return
103  val;
104  }
105 
106  /// Provide an unary operator \c OP
107 #define PROVIDE_UNARY_OPERATOR(OP)
108  /*! Unary operator \c OP with update */
109  template <typename V>
110  ValWithExtreme& operator OP (const V& oth)
111  {
112  val OP
113  oth;
114 
115  return
116  updateExtreme();
117  }
118 
120 
122 
124 
125 #undef PROVIDE_UNARY_OPERATOR
126  };
127 
128  /// class to keep a value and its maximum
129  template <typename T>
130  using ValWithMax=
131  ValWithExtreme<T,MAXIMUM>;
132 }
133 
134 #endif
operator const T &() const
Implicit cast to const value reference.
T extr
Extreme value.
ValWithExtreme(const V &init)
Constructor.
const T & extreme() const
Retrurn extreme value.
ValWithExtreme & updateExtreme()
Update the extreme.
Extreme
Possible extreme types.
#define PROVIDE_UNARY_OPERATOR(OP)
Provide an unary operator OP.
Class which keeps track of extreme values of a given type.
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
void reset()
Reset to standard value.
ValWithExtreme()
Default constructor.