llvm.org GIT mirror llvm / 726837e
[ADT] Implement llvm::bsearch() with std::partition_point() Summary: Delete the begin-end form because the standard std::partition_point can be easily used as a replacement. The ranges-style llvm::bsearch will be renamed to llvm::partition_point in the next clean-up patch. The name "bsearch" doesn't meet people's expectation because in C: > If two or more members compare equal, which member is returned is unspecified. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D63718 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364719 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song a month ago
3 changed file(s) with 7 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
13211321 std::stable_sort(adl_begin(Range), adl_end(Range), C);
13221322 }
13231323
1324 /// Binary search for the first index where a predicate is true.
1325 /// Returns the first I in [Lo, Hi) where C(I) is true, or Hi if it never is.
1326 /// Requires that C is always false below some limit, and always true above it.
1327 ///
1328 /// Example:
1329 /// size_t DawnModernEra = bsearch(1776, 2050, [](size_t Year){
1330 /// return Presidents.for(Year).twitterHandle() != None;
1331 /// });
1332 ///
1333 /// Note the return value differs from std::binary_search!
1334 template
1335 size_t bsearch(size_t Lo, size_t Hi, Predicate P) {
1336 while (Lo != Hi) {
1337 assert(Hi > Lo);
1338 size_t Mid = Lo + (Hi - Lo) / 2;
1339 if (P(Mid))
1340 Hi = Mid;
1341 else
1342 Lo = Mid + 1;
1343 }
1344 return Hi;
1345 }
1346
1347 /// Binary search for the first iterator where a predicate is true.
1348 /// Returns the first I in [Lo, Hi) where C(*I) is true, or Hi if it never is.
1349 /// Requires that C is always false below some limit, and always true above it.
1350 template
1351 typename Val = decltype(*std::declval())>
1352 It bsearch(It Lo, It Hi, Predicate P) {
1353 return std::lower_bound(Lo, Hi, 0u,
1354 [&](const Val &V, unsigned) { return !P(V); });
1355 }
1356
13571324 /// Binary search for the first iterator in a range where a predicate is true.
13581325 /// Requires that C is always false below some limit, and always true above it.
1359 template >
1326 template ,
1327 typename Val = decltype(*adl_begin(std::declval()))>
13601328 auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1361 return bsearch(adl_begin(Range), adl_end(Range), P);
1329 return std::partition_point(adl_begin(Range), adl_end(Range),
1330 [&](const Val &V) { return !P(V); });
13621331 }
13631332
13641333 /// Wrapper function around std::equal to detect if all elements
491491 /// Move iterator to the next IdxMBBPair where the SlotIndex is greater or
492492 /// equal to \p To.
493493 MBBIndexIterator advanceMBBIndex(MBBIndexIterator I, SlotIndex To) const {
494 return llvm::bsearch(I, idx2MBBMap.end(),
495 [=](const IdxMBBPair &IM) { return To <= IM.first; });
494 return std::partition_point(
495 I, idx2MBBMap.end(),
496 [=](const IdxMBBPair &IM) { return IM.first < To; });
496497 }
497498
498499 /// Get an iterator pointing to the IdxMBBPair with the biggest SlotIndex
469469 }
470470
471471 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 >= 1; }));
475 EXPECT_EQ(10u, bsearch(5, 10, [](unsigned X) { return X >= 50; }));
476
477 // Iterator version.
478472 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 >= 1; }));
483 EXPECT_EQ(V.end(),
484 bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 50; }));
485473
486474 // Range version.
487475 EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));