summaryrefslogtreecommitdiffstats
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-02-04 04:46:59 +0000
committerChris Lattner <sabre@nondot.org>2009-02-04 04:46:59 +0000
commit334a2ada0674950dafff4423fc9b254b5e4f7b5d (patch)
tree5606ba2b65400d331f2d6d73e60b12746624113e /clang/lib/Basic/SourceManager.cpp
parent679073b420bee6bae7717c98789b219c42f81cee (diff)
downloadbcm5719-llvm-334a2ada0674950dafff4423fc9b254b5e4f7b5d.tar.gz
bcm5719-llvm-334a2ada0674950dafff4423fc9b254b5e4f7b5d.zip
replace gimpy linear search with svelte binary search ;-)
llvm-svn: 63717
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp24
1 files changed, 17 insertions, 7 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index d9cf5e1a54b..6381687d6ef 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -79,7 +79,14 @@ struct LineEntry {
return E;
}
};
-
+
+inline bool operator<(const LineEntry &E, unsigned Offset) {
+ return E.FileOffset < Offset;
+}
+
+inline bool operator<(unsigned Offset, const LineEntry &E) {
+ return Offset < E.FileOffset;
+}
/// LineTableInfo - This class is used to hold and unique data used to
/// represent #line information.
@@ -160,13 +167,16 @@ const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
const std::vector<LineEntry> &Entries = LineEntries[FID];
assert(!Entries.empty() && "No #line entries for this FID after all!");
+ // It is very common for the query to be after the last #line, check this
+ // first.
+ if (Entries.back().FileOffset <= Offset)
+ return &Entries.back();
- // FIXME: Dumb linear search.
- // Find the maximal element that is still before Offset.
- for (std::vector<LineEntry>::const_reverse_iterator I = Entries.rbegin(),
- E = Entries.rend(); I != E; ++I)
- if (I->FileOffset <= Offset) return &*I;
- return 0;
+ // Do a binary search to find the maximal element that is still before Offset.
+ std::vector<LineEntry>::const_iterator I =
+ std::upper_bound(Entries.begin(), Entries.end(), Offset);
+ if (I == Entries.begin()) return 0;
+ return &*--I;
}
OpenPOWER on IntegriCloud