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