1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___ALGORITHM_FIND_H 11 #define _LIBCPP___ALGORITHM_FIND_H 12 13 #include <__algorithm/min.h> 14 #include <__algorithm/unwrap_iter.h> 15 #include <__bit/countr.h> 16 #include <__bit/invert_if.h> 17 #include <__config> 18 #include <__functional/identity.h> 19 #include <__functional/invoke.h> 20 #include <__fwd/bit_reference.h> 21 #include <__string/constexpr_c_functions.h> 22 #include <__type_traits/is_same.h> 23 24 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 25 # include <cwchar> 26 #endif 27 28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 29 # pragma GCC system_header 30 #endif 31 32 _LIBCPP_PUSH_MACROS 33 #include <__undef_macros> 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 // generic implementation 38 template <class _Iter, class _Sent, class _Tp, class _Proj> 39 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter 40 __find_impl(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) { 41 for (; __first != __last; ++__first) 42 if (std::__invoke(__proj, *__first) == __value) 43 break; 44 return __first; 45 } 46 47 // trivially equality comparable implementations 48 template <class _Tp, 49 class _Up, 50 class _Proj, 51 __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value && 52 sizeof(_Tp) == 1, 53 int> = 0> 54 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* 55 __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) { 56 if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first)) 57 return __ret; 58 return __last; 59 } 60 61 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 62 template <class _Tp, 63 class _Up, 64 class _Proj, 65 __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value && 66 sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t), 67 int> = 0> 68 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* 69 __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) { 70 if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first)) 71 return __ret; 72 return __last; 73 } 74 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 75 76 // __bit_iterator implementation 77 template <bool _ToFind, class _Cp, bool _IsConst> 78 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst> 79 __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { 80 using _It = __bit_iterator<_Cp, _IsConst>; 81 using __storage_type = typename _It::__storage_type; 82 83 const int __bits_per_word = _It::__bits_per_word; 84 // do first partial word 85 if (__first.__ctz_ != 0) { 86 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 87 __storage_type __dn = std::min(__clz_f, __n); 88 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 89 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m; 90 if (__b) 91 return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b))); 92 if (__n == __dn) 93 return __first + __n; 94 __n -= __dn; 95 ++__first.__seg_; 96 } 97 // do middle whole words 98 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) { 99 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_); 100 if (__b) 101 return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b))); 102 } 103 // do last partial word 104 if (__n > 0) { 105 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 106 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m; 107 if (__b) 108 return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b))); 109 } 110 return _It(__first.__seg_, static_cast<unsigned>(__n)); 111 } 112 113 template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0> 114 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst> 115 __find_impl(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { 116 if (static_cast<bool>(__value)) 117 return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first)); 118 return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first)); 119 } 120 121 template <class _InputIterator, class _Tp> 122 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator 123 find(_InputIterator __first, _InputIterator __last, const _Tp& __value) { 124 __identity __proj; 125 return std::__rewrap_iter( 126 __first, std::__find_impl(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj)); 127 } 128 129 _LIBCPP_END_NAMESPACE_STD 130 131 _LIBCPP_POP_MACROS 132 133 #endif // _LIBCPP___ALGORITHM_FIND_H 134