aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
functors.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief This files contains several function objects that are not (yet) defined
25  * in the STL
26  *
27  * Generically, function objects are instances of a class with member function
28  * operator() defined.
29  * This member function allows the object to be used with the same syntax as a
30  * function call.
31  *
32  * @author Jean-Christophe MAGNAN
33  */
34 
35 // =========================================================================
36 #ifndef GUM_FUNCTORS_H
37 #define GUM_FUNCTORS_H
38 // =========================================================================
39 #include <cstdlib>
40 // =========================================================================
41 #include <agrum/tools/core/argMaxSet.h>
42 // =========================================================================
43 
44 
45 namespace gum {
46 
47  /**
48  * @struct Maximizes functors.h <agrum/tools/core/functors.h>
49  * @brief Maximization function object class
50  * @ingroup core
51  *
52  * Returns the maximum of its two arguments
53  */
54  template < class GUM_SCALAR >
55  struct Maximizes {
56  // ###########################################################################
57  /// @name Operator()
58  // ###########################################################################
59  /// @{
60 
61  GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const {
62  return x >= y ? x : y;
63  }
64 
65  /// @}
66 
67  typedef GUM_SCALAR first_argument_type;
68  typedef GUM_SCALAR second_argument_type;
69  typedef GUM_SCALAR result_type;
70  };
71 
72  /**
73  * @struct Minimizes functors.h <agrum/tools/core/functors.h>
74  * @brief Minimization function object class
75  * @ingroup core
76  *
77  * Returns the minimum of its two arguments
78  */
79  template < class GUM_SCALAR >
80  struct Minimizes {
81  // ###########################################################################
82  /// @name Operator()
83  // ###########################################################################
84  /// @{
85 
86  GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const {
87  return x <= y ? x : y;
88  }
89 
90  /// @}
91 
92  typedef GUM_SCALAR first_argument_type;
93  typedef GUM_SCALAR second_argument_type;
94  typedef GUM_SCALAR result_type;
95  };
96 
97  /**
98  * @struct ArgumentMaximises functors.h <agrum/tools/core/functors.h>
99  * @brief Arg Max function object class
100  * @ingroup core
101  *
102  * @param Operator() takes two std::pairs.
103  * First element in each pair is the values we compare to do the argmax.
104  * Second element is the argument that leads to this value.
105  *
106  * @return best pair => the argument that is the arg max is ret.second
107  */
108 
109  template < class GUM_SCALAR >
111  // ###########################################################################
112  /// @name Operator()
113  // ###########################################################################
114  /// @{
115 
116  GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const {
117  return x.first >= y.first ? x : y;
118  }
119 
120  /// @}
121 
122  typedef GUM_SCALAR first_argument_type;
123  typedef GUM_SCALAR second_argument_type;
124  typedef GUM_SCALAR result_type;
125  };
126 } // namespace gum
127 
128 #endif // GUM_FUNCTORS_H
GUM_SCALAR result_type
Definition: functors.h:124
GUM_SCALAR first_argument_type
Definition: functors.h:122
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
GUM_SCALAR second_argument_type
Definition: functors.h:123
GUM_SCALAR operator()(const GUM_SCALAR &x, const GUM_SCALAR &y) const
Definition: functors.h:116
GUM_SCALAR operator()(const GUM_SCALAR &x, const GUM_SCALAR &y) const
Definition: functors.h:86
Arg Max function object class.
Definition: functors.h:110
Maximization function object classReturns the maximum of its two arguments.
Definition: functors.h:55
GUM_SCALAR second_argument_type
Definition: functors.h:93
GUM_SCALAR first_argument_type
Definition: functors.h:67
Minimization function object classReturns the minimum of its two arguments.
Definition: functors.h:80
GUM_SCALAR first_argument_type
Definition: functors.h:92
GUM_SCALAR operator()(const GUM_SCALAR &x, const GUM_SCALAR &y) const
Definition: functors.h:61
GUM_SCALAR second_argument_type
Definition: functors.h:68
GUM_SCALAR result_type
Definition: functors.h:94
GUM_SCALAR result_type
Definition: functors.h:69