1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric
10fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_COUNT_H
11fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_COUNT_H
12fe6060f1SDimitry Andric
135f757f3fSDimitry Andric #include <__algorithm/iterator_operations.h>
145f757f3fSDimitry Andric #include <__algorithm/min.h>
155f757f3fSDimitry Andric #include <__bit/invert_if.h>
165f757f3fSDimitry Andric #include <__bit/popcount.h>
17fe6060f1SDimitry Andric #include <__config>
185f757f3fSDimitry Andric #include <__functional/identity.h>
195f757f3fSDimitry Andric #include <__functional/invoke.h>
205f757f3fSDimitry Andric #include <__fwd/bit_reference.h>
21fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
22fe6060f1SDimitry Andric
23fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24fe6060f1SDimitry Andric # pragma GCC system_header
25fe6060f1SDimitry Andric #endif
26fe6060f1SDimitry Andric
275f757f3fSDimitry Andric _LIBCPP_PUSH_MACROS
285f757f3fSDimitry Andric #include <__undef_macros>
295f757f3fSDimitry Andric
30fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
31fe6060f1SDimitry Andric
325f757f3fSDimitry Andric // generic implementation
335f757f3fSDimitry Andric template <class _AlgPolicy, class _Iter, class _Sent, class _Tp, class _Proj>
345f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter>
__count(_Iter __first,_Sent __last,const _Tp & __value,_Proj & __proj)355f757f3fSDimitry Andric __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
365f757f3fSDimitry Andric typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __r(0);
37fe6060f1SDimitry Andric for (; __first != __last; ++__first)
385f757f3fSDimitry Andric if (std::__invoke(__proj, *__first) == __value)
39fe6060f1SDimitry Andric ++__r;
40fe6060f1SDimitry Andric return __r;
41fe6060f1SDimitry Andric }
42fe6060f1SDimitry Andric
435f757f3fSDimitry Andric // __bit_iterator implementation
445f757f3fSDimitry Andric template <bool _ToCount, class _Cp, bool _IsConst>
455f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
__count_bool(__bit_iterator<_Cp,_IsConst> __first,typename _Cp::size_type __n)465f757f3fSDimitry Andric __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
475f757f3fSDimitry Andric using _It = __bit_iterator<_Cp, _IsConst>;
485f757f3fSDimitry Andric using __storage_type = typename _It::__storage_type;
495f757f3fSDimitry Andric using difference_type = typename _It::difference_type;
505f757f3fSDimitry Andric
515f757f3fSDimitry Andric const int __bits_per_word = _It::__bits_per_word;
525f757f3fSDimitry Andric difference_type __r = 0;
535f757f3fSDimitry Andric // do first partial word
545f757f3fSDimitry Andric if (__first.__ctz_ != 0) {
555f757f3fSDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
565f757f3fSDimitry Andric __storage_type __dn = std::min(__clz_f, __n);
575f757f3fSDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
585f757f3fSDimitry Andric __r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
595f757f3fSDimitry Andric __n -= __dn;
605f757f3fSDimitry Andric ++__first.__seg_;
615f757f3fSDimitry Andric }
625f757f3fSDimitry Andric // do middle whole words
635f757f3fSDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
645f757f3fSDimitry Andric __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
655f757f3fSDimitry Andric // do last partial word
665f757f3fSDimitry Andric if (__n > 0) {
675f757f3fSDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
685f757f3fSDimitry Andric __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
695f757f3fSDimitry Andric }
705f757f3fSDimitry Andric return __r;
715f757f3fSDimitry Andric }
725f757f3fSDimitry Andric
735f757f3fSDimitry Andric template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
745f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
__count(__bit_iterator<_Cp,_IsConst> __first,__bit_iterator<_Cp,_IsConst> __last,const _Tp & __value,_Proj &)755f757f3fSDimitry Andric __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
765f757f3fSDimitry Andric if (__value)
775f757f3fSDimitry Andric return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
785f757f3fSDimitry Andric return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
795f757f3fSDimitry Andric }
805f757f3fSDimitry Andric
815f757f3fSDimitry Andric template <class _InputIterator, class _Tp>
82*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<_InputIterator>
count(_InputIterator __first,_InputIterator __last,const _Tp & __value)835f757f3fSDimitry Andric count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
845f757f3fSDimitry Andric __identity __proj;
855f757f3fSDimitry Andric return std::__count<_ClassicAlgPolicy>(__first, __last, __value, __proj);
865f757f3fSDimitry Andric }
875f757f3fSDimitry Andric
88fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
89fe6060f1SDimitry Andric
905f757f3fSDimitry Andric _LIBCPP_POP_MACROS
915f757f3fSDimitry Andric
92fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_COUNT_H
93