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