1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_MINMAX_H
10 #define _LIBCPP___ALGORITHM_MINMAX_H
11 
12 #include <__config>
13 #include <__algorithm/comp.h>
14 #include <initializer_list>
15 #include <utility>
16 
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template<class _Tp, class _Compare>
28 _LIBCPP_NODISCARD_EXT inline
29 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
30 pair<const _Tp&, const _Tp&>
31 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
32 {
33     return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
34                               pair<const _Tp&, const _Tp&>(__a, __b);
35 }
36 
37 template<class _Tp>
38 _LIBCPP_NODISCARD_EXT inline
39 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
40 pair<const _Tp&, const _Tp&>
41 minmax(const _Tp& __a, const _Tp& __b)
42 {
43     return _VSTD::minmax(__a, __b, __less<_Tp>());
44 }
45 
46 #ifndef _LIBCPP_CXX03_LANG
47 
48 template<class _Tp, class _Compare>
49 _LIBCPP_NODISCARD_EXT inline
50 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
51 pair<_Tp, _Tp>
52 minmax(initializer_list<_Tp> __t, _Compare __comp)
53 {
54     typedef typename initializer_list<_Tp>::const_iterator _Iter;
55     _Iter __first = __t.begin();
56     _Iter __last  = __t.end();
57     pair<_Tp, _Tp> __result(*__first, *__first);
58 
59     ++__first;
60     if (__t.size() % 2 == 0)
61     {
62         if (__comp(*__first,  __result.first))
63             __result.first  = *__first;
64         else
65             __result.second = *__first;
66         ++__first;
67     }
68 
69     while (__first != __last)
70     {
71         _Tp __prev = *__first++;
72         if (__comp(*__first, __prev)) {
73             if ( __comp(*__first, __result.first)) __result.first  = *__first;
74             if (!__comp(__prev, __result.second))  __result.second = __prev;
75             }
76         else {
77             if ( __comp(__prev, __result.first))    __result.first  = __prev;
78             if (!__comp(*__first, __result.second)) __result.second = *__first;
79             }
80 
81         __first++;
82     }
83     return __result;
84 }
85 
86 template<class _Tp>
87 _LIBCPP_NODISCARD_EXT inline
88 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
89 pair<_Tp, _Tp>
90 minmax(initializer_list<_Tp> __t)
91 {
92     return _VSTD::minmax(__t, __less<_Tp>());
93 }
94 
95 #endif // _LIBCPP_CXX03_LANG
96 
97 _LIBCPP_END_NAMESPACE_STD
98 
99 _LIBCPP_POP_MACROS
100 
101 #endif // _LIBCPP___ALGORITHM_MINMAX_H
102