Skip to content

Commit 6b44291

Browse files
committed
[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 llvm-svn: 358540
1 parent d5bc5ca commit 6b44291

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,47 @@ auto upper_bound(R &&Range, T &&Value, Compare C)
13041304
return std::upper_bound(adl_begin(Range), adl_end(Range),
13051305
std::forward<T>(Value), C);
13061306
}
1307+
1308+
/// Binary search for the first index where a predicate is true.
1309+
/// Returns the first I in [Lo, Hi) where C(I) is true, or Hi if it never is.
1310+
/// Requires that C is always false below some limit, and always true above it.
1311+
///
1312+
/// Example:
1313+
/// size_t DawnModernEra = bsearch(1776, 2050, [](size_t Year){
1314+
/// return Presidents.for(Year).twitterHandle() != None;
1315+
/// });
1316+
///
1317+
/// Note the return value differs from std::binary_search!
1318+
template <typename Predicate>
1319+
size_t bsearch(size_t Lo, size_t Hi, Predicate P) {
1320+
while (Lo != Hi) {
1321+
assert(Hi > Lo);
1322+
size_t Mid = Lo + (Hi - Lo) / 2;
1323+
if (P(Mid))
1324+
Hi = Mid;
1325+
else
1326+
Lo = Mid + 1;
1327+
}
1328+
return Hi;
1329+
}
1330+
1331+
/// Binary search for the first iterator where a predicate is true.
1332+
/// Returns the first I in [Lo, Hi) where C(*I) is true, or Hi if it never is.
1333+
/// Requires that C is always false below some limit, and always true above it.
1334+
template <typename It, typename Predicate,
1335+
typename Val = decltype(*std::declval<It>())>
1336+
It bsearch(It Lo, It Hi, Predicate P) {
1337+
return std::lower_bound(Lo, Hi, 0u,
1338+
[&](const Val &V, unsigned) { return !P(V); });
1339+
}
1340+
1341+
/// Binary search for the first iterator in a range where a predicate is true.
1342+
/// Requires that C is always false below some limit, and always true above it.
1343+
template <typename R, typename Predicate>
1344+
auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1345+
return bsearch(adl_begin(Range), adl_end(Range), P);
1346+
}
1347+
13071348
/// Wrapper function around std::equal to detect if all elements
13081349
/// in a container are same.
13091350
template <typename R>

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,25 @@ TEST(STLExtrasTest, to_address) {
469469
EXPECT_EQ(V1, to_address(V3));
470470
}
471471

472+
TEST(STLExtrasTest, bsearch) {
473+
// Integer version.
474+
EXPECT_EQ(7u, bsearch(5, 10, [](unsigned X) { return X >= 7; }));
475+
EXPECT_EQ(5u, bsearch(5, 10, [](unsigned X) { return X >= 0; }));
476+
EXPECT_EQ(10u, bsearch(5, 10, [](unsigned X) { return X >= 50; }));
477+
478+
// Iterator version.
479+
std::vector<int> V = {1, 3, 5, 7, 9};
480+
EXPECT_EQ(V.begin() + 3,
481+
bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 7; }));
482+
EXPECT_EQ(V.begin(),
483+
bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 0; }));
484+
EXPECT_EQ(V.end(),
485+
bsearch(V.begin(), V.end(), [](unsigned X) { return X >= 50; }));
486+
487+
// Range version.
488+
EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));
489+
EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 0; }));
490+
EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; }));
491+
}
492+
472493
} // namespace

0 commit comments

Comments
 (0)