xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__algorithm/find.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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___CXX03___ALGORITHM_FIND_H
11 #define _LIBCPP___CXX03___ALGORITHM_FIND_H
12 
13 #include <__cxx03/__algorithm/find_segment_if.h>
14 #include <__cxx03/__algorithm/min.h>
15 #include <__cxx03/__algorithm/unwrap_iter.h>
16 #include <__cxx03/__bit/countr.h>
17 #include <__cxx03/__bit/invert_if.h>
18 #include <__cxx03/__config>
19 #include <__cxx03/__functional/identity.h>
20 #include <__cxx03/__fwd/bit_reference.h>
21 #include <__cxx03/__iterator/segmented_iterator.h>
22 #include <__cxx03/__string/constexpr_c_functions.h>
23 #include <__cxx03/__type_traits/is_integral.h>
24 #include <__cxx03/__type_traits/is_same.h>
25 #include <__cxx03/__type_traits/is_signed.h>
26 #include <__cxx03/__utility/move.h>
27 #include <__cxx03/limits>
28 
29 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
30 #  include <__cxx03/cwchar>
31 #endif
32 
33 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34 #  pragma GCC system_header
35 #endif
36 
37 _LIBCPP_PUSH_MACROS
38 #include <__cxx03/__undef_macros>
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 // generic implementation
43 template <class _Iter, class _Sent, class _Tp, class _Proj>
__find(_Iter __first,_Sent __last,const _Tp & __value,_Proj & __proj)44 _LIBCPP_HIDE_FROM_ABI _Iter __find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
45   for (; __first != __last; ++__first)
46     if (std::__invoke(__proj, *__first) == __value)
47       break;
48   return __first;
49 }
50 
51 // trivially equality comparable implementations
52 template <class _Tp,
53           class _Up,
54           class _Proj,
55           __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
56                             sizeof(_Tp) == 1,
57                         int> = 0>
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj &)58 _LIBCPP_HIDE_FROM_ABI _Tp* __find(_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>
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj &)71 _LIBCPP_HIDE_FROM_ABI _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {
72   if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))
73     return __ret;
74   return __last;
75 }
76 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
77 
78 // TODO: This should also be possible to get right with different signedness
79 // cast integral types to allow vectorization
80 template <class _Tp,
81           class _Up,
82           class _Proj,
83           __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&
84                             is_integral<_Tp>::value && is_integral<_Up>::value &&
85                             is_signed<_Tp>::value == is_signed<_Up>::value,
86                         int> = 0>
__find(_Tp * __first,_Tp * __last,const _Up & __value,_Proj & __proj)87 _LIBCPP_HIDE_FROM_ABI _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
88   if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())
89     return __last;
90   return std::__find(__first, __last, _Tp(__value), __proj);
91 }
92 
93 // __bit_iterator implementation
94 template <bool _ToFind, class _Cp, bool _IsConst>
95 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
__find_bool(__bit_iterator<_Cp,_IsConst> __first,typename _Cp::size_type __n)96 __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
97   using _It            = __bit_iterator<_Cp, _IsConst>;
98   using __storage_type = typename _It::__storage_type;
99 
100   const int __bits_per_word = _It::__bits_per_word;
101   // do first partial word
102   if (__first.__ctz_ != 0) {
103     __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
104     __storage_type __dn    = std::min(__clz_f, __n);
105     __storage_type __m     = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
106     __storage_type __b     = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
107     if (__b)
108       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
109     if (__n == __dn)
110       return __first + __n;
111     __n -= __dn;
112     ++__first.__seg_;
113   }
114   // do middle whole words
115   for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
116     __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
117     if (__b)
118       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
119   }
120   // do last partial word
121   if (__n > 0) {
122     __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
123     __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
124     if (__b)
125       return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
126   }
127   return _It(__first.__seg_, static_cast<unsigned>(__n));
128 }
129 
130 template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
131 inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
__find(__bit_iterator<_Cp,_IsConst> __first,__bit_iterator<_Cp,_IsConst> __last,const _Tp & __value,_Proj &)132 __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
133   if (static_cast<bool>(__value))
134     return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
135   return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
136 }
137 
138 // segmented iterator implementation
139 
140 template <class>
141 struct __find_segment;
142 
143 template <class _SegmentedIterator,
144           class _Tp,
145           class _Proj,
146           __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
147 _LIBCPP_HIDE_FROM_ABI _SegmentedIterator
__find(_SegmentedIterator __first,_SegmentedIterator __last,const _Tp & __value,_Proj & __proj)148 __find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {
149   return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);
150 }
151 
152 template <class _Tp>
153 struct __find_segment {
154   const _Tp& __value_;
155 
__find_segment__find_segment156   _LIBCPP_HIDE_FROM_ABI __find_segment(const _Tp& __value) : __value_(__value) {}
157 
158   template <class _InputIterator, class _Proj>
operator__find_segment159   _LIBCPP_HIDE_FROM_ABI _InputIterator operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {
160     return std::__find(__first, __last, __value_, __proj);
161   }
162 };
163 
164 // public API
165 template <class _InputIterator, class _Tp>
166 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _InputIterator
find(_InputIterator __first,_InputIterator __last,const _Tp & __value)167 find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
168   __identity __proj;
169   return std::__rewrap_iter(
170       __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));
171 }
172 
173 _LIBCPP_END_NAMESPACE_STD
174 
175 _LIBCPP_POP_MACROS
176 
177 #endif // _LIBCPP___CXX03___ALGORITHM_FIND_H
178