count.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ALGORITHM_COUNT_H
  10. #define _LIBCPP___ALGORITHM_COUNT_H
  11. #include <__algorithm/iterator_operations.h>
  12. #include <__algorithm/min.h>
  13. #include <__bit/invert_if.h>
  14. #include <__bit/popcount.h>
  15. #include <__config>
  16. #include <__functional/identity.h>
  17. #include <__functional/invoke.h>
  18. #include <__fwd/bit_reference.h>
  19. #include <__iterator/iterator_traits.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_PUSH_MACROS
  24. #include <__undef_macros>
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. // generic implementation
  27. template <class _AlgPolicy, class _Iter, class _Sent, class _Tp, class _Proj>
  28. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename _IterOps<_AlgPolicy>::template __difference_type<_Iter>
  29. __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
  30. typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __r(0);
  31. for (; __first != __last; ++__first)
  32. if (std::__invoke(__proj, *__first) == __value)
  33. ++__r;
  34. return __r;
  35. }
  36. // __bit_iterator implementation
  37. template <bool _ToCount, class _Cp, bool _IsConst>
  38. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
  39. __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
  40. using _It = __bit_iterator<_Cp, _IsConst>;
  41. using __storage_type = typename _It::__storage_type;
  42. using difference_type = typename _It::difference_type;
  43. const int __bits_per_word = _It::__bits_per_word;
  44. difference_type __r = 0;
  45. // do first partial word
  46. if (__first.__ctz_ != 0) {
  47. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  48. __storage_type __dn = std::min(__clz_f, __n);
  49. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  50. __r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
  51. __n -= __dn;
  52. ++__first.__seg_;
  53. }
  54. // do middle whole words
  55. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
  56. __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
  57. // do last partial word
  58. if (__n > 0) {
  59. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  60. __r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
  61. }
  62. return __r;
  63. }
  64. template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>
  65. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
  66. __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
  67. if (__value)
  68. return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
  69. return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
  70. }
  71. template <class _InputIterator, class _Tp>
  72. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<_InputIterator>
  73. count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
  74. __identity __proj;
  75. return std::__count<_ClassicAlgPolicy>(__first, __last, __value, __proj);
  76. }
  77. _LIBCPP_END_NAMESPACE_STD
  78. _LIBCPP_POP_MACROS
  79. #endif // _LIBCPP___ALGORITHM_COUNT_H