minmax.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef _LIBCPP___ALGORITHM_MINMAX_H
  9. #define _LIBCPP___ALGORITHM_MINMAX_H
  10. #include <__algorithm/comp.h>
  11. #include <__config>
  12. #include <__utility/pair.h>
  13. #include <initializer_list>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. template<class _Tp, class _Compare>
  19. _LIBCPP_NODISCARD_EXT inline
  20. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  21. pair<const _Tp&, const _Tp&>
  22. minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
  23. {
  24. return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
  25. pair<const _Tp&, const _Tp&>(__a, __b);
  26. }
  27. template<class _Tp>
  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)
  32. {
  33. return _VSTD::minmax(__a, __b, __less<_Tp>());
  34. }
  35. #ifndef _LIBCPP_CXX03_LANG
  36. template<class _Tp, class _Compare>
  37. _LIBCPP_NODISCARD_EXT inline
  38. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  39. pair<_Tp, _Tp>
  40. minmax(initializer_list<_Tp> __t, _Compare __comp)
  41. {
  42. typedef typename initializer_list<_Tp>::const_iterator _Iter;
  43. _Iter __first = __t.begin();
  44. _Iter __last = __t.end();
  45. pair<_Tp, _Tp> __result(*__first, *__first);
  46. ++__first;
  47. if (__t.size() % 2 == 0)
  48. {
  49. if (__comp(*__first, __result.first))
  50. __result.first = *__first;
  51. else
  52. __result.second = *__first;
  53. ++__first;
  54. }
  55. while (__first != __last)
  56. {
  57. _Tp __prev = *__first++;
  58. if (__comp(*__first, __prev)) {
  59. if ( __comp(*__first, __result.first)) __result.first = *__first;
  60. if (!__comp(__prev, __result.second)) __result.second = __prev;
  61. }
  62. else {
  63. if ( __comp(__prev, __result.first)) __result.first = __prev;
  64. if (!__comp(*__first, __result.second)) __result.second = *__first;
  65. }
  66. __first++;
  67. }
  68. return __result;
  69. }
  70. template<class _Tp>
  71. _LIBCPP_NODISCARD_EXT inline
  72. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  73. pair<_Tp, _Tp>
  74. minmax(initializer_list<_Tp> __t)
  75. {
  76. return _VSTD::minmax(__t, __less<_Tp>());
  77. }
  78. #endif // _LIBCPP_CXX03_LANG
  79. _LIBCPP_END_NAMESPACE_STD
  80. #endif // _LIBCPP___ALGORITHM_MINMAX_H