1 //=================================================================================================
2 /*!
3 //  \file blaze/util/algorithms/Min.h
4 //  \brief Header file for the generic min algorithm
5 //
6 //  Copyright (C) 2012-2020 Klaus Iglberger - All Rights Reserved
7 //
8 //  This file is part of the Blaze library. You can redistribute it and/or modify it under
9 //  the terms of the New (Revised) BSD License. Redistribution and use in source and binary
10 //  forms, with or without modification, are permitted provided that the following conditions
11 //  are met:
12 //
13 //  1. Redistributions of source code must retain the above copyright notice, this list of
14 //     conditions and the following disclaimer.
15 //  2. Redistributions in binary form must reproduce the above copyright notice, this list
16 //     of conditions and the following disclaimer in the documentation and/or other materials
17 //     provided with the distribution.
18 //  3. Neither the names of the Blaze development group nor the names of its contributors
19 //     may be used to endorse or promote products derived from this software without specific
20 //     prior written permission.
21 //
22 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 //  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 //  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 //  SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 //  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 //  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 //  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 //  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 //  DAMAGE.
32 */
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_ALGORITHMS_MIN_H_
36 #define _BLAZE_UTIL_ALGORITHMS_MIN_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
44 #include <blaze/util/EnableIf.h>
45 #include <blaze/util/mpl/If.h>
46 #include <blaze/util/typetraits/HasLessThan.h>
47 #include <blaze/util/typetraits/IsSigned.h>
48 #include <blaze/util/typetraits/IsUnsigned.h>
49 #include <blaze/util/typetraits/RemoveConst.h>
50 #include <blaze/util/typetraits/RemoveCVRef.h>
51 #include <blaze/util/typetraits/RemoveRValueReference.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 //  MIN ALGORITHMS
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
63 /*!\brief Minimum function for two values/objects.
64 // \ingroup algorithms
65 //
66 // \param a The first value/object.
67 // \param b The second value/object.
68 // \return The minimum of the two values/objects.
69 //
70 // This function determines the minimum of the two given values by means of a less-than comparison.
71 // The return type of the function is determined by the data types of the given arguments.
72 */
73 template< typename T1, typename T2
74         , typename R1 = RemoveCVRef_t<T1>
75         , typename R2 = RemoveCVRef_t<T2>
76         , EnableIf_t< HasLessThan_v<R2,R1> &&
77                       !( IsSigned_v<R1> && IsUnsigned_v<R2> ) &&
78                       !( IsUnsigned_v<R1> && IsSigned_v<R2> ) >* = nullptr >
decltype(auto)79 constexpr decltype(auto) min( T1&& a, T2&& b )
80 {
81    using Result = decltype( b < a ? std::forward<T2>( b ) : std::forward<T1>( a ) );
82    using Return = RemoveConst_t< RemoveRValueReference_t< Result > >;
83 
84    return static_cast<Return>( b < a ? std::forward<T2>( b ) : std::forward<T1>( a ) );
85 }
86 //*************************************************************************************************
87 
88 
89 //*************************************************************************************************
90 /*!\brief Minimum function for three values/objects.
91 // \ingroup algorithms
92 //
93 // \param a The first value/object.
94 // \param b The second value/object.
95 // \param c The third value/object.
96 // \return The minimum of the given values/objects.
97 //
98 // This function returns the minimum of the given values/objects. The return type of the function
99 // is determined by the data types of the given arguments.
100 */
101 template< typename T1, typename T2, typename T3 >
decltype(auto)102 constexpr decltype(auto) min( T1&& a, T2&& b, T3&& c )
103 {
104    using std::forward;
105    using blaze::min;
106 
107    return min( min( forward<T1>( a ), forward<T2>( b ) ), forward<T3>( c ) );
108 }
109 //*************************************************************************************************
110 
111 
112 //*************************************************************************************************
113 /*!\brief Minimum function for at least four values/objects.
114 // \ingroup algorithms
115 //
116 // \param a The first value/object.
117 // \param b The second value/object.
118 // \param c The third value/object.
119 // \param args The pack of additional values/objects.
120 // \return The minimum of the given values/objects.
121 //
122 // This function returns the minimum of the given values/objects. The return type of the function
123 // is determined by the data types of the given arguments.
124 */
125 template< typename T1, typename T2, typename T3, typename... Ts >
decltype(auto)126 constexpr decltype(auto) min( T1&& a, T2&& b, T3&& c, Ts&&... args )
127 {
128    using std::forward;
129    using blaze::min;
130 
131    return min( min( min( forward<T1>( a ), forward<T2>( b ) ), forward<T3>( c ) ), forward<Ts>( args )... );
132 }
133 //*************************************************************************************************
134 
135 } // namespace blaze
136 
137 #endif
138