[cfe-commits] r63717 - /cfe/trunk/lib/Basic/SourceManager.cpp
Chris Lattner
sabre at nondot.org
Tue Feb 3 20:47:01 PST 2009
Author: lattner
Date: Tue Feb 3 22:46:59 2009
New Revision: 63717
URL: http://llvm.org/viewvc/llvm-project?rev=63717&view=rev
Log:
replace gimpy linear search with svelte binary search ;-)
Modified:
cfe/trunk/lib/Basic/SourceManager.cpp
Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=63717&r1=63716&r2=63717&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Tue Feb 3 22:46:59 2009
@@ -79,7 +79,14 @@
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 std::vector<LineEntry> &Entries = LineEntries[FID];
assert(!Entries.empty() && "No #line entries for this FID after all!");
-
- // 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;
+ // It is very common for the query to be after the last #line, check this
+ // first.
+ if (Entries.back().FileOffset <= Offset)
+ return &Entries.back();
+
+ // 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;
}
More information about the cfe-commits
mailing list