1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric
10fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_FIND_H
11fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_FIND_H
12fe6060f1SDimitry Andric
135f757f3fSDimitry Andric #include <__algorithm/find_segment_if.h>
145f757f3fSDimitry Andric #include <__algorithm/min.h>
1506c3fb27SDimitry Andric #include <__algorithm/unwrap_iter.h>
165f757f3fSDimitry Andric #include <__bit/countr.h>
175f757f3fSDimitry Andric #include <__bit/invert_if.h>
18fe6060f1SDimitry Andric #include <__config>
1906c3fb27SDimitry Andric #include <__functional/identity.h>
2006c3fb27SDimitry Andric #include <__functional/invoke.h>
215f757f3fSDimitry Andric #include <__fwd/bit_reference.h>
225f757f3fSDimitry Andric #include <__iterator/segmented_iterator.h>
2306c3fb27SDimitry Andric #include <__string/constexpr_c_functions.h>
24cb14a3feSDimitry Andric #include <__type_traits/is_integral.h>
2506c3fb27SDimitry Andric #include <__type_traits/is_same.h>
26cb14a3feSDimitry Andric #include <__type_traits/is_signed.h>
275f757f3fSDimitry Andric #include <__utility/move.h>
28cb14a3feSDimitry Andric #include <limits>
2906c3fb27SDimitry Andric
3006c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3106c3fb27SDimitry Andric # include <cwchar>
3206c3fb27SDimitry Andric #endif
33fe6060f1SDimitry Andric
34fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
35fe6060f1SDimitry Andric # pragma GCC system_header
36fe6060f1SDimitry Andric #endif
37fe6060f1SDimitry Andric
385f757f3fSDimitry Andric _LIBCPP_PUSH_MACROS
395f757f3fSDimitry Andric #include <__undef_macros>
405f757f3fSDimitry Andric
41fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
42fe6060f1SDimitry Andric
435f757f3fSDimitry Andric // generic implementation
4406c3fb27SDimitry Andric template <class _Iter, class _Sent, class _Tp, class _Proj>
4506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
__find(_Iter __first,_Sent __last,const _Tp & __value,_Proj & __proj)46*0fca6ea1SDimitry Andric __find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
4706c3fb27SDimitry Andric for (; __first != __last; ++__first)
4806c3fb27SDimitry Andric if (std::__invoke(__proj, *__first) == __value)
4906c3fb27SDimitry Andric break;
5006c3fb27SDimitry Andric return __first;
5106c3fb27SDimitry Andric }
5206c3fb27SDimitry Andric
535f757f3fSDimitry Andric // trivially equality comparable implementations
5406c3fb27SDimitry Andric template <class _Tp,
5506c3fb27SDimitry Andric class _Up,
5606c3fb27SDimitry Andric class _Proj,
5706c3fb27SDimitry Andric __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
5806c3fb27SDimitry Andric sizeof(_Tp) == 1,
5906c3fb27SDimitry Andric int> = 0>
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj &)60*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
6106c3fb27SDimitry Andric if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))
6206c3fb27SDimitry Andric return __ret;
6306c3fb27SDimitry Andric return __last;
6406c3fb27SDimitry Andric }
6506c3fb27SDimitry Andric
6606c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
6706c3fb27SDimitry Andric template <class _Tp,
6806c3fb27SDimitry Andric class _Up,
6906c3fb27SDimitry Andric class _Proj,
7006c3fb27SDimitry Andric __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
7106c3fb27SDimitry Andric sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t),
7206c3fb27SDimitry Andric int> = 0>
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj &)73*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
7406c3fb27SDimitry Andric if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))
7506c3fb27SDimitry Andric return __ret;
7606c3fb27SDimitry Andric return __last;
7706c3fb27SDimitry Andric }
7806c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
7906c3fb27SDimitry Andric
80cb14a3feSDimitry Andric // TODO: This should also be possible to get right with different signedness
81cb14a3feSDimitry Andric // cast integral types to allow vectorization
82cb14a3feSDimitry Andric template <class _Tp,
83cb14a3feSDimitry Andric class _Up,
84cb14a3feSDimitry Andric class _Proj,
85cb14a3feSDimitry Andric __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
86cb14a3feSDimitry Andric is_integral<_Tp>::value && is_integral<_Up>::value &&
87cb14a3feSDimitry Andric is_signed<_Tp>::value == is_signed<_Up>::value,
88cb14a3feSDimitry Andric int> = 0>
89cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj & __proj)90*0fca6ea1SDimitry Andric __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
91cb14a3feSDimitry Andric if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
92cb14a3feSDimitry Andric return __last;
93*0fca6ea1SDimitry Andric return std::__find(__first, __last, _Tp(__value), __proj);
94cb14a3feSDimitry Andric }
95cb14a3feSDimitry Andric
965f757f3fSDimitry Andric // __bit_iterator implementation
975f757f3fSDimitry Andric template <bool _ToFind, class _Cp, bool _IsConst>
985f757f3fSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
__find_bool(__bit_iterator<_Cp,_IsConst> __first,typename _Cp::size_type __n)995f757f3fSDimitry Andric __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
1005f757f3fSDimitry Andric using _It = __bit_iterator<_Cp, _IsConst>;
1015f757f3fSDimitry Andric using __storage_type = typename _It::__storage_type;
1025f757f3fSDimitry Andric
1035f757f3fSDimitry Andric const int __bits_per_word = _It::__bits_per_word;
1045f757f3fSDimitry Andric // do first partial word
1055f757f3fSDimitry Andric if (__first.__ctz_ != 0) {
1065f757f3fSDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
1075f757f3fSDimitry Andric __storage_type __dn = std::min(__clz_f, __n);
1085f757f3fSDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
1095f757f3fSDimitry Andric __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
1105f757f3fSDimitry Andric if (__b)
1115f757f3fSDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
1125f757f3fSDimitry Andric if (__n == __dn)
1135f757f3fSDimitry Andric return __first + __n;
1145f757f3fSDimitry Andric __n -= __dn;
1155f757f3fSDimitry Andric ++__first.__seg_;
1165f757f3fSDimitry Andric }
1175f757f3fSDimitry Andric // do middle whole words
1185f757f3fSDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
1195f757f3fSDimitry Andric __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
1205f757f3fSDimitry Andric if (__b)
1215f757f3fSDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
1225f757f3fSDimitry Andric }
1235f757f3fSDimitry Andric // do last partial word
1245f757f3fSDimitry Andric if (__n > 0) {
1255f757f3fSDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
1265f757f3fSDimitry Andric __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
1275f757f3fSDimitry Andric if (__b)
1285f757f3fSDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
1295f757f3fSDimitry Andric }
1305f757f3fSDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(__n));
1315f757f3fSDimitry Andric }
1325f757f3fSDimitry Andric
1335f757f3fSDimitry Andric template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
1345f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
__find(__bit_iterator<_Cp,_IsConst> __first,__bit_iterator<_Cp,_IsConst> __last,const _Tp & __value,_Proj &)135*0fca6ea1SDimitry Andric __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
1365f757f3fSDimitry Andric if (static_cast<bool>(__value))
1375f757f3fSDimitry Andric return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
1385f757f3fSDimitry Andric return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
1395f757f3fSDimitry Andric }
1405f757f3fSDimitry Andric
1415f757f3fSDimitry Andric // segmented iterator implementation
1425f757f3fSDimitry Andric
1435f757f3fSDimitry Andric template <class>
1445f757f3fSDimitry Andric struct __find_segment;
1455f757f3fSDimitry Andric
1465f757f3fSDimitry Andric template <class _SegmentedIterator,
1475f757f3fSDimitry Andric class _Tp,
1485f757f3fSDimitry Andric class _Proj,
1495f757f3fSDimitry Andric __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
1505f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator
__find(_SegmentedIterator __first,_SegmentedIterator __last,const _Tp & __value,_Proj & __proj)151*0fca6ea1SDimitry Andric __find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
1525f757f3fSDimitry Andric return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);
1535f757f3fSDimitry Andric }
1545f757f3fSDimitry Andric
1555f757f3fSDimitry Andric template <class _Tp>
1565f757f3fSDimitry Andric struct __find_segment {
1575f757f3fSDimitry Andric const _Tp& __value_;
1585f757f3fSDimitry Andric
__find_segment__find_segment1595f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {}
1605f757f3fSDimitry Andric
1615f757f3fSDimitry Andric template <class _InputIterator, class _Proj>
1625f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator
operator__find_segment1635f757f3fSDimitry Andric operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {
164*0fca6ea1SDimitry Andric return std::__find(__first, __last, __value_, __proj);
1655f757f3fSDimitry Andric }
1665f757f3fSDimitry Andric };
1675f757f3fSDimitry Andric
1685f757f3fSDimitry Andric // public API
169fe6060f1SDimitry Andric template <class _InputIterator, class _Tp>
170*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
find(_InputIterator __first,_InputIterator __last,const _Tp & __value)171753f127fSDimitry Andric find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
17206c3fb27SDimitry Andric __identity __proj;
17306c3fb27SDimitry Andric return std::__rewrap_iter(
174*0fca6ea1SDimitry Andric __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
175fe6060f1SDimitry Andric }
176fe6060f1SDimitry Andric
177fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
178fe6060f1SDimitry Andric
1795f757f3fSDimitry Andric _LIBCPP_POP_MACROS
1805f757f3fSDimitry Andric
181fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_FIND_H
182