181ad6265SDimitry Andric //===----------------------------------------------------------------------===// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric 981ad6265SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_MIN_ELEMENT_H 1081ad6265SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_MIN_ELEMENT_H 1181ad6265SDimitry Andric 1281ad6265SDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__functional/identity.h> 1481ad6265SDimitry Andric #include <__functional/invoke.h> 1581ad6265SDimitry Andric #include <__functional/ranges_operations.h> 1681ad6265SDimitry Andric #include <__iterator/concepts.h> 1781ad6265SDimitry Andric #include <__iterator/projected.h> 1881ad6265SDimitry Andric #include <__ranges/access.h> 1981ad6265SDimitry Andric #include <__ranges/concepts.h> 2081ad6265SDimitry Andric #include <__ranges/dangling.h> 2181ad6265SDimitry Andric #include <__utility/forward.h> 2281ad6265SDimitry Andric 2381ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2481ad6265SDimitry Andric # pragma GCC system_header 2581ad6265SDimitry Andric #endif 2681ad6265SDimitry Andric 27b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS 28b3edf446SDimitry Andric #include <__undef_macros> 29b3edf446SDimitry Andric 3006c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 3181ad6265SDimitry Andric 3281ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 3381ad6265SDimitry Andric 3481ad6265SDimitry Andric namespace ranges { 3581ad6265SDimitry Andric 36fcaf7f86SDimitry Andric // TODO(ranges): `ranges::min_element` can now simply delegate to `std::__min_element`. 3781ad6265SDimitry Andric template <class _Ip, class _Sp, class _Proj, class _Comp> 3806c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Ip __min_element_impl(_Ip __first, _Sp __last, _Comp& __comp, _Proj& __proj) { 3981ad6265SDimitry Andric if (__first == __last) 4081ad6265SDimitry Andric return __first; 4181ad6265SDimitry Andric 4281ad6265SDimitry Andric _Ip __i = __first; 4381ad6265SDimitry Andric while (++__i != __last) 4481ad6265SDimitry Andric if (std::invoke(__comp, std::invoke(__proj, *__i), std::invoke(__proj, *__first))) 4581ad6265SDimitry Andric __first = __i; 4681ad6265SDimitry Andric return __first; 4781ad6265SDimitry Andric } 4881ad6265SDimitry Andric 4981ad6265SDimitry Andric namespace __min_element { 5081ad6265SDimitry Andric struct __fn { 5106c3fb27SDimitry Andric template <forward_iterator _Ip, 5206c3fb27SDimitry Andric sentinel_for<_Ip> _Sp, 5306c3fb27SDimitry Andric class _Proj = identity, 5481ad6265SDimitry Andric indirect_strict_weak_order<projected<_Ip, _Proj>> _Comp = ranges::less> 55*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip 5606c3fb27SDimitry Andric operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const { 5781ad6265SDimitry Andric return ranges::__min_element_impl(__first, __last, __comp, __proj); 5881ad6265SDimitry Andric } 5981ad6265SDimitry Andric 6006c3fb27SDimitry Andric template <forward_range _Rp, 6106c3fb27SDimitry Andric class _Proj = identity, 6281ad6265SDimitry Andric indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less> 63*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Rp> 6406c3fb27SDimitry Andric operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const { 6581ad6265SDimitry Andric return ranges::__min_element_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj); 6681ad6265SDimitry Andric } 6781ad6265SDimitry Andric }; 6881ad6265SDimitry Andric } // namespace __min_element 6981ad6265SDimitry Andric 7081ad6265SDimitry Andric inline namespace __cpo { 7181ad6265SDimitry Andric inline constexpr auto min_element = __min_element::__fn{}; 7281ad6265SDimitry Andric } // namespace __cpo 7381ad6265SDimitry Andric } // namespace ranges 7481ad6265SDimitry Andric 7581ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD 7681ad6265SDimitry Andric 7706c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 7881ad6265SDimitry Andric 79b3edf446SDimitry Andric _LIBCPP_POP_MACROS 80b3edf446SDimitry Andric 8181ad6265SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_MIN_ELEMENT_H 82