Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:10

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_RANGES_MINMAX_H
0010 #define _LIBCPP___ALGORITHM_RANGES_MINMAX_H
0011 
0012 #include <__algorithm/min_max_result.h>
0013 #include <__algorithm/minmax_element.h>
0014 #include <__assert>
0015 #include <__concepts/copyable.h>
0016 #include <__concepts/same_as.h>
0017 #include <__config>
0018 #include <__functional/identity.h>
0019 #include <__functional/invoke.h>
0020 #include <__functional/ranges_operations.h>
0021 #include <__iterator/concepts.h>
0022 #include <__iterator/next.h>
0023 #include <__iterator/projected.h>
0024 #include <__ranges/access.h>
0025 #include <__ranges/concepts.h>
0026 #include <__type_traits/desugars_to.h>
0027 #include <__type_traits/is_integral.h>
0028 #include <__type_traits/is_reference.h>
0029 #include <__type_traits/is_trivially_copyable.h>
0030 #include <__type_traits/remove_cvref.h>
0031 #include <__utility/forward.h>
0032 #include <__utility/move.h>
0033 #include <__utility/pair.h>
0034 #include <initializer_list>
0035 
0036 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0037 #  pragma GCC system_header
0038 #endif
0039 
0040 #if _LIBCPP_STD_VER >= 20
0041 
0042 _LIBCPP_PUSH_MACROS
0043 #  include <__undef_macros>
0044 
0045 _LIBCPP_BEGIN_NAMESPACE_STD
0046 
0047 namespace ranges {
0048 template <class _T1>
0049 using minmax_result = min_max_result<_T1>;
0050 
0051 struct __minmax {
0052   template <class _Type,
0053             class _Proj                                                      = identity,
0054             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
0055   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&>
0056   operator()(_LIBCPP_LIFETIMEBOUND const _Type& __a,
0057              _LIBCPP_LIFETIMEBOUND const _Type& __b,
0058              _Comp __comp = {},
0059              _Proj __proj = {}) const {
0060     if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)))
0061       return {__b, __a};
0062     return {__a, __b};
0063   }
0064 
0065   template <copyable _Type,
0066             class _Proj                                                      = identity,
0067             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
0068   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<_Type>
0069   operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const {
0070     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0071         __il.begin() != __il.end(), "initializer_list has to contain at least one element");
0072     auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
0073     return ranges::minmax_result<_Type>{*__iters.first, *__iters.second};
0074   }
0075 
0076   template <input_range _Range,
0077             class _Proj                                                            = identity,
0078             indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
0079     requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
0080   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<range_value_t<_Range>>
0081   operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
0082     auto __first  = ranges::begin(__r);
0083     auto __last   = ranges::end(__r);
0084     using _ValueT = range_value_t<_Range>;
0085 
0086     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range has to contain at least one element");
0087 
0088     // This optimiation is not in minmax_element because clang doesn't see through the pointers and as a result doesn't
0089     // vectorize the code.
0090     if constexpr (contiguous_range<_Range> && is_integral_v<_ValueT> &&
0091                   __is_cheap_to_copy<_ValueT> & __is_identity<_Proj>::value &&
0092                   __desugars_to_v<__less_tag, _Comp, _ValueT, _ValueT>) {
0093       minmax_result<_ValueT> __result = {__r[0], __r[0]};
0094       for (auto __e : __r) {
0095         if (__e < __result.min)
0096           __result.min = __e;
0097         if (__result.max < __e)
0098           __result.max = __e;
0099       }
0100       return __result;
0101     } else if constexpr (forward_range<_Range>) {
0102       // Special-case the one element case. Avoid repeatedly initializing objects from the result of an iterator
0103       // dereference when doing so might not be idempotent. The `if constexpr` avoids the extra branch in cases where
0104       // it's not needed.
0105       if constexpr (!same_as<remove_cvref_t<range_reference_t<_Range>>, _ValueT> ||
0106                     is_rvalue_reference_v<range_reference_t<_Range>>) {
0107         if (ranges::next(__first) == __last) {
0108           // During initialization, members are allowed to refer to already initialized members
0109           // (see http://eel.is/c++draft/dcl.init.aggr#6)
0110           minmax_result<_ValueT> __result = {*__first, __result.min};
0111           return __result;
0112         }
0113       }
0114       auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
0115       return {*__result.first, *__result.second};
0116     } else {
0117       // input_iterators can't be copied, so the implementation for input_iterators has to store
0118       // the values instead of a pointer to the correct values
0119       auto __less = [&](auto&& __a, auto&& __b) -> bool {
0120         return std::invoke(__comp,
0121                            std::invoke(__proj, std::forward<decltype(__a)>(__a)),
0122                            std::invoke(__proj, std::forward<decltype(__b)>(__b)));
0123       };
0124 
0125       // During initialization, members are allowed to refer to already initialized members
0126       // (see http://eel.is/c++draft/dcl.init.aggr#6)
0127       ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
0128       if (__first == __last || ++__first == __last)
0129         return __result;
0130 
0131       if (__less(*__first, __result.min))
0132         __result.min = *__first;
0133       else
0134         __result.max = *__first;
0135 
0136       while (++__first != __last) {
0137         _ValueT __i = *__first;
0138         if (++__first == __last) {
0139           if (__less(__i, __result.min))
0140             __result.min = __i;
0141           else if (!__less(__i, __result.max))
0142             __result.max = __i;
0143           return __result;
0144         }
0145 
0146         if (__less(*__first, __i)) {
0147           if (__less(*__first, __result.min))
0148             __result.min = *__first;
0149           if (!__less(__i, __result.max))
0150             __result.max = std::move(__i);
0151         } else {
0152           if (__less(__i, __result.min))
0153             __result.min = std::move(__i);
0154           if (!__less(*__first, __result.max))
0155             __result.max = *__first;
0156         }
0157       }
0158       return __result;
0159     }
0160   }
0161 };
0162 
0163 inline namespace __cpo {
0164 inline constexpr auto minmax = __minmax{};
0165 } // namespace __cpo
0166 } // namespace ranges
0167 
0168 _LIBCPP_END_NAMESPACE_STD
0169 
0170 _LIBCPP_POP_MACROS
0171 
0172 #endif // _LIBCPP_STD_VER >= 20
0173 
0174 #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H