Skip to content

Commit a40f63f

Browse files
committed
Use linear search instead of std::lower_bound
1 parent 75de36c commit a40f63f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

llvm/lib/Frontend/Directive/Spelling.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,30 @@
88

99
#include "llvm/Frontend/Directive/Spelling.h"
1010

11-
#include "llvm/ADT/STLExtras.h"
1211
#include "llvm/ADT/StringRef.h"
1312
#include "llvm/Support/MathExtras.h"
1413

1514
#include <cassert>
1615

16+
static bool Contains(llvm::directive::VersionRange V, int P) {
17+
return V.Min <= P && P <= V.Max;
18+
}
19+
1720
llvm::StringRef llvm::directive::FindName(
1821
llvm::iterator_range<const llvm::directive::Spelling *> Range,
1922
unsigned Version) {
2023
assert(llvm::isInt<8 * sizeof(int)>(Version) && "Version value out of range");
2124

2225
int V = Version;
23-
Spelling Tmp{StringRef(), {V, V}};
24-
auto F =
25-
llvm::lower_bound(Range, Tmp, [](const Spelling &A, const Spelling &B) {
26-
return A.Versions < B.Versions;
27-
});
28-
if (F != Range.end())
29-
return F->Name;
26+
// Do a linear search to find the first Spelling that contains Version.
27+
// The condition "contains(S, Version)" does not partition the list of
28+
// spellings, so std::[lower|upper]_bound cannot be used.
29+
// In practice the list of spellings is expected to be very short, so
30+
// linear search seems appropriate. In general, an interval tree may be
31+
// a better choice, but in this case it may be an overkill.
32+
for (auto &S : Range) {
33+
if (Contains(S.Versions, V))
34+
return S.Name;
35+
}
3036
return StringRef();
3137
}

0 commit comments

Comments
 (0)