[PATCH] D60779: [ADT] llvm::bsearch, binary search for mere mortals

Sam McCall via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 09:14:37 PDT 2019


sammccall updated this revision to Diff 195401.
sammccall added a comment.

Predicate, not compare.


Repository:
  rL LLVM

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

https://reviews.llvm.org/D60779

Files:
  include/llvm/ADT/STLExtras.h
  unittests/ADT/STLExtrasTest.cpp


Index: unittests/ADT/STLExtrasTest.cpp
===================================================================
--- unittests/ADT/STLExtrasTest.cpp
+++ unittests/ADT/STLExtrasTest.cpp
@@ -469,4 +469,25 @@
   EXPECT_EQ(V1, to_address(V3));
 }
 
+TEST(STLExtrasTest, bsearch) {
+  // Integer version.
+  EXPECT_EQ(7u, bsearch(5, 10, [](unsigned X) { return X >= 7; }));
+  EXPECT_EQ(5u, bsearch(5, 10, [](unsigned X) { return X >= 0; }));
+  EXPECT_EQ(10u, bsearch(5, 10, [](unsigned X) { return X >= 50; }));
+
+  // Iterator version.
+  std::vector<int> V = {1, 3, 5, 7, 9};
+  EXPECT_EQ(V.begin() + 3,
+            bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 7; }));
+  EXPECT_EQ(V.begin(),
+            bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 0; }));
+  EXPECT_EQ(V.end(),
+            bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 50; }));
+
+  // Range version.
+  EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));
+  EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 0; }));
+  EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; }));
+}
+
 } // namespace
Index: include/llvm/ADT/STLExtras.h
===================================================================
--- include/llvm/ADT/STLExtras.h
+++ include/llvm/ADT/STLExtras.h
@@ -1300,6 +1300,46 @@
     -> decltype(adl_begin(Range)) {
   return std::upper_bound(adl_begin(Range), adl_end(Range), I, C);
 }
+
+/// Binary search for the first index where a predicate is true.
+/// Returns the first I in [Lo, Hi) where C(I) is true, or Hi if it never is.
+/// Requires that C is always false below some limit, and always true above it.
+///
+/// Example:
+///   int DawnModernEra = bsearch(1776, 2050, [](int Year){
+///     return Presidents.for(Year).twitterHandle() != None;
+///   });
+///
+/// Note the return value differs from std::binary_search!
+template <typename Predicate>
+unsigned bsearch(unsigned Lo, unsigned Hi, Predicate P) {
+  while (Lo != Hi) {
+    unsigned Mid = Lo + ((Hi - Lo) >> 1);
+    if (P(Mid))
+      Hi = Mid;
+    else
+      Lo = Mid + 1;
+  }
+  return Hi;
+}
+
+/// Binary search for the first iterator where a predicate is true.
+/// Returns the first I in [Lo, Hi) where C(*I) is true, or Hi if it never is.
+/// Requires that C is always false below some limit, and always true above it.
+template <typename It, typename Predicate,
+          typename Val = decltype(*std::declval<It>())>
+It bsearch(It Lo, It Hi, Predicate P) {
+  return std::lower_bound(Lo, Hi, 0u,
+                          [&](const Val &V, unsigned) { return !P(V); });
+}
+
+/// Binary search for the first iterator in a range where a predicate is true.
+/// Requires that C is always false below some limit, and always true above it.
+template <typename R, typename Predicate>
+auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
+  return bsearch(adl_begin(Range), adl_end(Range), P);
+}
+
 /// Wrapper function around std::equal to detect if all elements
 /// in a container are same.
 template <typename R>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60779.195401.patch
Type: text/x-patch
Size: 3078 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190416/980b6d64/attachment.bin>


More information about the llvm-commits mailing list