[libcxx-commits] [libcxx] [libcxx] Added segmented iterator for count_if (PR #105888)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 23 14:15:02 PDT 2024


https://github.com/adeel10x updated https://github.com/llvm/llvm-project/pull/105888

>From 7e47590501af2ddef9b889b0d0a83051d9c63937 Mon Sep 17 00:00:00 2001
From: adeel10x <adeel.ahmad at 10xengineers.ai>
Date: Sat, 24 Aug 2024 02:04:23 +0500
Subject: [PATCH 1/2] [libcxx] Added segmented iterator for count_if

---
 libcxx/include/__algorithm/count_if.h | 33 +++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__algorithm/count_if.h b/libcxx/include/__algorithm/count_if.h
index 25782069d03275..ba1cdb16b56b41 100644
--- a/libcxx/include/__algorithm/count_if.h
+++ b/libcxx/include/__algorithm/count_if.h
@@ -10,8 +10,10 @@
 #ifndef _LIBCPP___ALGORITHM_COUNT_IF_H
 #define _LIBCPP___ALGORITHM_COUNT_IF_H
 
+#include <__algorithm/for_each.h>
 #include <__config>
 #include <__iterator/iterator_traits.h>
+#include <__iterator/segmented_iterator.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -19,10 +21,12 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIterator, class _Predicate>
+template <class _InputIterator,
+          class _Predicate,
+          __enable_if_t<!__is_segmented_iterator<_InputIterator>::value, int> = 0>
 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-typename iterator_traits<_InputIterator>::difference_type
-count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+    typename iterator_traits<_InputIterator>::difference_type
+    __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
   typename iterator_traits<_InputIterator>::difference_type __r(0);
   for (; __first != __last; ++__first)
     if (__pred(*__first))
@@ -30,6 +34,27 @@ count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
   return __r;
 }
 
+template <class _SegmentedIterator,
+          class _Predicate,
+          __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
+_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+    typename iterator_traits<_SegmentedIterator>::difference_type
+    __count_if(_SegmentedIterator __first, _SegmentedIterator __last, _Predicate __pred) {
+  typename iterator_traits<_SegmentedIterator>::difference_type __r(0);
+  std::for_each(__first, __last, [&__r, __pred](auto& __val) mutable {
+    if (__pred(__val))
+      ++__r;
+  });
+  return __r;
+}
+
+template <class _InputIterator, class _Predicate>
+_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+    typename iterator_traits<_InputIterator>::difference_type
+    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
+  return __count_if(__first, __last, __pred);
+}
+
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
+#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
\ No newline at end of file

>From f6e14b363683aac98df71df2e0b2b5ca3a3e2769 Mon Sep 17 00:00:00 2001
From: adeel10x <adeel.ahmad at 10xengineers.ai>
Date: Sat, 24 Aug 2024 02:16:10 +0500
Subject: [PATCH 2/2] Added newline at the end of the file

---
 libcxx/include/__algorithm/count_if.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__algorithm/count_if.h b/libcxx/include/__algorithm/count_if.h
index ba1cdb16b56b41..4cf0fc81481b13 100644
--- a/libcxx/include/__algorithm/count_if.h
+++ b/libcxx/include/__algorithm/count_if.h
@@ -57,4 +57,5 @@ _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
 
 _LIBCPP_END_NAMESPACE_STD
 
-#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
\ No newline at end of file
+#endif // _LIBCPP___ALGORITHM_COUNT_IF_H
+



More information about the libcxx-commits mailing list