find.h 5.1 KB

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