[llvm] r364719 - [ADT] Implement llvm::bsearch() with std::partition_point()
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 30 02:18:00 PDT 2019
Author: maskray
Date: Sun Jun 30 02:17:59 2019
New Revision: 364719
URL: http://llvm.org/viewvc/llvm-project?rev=364719&view=rev
Log:
[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
Modified:
llvm/trunk/include/llvm/ADT/STLExtras.h
llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
llvm/trunk/unittests/ADT/STLExtrasTest.cpp
Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=364719&r1=364718&r2=364719&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Sun Jun 30 02:17:59 2019
@@ -1322,44 +1322,13 @@ void stable_sort(R &&Range, Compare C) {
std::stable_sort(adl_begin(Range), adl_end(Range), 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:
-/// size_t DawnModernEra = bsearch(1776, 2050, [](size_t Year){
-/// return Presidents.for(Year).twitterHandle() != None;
-/// });
-///
-/// Note the return value differs from std::binary_search!
-template <typename Predicate>
-size_t bsearch(size_t Lo, size_t Hi, Predicate P) {
- while (Lo != Hi) {
- assert(Hi > Lo);
- size_t Mid = Lo + (Hi - Lo) / 2;
- 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>
+template <typename R, typename Predicate,
+ typename Val = decltype(*adl_begin(std::declval<R>()))>
auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
- return bsearch(adl_begin(Range), adl_end(Range), P);
+ return std::partition_point(adl_begin(Range), adl_end(Range),
+ [&](const Val &V) { return !P(V); });
}
/// Wrapper function around std::equal to detect if all elements
Modified: llvm/trunk/include/llvm/CodeGen/SlotIndexes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SlotIndexes.h?rev=364719&r1=364718&r2=364719&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/SlotIndexes.h (original)
+++ llvm/trunk/include/llvm/CodeGen/SlotIndexes.h Sun Jun 30 02:17:59 2019
@@ -492,8 +492,9 @@ class raw_ostream;
/// Move iterator to the next IdxMBBPair where the SlotIndex is greater or
/// equal to \p To.
MBBIndexIterator advanceMBBIndex(MBBIndexIterator I, SlotIndex To) const {
- return llvm::bsearch(I, idx2MBBMap.end(),
- [=](const IdxMBBPair &IM) { return To <= IM.first; });
+ return std::partition_point(
+ I, idx2MBBMap.end(),
+ [=](const IdxMBBPair &IM) { return IM.first < To; });
}
/// Get an iterator pointing to the IdxMBBPair with the biggest SlotIndex
Modified: llvm/trunk/unittests/ADT/STLExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/STLExtrasTest.cpp?rev=364719&r1=364718&r2=364719&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/STLExtrasTest.cpp (original)
+++ llvm/trunk/unittests/ADT/STLExtrasTest.cpp Sun Jun 30 02:17:59 2019
@@ -470,19 +470,7 @@ TEST(STLExtrasTest, to_address) {
}
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 >= 1; }));
- 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 >= 1; }));
- 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; }));
More information about the llvm-commits
mailing list