Back to home page

EIC code displayed by LXR

 
 

    


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

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_LEXICOGRAPHICAL_COMPARE_H
0010 #define _LIBCPP___ALGORITHM_LEXICOGRAPHICAL_COMPARE_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/min.h>
0014 #include <__algorithm/mismatch.h>
0015 #include <__algorithm/simd_utils.h>
0016 #include <__algorithm/unwrap_iter.h>
0017 #include <__config>
0018 #include <__functional/identity.h>
0019 #include <__iterator/iterator_traits.h>
0020 #include <__string/constexpr_c_functions.h>
0021 #include <__type_traits/desugars_to.h>
0022 #include <__type_traits/enable_if.h>
0023 #include <__type_traits/invoke.h>
0024 #include <__type_traits/is_equality_comparable.h>
0025 #include <__type_traits/is_integral.h>
0026 #include <__type_traits/is_trivially_lexicographically_comparable.h>
0027 #include <__type_traits/is_volatile.h>
0028 
0029 #if _LIBCPP_HAS_WIDE_CHARACTERS
0030 #  include <cwchar>
0031 #endif
0032 
0033 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0034 #  pragma GCC system_header
0035 #endif
0036 
0037 _LIBCPP_PUSH_MACROS
0038 #include <__undef_macros>
0039 
0040 _LIBCPP_BEGIN_NAMESPACE_STD
0041 
0042 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Comp>
0043 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __lexicographical_compare(
0044     _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
0045   while (__first2 != __last2) {
0046     if (__first1 == __last1 ||
0047         std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
0048       return true;
0049     if (std::__invoke(__comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
0050       return false;
0051     ++__first1;
0052     ++__first2;
0053   }
0054   return false;
0055 }
0056 
0057 #if _LIBCPP_STD_VER >= 14
0058 
0059 // If the comparison operation is equivalent to < and that is a total order, we know that we can use equality comparison
0060 // on that type instead to extract some information. Furthermore, if equality comparison on that type is trivial, the
0061 // user can't observe that we're calling it. So instead of using the user-provided total order, we use std::mismatch,
0062 // which uses equality comparison (and is vertorized). Additionally, if the type is trivially lexicographically
0063 // comparable, we can go one step further and use std::memcmp directly instead of calling std::mismatch.
0064 template <class _Tp,
0065           class _Proj1,
0066           class _Proj2,
0067           class _Comp,
0068           __enable_if_t<__desugars_to_v<__totally_ordered_less_tag, _Comp, _Tp, _Tp> && !is_volatile<_Tp>::value &&
0069                             __libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value &&
0070                             __is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
0071                         int> = 0>
0072 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0073 __lexicographical_compare(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Tp* __last2, _Comp&, _Proj1&, _Proj2&) {
0074   if constexpr (__is_trivially_lexicographically_comparable_v<_Tp, _Tp>) {
0075     auto __res =
0076         std::__constexpr_memcmp(__first1, __first2, __element_count(std::min(__last1 - __first1, __last2 - __first2)));
0077     if (__res == 0)
0078       return __last1 - __first1 < __last2 - __first2;
0079     return __res < 0;
0080   }
0081 #  if _LIBCPP_HAS_WIDE_CHARACTERS
0082   else if constexpr (is_same<__remove_cv_t<_Tp>, wchar_t>::value) {
0083     auto __res = std::__constexpr_wmemcmp(__first1, __first2, std::min(__last1 - __first1, __last2 - __first2));
0084     if (__res == 0)
0085       return __last1 - __first1 < __last2 - __first2;
0086     return __res < 0;
0087   }
0088 #  endif // _LIBCPP_HAS_WIDE_CHARACTERS
0089   else {
0090     auto __res = std::mismatch(__first1, __last1, __first2, __last2);
0091     if (__res.second == __last2)
0092       return false;
0093     if (__res.first == __last1)
0094       return true;
0095     return *__res.first < *__res.second;
0096   }
0097 }
0098 
0099 #endif // _LIBCPP_STD_VER >= 14
0100 
0101 template <class _InputIterator1, class _InputIterator2, class _Compare>
0102 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool lexicographical_compare(
0103     _InputIterator1 __first1,
0104     _InputIterator1 __last1,
0105     _InputIterator2 __first2,
0106     _InputIterator2 __last2,
0107     _Compare __comp) {
0108   __identity __proj;
0109   return std::__lexicographical_compare(
0110       std::__unwrap_iter(__first1),
0111       std::__unwrap_iter(__last1),
0112       std::__unwrap_iter(__first2),
0113       std::__unwrap_iter(__last2),
0114       __comp,
0115       __proj,
0116       __proj);
0117 }
0118 
0119 template <class _InputIterator1, class _InputIterator2>
0120 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool lexicographical_compare(
0121     _InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
0122   return std::lexicographical_compare(__first1, __last1, __first2, __last2, __less<>());
0123 }
0124 
0125 _LIBCPP_END_NAMESPACE_STD
0126 
0127 _LIBCPP_POP_MACROS
0128 
0129 #endif // _LIBCPP___ALGORITHM_LEXICOGRAPHICAL_COMPARE_H