xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/minmax.h (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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_BEGIN_NAMESPACE_STD
23 
24 template<class _Tp, class _Compare>
25 _LIBCPP_NODISCARD_EXT inline
26 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
27 pair<const _Tp&, const _Tp&>
28 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
29 {
30     return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
31                               pair<const _Tp&, const _Tp&>(__a, __b);
32 }
33 
34 template<class _Tp>
35 _LIBCPP_NODISCARD_EXT inline
36 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
37 pair<const _Tp&, const _Tp&>
38 minmax(const _Tp& __a, const _Tp& __b)
39 {
40     return _VSTD::minmax(__a, __b, __less<_Tp>());
41 }
42 
43 #ifndef _LIBCPP_CXX03_LANG
44 
45 template<class _Tp, class _Compare>
46 _LIBCPP_NODISCARD_EXT inline
47 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
48 pair<_Tp, _Tp>
49 minmax(initializer_list<_Tp> __t, _Compare __comp)
50 {
51     typedef typename initializer_list<_Tp>::const_iterator _Iter;
52     _Iter __first = __t.begin();
53     _Iter __last  = __t.end();
54     pair<_Tp, _Tp> __result(*__first, *__first);
55 
56     ++__first;
57     if (__t.size() % 2 == 0)
58     {
59         if (__comp(*__first,  __result.first))
60             __result.first  = *__first;
61         else
62             __result.second = *__first;
63         ++__first;
64     }
65 
66     while (__first != __last)
67     {
68         _Tp __prev = *__first++;
69         if (__comp(*__first, __prev)) {
70             if ( __comp(*__first, __result.first)) __result.first  = *__first;
71             if (!__comp(__prev, __result.second))  __result.second = __prev;
72             }
73         else {
74             if ( __comp(__prev, __result.first))    __result.first  = __prev;
75             if (!__comp(*__first, __result.second)) __result.second = *__first;
76             }
77 
78         __first++;
79     }
80     return __result;
81 }
82 
83 template<class _Tp>
84 _LIBCPP_NODISCARD_EXT inline
85 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
86 pair<_Tp, _Tp>
87 minmax(initializer_list<_Tp> __t)
88 {
89     return _VSTD::minmax(__t, __less<_Tp>());
90 }
91 
92 #endif // _LIBCPP_CXX03_LANG
93 
94 _LIBCPP_END_NAMESPACE_STD
95 
96 #endif // _LIBCPP___ALGORITHM_MINMAX_H
97