llvm.org GIT mirror llvm / 7d8cf48
[ADT] llvm::bsearch, binary search for mere mortals Summary: Add to STLExtras a binary search function with a simple mental model: You provide a range and a predicate which is true above a certain point. bsearch() tells you that point. Overloads are provided for integers, iterators, and containers. This is more suitable than std:: alternatives in many cases: - std::binary_search only indicates presence/absence - upper_bound/lower_bound give you the opportunity to pick the wrong one - all of the options have confusing names and definitions when your predicate doesn't have simple "less than" semantics - all of the options require iterators - we plumb around a useless `value` parameter that should be a lambda capture The API is inspired by Go's standard library, but we add an extra parameter as well as some overloads and templates to show how clever C++ is. Reviewers: ilya-biryukov, gribozavr Subscribers: dexonsmith, kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60779 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358540 91177308-0d34-0410-b5e6-96231b3b80d8 Sam McCall 4 months ago
2 changed file(s) with 62 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
13031303 return std::upper_bound(adl_begin(Range), adl_end(Range),
13041304 std::forward(Value), C);
13051305 }
1306
1307 /// Binary search for the first index where a predicate is true.
1308 /// Returns the first I in [Lo, Hi) where C(I) is true, or Hi if it never is.
1309 /// Requires that C is always false below some limit, and always true above it.
1310 ///
1311 /// Example:
1312 /// size_t DawnModernEra = bsearch(1776, 2050, [](size_t Year){
1313 /// return Presidents.for(Year).twitterHandle() != None;
1314 /// });
1315 ///
1316 /// Note the return value differs from std::binary_search!
1317 template
1318 size_t bsearch(size_t Lo, size_t Hi, Predicate P) {
1319 while (Lo != Hi) {
1320 assert(Hi > Lo);
1321 size_t Mid = Lo + (Hi - Lo) / 2;
1322 if (P(Mid))
1323 Hi = Mid;
1324 else
1325 Lo = Mid + 1;
1326 }
1327 return Hi;
1328 }
1329
1330 /// Binary search for the first iterator where a predicate is true.
1331 /// Returns the first I in [Lo, Hi) where C(*I) is true, or Hi if it never is.
1332 /// Requires that C is always false below some limit, and always true above it.
1333 template
1334 typename Val = decltype(*std::declval())>
1335 It bsearch(It Lo, It Hi, Predicate P) {
1336 return std::lower_bound(Lo, Hi, 0u,
1337 [&](const Val &V, unsigned) { return !P(V); });
1338 }
1339
1340 /// Binary search for the first iterator in a range where a predicate is true.
1341 /// Requires that C is always false below some limit, and always true above it.
1342 template
1343 auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1344 return bsearch(adl_begin(Range), adl_end(Range), P);
1345 }
1346
13061347 /// Wrapper function around std::equal to detect if all elements
13071348 /// in a container are same.
13081349 template
468468 EXPECT_EQ(V1, to_address(V3));
469469 }
470470
471 TEST(STLExtrasTest, bsearch) {
472 // Integer version.
473 EXPECT_EQ(7u, bsearch(5, 10, [](unsigned X) { return X >= 7; }));
474 EXPECT_EQ(5u, bsearch(5, 10, [](unsigned X) { return X >= 0; }));
475 EXPECT_EQ(10u, bsearch(5, 10, [](unsigned X) { return X >= 50; }));
476
477 // Iterator version.
478 std::vector V = {1, 3, 5, 7, 9};
479 EXPECT_EQ(V.begin() + 3,
480 bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 7; }));
481 EXPECT_EQ(V.begin(),
482 bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 0; }));
483 EXPECT_EQ(V.end(),
484 bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 50; }));
485
486 // Range version.
487 EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));
488 EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 0; }));
489 EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; }));
490 }
491
471492 } // namespace