Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/find.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ALGORITHM_FIND_H10#define _LIBCPP___ALGORITHM_FIND_H1112#include <__algorithm/find_segment_if.h>13#include <__algorithm/min.h>14#include <__algorithm/unwrap_iter.h>15#include <__bit/countr.h>16#include <__bit/invert_if.h>17#include <__config>18#include <__functional/identity.h>19#include <__functional/invoke.h>20#include <__fwd/bit_reference.h>21#include <__iterator/segmented_iterator.h>22#include <__string/constexpr_c_functions.h>23#include <__type_traits/is_integral.h>24#include <__type_traits/is_same.h>25#include <__type_traits/is_signed.h>26#include <__utility/move.h>27#include <limits>2829#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS30# include <cwchar>31#endif3233#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)34# pragma GCC system_header35#endif3637_LIBCPP_PUSH_MACROS38#include <__undef_macros>3940_LIBCPP_BEGIN_NAMESPACE_STD4142// generic implementation43template <class _Iter, class _Sent, class _Tp, class _Proj>44_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter45__find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {46for (; __first != __last; ++__first)47if (std::__invoke(__proj, *__first) == __value)48break;49return __first;50}5152// trivially equality comparable implementations53template <class _Tp,54class _Up,55class _Proj,56__enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&57sizeof(_Tp) == 1,58int> = 0>59_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {60if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))61return __ret;62return __last;63}6465#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS66template <class _Tp,67class _Up,68class _Proj,69__enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&70sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t),71int> = 0>72_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {73if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))74return __ret;75return __last;76}77#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS7879// TODO: This should also be possible to get right with different signedness80// cast integral types to allow vectorization81template <class _Tp,82class _Up,83class _Proj,84__enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&85is_integral<_Tp>::value && is_integral<_Up>::value &&86is_signed<_Tp>::value == is_signed<_Up>::value,87int> = 0>88_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*89__find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {90if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())91return __last;92return std::__find(__first, __last, _Tp(__value), __proj);93}9495// __bit_iterator implementation96template <bool _ToFind, class _Cp, bool _IsConst>97_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>98__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {99using _It = __bit_iterator<_Cp, _IsConst>;100using __storage_type = typename _It::__storage_type;101102const int __bits_per_word = _It::__bits_per_word;103// do first partial word104if (__first.__ctz_ != 0) {105__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);106__storage_type __dn = std::min(__clz_f, __n);107__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));108__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;109if (__b)110return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));111if (__n == __dn)112return __first + __n;113__n -= __dn;114++__first.__seg_;115}116// do middle whole words117for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {118__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);119if (__b)120return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));121}122// do last partial word123if (__n > 0) {124__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);125__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;126if (__b)127return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));128}129return _It(__first.__seg_, static_cast<unsigned>(__n));130}131132template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>133inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>134__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {135if (static_cast<bool>(__value))136return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));137return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));138}139140// segmented iterator implementation141142template <class>143struct __find_segment;144145template <class _SegmentedIterator,146class _Tp,147class _Proj,148__enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>149_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator150__find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {151return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);152}153154template <class _Tp>155struct __find_segment {156const _Tp& __value_;157158_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {}159160template <class _InputIterator, class _Proj>161_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator162operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {163return std::__find(__first, __last, __value_, __proj);164}165};166167// public API168template <class _InputIterator, class _Tp>169_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator170find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {171__identity __proj;172return std::__rewrap_iter(173__first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));174}175176_LIBCPP_END_NAMESPACE_STD177178_LIBCPP_POP_MACROS179180#endif // _LIBCPP___ALGORITHM_FIND_H181182183