min.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_MIN_H
  9. #define _LIBCPP___ALGORITHM_MIN_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/min_element.h>
  13. #include <__config>
  14. #include <initializer_list>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _Tp, class _Compare>
  22. _LIBCPP_NODISCARD_EXT inline
  23. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  24. const _Tp&
  25. min(const _Tp& __a, const _Tp& __b, _Compare __comp)
  26. {
  27. return __comp(__b, __a) ? __b : __a;
  28. }
  29. template <class _Tp>
  30. _LIBCPP_NODISCARD_EXT inline
  31. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  32. const _Tp&
  33. min(const _Tp& __a, const _Tp& __b)
  34. {
  35. return _VSTD::min(__a, __b, __less<_Tp>());
  36. }
  37. #ifndef _LIBCPP_CXX03_LANG
  38. template<class _Tp, class _Compare>
  39. _LIBCPP_NODISCARD_EXT inline
  40. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  41. _Tp
  42. min(initializer_list<_Tp> __t, _Compare __comp)
  43. {
  44. typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
  45. return *_VSTD::__min_element<_Comp_ref>(__t.begin(), __t.end(), __comp);
  46. }
  47. template<class _Tp>
  48. _LIBCPP_NODISCARD_EXT inline
  49. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  50. _Tp
  51. min(initializer_list<_Tp> __t)
  52. {
  53. return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
  54. }
  55. #endif // _LIBCPP_CXX03_LANG
  56. _LIBCPP_END_NAMESPACE_STD
  57. _LIBCPP_POP_MACROS
  58. #endif // _LIBCPP___ALGORITHM_MIN_H