71 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_MIN_ELEMENT_H10#define _LIBCPP___CXX03___ALGORITHM_MIN_ELEMENT_H11 12#include <__cxx03/__algorithm/comp.h>13#include <__cxx03/__algorithm/comp_ref_type.h>14#include <__cxx03/__config>15#include <__cxx03/__functional/identity.h>16#include <__cxx03/__iterator/iterator_traits.h>17#include <__cxx03/__type_traits/invoke.h>18#include <__cxx03/__type_traits/is_callable.h>19#include <__cxx03/__utility/move.h>20 21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif24 25_LIBCPP_PUSH_MACROS26#include <__cxx03/__undef_macros>27 28_LIBCPP_BEGIN_NAMESPACE_STD29 30template <class _Comp, class _Iter, class _Sent, class _Proj>31inline _LIBCPP_HIDE_FROM_ABI _Iter __min_element(_Iter __first, _Sent __last, _Comp __comp, _Proj& __proj) {32 if (__first == __last)33 return __first;34 35 _Iter __i = __first;36 while (++__i != __last)37 if (std::__invoke(__comp, std::__invoke(__proj, *__i), std::__invoke(__proj, *__first)))38 __first = __i;39 40 return __first;41}42 43template <class _Comp, class _Iter, class _Sent>44_LIBCPP_HIDE_FROM_ABI _Iter __min_element(_Iter __first, _Sent __last, _Comp __comp) {45 auto __proj = __identity();46 return std::__min_element<_Comp>(std::move(__first), std::move(__last), __comp, __proj);47}48 49template <class _ForwardIterator, class _Compare>50_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator51min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {52 static_assert(53 __has_forward_iterator_category<_ForwardIterator>::value, "std::min_element requires a ForwardIterator");54 static_assert(55 __is_callable<_Compare, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");56 57 return std::__min_element<__comp_ref_type<_Compare> >(std::move(__first), std::move(__last), __comp);58}59 60template <class _ForwardIterator>61_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator62min_element(_ForwardIterator __first, _ForwardIterator __last) {63 return std::min_element(__first, __last, __less<>());64}65 66_LIBCPP_END_NAMESPACE_STD67 68_LIBCPP_POP_MACROS69 70#endif // _LIBCPP___CXX03___ALGORITHM_MIN_ELEMENT_H71