brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · 8529d11 Raw
96 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___ALGORITHM_COUNT_H11#define _LIBCPP___ALGORITHM_COUNT_H12 13#include <__algorithm/iterator_operations.h>14#include <__algorithm/min.h>15#include <__bit/invert_if.h>16#include <__bit/popcount.h>17#include <__config>18#include <__functional/identity.h>19#include <__fwd/bit_reference.h>20#include <__iterator/iterator_traits.h>21#include <__type_traits/enable_if.h>22#include <__type_traits/invoke.h>23 24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25#  pragma GCC system_header26#endif27 28_LIBCPP_PUSH_MACROS29#include <__undef_macros>30 31_LIBCPP_BEGIN_NAMESPACE_STD32 33// generic implementation34template <class _AlgPolicy, class _Iter, class _Sent, class _Tp, class _Proj>35_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter>36__count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {37  typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __r(0);38  for (; __first != __last; ++__first)39    if (std::__invoke(__proj, *__first) == __value)40      ++__r;41  return __r;42}43 44// __bit_iterator implementation45template <bool _ToCount, class _Cp, bool _IsConst>46_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type47__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {48  using _It             = __bit_iterator<_Cp, _IsConst>;49  using __storage_type  = typename _It::__storage_type;50  using difference_type = typename _It::difference_type;51 52  const int __bits_per_word = _It::__bits_per_word;53  difference_type __r       = 0;54  // do first partial word55  if (__first.__ctz_ != 0) {56    __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);57    __storage_type __dn    = std::min(__clz_f, __n);58    __storage_type __m     = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);59    __r                    = std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));60    __n -= __dn;61    ++__first.__seg_;62  }63  // do middle whole words64  for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)65    __r += std::__popcount(std::__invert_if<!_ToCount>(*__first.__seg_));66  // do last partial word67  if (__n > 0) {68    __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);69    __r += std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));70  }71  return __r;72}73 74template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>75_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iterator_difference_type<__bit_iterator<_Cp, _IsConst> >76__count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {77  if (__value)78    return std::__count_bool<true>(79        __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));80  return std::__count_bool<false>(81      __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));82}83 84template <class _InputIterator, class _Tp>85[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iterator_difference_type<_InputIterator>86count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {87  __identity __proj;88  return std::__count<_ClassicAlgPolicy>(__first, __last, __value, __proj);89}90 91_LIBCPP_END_NAMESPACE_STD92 93_LIBCPP_POP_MACROS94 95#endif // _LIBCPP___ALGORITHM_COUNT_H96