File indexing completed on 2026-05-03 08:13:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___ALGORITHM_COUNT_H
0011 #define _LIBCPP___ALGORITHM_COUNT_H
0012
0013 #include <__algorithm/iterator_operations.h>
0014 #include <__algorithm/min.h>
0015 #include <__bit/invert_if.h>
0016 #include <__bit/popcount.h>
0017 #include <__config>
0018 #include <__functional/identity.h>
0019 #include <__fwd/bit_reference.h>
0020 #include <__iterator/iterator_traits.h>
0021 #include <__type_traits/enable_if.h>
0022 #include <__type_traits/invoke.h>
0023
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 # pragma GCC system_header
0026 #endif
0027
0028 _LIBCPP_PUSH_MACROS
0029 #include <__undef_macros>
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032
0033
0034 template <class _AlgPolicy, class _Iter, class _Sent, class _Tp, class _Proj>
0035 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter>
0036 __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
0037 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __r(0);
0038 for (; __first != __last; ++__first)
0039 if (std::__invoke(__proj, *__first) == __value)
0040 ++__r;
0041 return __r;
0042 }
0043
0044
0045 template <bool _ToCount, class _Cp, bool _IsConst>
0046 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
0047 __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
0048 using _It = __bit_iterator<_Cp, _IsConst>;
0049 using __storage_type = typename _It::__storage_type;
0050 using difference_type = typename _It::difference_type;
0051
0052 const int __bits_per_word = _It::__bits_per_word;
0053 difference_type __r = 0;
0054
0055 if (__first.__ctz_ != 0) {
0056 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
0057 __storage_type __dn = std::min(__clz_f, __n);
0058 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
0059 __r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
0060 __n -= __dn;
0061 ++__first.__seg_;
0062 }
0063
0064 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
0065 __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
0066
0067 if (__n > 0) {
0068 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
0069 __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
0070 }
0071 return __r;
0072 }
0073
0074 template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
0075 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
0076 __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
0077 if (__value)
0078 return std::__count_bool<true>(
0079 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
0080 return std::__count_bool<false>(
0081 __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
0082 }
0083
0084 template <class _InputIterator, class _Tp>
0085 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<_InputIterator>
0086 count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
0087 __identity __proj;
0088 return std::__count<_ClassicAlgPolicy>(__first, __last, __value, __proj);
0089 }
0090
0091 _LIBCPP_END_NAMESPACE_STD
0092
0093 _LIBCPP_POP_MACROS
0094
0095 #endif