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