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_CONSTEXPR_SINCE_CXX14 _Iter __find_generic(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
51 return std::__find_if(
52 std::move(__first),
53 std::move(__last),
54 [&]<class _ValT>(_ValT&& __val) -> bool { return __val == __value; },
55 __proj);
56}
57
58template <class _Iter, class _Sent, class _Tp, class _Proj>
59_LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter __find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
60 return std::__find_generic(std::move(__first), std::move(__last), __value, __proj);
61}
62
63#if _LIBCPP_VECTORIZE_ALGORITHMS
64template <class _Tp, class _Up>
65[[__nodiscard__]]
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_generic(__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_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 // wmemchr is very slow on apple platforms - we can do significantly better on our own.
128 // TODO: Remove this special-case once Apple's wmemchr is fixed.
129# if _LIBCPP_HAS_WIDE_CHARACTERS && !defined(__APPLE__)
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_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
158 if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
159 return __last;
160 return std::__find(__first, __last, _Tp(__value), __proj);
161}
162
163// __bit_iterator implementation
164template <bool _ToFind, class _Cp, bool _IsConst>
165_LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
166__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
167 using _It = __bit_iterator<_Cp, _IsConst>;
168 using __storage_type = typename _It::__storage_type;
169
170 const int __bits_per_word = _It::__bits_per_word;
171 // do first partial word
172 if (__first.__ctz_ != 0) {
173 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
174 __storage_type __dn = std::min(__clz_f, __n);
175 __storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
176 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
177 if (__b)
178 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
179 if (__n == __dn)
180 return __first + __n;
181 __n -= __dn;
182 ++__first.__seg_;
183 }
184 // do middle whole words
185 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
186 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
187 if (__b)
188 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
189 }
190 // do last partial word
191 if (__n > 0) {
192 __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
193 __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
194 if (__b)
195 return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
196 }
197 return _It(__first.__seg_, static_cast<unsigned>(__n));
198}
199
200template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
201inline _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
202__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
203 if (static_cast<bool>(__value))
204 return std::__find_bool<true>(
205 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
206 return std::__find_bool<false>(
207 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
208}
209
210// segmented iterator implementation
211
212template <class _SegmentedIterator,
213 class _Tp,
214 class _Proj,
215 __enable_if_t<__is_segmented_iterator_v<_SegmentedIterator>, int> = 0>
216_LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
217__find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
218 using __local_iterator = typename __segmented_iterator_traits<_SegmentedIterator>::__local_iterator;
219 return std::__find_segment_if(
220 std::move(__first), std::move(__last), [&__value, &__proj](__local_iterator __lfirst, __local_iterator __llast) {
221 return std::__rewrap_iter(
222 __lfirst, std::__find(std::__unwrap_iter(__lfirst), std::__unwrap_iter(__llast), __value, __proj));
223 });
224}
225
226// public API
227template <class _InputIterator, class _Tp>
228[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
229find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
230 __identity __proj;
231 return std::__rewrap_iter(
232 __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
233}
234
235_LIBCPP_END_NAMESPACE_STD
236
237_LIBCPP_POP_MACROS
238
239#endif // _LIBCPP___ALGORITHM_FIND_H
240