Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___ALGORITHM_FIND_H
0011 #define _LIBCPP___ALGORITHM_FIND_H
0012 
0013 #include <__algorithm/find_segment_if.h>
0014 #include <__algorithm/min.h>
0015 #include <__algorithm/unwrap_iter.h>
0016 #include <__bit/countr.h>
0017 #include <__bit/invert_if.h>
0018 #include <__config>
0019 #include <__functional/identity.h>
0020 #include <__fwd/bit_reference.h>
0021 #include <__iterator/segmented_iterator.h>
0022 #include <__string/constexpr_c_functions.h>
0023 #include <__type_traits/enable_if.h>
0024 #include <__type_traits/invoke.h>
0025 #include <__type_traits/is_equality_comparable.h>
0026 #include <__type_traits/is_integral.h>
0027 #include <__type_traits/is_signed.h>
0028 #include <__utility/move.h>
0029 #include <limits>
0030 
0031 #if _LIBCPP_HAS_WIDE_CHARACTERS
0032 #  include <cwchar>
0033 #endif
0034 
0035 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0036 #  pragma GCC system_header
0037 #endif
0038 
0039 _LIBCPP_PUSH_MACROS
0040 #include <__undef_macros>
0041 
0042 _LIBCPP_BEGIN_NAMESPACE_STD
0043 
0044 // generic implementation
0045 template <class _Iter, class _Sent, class _Tp, class _Proj>
0046 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
0047 __find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
0048   for (; __first != __last; ++__first)
0049     if (std::__invoke(__proj, *__first) == __value)
0050       break;
0051   return __first;
0052 }
0053 
0054 // trivially equality comparable implementations
0055 template <class _Tp,
0056           class _Up,
0057           class _Proj,
0058           __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
0059                             sizeof(_Tp) == 1,
0060                         int> = 0>
0061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
0062   if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))
0063     return __ret;
0064   return __last;
0065 }
0066 
0067 #if _LIBCPP_HAS_WIDE_CHARACTERS
0068 template <class _Tp,
0069           class _Up,
0070           class _Proj,
0071           __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
0072                             sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t),
0073                         int> = 0>
0074 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
0075   if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))
0076     return __ret;
0077   return __last;
0078 }
0079 #endif // _LIBCPP_HAS_WIDE_CHARACTERS
0080 
0081 // TODO: This should also be possible to get right with different signedness
0082 // cast integral types to allow vectorization
0083 template <class _Tp,
0084           class _Up,
0085           class _Proj,
0086           __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
0087                             is_integral<_Tp>::value && is_integral<_Up>::value &&
0088                             is_signed<_Tp>::value == is_signed<_Up>::value,
0089                         int> = 0>
0090 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
0091 __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
0092   if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
0093     return __last;
0094   return std::__find(__first, __last, _Tp(__value), __proj);
0095 }
0096 
0097 // __bit_iterator implementation
0098 template <bool _ToFind, class _Cp, bool _IsConst>
0099 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
0100 __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
0101   using _It            = __bit_iterator<_Cp, _IsConst>;
0102   using __storage_type = typename _It::__storage_type;
0103 
0104   const int __bits_per_word = _It::__bits_per_word;
0105   // do first partial word
0106   if (__first.__ctz_ != 0) {
0107     __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
0108     __storage_type __dn    = std::min(__clz_f, __n);
0109     __storage_type __m     = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
0110     __storage_type __b     = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
0111     if (__b)
0112       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
0113     if (__n == __dn)
0114       return __first + __n;
0115     __n -= __dn;
0116     ++__first.__seg_;
0117   }
0118   // do middle whole words
0119   for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
0120     __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
0121     if (__b)
0122       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
0123   }
0124   // do last partial word
0125   if (__n > 0) {
0126     __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
0127     __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
0128     if (__b)
0129       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
0130   }
0131   return _It(__first.__seg_, static_cast<unsigned>(__n));
0132 }
0133 
0134 template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
0135 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
0136 __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
0137   if (static_cast<bool>(__value))
0138     return std::__find_bool<true>(
0139         __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
0140   return std::__find_bool<false>(
0141       __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
0142 }
0143 
0144 // segmented iterator implementation
0145 
0146 template <class>
0147 struct __find_segment;
0148 
0149 template <class _SegmentedIterator,
0150           class _Tp,
0151           class _Proj,
0152           __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
0153 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
0154 __find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
0155   return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);
0156 }
0157 
0158 template <class _Tp>
0159 struct __find_segment {
0160   const _Tp& __value_;
0161 
0162   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {}
0163 
0164   template <class _InputIterator, class _Proj>
0165   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator
0166   operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {
0167     return std::__find(__first, __last, __value_, __proj);
0168   }
0169 };
0170 
0171 // public API
0172 template <class _InputIterator, class _Tp>
0173 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
0174 find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
0175   __identity __proj;
0176   return std::__rewrap_iter(
0177       __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
0178 }
0179 
0180 _LIBCPP_END_NAMESPACE_STD
0181 
0182 _LIBCPP_POP_MACROS
0183 
0184 #endif // _LIBCPP___ALGORITHM_FIND_H