blob: bdcf57b101e44e2a5e9f773a9a18d334281dd5c8 [file] [log] [blame]
Yi Kong83283012023-12-13 12:57:00 +09001//===----------------------------------------------------------------------===//
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 <__algorithm/comp.h>
13#include <__algorithm/minmax_element.h>
14#include <__config>
15#include <__functional/identity.h>
16#include <__type_traits/is_callable.h>
17#include <__utility/pair.h>
18#include <initializer_list>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template<class _Tp, class _Compare>
27_LIBCPP_NODISCARD_EXT inline
28_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
29pair<const _Tp&, const _Tp&>
30minmax(_LIBCPP_LIFETIMEBOUND const _Tp& __a, _LIBCPP_LIFETIMEBOUND const _Tp& __b, _Compare __comp)
31{
32 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
33 pair<const _Tp&, const _Tp&>(__a, __b);
34}
35
36template<class _Tp>
37_LIBCPP_NODISCARD_EXT inline
38_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
39pair<const _Tp&, const _Tp&>
40minmax(_LIBCPP_LIFETIMEBOUND const _Tp& __a, _LIBCPP_LIFETIMEBOUND const _Tp& __b)
41{
42 return std::minmax(__a, __b, __less<>());
43}
44
45#ifndef _LIBCPP_CXX03_LANG
46
47template<class _Tp, class _Compare>
48_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
49pair<_Tp, _Tp> minmax(initializer_list<_Tp> __t, _Compare __comp) {
50 static_assert(__is_callable<_Compare, _Tp, _Tp>::value, "The comparator has to be callable");
51 __identity __proj;
52 auto __ret = std::__minmax_element_impl(__t.begin(), __t.end(), __comp, __proj);
53 return pair<_Tp, _Tp>(*__ret.first, *__ret.second);
54}
55
56template<class _Tp>
57_LIBCPP_NODISCARD_EXT inline
58_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
59pair<_Tp, _Tp>
60minmax(initializer_list<_Tp> __t)
61{
62 return std::minmax(__t, __less<>());
63}
64
65#endif // _LIBCPP_CXX03_LANG
66
67_LIBCPP_END_NAMESPACE_STD
68
69#endif // _LIBCPP___ALGORITHM_MINMAX_H