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_if.h>
14#include <__algorithm/find_segment_if.h>
15#include <__algorithm/min.h>
16#include <__algorithm/simd_utils.h>
17#include <__algorithm/unwrap_iter.h>
18#include <__bit/countr.h>
19#include <__bit/invert_if.h>
20#include <__config>
21#include <__cstddef/size_t.h>
22#include <__functional/identity.h>
23#include <__fwd/bit_reference.h>
24#include <__iterator/segmented_iterator.h>
25#include <__string/constexpr_c_functions.h>
26#include <__type_traits/enable_if.h>
27#include <__type_traits/invoke.h>
28#include <__type_traits/is_constant_evaluated.h>
29#include <__type_traits/is_equality_comparable.h>
30#include <__type_traits/is_integral.h>
31#include <__type_traits/is_signed.h>
32#include <__utility/move.h>
33#include <limits>
34
35#if _LIBCPP_HAS_WIDE_CHARACTERS
36# include <cwchar>
37#endif
38
39#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
40# pragma GCC system_header
41#endif
42
43_LIBCPP_PUSH_MACROS
44#include <__undef_macros>
45
46_LIBCPP_BEGIN_NAMESPACE_STD
47
48// generic implementation
49template <class _Iter, class _Sent, class _Tp, class _Proj>
50_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
51__find_generic(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
52 return std::__find_if(
53 std::move(__first),
54 std::move(__last),
55 [&]<class _ValT>(_ValT&& __val) -> bool { return __val == __value; },
56 __proj);
57}
58
59template <class _Iter, class _Sent, class _Tp, class _Proj>
60_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
61__find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
62 return std::__find_generic(std::move(__first), std::move(__last), __value, __proj);
63}
64
65#if _LIBCPP_VECTORIZE_ALGORITHMS
66template <class _Tp, class _Up>
67[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
68_LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find_vectorized(_Tp* __first, _Tp* __last, _Up __value) {
69 if (!__libcpp_is_constant_evaluated()) {
70 constexpr size_t __unroll_count = 4;
71 constexpr size_t __vec_size = __native_vector_size<_Tp>;
72 using __vec = __simd_vector<_Tp, __vec_size>;
73
74 auto __orig_first = __first;
75
76 auto __values = static_cast<__simd_vector<_Tp, __vec_size>>(__value); // broadcast the value
77 while (static_cast<size_t>(__last - __first) >= __unroll_count * __vec_size) [[__unlikely__]] {
78 __vec __lhs[__unroll_count];
79
80 for (size_t __i = 0; __i != __unroll_count; ++__i)
81 __lhs[__i] = std::__load_vector<__vec>(__first + __i * __vec_size);
82
83 for (size_t __i = 0; __i != __unroll_count; ++__i) {
84 if (auto __cmp_res = __lhs[__i] == __values; std::__any_of(__cmp_res)) {
85 auto __offset = __i * __vec_size + std::__find_first_set(__cmp_res);
86 return __first + __offset;
87 }
88 }
89
90 __first += __unroll_count * __vec_size;
91 }
92
93 // check the remaining 0-3 vectors
94 while (static_cast<size_t>(__last - __first) >= __vec_size) {
95 if (auto __cmp_res = std::__load_vector<__vec>(__first) == __values; std::__any_of(__cmp_res)) {
96 return __first + std::__find_first_set(__cmp_res);
97 }
98 __first += __vec_size;
99 }
100
101 if (__last - __first == 0)
102 return __first;
103
104 // Check if we can load elements in front of the current pointer. If that's the case load a vector at
105 // (last - vector_size) to check the remaining elements
106 if (static_cast<size_t>(__first - __orig_first) >= __vec_size) {
107 __first = __last - __vec_size;
108 return __first + std::__find_first_set(std::__load_vector<__vec>(__first) == __values);
109 }
110 }
111
112 __identity __proj;
113 return std::__find_generic(__first, __last, __value, __proj);
114}
115#endif
116
117#ifndef _LIBCPP_CXX03_LANG
118// trivially equality comparable implementations
119template <class _Tp,
120 class _Up,
121 class _Proj,
122 __enable_if_t<__is_identity<_Proj>::value && __is_trivially_equality_comparable_v<_Tp, _Up>, int> = 0>
123_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
124 if constexpr (sizeof(_Tp) == 1) {
125 if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))
126 return __ret;
127 return __last;
128 }
129# if _LIBCPP_HAS_WIDE_CHARACTERS
130 else if constexpr (sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t)) {
131 if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))
132 return __ret;
133 return __last;
134 }
135# endif
136# if _LIBCPP_VECTORIZE_ALGORITHMS
137 else if constexpr (is_integral<_Tp>::value) {
138 return std::__find_vectorized(__first, __last, __value);
139 }
140# endif
141 else {
142 __identity __proj;
143 return std::__find_generic(__first, __last, __value, __proj);
144 }
145}
146#endif
147
148// TODO: This should also be possible to get right with different signedness
149// cast integral types to allow vectorization
150template <class _Tp,
151 class _Up,
152 class _Proj,
153 __enable_if_t<__is_identity<_Proj>::value && !__is_trivially_equality_comparable_v<_Tp, _Up> &&
154 is_integral<_Tp>::value && is_integral<_Up>::value &&
155 is_signed<_Tp>::value == is_signed<_Up>::value,
156 int> = 0>
157_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
158__find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
159 if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
160 return __last;
161 return std::__find(__first, __last, _Tp(__value), __proj);
162}
163
164// __bit_iterator implementation
165template <bool _ToFind, class _Cp, bool _IsConst>
166_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
167__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
168 using _It = __bit_iterator<_Cp, _IsConst>;
169 using __storage_type = typename _It::__storage_type;
170
171 const int __bits_per_word = _It::__bits_per_word;
172 // do first partial word
173 if (__first.__ctz_ != 0) {
174 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
175 __storage_type __dn = std::min(__clz_f, __n);
176 __storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
177 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
178 if (__b)
179 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
180 if (__n == __dn)
181 return __first + __n;
182 __n -= __dn;
183 ++__first.__seg_;
184 }
185 // do middle whole words
186 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
187 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
188 if (__b)
189 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
190 }
191 // do last partial word
192 if (__n > 0) {
193 __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
194 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
195 if (__b)
196 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
197 }
198 return _It(__first.__seg_, static_cast<unsigned>(__n));
199}
200
201template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
202inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
203__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
204 if (static_cast<bool>(__value))
205 return std::__find_bool<true>(
206 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
207 return std::__find_bool<false>(
208 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
209}
210
211// segmented iterator implementation
212
213template <class _SegmentedIterator,
214 class _Tp,
215 class _Proj,
216 __enable_if_t<__is_segmented_iterator_v<_SegmentedIterator>, int> = 0>
217_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
218__find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
219 using __local_iterator = typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator;
220 return std::__find_segment_if(
221 std::move(__first), std::move(__last), [&__value, &__proj](__local_iterator __lfirst, __local_iterator __llast) {
222 return std::__rewrap_iter(
223 __lfirst, std::__find(std::__unwrap_iter(__lfirst), std::__unwrap_iter(__llast), __value, __proj));
224 });
225}
226
227// public API
228template <class _InputIterator, class _Tp>
229[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
230find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
231 __identity __proj;
232 return std::__rewrap_iter(
233 __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
234}
235
236_LIBCPP_END_NAMESPACE_STD
237
238_LIBCPP_POP_MACROS
239
240#endif // _LIBCPP___ALGORITHM_FIND_H
241