Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/count.h
35232 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_COUNT_H10#define _LIBCPP___ALGORITHM_COUNT_H1112#include <__algorithm/iterator_operations.h>13#include <__algorithm/min.h>14#include <__bit/invert_if.h>15#include <__bit/popcount.h>16#include <__config>17#include <__functional/identity.h>18#include <__functional/invoke.h>19#include <__fwd/bit_reference.h>20#include <__iterator/iterator_traits.h>2122#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif2526_LIBCPP_PUSH_MACROS27#include <__undef_macros>2829_LIBCPP_BEGIN_NAMESPACE_STD3031// generic implementation32template <class _AlgPolicy, class _Iter, class _Sent, class _Tp, class _Proj>33_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter>34__count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {35typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __r(0);36for (; __first != __last; ++__first)37if (std::__invoke(__proj, *__first) == __value)38++__r;39return __r;40}4142// __bit_iterator implementation43template <bool _ToCount, class _Cp, bool _IsConst>44_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type45__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {46using _It = __bit_iterator<_Cp, _IsConst>;47using __storage_type = typename _It::__storage_type;48using difference_type = typename _It::difference_type;4950const int __bits_per_word = _It::__bits_per_word;51difference_type __r = 0;52// do first partial word53if (__first.__ctz_ != 0) {54__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);55__storage_type __dn = std::min(__clz_f, __n);56__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));57__r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);58__n -= __dn;59++__first.__seg_;60}61// do middle whole words62for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)63__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));64// do last partial word65if (__n > 0) {66__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);67__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);68}69return __r;70}7172template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>73_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >74__count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {75if (__value)76return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));77return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));78}7980template <class _InputIterator, class _Tp>81_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<_InputIterator>82count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {83__identity __proj;84return std::__count<_ClassicAlgPolicy>(__first, __last, __value, __proj);85}8687_LIBCPP_END_NAMESPACE_STD8889_LIBCPP_POP_MACROS9091#endif // _LIBCPP___ALGORITHM_COUNT_H929394