find.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef _LIBCPP___ALGORITHM_FIND_H
  10. #define _LIBCPP___ALGORITHM_FIND_H
  11. #include <__algorithm/find_segment_if.h>
  12. #include <__algorithm/min.h>
  13. #include <__algorithm/unwrap_iter.h>
  14. #include <__bit/countr.h>
  15. #include <__bit/invert_if.h>
  16. #include <__config>
  17. #include <__functional/identity.h>
  18. #include <__functional/invoke.h>
  19. #include <__fwd/bit_reference.h>
  20. #include <__iterator/segmented_iterator.h>
  21. #include <__string/constexpr_c_functions.h>
  22. #include <__type_traits/is_integral.h>
  23. #include <__type_traits/is_same.h>
  24. #include <__type_traits/is_signed.h>
  25. #include <__utility/move.h>
  26. #include <limits>
  27. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  28. # include <cwchar>
  29. #endif
  30. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  31. # pragma GCC system_header
  32. #endif
  33. _LIBCPP_PUSH_MACROS
  34. #include <__undef_macros>
  35. _LIBCPP_BEGIN_NAMESPACE_STD
  36. // generic implementation
  37. template <class _Iter, class _Sent, class _Tp, class _Proj>
  38. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
  39. __find_impl(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
  40. for (; __first != __last; ++__first)
  41. if (std::__invoke(__proj, *__first) == __value)
  42. break;
  43. return __first;
  44. }
  45. // trivially equality comparable implementations
  46. template <class _Tp,
  47. class _Up,
  48. class _Proj,
  49. __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
  50. sizeof(_Tp) == 1,
  51. int> = 0>
  52. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
  53. __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
  54. if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))
  55. return __ret;
  56. return __last;
  57. }
  58. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  59. template <class _Tp,
  60. class _Up,
  61. class _Proj,
  62. __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
  63. sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t),
  64. int> = 0>
  65. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
  66. __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
  67. if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))
  68. return __ret;
  69. return __last;
  70. }
  71. #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  72. // TODO: This should also be possible to get right with different signedness
  73. // cast integral types to allow vectorization
  74. template <class _Tp,
  75. class _Up,
  76. class _Proj,
  77. __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
  78. is_integral<_Tp>::value && is_integral<_Up>::value &&
  79. is_signed<_Tp>::value == is_signed<_Up>::value,
  80. int> = 0>
  81. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
  82. __find_impl(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
  83. if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
  84. return __last;
  85. return std::__find_impl(__first, __last, _Tp(__value), __proj);
  86. }
  87. // __bit_iterator implementation
  88. template <bool _ToFind, class _Cp, bool _IsConst>
  89. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
  90. __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
  91. using _It = __bit_iterator<_Cp, _IsConst>;
  92. using __storage_type = typename _It::__storage_type;
  93. const int __bits_per_word = _It::__bits_per_word;
  94. // do first partial word
  95. if (__first.__ctz_ != 0) {
  96. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  97. __storage_type __dn = std::min(__clz_f, __n);
  98. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  99. __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
  100. if (__b)
  101. return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
  102. if (__n == __dn)
  103. return __first + __n;
  104. __n -= __dn;
  105. ++__first.__seg_;
  106. }
  107. // do middle whole words
  108. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
  109. __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
  110. if (__b)
  111. return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
  112. }
  113. // do last partial word
  114. if (__n > 0) {
  115. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  116. __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
  117. if (__b)
  118. return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
  119. }
  120. return _It(__first.__seg_, static_cast<unsigned>(__n));
  121. }
  122. template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
  123. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
  124. __find_impl(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
  125. if (static_cast<bool>(__value))
  126. return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
  127. return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
  128. }
  129. // segmented iterator implementation
  130. template <class>
  131. struct __find_segment;
  132. template <class _SegmentedIterator,
  133. class _Tp,
  134. class _Proj,
  135. __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
  136. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
  137. __find_impl(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
  138. return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);
  139. }
  140. template <class _Tp>
  141. struct __find_segment {
  142. const _Tp& __value_;
  143. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {}
  144. template <class _InputIterator, class _Proj>
  145. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator
  146. operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {
  147. return std::__find_impl(__first, __last, __value_, __proj);
  148. }
  149. };
  150. // public API
  151. template <class _InputIterator, class _Tp>
  152. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
  153. find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
  154. __identity __proj;
  155. return std::__rewrap_iter(
  156. __first, std::__find_impl(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
  157. }
  158. _LIBCPP_END_NAMESPACE_STD
  159. _LIBCPP_POP_MACROS
  160. #endif // _LIBCPP___ALGORITHM_FIND_H