ranges_count.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_RANGES_COUNT_H
  9. #define _LIBCPP___ALGORITHM_RANGES_COUNT_H
  10. #include <__algorithm/ranges_count_if.h>
  11. #include <__config>
  12. #include <__functional/identity.h>
  13. #include <__functional/ranges_operations.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/incrementable_traits.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__iterator/projected.h>
  18. #include <__ranges/access.h>
  19. #include <__ranges/concepts.h>
  20. #include <__utility/move.h>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. #if _LIBCPP_STD_VER >= 20
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. namespace ranges {
  27. namespace __count {
  28. struct __fn {
  29. template <input_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
  30. requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
  31. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Iter>
  32. operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) const {
  33. auto __pred = [&](auto&& __e) { return __e == __value; };
  34. return ranges::__count_if_impl(std::move(__first), std::move(__last), __pred, __proj);
  35. }
  36. template <input_range _Range, class _Type, class _Proj = identity>
  37. requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
  38. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr range_difference_t<_Range>
  39. operator()(_Range&& __r, const _Type& __value, _Proj __proj = {}) const {
  40. auto __pred = [&](auto&& __e) { return __e == __value; };
  41. return ranges::__count_if_impl(ranges::begin(__r), ranges::end(__r), __pred, __proj);
  42. }
  43. };
  44. } // namespace __count
  45. inline namespace __cpo {
  46. inline constexpr auto count = __count::__fn{};
  47. } // namespace __cpo
  48. } // namespace ranges
  49. _LIBCPP_END_NAMESPACE_STD
  50. #endif // _LIBCPP_STD_VER >= 20
  51. #endif // _LIBCPP___ALGORITHM_RANGES_COUNT_H