[PATCH] D74967: [NFC] Add filters to hasNItems and hasNItemsOrMore

Tyker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 03:03:38 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG61211fec8649: [NFC] Add filters to hasNItems and hasNItemsOrMore (authored by Tyker).

Changed prior to commit:
  https://reviews.llvm.org/D74967?vs=245857&id=249873#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74967/new/

https://reviews.llvm.org/D74967

Files:
  llvm/include/llvm/ADT/STLExtras.h


Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1520,33 +1520,45 @@
 
 /// Return true if the sequence [Begin, End) has exactly N items. Runs in O(N)
 /// time. Not meant for use with random-access iterators.
-template <typename IterTy>
+/// Can optionally take a predicate to filter lazily some items.
+template<typename IterTy,
+         typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)>
 bool hasNItems(
     IterTy &&Begin, IterTy &&End, unsigned N,
+    Pred &&ShouldBeCounted =
+        [](const decltype(*std::declval<IterTy>()) &) { return true; },
     std::enable_if_t<
         !std::is_same<typename std::iterator_traits<std::remove_reference_t<
                           decltype(Begin)>>::iterator_category,
                       std::random_access_iterator_tag>::value,
         void> * = nullptr) {
-  for (; N; --N, ++Begin)
+  for (; N; ++Begin) {
     if (Begin == End)
       return false; // Too few.
+    N -= ShouldBeCounted(*Begin);
+  }
   return Begin == End;
 }
 
 /// Return true if the sequence [Begin, End) has N or more items. Runs in O(N)
 /// time. Not meant for use with random-access iterators.
-template <typename IterTy>
+/// Can optionally take a predicate to filter lazily some items.
+template<typename IterTy,
+         typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)>
 bool hasNItemsOrMore(
     IterTy &&Begin, IterTy &&End, unsigned N,
+    Pred &&ShouldBeCounted =
+        [](const decltype(*std::declval<IterTy>()) &) { return true; },
     std::enable_if_t<
         !std::is_same<typename std::iterator_traits<std::remove_reference_t<
                           decltype(Begin)>>::iterator_category,
                       std::random_access_iterator_tag>::value,
         void> * = nullptr) {
-  for (; N; --N, ++Begin)
+  for (; N; ++Begin) {
     if (Begin == End)
       return false; // Too few.
+    N -= ShouldBeCounted(*Begin);
+  }
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74967.249873.patch
Type: text/x-patch
Size: 2093 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200312/6ea0a740/attachment.bin>


More information about the llvm-commits mailing list