Back to home page

EIC code displayed by LXR

 
 

    


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

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