1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_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 
operatorMaximizes61     GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const { return x >= y ? x : y; }
62 
63     /// @}
64 
65     typedef GUM_SCALAR first_argument_type;
66     typedef GUM_SCALAR second_argument_type;
67     typedef GUM_SCALAR result_type;
68   };
69 
70   /**
71    * @struct Minimizes functors.h <agrum/tools/core/functors.h>
72    * @brief Minimization function object class
73    * @ingroup core
74    *
75    * Returns the minimum of its two arguments
76    */
77   template < class GUM_SCALAR >
78   struct Minimizes {
79     // ###########################################################################
80     /// @name Operator()
81     // ###########################################################################
82     /// @{
83 
operatorMinimizes84     GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const { return x <= y ? x : y; }
85 
86     /// @}
87 
88     typedef GUM_SCALAR first_argument_type;
89     typedef GUM_SCALAR second_argument_type;
90     typedef GUM_SCALAR result_type;
91   };
92 
93   /**
94    * @struct ArgumentMaximises  functors.h <agrum/tools/core/functors.h>
95    * @brief Arg Max function object class
96    * @ingroup core
97    *
98    * @param Operator() takes two std::pairs.
99    * First element in each pair is the values we compare to do the argmax.
100    * Second element is the argument that leads to this value.
101    *
102    * @return best pair => the argument that is the arg max is ret.second
103    */
104 
105   template < class GUM_SCALAR >
106   struct ArgumentMaximises {
107     // ###########################################################################
108     /// @name Operator()
109     // ###########################################################################
110     /// @{
111 
operatorArgumentMaximises112     GUM_SCALAR operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const {
113       return x.first >= y.first ? x : y;
114     }
115 
116     /// @}
117 
118     typedef GUM_SCALAR first_argument_type;
119     typedef GUM_SCALAR second_argument_type;
120     typedef GUM_SCALAR result_type;
121   };
122 }   // namespace gum
123 
124 #endif   // GUM_FUNCTORS_H
125