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