min.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Tp&
  23. min(_LIBCPP_LIFETIMEBOUND const _Tp& __a, _LIBCPP_LIFETIMEBOUND const _Tp& __b, _Compare __comp) {
  24. return __comp(__b, __a) ? __b : __a;
  25. }
  26. template <class _Tp>
  27. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Tp&
  28. min(_LIBCPP_LIFETIMEBOUND const _Tp& __a, _LIBCPP_LIFETIMEBOUND const _Tp& __b) {
  29. return std::min(__a, __b, __less<>());
  30. }
  31. #ifndef _LIBCPP_CXX03_LANG
  32. template <class _Tp, class _Compare>
  33. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp
  34. min(initializer_list<_Tp> __t, _Compare __comp) {
  35. return *std::__min_element<__comp_ref_type<_Compare> >(__t.begin(), __t.end(), __comp);
  36. }
  37. template <class _Tp>
  38. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp min(initializer_list<_Tp> __t) {
  39. return *std::min_element(__t.begin(), __t.end(), __less<>());
  40. }
  41. #endif // _LIBCPP_CXX03_LANG
  42. _LIBCPP_END_NAMESPACE_STD
  43. _LIBCPP_POP_MACROS
  44. #endif // _LIBCPP___ALGORITHM_MIN_H